-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr_test.go
145 lines (124 loc) · 3.02 KB
/
expr_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
package salix
import (
"testing"
)
func TestAdd(t *testing.T) {
res := execStr(t, `#(3 + 1)`, nil)
if res != "4" {
t.Errorf("Expected 4, got %s", res)
}
}
func TestSub(t *testing.T) {
res := execStr(t, `#(3 - 1)`, nil)
if res != "2" {
t.Errorf("Expected 2, got %s", res)
}
}
func TestMul(t *testing.T) {
res := execStr(t, `#(3 * 2)`, nil)
if res != "6" {
t.Errorf("Expected 6, got %s", res)
}
}
func TestDiv(t *testing.T) {
res := execStr(t, `#(8 / 4)`, nil)
if res != "2" {
t.Errorf("Expected 2, got %s", res)
}
}
func TestMod(t *testing.T) {
res := execStr(t, `#(4 % 4)`, nil)
if res != "0" {
t.Errorf("Expected 0, got %s", res)
}
}
func TestEq(t *testing.T) {
res := execStr(t, `#("x" == "y")`, nil)
if res != "false" {
t.Errorf("Expected false, got %s", res)
}
}
func TestGEq(t *testing.T) {
res := execStr(t, `#(2 >= 2)`, nil)
if res != "true" {
t.Errorf("Expected true, got %s", res)
}
}
func TestGt(t *testing.T) {
res := execStr(t, `#(len("hi") > 2)`, nil)
if res != "false" {
t.Errorf("Expected false, got %s", res)
}
}
func TestLEq(t *testing.T) {
res := execStr(t, `#(4 <= 4)`, nil)
if res != "true" {
t.Errorf("Expected true, got %s", res)
}
}
func TestLt(t *testing.T) {
res := execStr(t, `#(4 < 4)`, nil)
if res != "false" {
t.Errorf("Expected false, got %s", res)
}
}
func TestAnd(t *testing.T) {
res := execStr(t, `#(true && false)`, nil)
if res != "false" {
t.Errorf("Expected false, got %s", res)
}
}
func TestOr(t *testing.T) {
res := execStr(t, `#(true || false)`, nil)
if res != "true" {
t.Errorf("Expected true, got %s", res)
}
}
func TestInString(t *testing.T) {
res := execStr(t, `#("h" in "hello")`, nil)
if res != "true" {
t.Errorf("Expected true, got %s", res)
}
}
func TestInSlice(t *testing.T) {
res := execStr(t, `#(5 in slice) #(6 in slice)`, map[string]any{"slice": []int{1, 2, 3, 4, 5}})
if res != "true false" {
t.Errorf("Expected %q, got %q", "true false", res)
}
}
func TestInMap(t *testing.T) {
res := execStr(t, `#(3.4234 in map)`, map[string]any{"map": map[float32]uint{3.4234: 0}})
if res != "true" {
t.Errorf("Expected %q, got %q", "true", res)
}
}
func TestParenExpr(t *testing.T) {
res := execStr(t, `#(5 - 4.0 - 3 - 2) #(5 - (4.0 - 3) - 2)`, nil)
if res != "-4 2" {
t.Errorf("Expected %q, got %q", "4 -2", res)
}
}
func TestCoalescing(t *testing.T) {
res := execStr(t, `#(hello | "nothing") #(x | "nothing")`, map[string]any{"hello": "world"})
if res != "world nothing" {
t.Errorf("Expected %q, got %q", "world nothing", res)
}
}
func TestTernary(t *testing.T) {
res := execStr(t, `#(2.0 == 2.0 ? "equal" : "non-equal") #(2.0 == 2.5 ? "equal" : "non-equal")`, nil)
if res != "equal non-equal" {
t.Errorf("Expected %q, got %q", "equal non-equal", res)
}
}
func TestNot(t *testing.T) {
res := execStr(t, `#(!true)`, nil)
if res != "false" {
t.Errorf("Expected %q, got %q", "false", res)
}
}
func TestAssignment(t *testing.T) {
res := execStr(t, `#(x = 4)#(x)`, nil)
if res != "4" {
t.Errorf("Expected %q, got %q", "4", res)
}
}