-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinteger_test.go
67 lines (63 loc) · 1.81 KB
/
integer_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
package schema
import (
"testing"
"github.com/matryer/is"
)
const notBareInt = false
func TestCastInt(t *testing.T) {
t.Run("Success", func(t *testing.T) {
data := []struct {
desc string
number string
want int64
bn bool
}{
{"Positive_WithSignal", "+10", 10, defaultBareNumber},
{"Positive_WithoutSignal", "10", 10, defaultBareNumber},
{"Negative", "-10", -10, defaultBareNumber},
{"BareNumber", "€95", 95, notBareInt},
{"BareNumber_TrailingAtBeginning", "€95", 95, notBareInt},
{"BareNumber_TrailingAtBeginningSpace", "EUR 95", 95, notBareInt},
{"BareNumber_TrailingAtEnd", "95%", 95, notBareInt},
{"BareNumber_TrailingAtEndSpace", "95 %", 95, notBareInt},
}
for _, d := range data {
t.Run(d.desc, func(t *testing.T) {
is := is.New(t)
got, err := castInt(d.bn, d.number, Constraints{})
is.NoErr(err)
is.Equal(d.want, got)
})
}
})
t.Run("ValidMaximum", func(t *testing.T) {
is := is.New(t)
_, err := castInt(defaultBareNumber, "2", Constraints{Maximum: "2"})
is.NoErr(err)
})
t.Run("ValidMinimum", func(t *testing.T) {
is := is.New(t)
_, err := castInt(defaultBareNumber, "2", Constraints{Minimum: "1"})
is.NoErr(err)
})
t.Run("Error", func(t *testing.T) {
data := []struct {
desc string
number string
constraints Constraints
}{
{"InvalidIntToStrip_TooManyNumbers", "+10++10", Constraints{}},
{"NumBiggerThanMaximum", "3", Constraints{Maximum: "2"}},
{"InvalidMaximum", "1", Constraints{Maximum: "boo"}},
{"NumSmallerThanMinimum", "1", Constraints{Minimum: "2"}},
{"InvalidMinimum", "1", Constraints{Minimum: "boo"}},
}
for _, d := range data {
t.Run(d.desc, func(t *testing.T) {
is := is.New(t)
_, err := castInt(defaultBareNumber, d.number, d.constraints)
is.True(err != nil)
})
}
})
}