-
-
Notifications
You must be signed in to change notification settings - Fork 437
/
Copy pathprint_test.go
132 lines (124 loc) · 3.24 KB
/
print_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
package ast_test
import (
"testing"
"github.com/expr-lang/expr/internal/testify/assert"
"github.com/expr-lang/expr/internal/testify/require"
"github.com/expr-lang/expr/ast"
"github.com/expr-lang/expr/parser"
)
func TestPrint(t *testing.T) {
tests := []struct {
input string
want string
}{
{`nil`, `nil`},
{`true`, `true`},
{`false`, `false`},
{`1`, `1`},
{`1.1`, `1.1`},
{`"a"`, `"a"`},
{`'a'`, `"a"`},
{`a`, `a`},
{`a.b`, `a.b`},
{`a[0]`, `a[0]`},
{`a["the b"]`, `a["the b"]`},
{`a.b[0]`, `a.b[0]`},
{`a?.b`, `a?.b`},
{`x[0][1]`, `x[0][1]`},
{`x?.[0]?.[1]`, `x?.[0]?.[1]`},
{`-a`, `-a`},
{`!a`, `!a`},
{`not a`, `not a`},
{`a + b`, `a + b`},
{`a + b * c`, `a + b * c`},
{`(a + b) * c`, `(a + b) * c`},
{`a * (b + c)`, `a * (b + c)`},
{`-(a + b) * c`, `-(a + b) * c`},
{`a == b`, `a == b`},
{`a matches b`, `a matches b`},
{`a in b`, `a in b`},
{`a not in b`, `not (a in b)`},
{`a and b`, `a and b`},
{`a or b`, `a or b`},
{`a or b and c`, `a or (b and c)`},
{`a or (b and c)`, `a or (b and c)`},
{`(a or b) and c`, `(a or b) and c`},
{`a ? b : c`, `a ? b : c`},
{`a ? b : c ? d : e`, `a ? b : (c ? d : e)`},
{`(a ? b : c) ? d : e`, `(a ? b : c) ? d : e`},
{`a ? (b ? c : d) : e`, `a ? (b ? c : d) : e`},
{`func()`, `func()`},
{`func(a)`, `func(a)`},
{`func(a, b)`, `func(a, b)`},
{`{}`, `{}`},
{`{a: b}`, `{a: b}`},
{`{a: b, c: d}`, `{a: b, c: d}`},
{`{"a": b, 'c': d}`, `{a: b, c: d}`},
{`{"a": b, c: d}`, `{a: b, c: d}`},
{`{"a": b, 8: 8}`, `{a: b, "8": 8}`},
{`{"9": 9, '8': 8, "foo": d}`, `{"9": 9, "8": 8, foo: d}`},
{`[]`, `[]`},
{`[a]`, `[a]`},
{`[a, b]`, `[a, b]`},
{`len(a)`, `len(a)`},
{`map(a, # > 0)`, `map(a, # > 0)`},
{`map(a, {# > 0})`, `map(a, # > 0)`},
{`map(a, .b)`, `map(a, .b)`},
{`a.b()`, `a.b()`},
{`a.b(c)`, `a.b(c)`},
{`a[1:-1]`, `a[1:-1]`},
{`a[1:]`, `a[1:]`},
{`a[1:]`, `a[1:]`},
{`a[:]`, `a[:]`},
{`(nil ?? 1) > 0`, `(nil ?? 1) > 0`},
{`{("a" + "b"): 42}`, `{("a" + "b"): 42}`},
{`(One == 1 ? true : false) && Two == 2`, `(One == 1 ? true : false) && Two == 2`},
{`not (a == 1 ? b > 1 : b < 2)`, `not (a == 1 ? b > 1 : b < 2)`},
{`(-(1+1)) ** 2`, `(-(1 + 1)) ** 2`},
{`2 ** (-(1+1))`, `2 ** -(1 + 1)`},
{`(2 ** 2) ** 3`, `(2 ** 2) ** 3`},
{`(3 + 5) / (5 % 3)`, `(3 + 5) / (5 % 3)`},
{`(-(1+1)) == 2`, `-(1 + 1) == 2`},
{`if true { 1 } else { 2 }`, `true ? 1 : 2`},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
tree, err := parser.Parse(tt.input)
require.NoError(t, err)
assert.Equal(t, tt.want, tree.Node.String())
})
}
}
func TestPrint_MemberNode(t *testing.T) {
node := &ast.MemberNode{
Node: &ast.IdentifierNode{
Value: "a",
},
Property: &ast.StringNode{Value: "b c"},
Optional: true,
}
require.Equal(t, `a?.["b c"]`, node.String())
}
func TestPrint_ConstantNode(t *testing.T) {
tests := []struct {
input any
want string
}{
{nil, `nil`},
{true, `true`},
{false, `false`},
{1, `1`},
{1.1, `1.1`},
{"a", `"a"`},
{[]int{1, 2, 3}, `[1,2,3]`},
{map[string]int{"a": 1}, `{"a":1}`},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
node := &ast.ConstantNode{
Value: tt.input,
}
require.Equal(t, tt.want, node.String())
})
}
}