Skip to content

Commit 6552dab

Browse files
committed
filters + more tests
1 parent 6e2f27a commit 6552dab

17 files changed

Lines changed: 2394 additions & 29 deletions
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package integration
2+
3+
import (
4+
"testing"
5+
6+
"github.com/Notifuse/liquidgo/liquid"
7+
"github.com/Notifuse/liquidgo/liquid/tags"
8+
)
9+
10+
func TestForloopLastDetailed(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
template string
14+
want string
15+
}{
16+
{
17+
name: "simple forloop.last",
18+
template: `{% for item in items %}{{ item }}{% if forloop.last %}!{% endif %}{% endfor %}`,
19+
want: "123!",
20+
},
21+
{
22+
name: "forloop.last with trailing comma",
23+
template: `{% for item in items %}{{ item }},{% endfor %}`,
24+
want: "1,2,3,",
25+
},
26+
{
27+
name: "forloop.last controlling comma",
28+
template: `{% for item in items %}{{ item }}{% unless forloop.last %},{% endunless %}{% endfor %}`,
29+
want: "1,2,3",
30+
},
31+
{
32+
name: "forloop.last with content then comma",
33+
template: `{% for item in items %}{{ item }}{% if forloop.last %} last{% endif %},{% endfor %}`,
34+
want: "1,2,3 last,",
35+
},
36+
{
37+
name: "forloop.last debug",
38+
template: `{% for item in items %}{{ forloop.last }}{% endfor %}`,
39+
want: "falsefalsetrue",
40+
},
41+
}
42+
43+
for _, tt := range tests {
44+
t.Run(tt.name, func(t *testing.T) {
45+
env := liquid.NewEnvironment()
46+
tags.RegisterStandardTags(env)
47+
tmpl := liquid.NewTemplate(&liquid.TemplateOptions{Environment: env})
48+
49+
err := tmpl.Parse(tt.template, nil)
50+
if err != nil {
51+
t.Fatalf("Parse failed: %v", err)
52+
}
53+
54+
vars := map[string]interface{}{"items": []interface{}{1, 2, 3}}
55+
got := tmpl.Render(vars, nil)
56+
57+
if got != tt.want {
58+
t.Errorf("FAILED\nWant: %q\nGot: %q", tt.want, got)
59+
} else {
60+
t.Logf("PASS: %q", got)
61+
}
62+
})
63+
}
64+
}

integration/test_array_last.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package integration
2+
3+
import (
4+
"testing"
5+
6+
"github.com/Notifuse/liquidgo/liquid"
7+
"github.com/Notifuse/liquidgo/liquid/tags"
8+
)
9+
10+
func TestArrayCommandMethods(t *testing.T) {
11+
env := liquid.NewEnvironment()
12+
tags.RegisterStandardTags(env)
13+
14+
tests := []struct {
15+
name string
16+
template string
17+
vars map[string]interface{}
18+
want string
19+
}{
20+
{
21+
name: "array.last",
22+
template: `{{ items.last }}`,
23+
vars: map[string]interface{}{"items": []interface{}{1, 2, 3}},
24+
want: "3",
25+
},
26+
{
27+
name: "array.first",
28+
template: `{{ items.first }}`,
29+
vars: map[string]interface{}{"items": []interface{}{1, 2, 3}},
30+
want: "1",
31+
},
32+
{
33+
name: "array.size",
34+
template: `{{ items.size }}`,
35+
vars: map[string]interface{}{"items": []interface{}{1, 2, 3}},
36+
want: "3",
37+
},
38+
{
39+
name: "string.size",
40+
template: `{{ str.size }}`,
41+
vars: map[string]interface{}{"str": "hello"},
42+
want: "5",
43+
},
44+
}
45+
46+
for _, tt := range tests {
47+
t.Run(tt.name, func(t *testing.T) {
48+
tmpl := liquid.NewTemplate(&liquid.TemplateOptions{Environment: env})
49+
err := tmpl.Parse(tt.template, nil)
50+
if err != nil {
51+
t.Fatalf("Parse failed: %v", err)
52+
}
53+
54+
got := tmpl.Render(tt.vars, nil)
55+
if got != tt.want {
56+
t.Errorf("FAILED\nTemplate: %s\nWant: %q\nGot: %q", tt.template, tt.want, got)
57+
} else {
58+
t.Logf("PASS: %q", got)
59+
}
60+
})
61+
}
62+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package integration
2+
3+
import (
4+
"testing"
5+
6+
"github.com/Notifuse/liquidgo/liquid"
7+
"github.com/Notifuse/liquidgo/liquid/tags"
8+
)
9+
10+
func TestTraceForloopRendering(t *testing.T) {
11+
env := liquid.NewEnvironment()
12+
tags.RegisterStandardTags(env)
13+
14+
// Test direct variable lookup in context
15+
ctx := liquid.BuildContext(liquid.ContextConfig{Environment: env})
16+
17+
// Create a forloop drop and add it to context
18+
drop := liquid.NewForloopDrop("test", 3, nil)
19+
ctx.Set("forloop", drop)
20+
21+
// Test 1: Direct lookup
22+
forloopVar := ctx.FindVariable("forloop", false)
23+
t.Logf("FindVariable('forloop') = %#v (type: %T)", forloopVar, forloopVar)
24+
25+
// Test 2: Evaluate a VariableLookup
26+
vl := liquid.VariableLookupParse("forloop.last", liquid.NewStringScanner(""), nil)
27+
result := vl.Evaluate(ctx)
28+
t.Logf("VariableLookup.Evaluate('forloop.last') = %#v (type: %T)", result, result)
29+
30+
// Test 3: ToS on the result
31+
resultStr := liquid.ToS(result, nil)
32+
t.Logf("ToS(result) = %q", resultStr)
33+
34+
// Test 4: Full template rendering
35+
template := `{{ forloop.last }}`
36+
tmpl := liquid.NewTemplate(&liquid.TemplateOptions{Environment: env})
37+
err := tmpl.Parse(template, nil)
38+
if err != nil {
39+
t.Fatalf("Parse failed: %v", err)
40+
}
41+
42+
vars := map[string]interface{}{"forloop": drop}
43+
got := tmpl.Render(vars, nil)
44+
t.Logf("Template render result = %q", got)
45+
46+
if got == "" {
47+
t.Error("Template rendered empty string instead of 'false'")
48+
}
49+
}

0 commit comments

Comments
 (0)