-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathboolean_test.go
78 lines (73 loc) · 1.89 KB
/
boolean_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
package schema
import (
"testing"
"github.com/matryer/is"
)
func TestCastBoolean(t *testing.T) {
data := []struct {
Desc string
TrueValues []string
FalseValues []string
Value string
Expected bool
}{
{"simple true value", []string{"1"}, []string{"0"}, "1", true},
{"simple false value", []string{"1"}, []string{"0"}, "0", false},
{"duplicate value, true wins", []string{"1"}, []string{"1"}, "1", true},
}
for _, d := range data {
t.Run(d.Desc, func(t *testing.T) {
is := is.New(t)
b, err := castBoolean(d.Value, d.TrueValues, d.FalseValues)
is.NoErr(err)
is.Equal(b, d.Expected)
})
}
}
func TestCastBoolean_Error(t *testing.T) {
is := is.New(t)
_, err := castBoolean("foo", defaultTrueValues, defaultFalseValues)
is.True(err != nil)
}
func TestUncastBoolean(t *testing.T) {
t.Run("Success", func(t *testing.T) {
data := []struct {
desc string
value interface{}
want string
trueValues []string
falseValues []string
}{
{"True", true, "true", []string{}, []string{}},
{"False", false, "false", []string{}, []string{}},
{"TrueFromTrueValues", "0", "0", []string{"0"}, []string{}},
{"FalseFromFalseValues", "1", "1", []string{}, []string{"1"}},
}
for _, d := range data {
t.Run(d.desc, func(t *testing.T) {
is := is.New(t)
got, err := uncastBoolean(d.value, d.trueValues, d.falseValues)
is.NoErr(err)
is.Equal(d.want, got)
})
}
})
t.Run("Error", func(t *testing.T) {
data := []struct {
desc string
value interface{}
trueValues []string
falseValues []string
}{
{"InvalidType", 10, []string{}, []string{}},
{"NotInTrueOrFalseValues", "1", []string{}, []string{}},
}
for _, d := range data {
t.Run(d.desc, func(t *testing.T) {
is := is.New(t)
_, err := uncastBoolean(d.value, d.trueValues, d.falseValues)
is.True(err != nil)
})
}
})
}