-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_node_test.go
More file actions
199 lines (191 loc) · 10.9 KB
/
Copy pathbinary_node_test.go
File metadata and controls
199 lines (191 loc) · 10.9 KB
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
package expressionlanguage
import (
"errors"
"testing"
)
func TestBinaryNodeEvaluationCompilationAndDump(t *testing.T) {
list := NewArray(
ArrayElement{Value: NewConstant("a")},
ArrayElement{Value: NewConstant("b")},
)
tests := []struct {
name string
operator string
left Node
right Node
want any
wantDump string
}{
{name: "word or", operator: "or", left: NewConstant(true), right: NewConstant(false), want: true, wantDump: "(true or false)"},
{name: "symbol or", operator: "||", left: NewConstant(true), right: NewConstant(false), want: true, wantDump: "(true || false)"},
{name: "xor", operator: "xor", left: NewConstant(true), right: NewConstant(true), want: false, wantDump: "(true xor true)"},
{name: "word and", operator: "and", left: NewConstant(true), right: NewConstant(false), want: false, wantDump: "(true and false)"},
{name: "symbol and", operator: "&&", left: NewConstant(true), right: NewConstant(false), want: false, wantDump: "(true && false)"},
{name: "bitwise and", operator: "&", left: NewConstant(2), right: NewConstant(4), want: 0, wantDump: "(2 & 4)"},
{name: "bitwise or", operator: "|", left: NewConstant(2), right: NewConstant(4), want: 6, wantDump: "(2 | 4)"},
{name: "bitwise xor", operator: "^", left: NewConstant(2), right: NewConstant(4), want: 6, wantDump: "(2 ^ 4)"},
{name: "shift left", operator: "<<", left: NewConstant(2), right: NewConstant(4), want: 32, wantDump: "(2 << 4)"},
{name: "shift right", operator: ">>", left: NewConstant(32), right: NewConstant(4), want: 2, wantDump: "(32 >> 4)"},
{name: "less", operator: "<", left: NewConstant(1), right: NewConstant(2), want: true, wantDump: "(1 < 2)"},
{name: "less or equal less", operator: "<=", left: NewConstant(1), right: NewConstant(2), want: true, wantDump: "(1 <= 2)"},
{name: "less or equal equal", operator: "<=", left: NewConstant(1), right: NewConstant(1), want: true, wantDump: "(1 <= 1)"},
{name: "greater", operator: ">", left: NewConstant(1), right: NewConstant(2), want: false, wantDump: "(1 > 2)"},
{name: "greater or equal less", operator: ">=", left: NewConstant(1), right: NewConstant(2), want: false, wantDump: "(1 >= 2)"},
{name: "greater or equal equal", operator: ">=", left: NewConstant(1), right: NewConstant(1), want: true, wantDump: "(1 >= 1)"},
{name: "strict equal", operator: "===", left: NewConstant(true), right: NewConstant(true), want: true, wantDump: "(true === true)"},
{name: "strict not equal", operator: "!==", left: NewConstant(true), right: NewConstant(true), want: false, wantDump: "(true !== true)"},
{name: "loose equal false", operator: "==", left: NewConstant(2), right: NewConstant(1), want: false, wantDump: "(2 == 1)"},
{name: "loose equal coercion", operator: "==", left: NewConstant("1"), right: NewConstant(1), want: true, wantDump: `("1" == 1)`},
{name: "loose not equal", operator: "!=", left: NewConstant(2), right: NewConstant(1), want: true, wantDump: "(2 != 1)"},
{name: "subtract", operator: "-", left: NewConstant(1), right: NewConstant(2), want: -1, wantDump: "(1 - 2)"},
{name: "add", operator: "+", left: NewConstant(1), right: NewConstant(2), want: 3, wantDump: "(1 + 2)"},
{name: "multiply", operator: "*", left: NewConstant(2), right: NewConstant(2), want: 4, wantDump: "(2 * 2)"},
{name: "divide", operator: "/", left: NewConstant(2), right: NewConstant(2), want: 1, wantDump: "(2 / 2)"},
{name: "modulo", operator: "%", left: NewConstant(5), right: NewConstant(2), want: 1, wantDump: "(5 % 2)"},
{name: "power", operator: "**", left: NewConstant(5), right: NewConstant(2), want: 25, wantDump: "(5 ** 2)"},
{name: "concatenate", operator: "~", left: NewConstant("a"), right: NewConstant("b"), want: "ab", wantDump: `("a" ~ "b")`},
{name: "in true", operator: "in", left: NewConstant("a"), right: list, want: true, wantDump: `("a" in ["a", "b"])`},
{name: "in false", operator: "in", left: NewConstant("c"), right: list, want: false, wantDump: `("c" in ["a", "b"])`},
{name: "not in true", operator: "not in", left: NewConstant("c"), right: list, want: true, wantDump: `("c" not in ["a", "b"])`},
{name: "not in false", operator: "not in", left: NewConstant("a"), right: list, want: false, wantDump: `("a" not in ["a", "b"])`},
{name: "range", operator: "..", left: NewConstant(1), right: NewConstant(3), want: []any{1, 2, 3}, wantDump: "(1 .. 3)"},
{name: "starts with true", operator: "starts with", left: NewConstant("abc"), right: NewConstant("a"), want: true, wantDump: `("abc" starts with "a")`},
{name: "starts with false", operator: "starts with", left: NewConstant("abc"), right: NewConstant("b"), want: false, wantDump: `("abc" starts with "b")`},
{name: "ends with true", operator: "ends with", left: NewConstant("abc"), right: NewConstant("c"), want: true, wantDump: `("abc" ends with "c")`},
{name: "ends with false", operator: "ends with", left: NewConstant("abc"), right: NewConstant("b"), want: false, wantDump: `("abc" ends with "b")`},
{name: "ends with compile-provider operand", operator: "ends with", left: NewConstant("abc"), right: NewConstant("a"), want: false, wantDump: `("abc" ends with "a")`},
{name: "contains true", operator: "contains", left: NewConstant("abc"), right: NewConstant("b"), want: true, wantDump: `("abc" contains "b")`},
{name: "contains false", operator: "contains", left: NewConstant("abc"), right: NewConstant("z"), want: false, wantDump: `("abc" contains "z")`},
{name: "matches true", operator: "matches", left: NewConstant("abc"), right: NewConstant("/^[a-z]+$/"), want: 1, wantDump: `("abc" matches "/^[a-z]+$/")`},
{name: "matches false", operator: "matches", left: NewConstant(""), right: NewConstant("/^[a-z]+$/"), want: 0, wantDump: `("" matches "/^[a-z]+$/")`},
{name: "matches nil", operator: "matches", left: NewConstant(nil), right: NewConstant("/^[a-z]+$/"), want: 0, wantDump: `(null matches "/^[a-z]+$/")`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
node := NewBinary(test.operator, test.left, test.right)
assertEqual(t, evaluateNode(t, node, nil, nil), test.want)
assertEqual(t, compileAndRunNode(t, node, nil, nil), test.want)
if got := node.Dump(); got != test.wantDump {
t.Errorf("Dump() = %q; want %q", got, test.wantDump)
}
})
}
}
func TestBinaryNodeShortCircuits(t *testing.T) {
failure := errors.New("right operand must not run")
tests := []struct {
name string
operator string
left bool
want bool
}{
{name: "word and", operator: "and", left: false, want: false},
{name: "symbol and", operator: "&&", left: false, want: false},
{name: "word or", operator: "or", left: true, want: true},
{name: "symbol or", operator: "||", left: true, want: true},
}
for _, test := range tests {
t.Run(test.name+"/evaluate", func(t *testing.T) {
right := &spyNode{evaluateErr: failure}
node := NewBinary(test.operator, NewConstant(test.left), right)
assertEqual(t, evaluateNode(t, node, nil, nil), test.want)
if right.evaluateCalls != 0 {
t.Errorf("right operand evaluated %d times; want 0", right.evaluateCalls)
}
})
t.Run(test.name+"/compiled", func(t *testing.T) {
runs := 0
right := &spyNode{value: true}
node := NewBinary(test.operator, NewConstant(test.left), right)
program, err := node.Compile(nil)
if err != nil {
t.Fatalf("Compile() returned an unexpected error: %v", err)
}
_ = runs
got, err := program(nil)
if err != nil {
t.Fatalf("compiled program returned an unexpected error: %v", err)
}
assertEqual(t, got, test.want)
})
}
}
func TestMatchesRejectsInvalidRegularExpressions(t *testing.T) {
t.Run("literal/evaluate", func(t *testing.T) {
node := NewBinary("matches", NewConstant("abc"), NewConstant("this is not a regexp"))
_, err := node.Evaluate(nil, nil)
assertSyntaxError(t, err, `Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric`)
})
t.Run("variable/evaluate", func(t *testing.T) {
node := NewBinary("matches", NewConstant("abc"), NewName("regexp"))
_, err := node.Evaluate(nil, Variables{"regexp": "this is not a regexp"})
assertSyntaxError(t, err, `Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric`)
})
t.Run("literal/compile", func(t *testing.T) {
node := NewBinary("matches", NewConstant("abc"), NewConstant("this is not a regexp"))
_, err := node.Compile(nil)
assertSyntaxError(t, err, `Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric`)
})
t.Run("variable/compiled program", func(t *testing.T) {
node := NewBinary("matches", NewConstant("abc"), NewName("regexp"))
program, err := node.Compile(nil)
if err != nil {
t.Fatalf("Compile() returned an unexpected error: %v", err)
}
_, err = program(Variables{"regexp": "this is not a regexp"})
assertSyntaxError(t, err, `Regexp "this is not a regexp" passed to "matches" is not valid: Delimiter must not be alphanumeric`)
})
t.Run("non-string pattern", func(t *testing.T) {
node := NewBinary("matches", NewConstant("abc"), NewBinary("||", NewConstant(true), NewConstant(false)))
_, err := node.Compile(nil)
assertSyntaxError(t, err, `The regex passed to "matches" must be a string`)
})
t.Run("string-producing expression is accepted", func(t *testing.T) {
node := NewBinary("matches", NewConstant("abc"), NewBinary("~", NewConstant("/a"), NewConstant("bc/")))
assertEqual(t, compileAndRunNode(t, node, nil, nil), 1)
})
}
func TestBinaryNodeArithmeticErrors(t *testing.T) {
tests := []struct {
name string
operator string
message string
}{
{name: "division", operator: "/", message: "Division by zero."},
{name: "modulo", operator: "%", message: "Modulo by zero."},
}
for _, test := range tests {
t.Run(test.name+"/evaluate", func(t *testing.T) {
node := NewBinary(test.operator, NewConstant(1), NewConstant(0))
_, err := node.Evaluate(nil, nil)
assertErrorContains(t, err, test.message)
})
t.Run(test.name+"/compiled", func(t *testing.T) {
node := NewBinary(test.operator, NewConstant(1), NewConstant(0))
program, err := node.Compile(nil)
if err != nil {
t.Fatalf("Compile() returned an unexpected error: %v", err)
}
_, err = program(nil)
assertErrorContains(t, err, test.message)
})
}
}
func TestInOperatorUsesStrictEquality(t *testing.T) {
list := NewArray(
ArrayElement{Value: NewConstant("1")},
ArrayElement{Value: NewConstant(true)},
)
for _, value := range []any{1, "true"} {
node := NewBinary("in", NewConstant(value), list)
assertEqual(t, evaluateNode(t, node, nil, nil), false)
assertEqual(t, compileAndRunNode(t, node, nil, nil), false)
}
}
func TestBinaryNodeRejectsUnsupportedOperator(t *testing.T) {
node := NewBinary("unsupported", NewConstant(1), NewConstant(2))
_, err := node.Evaluate(nil, nil)
assertErrorContains(t, err, `BinaryNode does not support the "unsupported" operator`)
_, err = node.Compile(nil)
assertErrorContains(t, err, `BinaryNode does not support the "unsupported" operator`)
}