-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfunc_test.go
270 lines (246 loc) · 7.34 KB
/
func_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package roulette
import (
"bytes"
"fmt"
"log"
"testing"
"text/template"
"github.com/stretchr/testify/assert"
)
type cmpTest struct {
expr string
truth string
ok bool
}
var cmpTests = []cmpTest{
{"eq 1 2 | in 2 1 3 ", "false", true},
{"in 2 1 3 ", "true", true},
{"eq 1 1 | in 2 1 3 ", "true", true},
{"and (eq 1 1) (ne 1 2)", "true", true},
{"eq 1 1 | and (eq 1 1) (ne 1 2)", "true", true},
{"eq 1 2 | and (eq 1 1) (ne 1 2)", "false", true},
{"or (eq 1 1) (ne 1 1)", "true", true},
{"eq 1 2 | or (eq 1 1) (ne 1 1)", "false", true},
{"ne 1 2 | or (eq 1 1) (ne 1 1)", "true", true},
{"or (eq 2 1) (ne 1 2)", "true", true},
{"or (eq 2 1) (ne 1 1)", "false", true},
{"not false", "true", true},
{"eq 1 2 | not false", "false", true},
{"eq 1 1 | not false", "true", true},
{"eq true true", "true", true},
{"ne 1 2 | eq true true", "true", true},
{"eq true false", "false", true},
{"eq 1+2i 1+2i", "true", true},
{"eq 1+2i 1+3i", "false", true},
{"eq 1.5 1.5", "true", true},
{"eq 1.5 2.5", "false", true},
{"eq 1 1", "true", true},
{"eq 1 2", "false", true},
{"eq `xy` `xy`", "true", true},
{"eq `xy` `xyz`", "false", true},
{"eq .Uthree .Uthree", "true", true},
{"eq .Uthree .Ufour", "false", true},
{"eq 3 4 5 6 3", "true", true},
{"eq 3 4 5 6 7", "false", true},
{"ne true true", "false", true},
{"eq 1 1 | ne true true", "false", true},
{"ne true false", "true", true},
{"eq 1 2 | ne true false", "false", true},
{"ne 1+2i 1+2i", "false", true},
{"ne 1+2i 1+3i", "true", true},
{"ne 1.5 1.5", "false", true},
{"ne 1.5 2.5", "true", true},
{"ne 1 1", "false", true},
{"ne 1 2", "true", true},
{"ne `xy` `xy`", "false", true},
{"ne `xy` `xyz`", "true", true},
{"ne .Uthree .Uthree", "false", true},
{"ne .Uthree .Ufour", "true", true},
{"lt 1.5 1.5", "false", true},
{"eq 1 1 | lt 1.5 1.5", "false", true},
{"lt 1.5 2.5", "true", true},
{"eq 1 2 | lt 1.5 2.5", "false", true},
{"lt 1 1", "false", true},
{"lt 1 2", "true", true},
{"lt `xy` `xy`", "false", true},
{"lt `xy` `xyz`", "true", true},
{"lt .Uthree .Uthree", "false", true},
{"lt .Uthree .Ufour", "true", true},
{"le 1.5 1.5", "true", true},
{"eq 1 1 | le 1.5 1.5", "true", true},
{"le 1.5 2.5", "true", true},
{"le 2.5 1.5", "false", true},
{"le 1 1", "true", true},
{"le 1 2", "true", true},
{"le 2 1", "false", true},
{"le `xy` `xy`", "true", true},
{"le `xy` `xyz`", "true", true},
{"le `xyz` `xy`", "false", true},
{"le .Uthree .Uthree", "true", true},
{"le .Uthree .Ufour", "true", true},
{"le .Ufour .Uthree", "false", true},
{"gt 1.5 1.5", "false", true},
{"eq 1 2 | gt 1.5 1.5", "false", true},
{"gt 1.5 2.5", "false", true},
{"gt 1 1", "false", true},
{"gt 2 1", "true", true},
{"gt 1 2", "false", true},
{"gt `xy` `xy`", "false", true},
{"gt `xy` `xyz`", "false", true},
{"gt .Uthree .Uthree", "false", true},
{"gt .Uthree .Ufour", "false", true},
{"gt .Ufour .Uthree", "true", true},
{"ge 1.5 1.5", "true", true},
{"eq 1 1 | ge 1.5 1.5", "true", true},
{"ge 1.5 2.5", "false", true},
{"ge 2.5 1.5", "true", true},
{"ge 1 1", "true", true},
{"ge 1 2", "false", true},
{"ge 2 1", "true", true},
{"ge `xy` `xy`", "true", true},
{"ge `xy` `xyz`", "false", true},
{"ge `xyz` `xy`", "true", true},
{"ge .Uthree .Uthree", "true", true},
{"ge .Uthree .Ufour", "false", true},
{"ge .Ufour .Uthree", "true", true},
// Mixing signed and unsigned integers.
{"eq .Uthree .Three", "true", true},
{"eq .Three .Uthree", "true", true},
{"le .Uthree .Three", "true", true},
{"le .Three .Uthree", "true", true},
{"ge .Uthree .Three", "true", true},
{"ge .Three .Uthree", "true", true},
{"lt .Uthree .Three", "false", true},
{"lt .Three .Uthree", "false", true},
{"gt .Uthree .Three", "false", true},
{"gt .Three .Uthree", "false", true},
{"eq .Ufour .Three", "false", true},
{"lt .Ufour .Three", "false", true},
{"gt .Ufour .Three", "true", true},
{"eq .NegOne .Uthree", "false", true},
{"eq .Uthree .NegOne", "false", true},
{"ne .NegOne .Uthree", "true", true},
{"ne .Uthree .NegOne", "true", true},
{"lt .NegOne .Uthree", "true", true},
{"lt .Uthree .NegOne", "false", true},
{"le .NegOne .Uthree", "true", true},
{"le .Uthree .NegOne", "false", true},
{"gt .NegOne .Uthree", "false", true},
{"gt .Uthree .NegOne", "true", true},
{"ge .NegOne .Uthree", "false", true},
{"ge .Uthree .NegOne", "true", true},
{"eq (index `x` 0) 'x'", "true", true}, // The example that triggered this rule.
{"eq (index `x` 0) 'y'", "false", true},
// Errors
{"eq `xy` 1", "", false}, // Different types.
{"eq 2 2.0", "", false}, // Different types.
{"lt true true", "", false}, // Unordered types.
{"lt 1+0i 1+0i", "", false}, // Unordered types.
}
func TestComparison(t *testing.T) {
b := new(bytes.Buffer)
var cmpStruct = struct {
Uthree, Ufour uint
NegOne, Three int
}{3, 4, -1, 3}
for _, test := range cmpTests {
text := fmt.Sprintf("{{if %s}}true{{else}}false{{end}}", test.expr)
tmpl, err := template.New("empty").Funcs(defaultFuncMap).Parse(text)
if err != nil {
t.Fatalf("%q: %s", test.expr, err)
}
b.Reset()
err = tmpl.Execute(b, &cmpStruct)
if test.ok && err != nil {
t.Errorf("%s errored incorrectly: %s", test.expr, err)
continue
}
if !test.ok && err == nil {
t.Errorf("%s did not error", test.expr)
continue
}
if b.String() != test.truth {
t.Errorf("%s: want %s; got %s", test.expr, test.truth, b.String())
}
}
}
func TestValidateFuncs(t *testing.T) {
testFuncMap1 := template.FuncMap{
"test1": func(a, b int, prev ...bool) bool {
return true
},
}
err := validateFuncs(testFuncMap1)
if err != nil {
log.Fatal("testFuncMap1 must be valid")
}
testFuncMap2 := template.FuncMap{
"test2": func(a, b int, prev ...bool) (bool, error) {
return false, nil
},
}
err = validateFuncs(testFuncMap2)
if err != nil {
log.Fatal("testFuncMap2 must be valid")
}
testFuncMap3 := template.FuncMap{
"test3": func(a, b int, prev ...bool) (bool, int) {
return false, 0
},
}
err = validateFuncs(testFuncMap3)
if err == nil {
log.Fatal("testFuncMap3 must be invalid")
}
var _f = func(a, b int, prev ...bool) (bool, error) {
return false, nil
}
testFuncMap4 := template.FuncMap{
"_%f": _f,
}
err = validateFuncs(testFuncMap4)
if err == nil {
log.Fatal("testFuncMap4 must be invalid")
}
testFuncMap4 = template.FuncMap{
"_f%f": _f,
}
err = validateFuncs(testFuncMap4)
if err == nil {
log.Fatal("testFuncMap4 must be invalid")
}
testFuncMap4 = template.FuncMap{
"": _f,
}
err = validateFuncs(testFuncMap4)
if err == nil {
log.Fatal("testFuncMap4 must be invalid")
}
testFuncMap4 = template.FuncMap{
"": err,
}
err = validateFuncs(testFuncMap4)
if err == nil {
log.Fatal("testFuncMap4 must be invalid")
}
}
func TestCeil(t *testing.T) {
assert.Equal(t, 123.0, ceil(123))
assert.Equal(t, 123.0, ceil("123"))
assert.Equal(t, 124.0, ceil(123.01))
assert.Equal(t, 124.0, ceil("123.01"))
}
func TestFloor(t *testing.T) {
assert.Equal(t, 123.0, floor(123))
assert.Equal(t, 123.0, floor("123"))
assert.Equal(t, 123.0, floor(123.9999))
assert.Equal(t, 123.0, floor("123.9999"))
}
func TestRound(t *testing.T) {
assert.Equal(t, 123.556, round(123.5555, 3))
assert.Equal(t, 123.556, round("123.55555", 3))
assert.Equal(t, 124.0, round(123.500001, 0))
assert.Equal(t, 123.0, round(123.49999999, 0))
assert.Equal(t, 123.23, round(123.2329999, 2, .3))
assert.Equal(t, 123.24, round(123.233, 2, .3))
}