| 1 | package gallery |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/rastrillo/fiesta" |
| 8 | ) |
| 9 | |
| 10 | // What a file weighs, written the way the page's language writes a |
| 11 | // number. The figures are the compressor's and are never pinned; the |
| 12 | // formatting and the shape of the line are. |
| 13 | |
| 14 | // KiB of 1024, one decimal below 10 KiB so a 634-byte file is not "1" |
| 15 | // or "0", whole numbers above, in the locale's own number format. |
| 16 | func TestKiBIsOneDecimalBelowTenAndWholeAbove(t *testing.T) { |
| 17 | for _, c := range []struct { |
| 18 | locale string |
| 19 | n int |
| 20 | want string |
| 21 | }{ |
| 22 | {"en", 634, "0.6"}, |
| 23 | {"en", 1024, "1.0"}, |
| 24 | {"en", 10*1024 - 1, "10.0"}, |
| 25 | {"en", 10 * 1024, "10"}, |
| 26 | {"en", 107279, "105"}, |
| 27 | {"en", 2048 * 1024, "2,048"}, |
| 28 | {"es", 634, "0,6"}, |
| 29 | {"ru", 4300, "4,2"}, |
| 30 | } { |
| 31 | if got := kib(c.locale, c.n); got != c.want { |
| 32 | t.Errorf("kib(%s, %d) = %q, want %q", c.locale, c.n, got, c.want) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // A locale's own digits and separators, as x/text writes them: Arabic |
| 38 | // in Arabic-Indic digits with its own decimal mark, Bengali in Bengali |
| 39 | // digits, Hindi in Latin digits with Indian grouping. |
| 40 | func TestKiBWritesTheLocalesDigits(t *testing.T) { |
| 41 | for _, c := range []struct { |
| 42 | locale string |
| 43 | n int |
| 44 | want string |
| 45 | }{ |
| 46 | {"ar", 24576, "٢٤"}, |
| 47 | {"ar", 4300, "٤٫٢"}, |
| 48 | {"bn", 24576, "২৪"}, |
| 49 | {"hi", 123456 * 1024, "1,23,456"}, |
| 50 | } { |
| 51 | if got := kib(c.locale, c.n); got != c.want { |
| 52 | t.Errorf("kib(%s, %d) = %q, want %q", c.locale, c.n, got, c.want) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // The weight line has three parts split on " · ", the stripped figure |
| 58 | // alone in the last, so a file with no stripped figure, which is every |
| 59 | // release file, loses exactly that part. Every locale the line has a |
| 60 | // translation in is held to the shape; today that is English alone. |
| 61 | func TestTheWeightLineDropsItsLastPartCleanly(t *testing.T) { |
| 62 | s := newTestSite(t, fiesta.FS(), "") |
| 63 | w := weight{Raw: 20000, Gzip: 6000} |
| 64 | for _, locale := range Locales { |
| 65 | line := s.say(locale, "gallery.started.weight") |
| 66 | parts := strings.Split(line, weightSep) |
| 67 | if len(parts) != 3 { |
| 68 | t.Errorf("%s: the weight line has %d parts split on %q, want 3: %q", locale, len(parts), weightSep, line) |
| 69 | continue |
| 70 | } |
| 71 | if !strings.Contains(parts[2], "{stripped}") || strings.Contains(parts[0]+parts[1], "{stripped}") { |
| 72 | t.Errorf("%s: {stripped} is not alone in the last part: %q", locale, line) |
| 73 | } |
| 74 | got := s.weightIn(locale, w) |
| 75 | if strings.Count(got, weightSep) != 1 || strings.Contains(got, "{") || got == "" { |
| 76 | t.Errorf("%s: with nothing to strip the line should be two figures: %q", locale, got) |
| 77 | } |
| 78 | if !strings.Contains(got, kib(locale, w.Raw)) || !strings.Contains(got, kib(locale, w.Gzip)) { |
| 79 | t.Errorf("%s: the line %q does not carry both figures", locale, got) |
| 80 | } |
| 81 | } |
| 82 | if g := weigh([]byte(strings.Repeat("abc ", 1000))); g.Raw != 4000 || g.Gzip <= 0 || g.Gzip >= g.Raw { |
| 83 | t.Errorf("weigh(4000 bytes of repetition) = %+v; want Raw 4000 and a smaller Gzip", g) |
| 84 | } |
| 85 | } |
| 86 | |