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