-
Notifications
You must be signed in to change notification settings - Fork 3
/
numbers_test.go
109 lines (83 loc) · 2.15 KB
/
numbers_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
101
102
103
104
105
106
107
108
109
package numwords
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNumbers_Pattern(t *testing.T) {
t.Parallel()
ns := numbers{
{typ: numAnd},
{typ: numDirect},
{typ: numSingle},
{typ: numTens},
{typ: numBig},
{typ: numFraction},
{typ: numDirectOrdinal},
{typ: numSingleOrdinal},
{typ: numTensOrdinal},
{typ: numBigOrdinal},
{typ: numDone},
{typ: numDone + 1},
}
assert.Equal(t, "&dstbfDSTB__", ns.pattern())
}
func TestNumbers_Strings(t *testing.T) {
t.Parallel()
ns := numbers{
number{numerator: 1000, denominator: 1, typ: numBig},
number{numerator: 2, denominator: 1, typ: numSingleOrdinal, ordinal: true},
number{numerator: 1, denominator: 2, typ: numFraction},
}
assert.EqualValues(t, []string{"1000", "2nd", "0.5"}, ns.strings())
}
func TestNumbers_String(t *testing.T) {
t.Parallel()
ns := numbers{
number{numerator: 1000, denominator: 1, typ: numBig},
number{numerator: 2, denominator: 1, typ: numSingleOrdinal, ordinal: true},
number{numerator: 1, denominator: 2, typ: numFraction},
}
assert.EqualValues(t, "1000 2nd 0.5", ns.String())
}
func TestNumbers_Reduce(t *testing.T) {
t.Parallel()
ns := numbers{
number{numerator: 1000, denominator: 1, typ: numBig},
number{numerator: 2, denominator: 1, typ: numSingle},
number{numerator: 100, denominator: 1, typ: numBig},
}
out := reduce(ns)
assert.Len(t, out, 1)
assert.Equal(t, float64(1200), out[0].Value())
assert.Equal(t, numBig, out[0].typ)
}
func TestNumbers_Float(t *testing.T) {
t.Parallel()
ns := numbers{
number{numerator: 3, denominator: 2},
}
out, err := ns.Float()
assert.NoError(t, err)
assert.Equal(t, float64(1.5), out)
ns = append(ns, number{})
_, err = ns.Float()
assert.Equal(t, ErrManyNumbers, err)
ns = ns[:0]
_, err = ns.Float()
assert.Equal(t, ErrNoNumbers, err)
}
func TestNumbers_Int(t *testing.T) {
t.Parallel()
ns := numbers{
number{numerator: 3, denominator: 2},
}
out, err := ns.Int()
assert.NoError(t, err)
assert.Equal(t, 1, out)
ns = append(ns, number{})
_, err = ns.Int()
assert.Equal(t, ErrManyNumbers, err)
ns = ns[:0]
_, err = ns.Int()
assert.Equal(t, ErrNoNumbers, err)
}