forked from kaptinlin/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze_expressions_test.go
More file actions
99 lines (95 loc) · 2.6 KB
/
analyze_expressions_test.go
File metadata and controls
99 lines (95 loc) · 2.6 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
package template
import (
"reflect"
"testing"
)
func TestLexer_Lex(t *testing.T) {
tests := []struct {
name string
input string
expected []Token
}{
{
name: "Complex conditional expression",
input: "user.age >= 18 && (is_admin || !is_guest) | upper",
expected: []Token{
{Typ: TokenIdentifier, Val: "user.age"},
{Typ: TokenOperator, Val: ">="},
{Typ: TokenNumber, Val: "18"},
{Typ: TokenOperator, Val: "&&"},
{Typ: TokenLParen, Val: "("},
{Typ: TokenIdentifier, Val: "is_admin"},
{Typ: TokenOperator, Val: "||"},
{Typ: TokenNot, Val: "!"},
{Typ: TokenIdentifier, Val: "is_guest"},
{Typ: TokenRParen, Val: ")"},
{Typ: TokenPipe, Val: "|"},
{Typ: TokenFilter, Val: "upper"},
{Typ: TokenEOF, Val: "EOF"},
},
},
{
name: "Simple boolean expression",
input: "is_active == true",
expected: []Token{
{Typ: TokenIdentifier, Val: "is_active"},
{Typ: TokenOperator, Val: "=="},
{Typ: TokenBool, Val: "true"},
{Typ: TokenEOF, Val: "EOF"},
},
},
{
name: "Numeric comparison expression",
input: "count <= 10 && count > 0",
expected: []Token{
{Typ: TokenIdentifier, Val: "count"},
{Typ: TokenOperator, Val: "<="},
{Typ: TokenNumber, Val: "10"},
{Typ: TokenOperator, Val: "&&"},
{Typ: TokenIdentifier, Val: "count"},
{Typ: TokenOperator, Val: ">"},
{Typ: TokenNumber, Val: "0"},
{Typ: TokenEOF, Val: "EOF"},
},
},
{
name: "String concatenation expression",
input: `user.name + " is " + user.age + " years old" 3.3 * user.age`,
expected: []Token{
{Typ: TokenIdentifier, Val: "user.name"},
{Typ: TokenArithOp, Val: "+"},
{Typ: TokenString, Val: " is "},
{Typ: TokenArithOp, Val: "+"},
{Typ: TokenIdentifier, Val: "user.age"},
{Typ: TokenArithOp, Val: "+"},
{Typ: TokenString, Val: " years old"},
{Typ: TokenNumber, Val: "3.3"},
{Typ: TokenArithOp, Val: "*"},
{Typ: TokenIdentifier, Val: "user.age"},
{Typ: TokenEOF, Val: "EOF"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lexer := &Lexer{
input: tt.input,
}
got, err := lexer.Lex()
if err != nil {
t.Fatalf("Lexer.Lex() error = %v", err)
}
if !reflect.DeepEqual(got, tt.expected) {
t.Errorf("Lexer.Lex() = %v, want %v", got, tt.expected)
t.Errorf("\nActual tokens:")
for i, token := range got {
t.Errorf("%d: {Type: %v, Val: %q}", i, token.Typ, token.Val)
}
t.Errorf("\nExpected tokens:")
for i, token := range tt.expected {
t.Errorf("%d: {Type: %v, Val: %q}", i, token.Typ, token.Val)
}
}
})
}
}