-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval_attributes_test.go
More file actions
558 lines (528 loc) · 16.4 KB
/
eval_attributes_test.go
File metadata and controls
558 lines (528 loc) · 16.4 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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package vuego_test
import (
"bytes"
"fmt"
"testing"
"testing/fstest"
"github.com/stretchr/testify/require"
"github.com/titpetric/vuego"
"github.com/titpetric/vuego/diff"
)
func TestVue_EvalAttributes_BoundAndInterpolated(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "colon prefix binding",
template: `<div :id="id"></div>`,
data: map[string]any{"id": "test-div"},
expected: `<div id="test-div"></div>`,
},
{
name: "colon prefix binding escape",
template: `<div [:id]="id"></div>`,
data: map[string]any{"id": "test-div"},
expected: `<div :id="id"></div>`,
},
{
name: "colon prefix binding escape with interpolation",
template: `<div [:field]="{{ field | json }}"></div>`,
data: map[string]any{
"field": map[string]any{"id": "test-div"},
},
expected: `<div :field="{"id":"test-div"}"></div>`,
},
{
name: "v-bind prefix binding",
template: `<div v-bind:class="class"></div>`,
data: map[string]any{"class": "active"},
expected: `<div class="active"></div>`,
},
{
name: "bound attribute with falsey value",
template: `<button :disabled="disabled"></button>`,
data: map[string]any{"disabled": false},
expected: `<button></button>`,
},
{
name: "bound attribute with truthy value",
template: `<button :disabled="disabled"></button>`,
data: map[string]any{"disabled": true},
expected: `<button disabled="true"></button>`,
},
{
name: "normal attribute with interpolation",
template: `<div class="item-{{ id }}"></div>`,
data: map[string]any{"id": "123"},
expected: `<div class="item-123"></div>`,
},
{
name: "normal attribute without interpolation",
template: `<div class="static"></div>`,
data: map[string]any{},
expected: `<div class="static"></div>`,
},
{
name: "bound attribute missing variable",
template: `<div :title="missing"></div>`,
data: map[string]any{},
expected: `<div></div>`,
},
{
name: "empty string is falsey",
template: `<input :required="value" />`,
data: map[string]any{"value": ""},
expected: `<input />`,
},
{
name: "zero is falsey",
template: `<span :data-count="counter"></span>`,
data: map[string]any{"counter": 0},
expected: `<span></span>`,
},
{
name: "non-zero is truthy",
template: `<span :data-count="counter"></span>`,
data: map[string]any{"counter": 5},
expected: `<span data-count="5"></span>`,
},
}
for idx, tc := range tests {
t.Run(fmt.Sprintf("%d: %s", idx, tc.name), func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}
func TestVue_ObjectBinding_Class(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "object binding with truthy values",
template: `<div :class="{active: true, disabled: false}"></div>`,
data: map[string]any{},
expected: `<div class="active"></div>`,
},
{
name: "object binding with truthy values, quoted",
template: `<div :class="{'active': true, 'disabled': false}"></div>`,
data: map[string]any{},
expected: `<div class="active"></div>`,
},
{
name: "object binding with variable values",
template: `<div :class="{active: isActive, error: hasError}"></div>`,
data: map[string]any{"isActive": true, "hasError": false},
expected: `<div class="active"></div>`,
},
{
name: "all classes included when truthy",
template: `<div :class="{btn: true, primary: true, large: true}"></div>`,
data: map[string]any{},
expected: `<div class="btn primary large"></div>`,
},
{
name: "no classes when all falsey",
template: `<div :class="{active: false, disabled: false}"></div>`,
data: map[string]any{},
expected: `<div></div>`,
},
{
name: "class object with static class",
template: `<div class="static-class" :class="{dynamic: true}"></div>`,
data: map[string]any{},
expected: `<div class="static-class dynamic"></div>`,
},
{
name: "class object with zero is falsey",
template: `<div :class="{active: count}"></div>`,
data: map[string]any{"count": 0},
expected: `<div></div>`,
},
{
name: "class object with non-zero is truthy",
template: `<div :class="{active: count}"></div>`,
data: map[string]any{"count": 1},
expected: `<div class="active"></div>`,
},
{
name: "true is true",
template: `<div :class="{active: status, inactive: !status}"></div>`,
data: map[string]any{"status": true},
expected: `<div class="active"></div>`,
},
{
name: "!true is false",
template: `<div :class="{active: status, inactive: !status}"></div>`,
data: map[string]any{"status": false},
expected: `<div class="inactive"></div>`,
},
{
name: "!nil is true",
template: `<div :class="{active: status?, inactive: !(type(status) == 'bool' && status)}"></div>`,
data: map[string]any{},
expected: `<div class="inactive"></div>`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}
func TestVue_ObjectBinding_Style(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "style object with string values",
template: `<div :style="{display: 'block', color: 'red'}"></div>`,
data: map[string]any{},
expected: `<div style="display:block;color:red;"></div>`,
},
{
name: "style object with variable values",
template: `<div :style="{color: textColor, fontSize: size}"></div>`,
data: map[string]any{"textColor": "blue", "size": "16px"},
expected: `<div style="color:blue;font-size:16px;"></div>`,
},
{
name: "style object with single property",
template: `<div :style="{display: 'none'}"></div>`,
data: map[string]any{},
expected: `<div style="display:none;"></div>`,
},
{
name: "style object combined with static style",
template: `<div style="padding: 10px;" :style="{color: 'red'}"></div>`,
data: map[string]any{},
expected: `<div style="padding:10px;color:red;"></div>`,
},
{
name: "style object with numeric values",
template: `<div :style="{width: '100px', height: '50px'}"></div>`,
data: map[string]any{},
expected: `<div style="width:100px;height:50px;"></div>`,
},
{
name: "style object overwrites conflicting static style",
template: `<div style="color: blue;" :style="{color: 'red'}"></div>`,
data: map[string]any{},
expected: `<div style="color:red;"></div>`,
},
{
name: "camelCase properties converted to kebab-case",
template: `<div :style="{fontSize: '16px', backgroundColor: 'blue', marginTop: '10px'}"></div>`,
data: map[string]any{},
expected: `<div style="font-size:16px;background-color:blue;margin-top:10px;"></div>`,
},
{
name: "camelCase with variable values",
template: `<div :style="{fontSize: size, fontWeight: weight}"></div>`,
data: map[string]any{"size": "14px", "weight": "bold"},
expected: `<div style="font-size:14px;font-weight:bold;"></div>`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}
func TestVue_CombinedStyleAttributes(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "static style with bound style",
template: `<div style="color: red;" :style="{fontSize: size}"></div>`,
data: map[string]any{"size": "14px"},
expected: `<div style="color:red;font-size:14px;"></div>`,
},
{
name: "v-show with static class",
template: `<div v-show="visible" class="box"></div>`,
data: map[string]any{"visible": false},
expected: `<div class="box" style="display:none;"></div>`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}
func TestVue_EvalBoundAttribute_WithComplexExpressions(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "bound attribute with direct expression",
template: `<div :data-value="id"></div>`,
data: map[string]any{"id": "123"},
expected: `<div data-value="123"></div>`,
},
{
name: "bound attribute with piped filter",
template: `<a :href="url"></a>`,
data: map[string]any{"url": "/posts/42"},
expected: `<a href="/posts/42"></a>`,
},
{
name: "bound attribute with default filter",
template: `<div :title="message | default('no title')"></div>`,
data: map[string]any{},
expected: `<div title="no title"></div>`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}
func TestVue_EvalBoundAttribute_WithFunctionCalls(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "bound attribute with function call",
template: `<div :title="len(items)"></div>`,
data: map[string]any{"items": []int{1, 2, 3}},
expected: `<div title="3"></div>`,
},
{
name: "bound attribute with nested function",
template: `<p :data-text="trim(text)"></p>`,
data: map[string]any{"text": " spaces "},
expected: `<p data-text="spaces"></p>`,
},
{
name: "bound attribute with default filter",
template: `<span :data-value="missing | default('fallback')"></span>`,
data: map[string]any{},
expected: `<span data-value="fallback"></span>`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}
func TestVue_ParseObjectPairs_EdgeCases(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "class object empty",
template: `<div :class="{}"></div>`,
data: map[string]any{},
expected: `<div></div>`,
},
{
name: "style object empty",
template: `<div :style="{}"></div>`,
data: map[string]any{},
expected: `<div></div>`,
},
{
name: "class object with spaces around colons",
template: `<div :class="{ active : isActive , error : hasError }"></div>`,
data: map[string]any{"isActive": true, "hasError": false},
expected: `<div class="active"></div>`,
},
{
name: "style with numeric string values",
template: `<div :style="{margin: '0', padding: '10px'}"></div>`,
data: map[string]any{},
expected: `<div style="margin:0;padding:10px;"></div>`,
},
{
name: "class object with variable values",
template: `<div :class="{btn: true, active: isActive, disabled: hasError}"></div>`,
data: map[string]any{"isActive": true, "hasError": false},
expected: `<div class="btn active"></div>`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}
func TestVue_EvalAttributes_BoundVsInterpolated(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "bound attr does not re-interpolate value",
template: `<div :title="content"></div>`,
data: map[string]any{"content": "{{ name }}", "name": "Alice"},
expected: `<div title="{{ name }}"></div>`,
},
{
name: "interpolated attr evaluates mustache syntax",
template: `<div title="{{ content }}"></div>`,
data: map[string]any{"content": "hello"},
expected: `<div title="hello"></div>`,
},
{
name: "bound attr preserves interpolation-like syntax in value",
template: `<span :data-code="code"></span>`,
data: map[string]any{"code": `echo "${{ version }}"`},
expected: `<span data-code="echo "${{ version }}""></span>`,
},
{
name: "interpolated attr evaluates nested interpolation",
template: `<div data-info="prefix-{{ value }}-suffix"></div>`,
data: map[string]any{"value": "middle"},
expected: `<div data-info="prefix-middle-suffix"></div>`,
},
{
name: "bound attr with pipe-like value is not filtered",
template: `<div :title="text"></div>`,
data: map[string]any{"text": "a | b"},
expected: `<div title="a | b"></div>`,
},
{
name: "bound and interpolated produce same result for simple string",
template: `<div :title="msg" data-alt="{{ msg }}"></div>`,
data: map[string]any{"msg": "hello"},
expected: `<div title="hello" data-alt="hello"></div>`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}
func TestVue_EvalAttributes_MultipleBindings(t *testing.T) {
tests := []struct {
name string
template string
data map[string]any
expected string
}{
{
name: "multiple bound attributes",
template: `<button :id="id" :class="btnClass" :disabled="disabled"></button>`,
data: map[string]any{"id": "submit-btn", "btnClass": "primary", "disabled": false},
expected: `<button id="submit-btn" class="primary"></button>`,
},
{
name: "bound attributes mixed with static",
template: `<input type="text" :value="name" :placeholder="hint" class="form-input" />`,
data: map[string]any{"name": "John", "hint": "Enter your name"},
expected: `<input type="text" value="John" placeholder="Enter your name" class="form-input" />`,
},
{
name: "v-bind shorthand with multiple attributes",
template: `<a v-bind:href="url" v-bind:target="target" v-bind:title="title">Link</a>`,
data: map[string]any{"url": "/page", "target": "_blank", "title": "External"},
expected: `<a href="/page" target="_blank" title="External">Link</a>`,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
template := []byte(tc.template)
fs := fstest.MapFS{
"test.vuego": &fstest.MapFile{Data: template},
}
vue := vuego.NewVue(fs)
var buf bytes.Buffer
err := vue.RenderFragment(t.Context(), &buf, "test.vuego", tc.data)
require.NoError(t, err)
diff.EqualHTML(t, []byte(tc.expected), buf.Bytes(), nil, nil)
})
}
}