1 module temple.tests.filter;
2 
3 version(TempleUnittest):
4 
5 import temple.tests.common;
6 
7 private struct SafeDemoFilter
8 {
9 	static struct SafeString
10 	{
11 		string value;
12 	}
13 
14 	static string temple_filter(SafeString ts)
15 	{
16 		return ts.value;
17 	}
18 
19 	static string temple_filter(string str)
20 	{
21 		return "!" ~ str ~ "!";
22 	}
23 
24 	static SafeString safe(string str)
25 	{
26 		return SafeString(str);
27 	}
28 }
29 
30 unittest
31 {
32 	static struct Filter
33 	{
34 		static string temple_filter(string raw_str)
35 		{
36 			return "!" ~ raw_str ~ "!";
37 		}
38 	}
39 
40 	auto render1 = compile_temple!(`<%= "foo" %> bar`, Filter);
41 	assert(isSameRender(render1, "!foo! bar"));
42 
43 	auto render2 = compile_temple_file!("test9_filter.emd", Filter);
44 	assert(isSameRender(render2, `!foo! bar`));
45 }
46 
47 unittest
48 {
49 	static struct Filter
50 	{
51 		static void temple_filter(ref TempleOutputStream os, string raw_str)
52 		{
53 			//return "!" ~ raw_str ~ "!";
54 			os.put("!");
55 			os.put(raw_str);
56 			os.put("!");
57 		}
58 	}
59 
60 	auto parent  = compile_temple_file!("test10_fp_layout.emd", Filter);
61 	auto partial = compile_temple_file!("test10_fp_partial.emd", Filter);
62 
63 	assert(isSameRender(parent.layout(&partial), readText("test/test10_fp.emd.txt")));
64 }
65 
66 unittest
67 {
68 	auto render1 = compile_temple!(q{
69 		foo (filtered):   <%= "mark me" %>
70 		foo (unfiltered): <%= safe("don't mark me") %>
71 	}, SafeDemoFilter);
72 
73 	assert(isSameRender(render1, `
74 		foo (filtered):   !mark me!
75 		foo (unfiltered): don't mark me
76 	`));
77 
78 	auto render2 = compile_temple!(q{
79 		<%
80 		auto helper1(void delegate() block)
81 		{
82 			return "a " ~ capture(block) ~ " b";
83 		}
84 		%>
85 
86 		<%= capture(() { %>
87 			foo1
88 			<%= "foo2" %>
89 		<% }); %>
90 
91 		<%= helper1(() { %>
92 			<%= "foo3" %>
93 		<% }); %>
94 
95 		<%= helper1(() { %>
96 			<%= safe("foo4") %>
97 		<% }); %>
98 	}, SafeDemoFilter);
99 
100 	assert(isSameRender(render2, `
101 		foo1
102 		!foo2!
103 		a !foo3! b
104 		a foo4 b
105 	`));
106 }
107 
108 unittest
109 {
110 	// Test nested filter (e.g., filters are propogated with calls to render()
111 	// and renderWith())
112 
113 	auto render = compile_temple!(q{
114 		<%= safe("foo1") %>
115 		<%= "foo2" %>
116 		<%= render!"test11_propogate_fp.emd"() %>
117 		<%= "after1" %>
118 		after2
119 	}, SafeDemoFilter);
120 
121 	assert(isSameRender(render, `
122 		foo1
123 		!foo2!
124 		bar1
125 		!bar2!
126 		bar3
127 		!after1!
128 		after2
129 	`));
130 }
131 
132 unittest
133 {
134 	alias FPGroup = TempleFilter!SafeDemoFilter;
135 	auto render = FPGroup.compile_temple!q{
136 		foo1
137 		<%= "foo2" %>
138 	};
139 
140 	assert(isSameRender(render, `
141 		foo1
142 		!foo2!
143 	`));
144 }
145 
146 unittest
147 {
148 	// Test unicode charachters embedded in templates
149 
150 	auto render = compile_temple!(`
151 		Ю ю	Ю ю	Yu	/ju/, /ʲu/
152 		Я я	Я я	Ya	/ja/, /ʲa/
153 
154 		% if(true) {
155 			А а	А а	A	/a/
156 			Б б	Б б	Be	/b/
157 			В в	В в	Ve	/v/
158 		% }
159 	`);
160 
161 	assert(isSameRender(render, `
162 		Ю ю	Ю ю	Yu	/ju/, /ʲu/
163 		Я я	Я я	Ya	/ja/, /ʲa/
164 		А а	А а	A	/a/
165 		Б б	Б б	Be	/b/
166 		В в	В в	Ve	/v/
167 	`));
168 }
169 
170 unittest
171 {
172 	auto render = compile_temple_file!"test14_unicode.emd";
173 	auto compare = readText("test/test14_unicode.emd.txt");
174 	assert(isSameRender(render, compare));
175 }