2016-12-28 23:09:31 -05:00
|
|
|
|
package lang
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
2021-08-02 10:24:04 -04:00
|
|
|
|
translators "github.com/gohugoio/localescompressed"
|
2019-08-10 15:05:17 -04:00
|
|
|
|
qt "github.com/frankban/quicktest"
|
2017-06-13 12:42:45 -04:00
|
|
|
|
"github.com/gohugoio/hugo/deps"
|
2016-12-28 23:09:31 -05:00
|
|
|
|
)
|
|
|
|
|
|
2021-07-28 06:28:52 -04:00
|
|
|
|
func TestNumFmt(t *testing.T) {
|
2016-12-28 23:09:31 -05:00
|
|
|
|
t.Parallel()
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c := qt.New(t)
|
2016-12-28 23:09:31 -05:00
|
|
|
|
|
2021-07-28 06:28:52 -04:00
|
|
|
|
ns := New(&deps.Deps{}, nil)
|
2016-12-28 23:09:31 -05:00
|
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
|
prec int
|
|
|
|
|
n float64
|
|
|
|
|
runes string
|
2018-10-02 10:41:48 -04:00
|
|
|
|
delim string
|
2016-12-28 23:09:31 -05:00
|
|
|
|
|
|
|
|
|
want string
|
|
|
|
|
}{
|
2018-10-02 10:41:48 -04:00
|
|
|
|
{2, -12345.6789, "", "", "-12,345.68"},
|
|
|
|
|
{2, -12345.6789, "- . ,", "", "-12,345.68"},
|
|
|
|
|
{2, -12345.1234, "- . ,", "", "-12,345.12"},
|
2016-12-28 23:09:31 -05:00
|
|
|
|
|
2018-10-02 10:41:48 -04:00
|
|
|
|
{2, 12345.6789, "- . ,", "", "12,345.68"},
|
|
|
|
|
{0, 12345.6789, "- . ,", "", "12,346"},
|
|
|
|
|
{11, -12345.6789, "- . ,", "", "-12,345.67890000000"},
|
2016-12-28 23:09:31 -05:00
|
|
|
|
|
2020-04-01 18:13:23 -04:00
|
|
|
|
{2, 927.675, "- .", "", "927.68"},
|
|
|
|
|
{2, 1927.675, "- .", "", "1927.68"},
|
|
|
|
|
{2, 2927.675, "- .", "", "2927.68"},
|
|
|
|
|
|
2018-10-02 10:41:48 -04:00
|
|
|
|
{3, -12345.6789, "- ,", "", "-12345,679"},
|
|
|
|
|
{6, -12345.6789, "- , .", "", "-12.345,678900"},
|
|
|
|
|
|
|
|
|
|
{3, -12345.6789, "-|,| ", "|", "-12 345,679"},
|
|
|
|
|
{6, -12345.6789, "-|,| ", "|", "-12 345,678900"},
|
2016-12-28 23:09:31 -05:00
|
|
|
|
|
|
|
|
|
// Arabic, ar_AE
|
2018-10-02 10:41:48 -04:00
|
|
|
|
{6, -12345.6789, "- ٫ ٬", "", "-12٬345٫678900"},
|
|
|
|
|
{6, -12345.6789, "-|٫| ", "|", "-12 345٫678900"},
|
2016-12-28 23:09:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
for _, cas := range cases {
|
2016-12-28 23:09:31 -05:00
|
|
|
|
var s string
|
|
|
|
|
var err error
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
if len(cas.runes) == 0 {
|
2021-07-28 06:28:52 -04:00
|
|
|
|
s, err = ns.FormatNumberCustom(cas.prec, cas.n)
|
2016-12-28 23:09:31 -05:00
|
|
|
|
} else {
|
2019-08-10 15:05:17 -04:00
|
|
|
|
if cas.delim == "" {
|
2021-07-28 06:28:52 -04:00
|
|
|
|
s, err = ns.FormatNumberCustom(cas.prec, cas.n, cas.runes)
|
2018-10-02 10:41:48 -04:00
|
|
|
|
} else {
|
2021-07-28 06:28:52 -04:00
|
|
|
|
s, err = ns.FormatNumberCustom(cas.prec, cas.n, cas.runes, cas.delim)
|
2018-10-02 10:41:48 -04:00
|
|
|
|
}
|
2016-12-28 23:09:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
c.Assert(s, qt.Equals, cas.want)
|
2016-12-28 23:09:31 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-28 06:28:52 -04:00
|
|
|
|
|
|
|
|
|
func TestFormatNumbers(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
c := qt.New(t)
|
|
|
|
|
|
|
|
|
|
nsNn := New(&deps.Deps{}, translators.GetTranslator("nn"))
|
|
|
|
|
nsEn := New(&deps.Deps{}, translators.GetTranslator("en"))
|
|
|
|
|
pi := 3.14159265359
|
|
|
|
|
|
|
|
|
|
c.Run("FormatNumber", func(c *qt.C) {
|
|
|
|
|
c.Parallel()
|
|
|
|
|
got, err := nsNn.FormatNumber(3, pi)
|
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
c.Assert(got, qt.Equals, "3,142")
|
|
|
|
|
|
|
|
|
|
got, err = nsEn.FormatNumber(3, pi)
|
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
c.Assert(got, qt.Equals, "3.142")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
c.Run("FormatPercent", func(c *qt.C) {
|
|
|
|
|
c.Parallel()
|
|
|
|
|
got, err := nsEn.FormatPercent(3, 67.33333)
|
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
c.Assert(got, qt.Equals, "67.333%")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
c.Run("FormatCurrency", func(c *qt.C) {
|
|
|
|
|
c.Parallel()
|
|
|
|
|
got, err := nsEn.FormatCurrency(2, "USD", 20000)
|
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
c.Assert(got, qt.Equals, "$20,000.00")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
c.Run("FormatAccounting", func(c *qt.C) {
|
|
|
|
|
c.Parallel()
|
|
|
|
|
got, err := nsEn.FormatAccounting(2, "USD", 20000)
|
|
|
|
|
c.Assert(err, qt.IsNil)
|
|
|
|
|
c.Assert(got, qt.Equals, "$20,000.00")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|