package gallery import ( "strings" "testing" "amadan.net/rastrillo/fiesta" ) // What a file weighs, written the way the page's language writes a // number. The figures are the compressor's and are never pinned; the // formatting and the shape of the line are. // KiB of 1024, one decimal below 10 KiB so a 634-byte file is not "1" // or "0", whole numbers above, in the locale's own number format. func TestKiBIsOneDecimalBelowTenAndWholeAbove(t *testing.T) { for _, c := range []struct { locale string n int want string }{ {"en", 634, "0.6"}, {"en", 1024, "1.0"}, {"en", 10*1024 - 1, "10.0"}, {"en", 10 * 1024, "10"}, {"en", 107279, "105"}, {"en", 2048 * 1024, "2,048"}, {"es", 634, "0,6"}, {"ru", 4300, "4,2"}, } { if got := kib(c.locale, c.n); got != c.want { t.Errorf("kib(%s, %d) = %q, want %q", c.locale, c.n, got, c.want) } } } // A locale's own digits and separators, as x/text writes them: Arabic // in Arabic-Indic digits with its own decimal mark, Bengali in Bengali // digits, Hindi in Latin digits with Indian grouping. func TestKiBWritesTheLocalesDigits(t *testing.T) { for _, c := range []struct { locale string n int want string }{ {"ar", 24576, "٢٤"}, {"ar", 4300, "٤٫٢"}, {"bn", 24576, "২৪"}, {"hi", 123456 * 1024, "1,23,456"}, } { if got := kib(c.locale, c.n); got != c.want { t.Errorf("kib(%s, %d) = %q, want %q", c.locale, c.n, got, c.want) } } } // The weight line has three parts split on " · ", the stripped figure // alone in the last, so a file with no stripped figure, which is every // release file, loses exactly that part. Every locale the line has a // translation in is held to the shape; today that is English alone. func TestTheWeightLineDropsItsLastPartCleanly(t *testing.T) { s := newTestSite(t, fiesta.FS(), "") w := weight{Raw: 20000, Gzip: 6000} for _, locale := range Locales { line := s.say(locale, "gallery.started.weight") parts := strings.Split(line, weightSep) if len(parts) != 3 { t.Errorf("%s: the weight line has %d parts split on %q, want 3: %q", locale, len(parts), weightSep, line) continue } if !strings.Contains(parts[2], "{stripped}") || strings.Contains(parts[0]+parts[1], "{stripped}") { t.Errorf("%s: {stripped} is not alone in the last part: %q", locale, line) } got := s.weightIn(locale, w) if strings.Count(got, weightSep) != 1 || strings.Contains(got, "{") || got == "" { t.Errorf("%s: with nothing to strip the line should be two figures: %q", locale, got) } if !strings.Contains(got, kib(locale, w.Raw)) || !strings.Contains(got, kib(locale, w.Gzip)) { t.Errorf("%s: the line %q does not carry both figures", locale, got) } } if g := weigh([]byte(strings.Repeat("abc ", 1000))); g.Raw != 4000 || g.Gzip <= 0 || g.Gzip >= g.Raw { t.Errorf("weigh(4000 bytes of repetition) = %+v; want Raw 4000 and a smaller Gzip", g) } }