-
Notifications
You must be signed in to change notification settings - Fork 3
/
number_test.go
100 lines (85 loc) · 1.93 KB
/
number_test.go
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
package numwords
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNumber_Value(t *testing.T) {
t.Parallel()
tests := []struct {
n int
d int
expected float64
}{
{1, 1, 1},
{2, 1, 2},
{1, 2, 0.5},
}
for _, test := range tests {
n := number{numerator: test.n, denominator: test.d}
assert.Equal(t, test.expected, n.Value(), "%+v", test)
}
}
func TestNumber_String(t *testing.T) {
t.Parallel()
tests := []struct {
n int
d int
o bool
expected string
}{
{1, 1, false, "1"},
{2, 1, false, "2"},
{1, 2, false, "0.5"},
{1, 3, false, "0.333333"},
{2, 3, false, "0.666667"},
{1, 1, true, "1st"},
{2, 1, true, "2nd"},
{3, 1, true, "3rd"},
{4, 1, true, "4th"},
{11, 1, true, "11th"},
{12, 1, true, "12th"},
{13, 1, true, "13th"},
{21, 1, true, "21st"},
{22, 1, true, "22nd"},
{23, 1, true, "23rd"},
{24, 1, true, "24th"},
{1, 2, true, "1st"},
}
for _, test := range tests {
n := number{
numerator: test.n,
denominator: test.d,
ordinal: test.o,
}
assert.Equal(t, test.expected, n.String(), "%+v", test)
}
}
func TestNumber_MaybeNumeric(t *testing.T) {
t.Parallel()
tests := []struct {
in string
val float64
typ numberType
ok bool
}{
{in: "foo", ok: false},
{in: "fist", ok: false},
{"12", float64(12), numDirect, true},
{"2", float64(2), numSingle, true},
{"22", float64(22), numTens, true},
{"222", float64(222), numBig, true},
{"0.5", float64(.5), numFraction, true},
{"12th", float64(12), numDirectOrdinal, true},
{"2nd", float64(2), numSingleOrdinal, true},
{"22nd", float64(22), numTensOrdinal, true},
{"222nd", float64(222), numBigOrdinal, true},
{"2,222,222", float64(2222222), numBig, true},
}
for _, test := range tests {
n, ok := maybeNumeric(test.in)
if assert.Equal(t, test.ok, ok) && ok {
assert.Equal(t, test.val, n.Value())
assert.Equal(t, test.typ, n.typ)
}
}
}