-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels_test.go
211 lines (194 loc) · 5.6 KB
/
models_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
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
211
package rule
import (
"errors"
"testing"
)
// TestInputTypeString verifies the String() method on InputType
func TestInputTypeString(t *testing.T) {
cases := []struct {
it InputType
expected string
}{
{FunctionCall, "FunctionCall"},
{Expression, "Expression"},
}
for i, c := range cases {
got := c.it.String()
if got != c.expected {
t.Errorf("[%d] InputType(%v).String() => %q, want %q",
i, c.it, got, c.expected)
}
}
}
// TestArgumentTypeString verifies the String() method on ArgumentType
func TestArgumentTypeString(t *testing.T) {
cases := []struct {
at ArgumentType
expected string
}{
{ArgTypeUnknown, "unknown"},
{ArgTypeString, "string"},
{ArgTypeInteger, "int"},
{ArgTypeUnsignedInteger, "uint"},
{ArgTypeFloat64, "float64"},
{ArgTypeBoolean, "bool"},
{ArgTypeNull, "nil"},
{ArgTypeList, "list"},
{ArgTypeFloat32, "float32"},
{ArgTypeInteger32, "int32"},
{ArgTypeInteger64, "int64"},
{ArgTypeUnsignedInteger64, "uint64"},
{ArgTypeUnsignedInteger32, "uint32"},
{ArgTypeDecimal, "decimal"},
}
for i, c := range cases {
got := c.at.String()
if got != c.expected {
t.Errorf("[%d] ArgumentType(%v).String() => %q, want %q",
i, c.at, got, c.expected)
}
}
}
// TestFunctionArgumentAndParameter just instantiates these structs.
func TestFunctionArgumentAndParameter(t *testing.T) {
// Create a function argument
arg := FunctionArgument{
ArgumentType: ArgTypeString,
Value: "test",
}
if arg.ArgumentType != ArgTypeString {
t.Errorf("expected ArgTypeString, got %v", arg.ArgumentType)
}
if arg.Value != "test" {
t.Errorf("expected Value='test', got %v", arg.Value)
}
// Create a Parameter
param := Parameter{
id: 1,
Name: "testParam",
InputType: FunctionCall,
FunctionArguments: []FunctionArgument{arg},
strictTypeCheck: true,
Expression: ArgTypeBoolean,
operator: "eq",
compareValue: true,
}
if param.id != 1 {
t.Errorf("expected id=1, got %d", param.id)
}
if param.Name != "testParam" {
t.Errorf("expected Name='testParam', got %q", param.Name)
}
if param.InputType != FunctionCall {
t.Errorf("expected InputType=FunctionCall, got %v", param.InputType)
}
if len(param.FunctionArguments) != 1 {
t.Errorf("expected 1 FunctionArgument, got %d", len(param.FunctionArguments))
}
if !param.strictTypeCheck {
t.Errorf("expected strictTypeCheck=true")
}
if param.Expression != ArgTypeBoolean {
t.Errorf("expected Expression=ArgTypeBoolean, got %v", param.Expression)
}
if param.operator != "eq" {
t.Errorf("expected operator='eq', got %q", param.operator)
}
if val, ok := param.compareValue.(bool); !ok || val != true {
t.Errorf("expected compareValue=true (bool), got %v", param.compareValue)
}
}
// TestExprTree instantiates a small exprTree node.
func TestExprTree(t *testing.T) {
p := &Parameter{Name: "dummy"}
tree := exprTree{
not: true,
op: "and",
left: nil,
right: nil,
param: p,
}
if !tree.not {
t.Errorf("expected not=true")
}
if tree.op != "and" {
t.Errorf("expected op='and', got %q", tree.op)
}
if tree.param != p {
t.Errorf("exprTree param mismatch")
}
}
// TestErrorListener ensures we can set the hasErrors / errMsg properly
func TestErrorListener(t *testing.T) {
el := &errorListener{}
el.SyntaxError(nil, nil, 1, 2, "test error", nil)
if !el.hasErrors {
t.Errorf("expected hasErrors=true after SyntaxError()")
}
if el.errMsg == nil {
t.Errorf("expected errMsg to be set")
} else if el.errMsg.Error() == "" {
t.Errorf("expected non-empty error message")
}
}
// TestRuleAndConfig does minimal usage coverage for these structs
func TestRuleAndConfig(t *testing.T) {
r := Rule{
Params: []Parameter{},
exprTree: exprTree{},
debugMode: true,
}
if !r.debugMode {
t.Error("expected debugMode=true")
}
conf := Config{DebugMode: false}
if conf.DebugMode {
t.Error("expected DebugMode=false")
}
}
// TestEvaluation ensures referencing the struct covers basic lines
func TestEvaluation(t *testing.T) {
p := Parameter{Name: "evaluationParam"}
e := Evaluation{
Param: p,
Result: 123,
}
if e.Param.Name != "evaluationParam" {
t.Errorf("got unexpected param name %q", e.Param.Name)
}
if e.Result != 123 {
t.Errorf("expected result=123, got %v", e.Result)
}
}
// TestNonExistentArgumentType ensures a fallback if we do something invalid
func TestNonExistentArgumentType(t *testing.T) {
var weirdArgumentType ArgumentType = 9999 // not in map
if weirdArgumentType.String() != "unknown" {
t.Errorf("expected 'unknown' for out-of-range ArgumentType, got %q",
weirdArgumentType.String())
}
}
// TestNonExistentInputType ensures coverage for an out-of-range InputType
func TestNonExistentInputType(t *testing.T) {
var weirdInputType InputType = 9999
if got := weirdInputType.String(); got != "Expression" {
t.Errorf("expected fallback to 'Expression' for unknown InputType, got %q", got)
}
}
// TestCustomANTLRListener just a quick check that errorListener can handle other methods (no-op)
func TestCustomANTLRListener(t *testing.T) {
el := &errorListener{}
el.ReportAmbiguity(nil, nil, 0, 0, false, nil, nil)
el.ReportAttemptingFullContext(nil, nil, 0, 0, nil, nil)
el.ReportContextSensitivity(nil, nil, 0, 0, 0, nil)
// no panics => pass
}
// TestErrorListenerCustomError ensures we can set our own error to simulate a parse fail
func TestErrorListenerCustomError(t *testing.T) {
el := &errorListener{}
el.hasErrors = true
el.errMsg = errors.New("custom error for coverage")
if el.errMsg.Error() != "custom error for coverage" {
t.Errorf("errorListener errMsg not matching expected message")
}
}