aboutsummaryrefslogtreecommitdiffhomepage
path: root/tpl/lang/lang_test.go
blob: aee5675024b3888942d82f54e5ca03cc631af10f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package lang

import (
	"fmt"
	"testing"

	"github.com/gohugoio/hugo/deps"
	"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
		delim string

		want string
	}{
		{2, -12345.6789, "", "", "-12,345.68"},
		{2, -12345.6789, "- . ,", "", "-12,345.68"},
		{2, -12345.1234, "- . ,", "", "-12,345.12"},

		{2, 12345.6789, "- . ,", "", "12,345.68"},
		{0, 12345.6789, "- . ,", "", "12,346"},
		{11, -12345.6789, "- . ,", "", "-12,345.67890000000"},

		{3, -12345.6789, "- ,", "", "-12345,679"},
		{6, -12345.6789, "- , .", "", "-12.345,678900"},

		{3, -12345.6789, "-|,| ", "|", "-12 345,679"},
		{6, -12345.6789, "-|,| ", "|", "-12 345,678900"},

		// Arabic, ar_AE
		{6, -12345.6789, "‏- ٫ ٬", "", "‏-12٬345٫678900"},
		{6, -12345.6789, "‏-|٫| ", "|", "‏-12 345٫678900"},
	}

	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 {
			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)
			}
		}

		require.NoError(t, err, errMsg)
		assert.Equal(t, c.want, s, errMsg)
	}
}