2016-12-28 23:09:31 -05:00
|
|
|
|
package lang
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
|
"github.com/gohugoio/hugo/deps"
|
2016-12-28 23:09:31 -05:00
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestNumFormat(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
ns := New(&deps.Deps{})
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, c := range cases {
|
|
|
|
|
errMsg := fmt.Sprintf("[%d] %v", i, c)
|
|
|
|
|
|
|
|
|
|
var s string
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
if len(c.runes) == 0 {
|
|
|
|
|
s, err = ns.NumFmt(c.prec, c.n)
|
|
|
|
|
} else {
|
2018-10-02 10:41:48 -04:00
|
|
|
|
if c.delim == "" {
|
|
|
|
|
s, err = ns.NumFmt(c.prec, c.n, c.runes)
|
|
|
|
|
} else {
|
|
|
|
|
s, err = ns.NumFmt(c.prec, c.n, c.runes, c.delim)
|
|
|
|
|
}
|
2016-12-28 23:09:31 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
require.NoError(t, err, errMsg)
|
|
|
|
|
assert.Equal(t, c.want, s, errMsg)
|
|
|
|
|
}
|
|
|
|
|
}
|