-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
160 lines (146 loc) · 4.39 KB
/
helpers_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package jsonlogic
import (
"math"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsLogic(t *testing.T) {
assert := assert.New(t)
for i, testCase := range []struct {
Obj interface{}
IsLogic bool
}{
{Obj: nil, IsLogic: false},
{Obj: true, IsLogic: false},
{Obj: float64(1.22), IsLogic: false},
{Obj: "x", IsLogic: false},
{Obj: []interface{}{"x"}, IsLogic: false},
{Obj: map[string]interface{}{}, IsLogic: false},
{Obj: map[string]interface{}{"var": ""}, IsLogic: true},
{Obj: map[string]interface{}{"x": "y", "a": 2}, IsLogic: false},
} {
assert.Equal(testCase.IsLogic, isLogic(testCase.Obj), "test case %d", i)
}
}
func TestGetLogic(t *testing.T) {
assert := assert.New(t)
for i, testCase := range []struct {
Obj interface{}
Op string
Params []interface{}
}{
{Obj: map[string]interface{}{"var": nil}, Op: "var", Params: []interface{}{nil}}, // {"var":null} -> [null]
{Obj: map[string]interface{}{"var": []interface{}{}}, Op: "var", Params: []interface{}{}}, // {"var":[]} -> []
{Obj: map[string]interface{}{"var": []interface{}{[]interface{}{}}}, Op: "var", Params: []interface{}{[]interface{}{}}}, // {"var":[[]]} -> [[]]
} {
op, params := getLogic(testCase.Obj)
assert.Equal(testCase.Op, op, "test case %d", i)
assert.Equal(testCase.Params, params, "test case %d", i)
}
}
func TestToBool(t *testing.T) {
assert := assert.New(t)
for i, testCase := range []struct {
Obj interface{}
IsTrue bool
}{
{Obj: nil, IsTrue: false},
{Obj: true, IsTrue: true},
{Obj: false, IsTrue: false},
{Obj: float64(0), IsTrue: false},
{Obj: float64(1.1), IsTrue: true},
{Obj: float64(-1.1), IsTrue: true},
{Obj: "", IsTrue: false},
{Obj: "x", IsTrue: true},
{Obj: []interface{}{}, IsTrue: false},
{Obj: []interface{}{"x"}, IsTrue: true},
{Obj: map[string]interface{}{}, IsTrue: true},
{Obj: map[string]interface{}{"var": "x"}, IsTrue: true},
} {
assert.Equal(testCase.IsTrue, ToBool(testCase.Obj), "test case %d", i)
}
}
func TestToNumeric(t *testing.T) {
assert := assert.New(t)
for i, testCase := range []struct {
Obj interface{}
N float64
Err bool
}{
{Obj: nil, N: 0},
{Obj: true, N: 1},
{Obj: false, N: 0},
{Obj: float64(1.11), N: 1.11},
{Obj: "1.11", N: 1.11},
{Obj: "a", Err: true},
{Obj: []interface{}{1}, Err: true},
{Obj: map[string]interface{}{}, Err: true},
{Obj: math.NaN(), Err: true},
{Obj: math.Inf(1), Err: true},
{Obj: math.Inf(-1), Err: true},
{Obj: "nan", Err: true},
{Obj: "-inf", Err: true},
{Obj: "+inf", Err: true},
} {
n, err := ToNumeric(testCase.Obj)
if testCase.Err {
assert.Error(err, "test case %d", i)
} else {
assert.NoError(err, "test case %d", i)
assert.Equal(testCase.N, n, "test case %d", i)
}
}
}
func TestToString(t *testing.T) {
assert := assert.New(t)
for i, testCase := range []struct {
Obj interface{}
S string
Err bool
}{
{Obj: nil, S: "null"},
{Obj: true, S: "true"},
{Obj: false, S: "false"},
{Obj: math.NaN(), Err: true},
{Obj: math.Inf(1), Err: true},
{Obj: math.Inf(-1), Err: true},
{Obj: 1.111, S: "1.111"},
{Obj: "xxx", S: "xxx"},
{Obj: []interface{}{1}, Err: true},
{Obj: map[string]interface{}{}, Err: true},
} {
s, err := ToString(testCase.Obj)
if testCase.Err {
assert.Error(err, "test case %d", i)
} else {
assert.NoError(err, "test case %d", i)
assert.Equal(testCase.S, s, "test case %d", i)
}
}
}
func TestCompareValues(t *testing.T) {
assert := assert.New(t)
for i, testCase := range []struct {
Symbol CompSymbol
Left interface{}
Right interface{}
Result bool
Err bool
}{
{Symbol: LT, Left: "1", Right: "a", Result: true}, // This is compared as strings.
{Symbol: LT, Left: "1", Right: float64(1.1), Result: true}, // This is compared as numerics.
{Symbol: LT, Left: float64(1), Right: "a", Err: true},
{Symbol: LT, Left: "a", Right: float64(1), Err: true},
{Symbol: LT, Left: []interface{}{}, Right: float64(1), Err: true},
{Symbol: EQ, Left: float64(3), Right: float64(3), Result: true},
{Symbol: EQ, Left: float64(3), Right: "3", Result: false},
} {
result, err := CompareValues(testCase.Symbol, testCase.Left, testCase.Right)
if testCase.Err {
assert.Error(err, "test case %d", i)
} else {
assert.NoError(err, "test case %d", i)
assert.Equal(testCase.Result, result, "test case %d", i)
}
}
}