-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.go
210 lines (186 loc) · 5.63 KB
/
tests.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
package builtins
import (
"strings"
"github.com/aisbergg/gonja/pkg/gonja/errors"
"github.com/aisbergg/gonja/pkg/gonja/exec"
)
var Tests = exec.TestSet{
"callable": testCallable,
"defined": testDefined,
"divisibleby": testDivisibleby,
"eq": testEqual,
"equalto": testEqual,
"==": testEqual,
// TODO: "escaped": testEscaped,
"even": testEven,
"ge": testGreaterEqual,
">=": testGreaterEqual,
"gt": testGreaterThan,
"greaterthan": testGreaterThan,
">": testGreaterThan,
"in": testIn,
"iterable": testIterable,
"le": testLessEqual,
"<=": testLessEqual,
"lower": testLower,
"lt": testLessThan,
"lessthan": testLessThan,
"<": testLessThan,
"mapping": testMapping,
"ne": testNotEqual,
"!=": testNotEqual,
"none": testNone,
"number": testNumber,
"odd": testOdd,
"sameas": testSameas,
"sequence": testIterable,
"string": testString,
"undefined": testUndefined,
"upper": testUpper,
}
// testCallable returns true if the input is a callable value.
func testCallable(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
return in.IsCallable()
}
// testDefined returns true if the input is a defined value.
func testDefined(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
return exec.IsDefined(in)
}
// testDivisibleby returns true if the input is divisible by the given number.
func testDivisibleby(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
p := params.ExpectArgs(1)
if p.IsError() {
errors.ThrowFilterArgumentError("divisibleby(value, num)", p.Error())
}
param := params.Args[0]
if param.Integer() == 0 {
return false
}
return in.Integer()%param.Integer() == 0
}
func testEqual(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
p := params.ExpectArgs(1)
if p.IsError() {
errors.ThrowFilterArgumentError("eq(value, other)", p.Error())
}
param := params.Args[0]
return in.Interface() == param.Interface()
}
func testEven(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
if !in.IsInteger() {
return false
}
return in.Integer()%2 == 0
}
func testGreaterEqual(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
p := params.ExpectArgs(1)
if p.IsError() {
errors.ThrowFilterArgumentError("ge(value, other)", p.Error())
}
param := params.Args[0]
if !in.IsNumber() || !param.IsNumber() {
return false
}
return in.Float() >= param.Float()
}
func testGreaterThan(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
p := params.ExpectArgs(1)
if p.IsError() {
errors.ThrowFilterArgumentError("gt(value, other)", p.Error())
}
param := params.Args[0]
if !in.IsNumber() || !param.IsNumber() {
return false
}
return in.Float() > param.Float()
}
func testIn(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
p := params.ExpectArgs(1)
if p.IsError() {
errors.ThrowFilterArgumentError("in(value, seq)", p.Error())
}
seq := params.Args[0]
return seq.Contains(in)
}
func testIterable(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
return in.IsSliceable()
}
func testLessEqual(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
p := params.ExpectArgs(1)
if p.IsError() {
errors.ThrowFilterArgumentError("le(value, other)", p.Error())
}
param := params.Args[0]
if !in.IsNumber() || !param.IsNumber() {
return false
}
return in.Float() <= param.Float()
}
func testLower(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
if !in.IsString() {
return false
}
return strings.ToLower(in.String()) == in.String()
}
func testLessThan(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
p := params.ExpectArgs(1)
if p.IsError() {
errors.ThrowFilterArgumentError("lt(value, other)", p.Error())
}
param := params.Args[0]
if !in.IsNumber() || !param.IsNumber() {
return false
}
return in.Float() < param.Float()
}
func testMapping(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
return in.IsDict()
}
func testNotEqual(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
p := params.ExpectArgs(1)
if p.IsError() {
errors.ThrowFilterArgumentError("ne(value, other)", p.Error())
}
param := params.Args[0]
return in.Interface() != param.Interface()
}
func testNone(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
return in.IsNil()
}
func testNumber(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
return in.IsNumber()
}
func testOdd(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
if !in.IsInteger() {
return false
}
return in.Integer()%2 == 1
}
// testSameas returns true if the input points to the same memory address as the
// other value.
func testSameas(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
p := params.ExpectArgs(1)
if p.IsError() {
errors.ThrowFilterArgumentError("sameas(other)", p.Error())
}
param := params.Args[0]
if in.IsNil() && param.IsNil() {
return true
} else if param.ReflectValue().CanAddr() && in.ReflectValue().CanAddr() {
return param.ReflectValue().Addr() == in.ReflectValue().Addr()
}
return false
// return reflect.Indirect(param.ReflectValue()) == reflect.Indirect(in.ReflectValue())
}
func testString(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
return in.IsString()
}
func testUndefined(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
return !exec.IsDefined(in)
}
func testUpper(ctx *exec.Context, in exec.Value, params *exec.VarArgs) bool {
if !in.IsString() {
return false
}
return strings.ToUpper(in.String()) == in.String()
}