-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparent_function_test.go
269 lines (223 loc) · 7.09 KB
/
parent_function_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
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
package twig
import (
"testing"
)
func TestParentFunction(t *testing.T) {
engine := New()
// Create a simple parent-child template relationship
baseTemplate := `
<!-- BASE TEMPLATE -->
<div class="base">
{% block test %}
<p>This is the parent content</p>
{% endblock %}
</div>
`
childTemplate := `
<!-- CHILD TEMPLATE -->
{% extends "base.twig" %}
{% block test %}
<h2>Child heading</h2>
{{ parent() }}
<p>Child footer</p>
{% endblock %}
`
// Register the templates
engine.RegisterString("base.twig", baseTemplate)
engine.RegisterString("child.twig", childTemplate)
// Render the child template
output, err := engine.Render("child.twig", nil)
if err != nil {
t.Fatalf("Failed to render template: %v", err)
}
// Print the output for debugging
t.Logf("Rendered output:\n%s", output)
// Check for expected content
mustContain(t, output, "BASE TEMPLATE")
mustContain(t, output, "Child heading")
mustContain(t, output, "This is the parent content")
mustContain(t, output, "Child footer")
// Verify ordering
inOrderCheck(t, output, "Child heading", "This is the parent content")
inOrderCheck(t, output, "This is the parent content", "Child footer")
}
func TestNestedParentFunction(t *testing.T) {
// Test multi-level inheritance with parent() functionality
engine := New()
// Create a simple parent-child relationship
baseTemplate := `<!-- BASE TEMPLATE -->
{% block content %}
<p>Base content</p>
{% endblock %}
`
// Skip the middle template and test direct parent-child
childTemplate := `<!-- CHILD TEMPLATE -->
{% extends "base.twig" %}
{% block content %}
<h1>Child content</h1>
{{ parent() }}
<p>More child content</p>
{% endblock %}
`
engine.RegisterString("base.twig", baseTemplate)
engine.RegisterString("child.twig", childTemplate)
// Render the child template
output, err := engine.Render("child.twig", nil)
if err != nil {
t.Fatalf("Failed to render template: %v", err)
}
// Print the output for debugging
t.Logf("Rendered output:\n%s", output)
// Check that content from parent and child is properly included
mustContain(t, output, "BASE TEMPLATE")
mustContain(t, output, "Base content")
mustContain(t, output, "Child content")
mustContain(t, output, "More child content")
// Verify ordering
inOrderCheck(t, output, "Child content", "Base content")
inOrderCheck(t, output, "Base content", "More child content")
}
func TestSimpleMultiLevelParentFunction(t *testing.T) {
// A simpler test with just two levels to isolate the issue
engine := New()
// Enable debug mode
SetDebugLevel(DebugInfo)
engine.SetDebug(true)
// Create a simpler template hierarchy with just parent and child
baseTemplate := `<!-- BASE TEMPLATE -->
{% block content %}
<div>Base content</div>
{% endblock %}
`
// Middle template with parent() call
middleTemplate := `<!-- MIDDLE TEMPLATE -->
{% extends "base.twig" %}
{% block content %}
<div class="middle">
{{ parent() }}
<p>Middle content</p>
</div>
{% endblock %}
`
// Register the templates
engine.RegisterString("base.twig", baseTemplate)
engine.RegisterString("middle.twig", middleTemplate)
// Render the middle template
output, err := engine.Render("middle.twig", nil)
if err != nil {
t.Fatalf("Failed to render template: %v", err)
}
// Print the output for debugging
t.Logf("Rendered output:\n%s", output)
// Test the output
mustContain(t, output, "BASE TEMPLATE")
mustContain(t, output, "Base content")
mustContain(t, output, "Middle content")
inOrderCheck(t, output, "Base content", "Middle content")
}
func TestThreeLevelParentFunction(t *testing.T) {
t.Skip("Multi-level parent() inheritance not yet implemented")
// Let's try a simpler approach to debug the issue
engine := New()
// Enable debug mode
SetDebugLevel(DebugInfo)
engine.SetDebug(true)
// Create a more basic version with just one middle parent() call
baseTemplate := `<!-- BASE TEMPLATE -->
{% block content %}
<div>Base content</div>
{% endblock %}
`
// Middle template with parent() call
middleTemplate := `<!-- MIDDLE TEMPLATE -->
{% extends "base.twig" %}
{% block content %}
<div class="middle">
<p>Middle content before parent</p>
{{ parent() }}
<p>Middle content after parent</p>
</div>
{% endblock %}
`
// Child template that just extends middle, no parent() call
childTemplate := `<!-- CHILD TEMPLATE -->
{% extends "middle.twig" %}
{% block content %}
<div class="child">
<h1>Child content</h1>
{{ parent() }}
</div>
{% endblock %}
`
// Register the templates
engine.RegisterString("base.twig", baseTemplate)
engine.RegisterString("middle.twig", middleTemplate)
engine.RegisterString("child.twig", childTemplate)
// Render the child template which should access both parent and grandparent
output, err := engine.Render("child.twig", nil)
if err != nil {
t.Fatalf("Failed to render template: %v", err)
}
// Print the output for debugging
t.Logf("Rendered output:\n%s", output)
// Test the output
mustContain(t, output, "BASE TEMPLATE")
mustContain(t, output, "Base content")
mustContain(t, output, "Middle content")
mustContain(t, output, "Child header")
mustContain(t, output, "Child footer")
// Check order of content - should nest properly
inOrderCheck(t, output, "Child header", "Base content")
inOrderCheck(t, output, "Base content", "Middle content")
inOrderCheck(t, output, "Middle content", "Child footer")
}
func TestParentFunctionErrors(t *testing.T) {
engine := New()
// Test parent() outside of a block
template := `{{ parent() }}`
engine.RegisterString("bad.twig", template)
_, err := engine.Render("bad.twig", nil)
if err == nil {
t.Errorf("Expected an error when calling parent() outside of a block")
}
// Test parent() in a template without inheritance
template2 := `{% block test %}{{ parent() }}{% endblock %}`
engine.RegisterString("no_parent.twig", template2)
_, err = engine.Render("no_parent.twig", nil)
if err == nil {
t.Errorf("Expected an error when calling parent() in a template without inheritance")
}
}
// Helper functions for tests
func stringContains(haystack, needle string) bool {
return stringIndexOf(haystack, needle) >= 0
}
func stringIndexOf(haystack, needle string) int {
for i := 0; i <= len(haystack)-len(needle); i++ {
if haystack[i:i+len(needle)] == needle {
return i
}
}
return -1
}
func inOrder(haystack, firstNeedle, secondNeedle string) bool {
firstIndex := stringIndexOf(haystack, firstNeedle)
secondIndex := stringIndexOf(haystack, secondNeedle)
return firstIndex != -1 && secondIndex != -1 && firstIndex < secondIndex
}
func mustContain(t *testing.T, haystack, needle string) {
if !stringContains(haystack, needle) {
t.Errorf("Expected output to contain '%s', but it didn't", needle)
}
}
func inOrderCheck(t *testing.T, haystack, firstNeedle, secondNeedle string) {
if !inOrder(haystack, firstNeedle, secondNeedle) {
if !stringContains(haystack, firstNeedle) {
t.Errorf("First string '%s' not found in output", firstNeedle)
} else if !stringContains(haystack, secondNeedle) {
t.Errorf("Second string '%s' not found in output", secondNeedle)
} else {
t.Errorf("Expected '%s' to come before '%s' in output", firstNeedle, secondNeedle)
}
}
}