-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval_expr_test.go
More file actions
469 lines (405 loc) · 12.6 KB
/
Copy patheval_expr_test.go
File metadata and controls
469 lines (405 loc) · 12.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package vuego_test
import (
"bytes"
"testing"
"testing/fstest"
"github.com/stretchr/testify/require"
"github.com/titpetric/vuego"
"github.com/titpetric/vuego/diff"
)
func TestExprEvaluator_SimpleExpressions(t *testing.T) {
evaluator := vuego.NewExprEvaluator()
tests := []struct {
name string
expr string
env map[string]any
expect any
}{
// Literal values
{"integer literal", "42", map[string]any{}, 42},
{"string literal", "'hello'", map[string]any{}, "hello"},
{"double-quoted string", `"world"`, map[string]any{}, "world"},
{"boolean true", "true", map[string]any{}, true},
{"boolean false", "false", map[string]any{}, false},
// Variable references
{"simple variable", "x", map[string]any{"x": 42}, 42},
{"nested property", "obj.name", map[string]any{"obj": map[string]any{"name": "Alice"}}, "Alice"},
{"array index", "items[0]", map[string]any{"items": []any{"first", "second"}}, "first"},
// Comparison operators
// Note: expr uses == for equality (equivalent to === in JavaScript)
{"equality ==", "x == 5", map[string]any{"x": 5}, true},
{"inequality !=", "x != 5", map[string]any{"x": 3}, true},
{"less than <", "x < 10", map[string]any{"x": 5}, true},
{"greater than >", "x > 3", map[string]any{"x": 5}, true},
{"less than or equal <=", "x <= 5", map[string]any{"x": 5}, true},
{"greater than or equal >=", "x >= 5", map[string]any{"x": 5}, true},
// Logical operators
{"logical AND", "x && y", map[string]any{"x": true, "y": true}, true},
{"logical AND false", "x && y", map[string]any{"x": true, "y": false}, false},
{"logical OR", "x || y", map[string]any{"x": false, "y": true}, true},
{"logical NOT", "!x", map[string]any{"x": false}, true},
{"logical NOT true", "!x", map[string]any{"x": true}, false},
{"logical NOT nil", "!(type(x) == 'bool' && x)", map[string]any{}, true},
{"logical NOT type nil", "!(type(x) != 'nil')", map[string]any{}, true},
// String comparisons
{"string equality", "name === 'Alice'", map[string]any{"name": "Alice"}, true},
{"string equality", "name == 'Alice'", map[string]any{"name": "Alice"}, true},
{"string inequality", "name != 'Bob'", map[string]any{"name": "Alice"}, true},
// Complex expressions
{"compound AND", "(x > 5) && (y < 10)", map[string]any{"x": 7, "y": 8}, true},
{"compound OR", "(x > 10) || (y < 5)", map[string]any{"x": 7, "y": 3}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := evaluator.Eval(tt.expr, tt.env)
require.NoError(t, err)
require.Equal(t, tt.expect, result)
})
}
}
func TestExprEvaluator_FunctionCalls(t *testing.T) {
evaluator := vuego.NewExprEvaluator()
tests := []struct {
name string
expr string
env map[string]any
expectErr bool
}{
// Built-in functions in expr
{"len function", "len(items) > 0", map[string]any{"items": []any{1, 2}}, false},
{"empty slice", "len(items) == 0", map[string]any{"items": []any{}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := evaluator.Eval(tt.expr, tt.env)
if tt.expectErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
func TestExprEvaluator_UndefinedVariables(t *testing.T) {
evaluator := vuego.NewExprEvaluator()
// expr allows undefined variables with AllowUndefinedVariables()
// They evaluate to nil
result, err := evaluator.Eval("missing", map[string]any{})
require.NoError(t, err)
require.Nil(t, result)
}
func TestExprEvaluator_Caching(t *testing.T) {
evaluator := vuego.NewExprEvaluator()
expr := "x + y"
env := map[string]any{"x": 5, "y": 3}
// First evaluation
result1, err1 := evaluator.Eval(expr, env)
require.NoError(t, err1)
// Second evaluation should use cache
result2, err2 := evaluator.Eval(expr, env)
require.NoError(t, err2)
require.Equal(t, result1, result2)
}
func TestVue_VIfWithExprComparison(t *testing.T) {
type testCase struct {
Priority string `json:"priority"`
Total int `json:"total"`
}
template := []byte(`
<div v-if="priority == 'high'">High Priority</div>
<div v-if="priority == 'low'">Low Priority</div>
<div v-if="total > 0">Has Items</div>
<div v-if="total <= 0">No Items</div>
`)
templateFS := &fstest.MapFS{
"test.vuego": {Data: template},
}
tests := []struct {
name string
data testCase
expect string
}{
{
"priority high",
testCase{Priority: "high", Total: 5},
"<div>High Priority</div>\n<div>Has Items</div>\n",
},
{
"priority low",
testCase{Priority: "low", Total: 0},
"<div>Low Priority</div>\n<div>No Items</div>\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tt.expect), buf.Bytes(), nil, nil)
})
}
}
func TestVue_VIfWithLogicalOperators(t *testing.T) {
type testCase struct {
IsActive bool `json:"isActive"`
HasPermission bool `json:"hasPermission"`
IsAdmin bool `json:"isAdmin"`
IsDeveloper bool `json:"isDeveloper"`
}
template := []byte(`
<div v-if="isActive && hasPermission">Active with Permission</div>
<div v-if="isAdmin || isDeveloper">Admin or Developer</div>
`)
templateFS := &fstest.MapFS{
"test.vuego": {Data: template},
}
tests := []struct {
name string
data testCase
expect string
}{
{
"both true",
testCase{IsActive: true, HasPermission: true, IsAdmin: false, IsDeveloper: false},
"<div>Active with Permission</div>\n",
},
{
"admin role",
testCase{IsActive: false, HasPermission: false, IsAdmin: true, IsDeveloper: false},
"<div>Admin or Developer</div>\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tt.expect), buf.Bytes(), nil, nil)
})
}
}
func TestVue_VIfWithNestedProperties(t *testing.T) {
type User struct {
Active bool `json:"active"`
}
type Item struct {
InStock bool `json:"inStock"`
}
type testCase struct {
User User `json:"user"`
Item Item `json:"item"`
}
template := []byte(`
<div v-if="user.active">User is Active</div>
<div v-if="item.inStock == true">In Stock</div>
`)
templateFS := &fstest.MapFS{
"test.vuego": {Data: template},
}
data := testCase{
User: User{Active: true},
Item: Item{InStock: true},
}
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", data)
require.NoError(t, err)
expected := "<div>User is Active</div>\n<div>In Stock</div>\n"
diff.EqualHTML(t, []byte(expected), buf.Bytes(), nil, nil)
}
func TestVue_InterpolationWithComparison(t *testing.T) {
type testCase struct {
Status string `json:"status"`
}
template := []byte(`
<p>Status: {{ status == 'active' }}</p>
`)
templateFS := &fstest.MapFS{
"test.vuego": {Data: template},
}
tests := []struct {
name string
data testCase
expect string
}{
{"active status", testCase{Status: "active"}, "<p>Status: true</p>\n"},
{"inactive status", testCase{Status: "inactive"}, "<p>Status: false</p>\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tt.expect), buf.Bytes(), nil, nil)
})
}
}
func TestVue_BackwardCompatibility(t *testing.T) {
type User struct {
Name string `json:"name"`
}
type testCase struct {
Show bool `json:"show"`
Hide bool `json:"hide"`
Message string `json:"message"`
User User `json:"user"`
}
// Ensure existing syntax still works
template := []byte(`
<div v-if="show">Visible</div>
<div v-if="!hide">Not Hidden</div>
<p>{{ message }}</p>
<p>{{ user.name }}</p>
`)
templateFS := &fstest.MapFS{
"test.vuego": {Data: template},
}
data := testCase{
Show: true,
Hide: false,
Message: "Hello",
User: User{Name: "Alice"},
}
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", data)
require.NoError(t, err)
expected := "<div>Visible</div>\n<div>Not Hidden</div>\n<p>Hello</p>\n<p>Alice</p>\n"
diff.EqualHTML(t, []byte(expected), buf.Bytes(), nil, nil)
}
// Test the exact examples from the original request
func TestRequestExample_TaskPriority(t *testing.T) {
type Task struct {
Priority string `json:"priority"`
}
type testCase struct {
Task Task `json:"task"`
}
// Test: v-if="task.priority == 'high'" as requested
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="task.priority == 'high'">High Priority Task</div>
<div v-if="task.priority == 'medium'">Medium Priority Task</div>
<div v-if="task.priority == 'low'">Low Priority Task</div>
`)},
}
tests := []struct {
name string
data testCase
expect string
}{
{"high priority", testCase{Task: Task{Priority: "high"}}, "High Priority Task"},
{"medium priority", testCase{Task: Task{Priority: "medium"}}, "Medium Priority Task"},
{"low priority", testCase{Task: Task{Priority: "low"}}, "Low Priority Task"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tt.data)
require.NoError(t, err)
require.Contains(t, buf.String(), tt.expect)
})
}
}
func TestRequestExample_BooleanOperations(t *testing.T) {
type testCase struct {
Score int `json:"score"`
Age int `json:"age"`
Verified bool `json:"verified"`
Role string `json:"role"`
}
// Test various boolean operators as requested
templateFS := &fstest.MapFS{
"test.vuego": {Data: []byte(`
<div v-if="score >= 90">Excellent</div>
<div v-if="age >= 18 && verified == true">Verified Adult</div>
<div v-if="role == 'admin' || role == 'moderator'">Staff</div>
`)},
}
data := testCase{
Score: 95,
Age: 21,
Verified: true,
Role: "admin",
}
var buf bytes.Buffer
vue := vuego.NewVue(templateFS)
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", data)
require.NoError(t, err)
t.Logf("Output: %s", buf.String())
require.Contains(t, buf.String(), "Excellent")
require.Contains(t, buf.String(), "Verified Adult")
require.Contains(t, buf.String(), "Staff")
}
func TestRequestExample_StackCompatibility(t *testing.T) {
// Test that stack map[string]any is compatible with expr
evaluator := vuego.NewExprEvaluator()
env := map[string]any{
"item": map[string]any{
"title": "Hello",
"id": 42,
},
"items": []any{
map[string]any{"name": "first"},
map[string]any{"name": "second"},
},
}
// Test property access like "item.title" as mentioned in request
result, err := evaluator.Eval("item.title", env)
require.NoError(t, err)
require.Equal(t, "Hello", result)
// Test comparison "item.id == 42"
result, err = evaluator.Eval("item.id == 42", env)
require.NoError(t, err)
require.Equal(t, true, result)
// Test nested access "items[0].name"
result, err = evaluator.Eval("items[0].name", env)
require.NoError(t, err)
require.Equal(t, "first", result)
}
func TestVue_VIfWithStrictEquality(t *testing.T) {
t.Run("=== is normalized to ==", func(t *testing.T) {
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{
Data: []byte(`<div v-if="status === 'active'"><p>Active</p></div><div v-if="status !== 'active'"><p>Inactive</p></div>`),
},
}
data := map[string]any{
"status": "active",
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
require.NoError(t, vue.RenderFragment(t.Context(), &buf, "test.vuego", data))
require.Contains(t, buf.String(), "Active")
require.NotContains(t, buf.String(), "Inactive")
})
t.Run("!== is normalized to !=", func(t *testing.T) {
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{
Data: []byte(`<div v-if="status !== 'inactive'"><p>Not Inactive</p></div>`),
},
}
data := map[string]any{
"status": "active",
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
require.NoError(t, vue.RenderFragment(t.Context(), &buf, "test.vuego", data))
require.Contains(t, buf.String(), "Not Inactive")
})
t.Run("=== with both sides as literals", func(t *testing.T) {
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{
Data: []byte(`<div v-if="'active' === 'active'"><p>Match</p></div>`),
},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
require.NoError(t, vue.RenderFragment(t.Context(), &buf, "test.vuego", map[string]any{}))
require.Contains(t, buf.String(), "Match")
})
}