lang_test.go (3391B)
1 package lang 2 3 import ( 4 "testing" 5 6 qt "github.com/frankban/quicktest" 7 "github.com/gohugoio/hugo/deps" 8 translators "github.com/gohugoio/localescompressed" 9 ) 10 11 func TestNumFmt(t *testing.T) { 12 t.Parallel() 13 c := qt.New(t) 14 15 ns := New(&deps.Deps{}, nil) 16 17 cases := []struct { 18 prec int 19 n float64 20 runes string 21 delim string 22 23 want string 24 }{ 25 {2, -12345.6789, "", "", "-12,345.68"}, 26 {2, -12345.6789, "- . ,", "", "-12,345.68"}, 27 {2, -12345.1234, "- . ,", "", "-12,345.12"}, 28 29 {2, 12345.6789, "- . ,", "", "12,345.68"}, 30 {0, 12345.6789, "- . ,", "", "12,346"}, 31 {11, -12345.6789, "- . ,", "", "-12,345.67890000000"}, 32 33 {2, 927.675, "- .", "", "927.68"}, 34 {2, 1927.675, "- .", "", "1927.68"}, 35 {2, 2927.675, "- .", "", "2927.68"}, 36 37 {3, -12345.6789, "- ,", "", "-12345,679"}, 38 {6, -12345.6789, "- , .", "", "-12.345,678900"}, 39 40 {3, -12345.6789, "-|,| ", "|", "-12 345,679"}, 41 {6, -12345.6789, "-|,| ", "|", "-12 345,678900"}, 42 43 // Arabic, ar_AE 44 {6, -12345.6789, "- ٫ ٬", "", "-12٬345٫678900"}, 45 {6, -12345.6789, "-|٫| ", "|", "-12 345٫678900"}, 46 } 47 48 for _, cas := range cases { 49 var s string 50 var err error 51 52 if len(cas.runes) == 0 { 53 s, err = ns.FormatNumberCustom(cas.prec, cas.n) 54 } else { 55 if cas.delim == "" { 56 s, err = ns.FormatNumberCustom(cas.prec, cas.n, cas.runes) 57 } else { 58 s, err = ns.FormatNumberCustom(cas.prec, cas.n, cas.runes, cas.delim) 59 } 60 } 61 62 c.Assert(err, qt.IsNil) 63 c.Assert(s, qt.Equals, cas.want) 64 } 65 } 66 67 func TestFormatNumbers(t *testing.T) { 68 69 c := qt.New(t) 70 71 nsNn := New(&deps.Deps{}, translators.GetTranslator("nn")) 72 nsEn := New(&deps.Deps{}, translators.GetTranslator("en")) 73 pi := 3.14159265359 74 75 c.Run("FormatNumber", func(c *qt.C) { 76 c.Parallel() 77 got, err := nsNn.FormatNumber(3, pi) 78 c.Assert(err, qt.IsNil) 79 c.Assert(got, qt.Equals, "3,142") 80 81 got, err = nsEn.FormatNumber(3, pi) 82 c.Assert(err, qt.IsNil) 83 c.Assert(got, qt.Equals, "3.142") 84 }) 85 86 c.Run("FormatPercent", func(c *qt.C) { 87 c.Parallel() 88 got, err := nsEn.FormatPercent(3, 67.33333) 89 c.Assert(err, qt.IsNil) 90 c.Assert(got, qt.Equals, "67.333%") 91 }) 92 93 c.Run("FormatCurrency", func(c *qt.C) { 94 c.Parallel() 95 got, err := nsEn.FormatCurrency(2, "USD", 20000) 96 c.Assert(err, qt.IsNil) 97 c.Assert(got, qt.Equals, "$20,000.00") 98 }) 99 100 c.Run("FormatAccounting", func(c *qt.C) { 101 c.Parallel() 102 got, err := nsEn.FormatAccounting(2, "USD", 20000) 103 c.Assert(err, qt.IsNil) 104 c.Assert(got, qt.Equals, "$20,000.00") 105 }) 106 107 } 108 109 // Issue 9446 110 func TestLanguageKeyFormat(t *testing.T) { 111 112 c := qt.New(t) 113 114 nsUnderscoreUpper := New(&deps.Deps{}, translators.GetTranslator("es_ES")) 115 nsUnderscoreLower := New(&deps.Deps{}, translators.GetTranslator("es_es")) 116 nsHyphenUpper := New(&deps.Deps{}, translators.GetTranslator("es-ES")) 117 nsHyphenLower := New(&deps.Deps{}, translators.GetTranslator("es-es")) 118 pi := 3.14159265359 119 120 c.Run("FormatNumber", func(c *qt.C) { 121 c.Parallel() 122 got, err := nsUnderscoreUpper.FormatNumber(3, pi) 123 c.Assert(err, qt.IsNil) 124 c.Assert(got, qt.Equals, "3,142") 125 126 got, err = nsUnderscoreLower.FormatNumber(3, pi) 127 c.Assert(err, qt.IsNil) 128 c.Assert(got, qt.Equals, "3,142") 129 130 got, err = nsHyphenUpper.FormatNumber(3, pi) 131 c.Assert(err, qt.IsNil) 132 c.Assert(got, qt.Equals, "3,142") 133 134 got, err = nsHyphenLower.FormatNumber(3, pi) 135 c.Assert(err, qt.IsNil) 136 c.Assert(got, qt.Equals, "3,142") 137 138 }) 139 140 }