1 module temple.tests.capture;
2 
3 version(TempleUnittest):
4 
5 import temple.tests.common;
6 unittest
7 {
8 	// test captures
9 	auto test = compile_temple!q{
10 		<% auto a = capture(() { %>
11 			This is captured in A
12 		<% }); %>
13 		<% auto b = capture(() { %>
14 			This is captured in B
15 		<% }); %>
16 
17 		B said: "<%= b %>"
18 		A said: "<%= a %>"
19 	};
20 
21 	assert(isSameRender(test, `
22 		B said: "This is captured in B"
23 		A said: "This is captured in A"
24 	`));
25 }
26 
27 unittest
28 {
29 	// Nested captures
30 	auto test = compile_temple!q{
31 		<% auto outer = capture(() { %>
32 			Outer, first
33 			<% auto inner = capture(() { %>
34 				Inner, first
35 			<% }); %>
36 			Outer, second
37 
38 			<%= inner %>
39 		<% }); %>
40 
41 		<%= outer %>
42 	};
43 
44 	assert(isSameRender(test, `
45 		Outer, first
46 		Outer, second
47 			Inner, first
48 	`));
49 }
50 
51 unittest
52 {
53 	auto render = compile_temple_file!"test8_building_helpers.emd";
54 	assert(isSameRender(render.toString(), readText("test/test8_building_helpers.emd.txt")));
55 }
56 
57 unittest
58 {
59 	auto test = compile_temple!q{
60 		<%= capture(() { %>
61 			directly printed
62 
63 			<% auto a = capture(() { %>
64 				a, captured
65 			<% }); %>
66 			<% auto b = capture(() { %>
67 				b, captured
68 			<% }); %>
69 
70 			<%= a %>
71 			<%= capture(() { %>
72 				directly printed from a nested capture
73 			<% }); %>
74 			<%= b %>
75 
76 		<% }); %>
77 	};
78 
79 	assert(isSameRender(test, `
80 		directly printed
81 			a, captured
82 			directly printed from a nested capture
83 			b, captured`));
84 }
85 
86 unittest
87 {
88 	auto test = compile_temple!q{
89 		<% string a; %>
90 		<%= a = capture(() { %>
91 			a capture
92 		<% }); %>
93 		<%= a %>
94 	};
95 
96 	assert(isSameRender(test, `
97 		a capture
98 		a capture
99 	`));
100 }
101 
102 /**
103  * Test CTFE compatibility
104  * Disabled for the API rewrite
105  */
106 version(none):
107 unittest
108 {
109 	const render = compile_temple!q{ <%= "foo" %> };
110 	static assert(isSameRender(render, "foo"));
111 }
112 
113 unittest
114 {
115 	alias render = compile_temple!q{
116 		<% if(true) { %>
117 			Bort
118 		<% } else { %>
119 			No bort!
120 		<% } %>
121 
122 		<% auto a = capture(() { %>
123 			inside a capture block
124 		<% }); %>
125 
126 		Before capture
127 		<%= a %>
128 		After capture
129 	};
130 
131 	const result = templeToString(&render);
132 	static assert(isSameRender(result, `
133 		Bort
134 		Before capture
135 		inside a capture block
136 		After capture
137 	`));
138 }