summaryrefslogtreecommitdiff
path: root/format_test.go
blob: bce622d19406d23fecac9850d714fe49f874f902 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package float

import "testing"

func cmp(a, b []string) bool {
	if len(a) != len(b) {
		return false
	}
	for i := 0; i < len(a); i++ {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}

func TestSplit(t *testing.T) {
	testCases := []struct {
		s string
		v []string
	}{
		{"", []string{}},
		{"1", []string{"1"}},
		{"12", []string{"12"}},
		{"123", []string{"123"}},
		{"1234", []string{"1", "234"}},
		{"12345", []string{"12", "345"}},
		{"123456", []string{"123", "456"}},
		{"1234567", []string{"1", "234", "567"}},
	}

	for _, tc := range testCases {
		t.Run(tc.s, func(t *testing.T) {
			v := split(tc.s, 3)
			if !cmp(v, tc.v) {
				t.Errorf("got %q, want %q", v, tc.v)
			}
		})
	}
}

func TestFill(t *testing.T) {
	testCases := []struct {
		s string
		v string
	}{
		{"", "00"},
		{"1", "10"},
		{"01", "01"},
		{"123", "12"},
	}

	for _, tc := range testCases {
		t.Run(tc.s, func(t *testing.T) {
			v := fill(tc.s, 2)
			if v != tc.v {
				t.Errorf("got %q, want %q", v, tc.v)
			}
		})
	}
}

func TestFormat(t *testing.T) {
	testCases := []struct {
		f float64
		s string
	}{
		{0.0, "0,00"},
		{1.0, "1,00"},
		{1000.0, "1 000,00"},
		{10000.0, "10 000,00"},
		{10000.10, "10 000,10"},
		{1234567.89, "1 234 567,89"},
		{-1234567.89, "-1 234 567,89"},
		{-1000.0, "-1 000,00"},
		{-100.0, "-100,00"},
		{-10.0, "-10,00"},
		{1.0 / 3, "0,33"},
		{0.555, "0,55"},
	}

	for _, tc := range testCases {
		t.Run(tc.s, func(t *testing.T) {
			s := Format(tc.f)
			if s != tc.s {
				t.Errorf("got %q, want %q", s, tc.s)
			}
		})
	}
}

func BenchmarkFormat(b *testing.B) {
	for i := 0; i < b.N; i++ {
		Format(1234567.89)
	}
}

func TestCountry(t *testing.T) {
	f := 1234567.89
	testCases := []struct {
		f Country
		v string
	}{
		{EN, "1 234 567.89"},
		{FR, "1 234 567,89"},
		{US, "1,234,567.89"},
		{DE, "1.234.567,89"},
		{IR, "1 234 567·89"},
		{CH, "1'234'567.89"},
		{IT, "1˙234˙567,89"},
		{CN, "123,4567.89"},
	}

	for _, tc := range testCases {
		t.Run(tc.v, func(t *testing.T) {
			v := tc.f.Format(f)
			if v != tc.v {
				t.Errorf("got %q, want %q", v, tc.v)
			}
		})
	}
}