1 module temple.tests.common; 2 3 4 version(TempleUnittest): 5 6 public import std.stdio, std.file : readText; 7 public import 8 temple, 9 temple.util, 10 temple.output_stream; 11 12 bool isSameRender(in CompiledTemple t, TempleContext tc, string r2) { 13 return isSameRender(t, r2, tc); 14 } 15 bool isSameRender(in CompiledTemple t, string r2, TempleContext tc = null) { 16 return isSameRender(t.toString(tc), r2); 17 } 18 bool isSameRender(string r1, string r2) 19 { 20 auto ret = r1.stripWs == r2.stripWs; 21 22 if(ret == false) 23 { 24 writeln("Renders differ: "); 25 writeln("Got: -------------------------"); 26 writeln(r1); 27 writeln("Expected: --------------------"); 28 writeln(r2); 29 writeln("------------------------------"); 30 } 31 32 return ret; 33 } 34 35 deprecated("Please use template.toString()") 36 string templeToString(CompiledTemple function() getr, TempleContext tc = null) { 37 return getr().toString(tc); 38 } 39 40 unittest 41 { 42 auto render = compile_temple!""; 43 assert(render.toString() == ""); 44 } 45 46 unittest 47 { 48 //Test to!string of eval delimers 49 auto render = compile_temple!`<%= "foo" %>`; 50 assert(render.toString == "foo"); 51 } 52 53 unittest 54 { 55 // Test delimer parsing 56 auto render = compile_temple!("<% if(true) { %>foo<% } %>"); 57 assert(render.toString == "foo"); 58 } 59 unittest 60 { 61 //Test raw text with no delimers 62 auto render = compile_temple!(`foo`); 63 assert(render.toString == "foo"); 64 } 65 66 unittest 67 { 68 //Test looping 69 const templ = `<% foreach(i; 0..3) { %>foo<% } %>`; 70 auto render = compile_temple!templ; 71 assert(render.toString == "foofoofoo"); 72 } 73 74 unittest 75 { 76 //Test looping 77 const templ = `<% foreach(i; 0..3) { %><%= i %><% } %>`; 78 auto render = compile_temple!templ; 79 assert(render.toString == "012"); 80 } 81 82 unittest 83 { 84 //Test escaping of " 85 const templ = `"`; 86 auto render = compile_temple!templ; 87 assert(render.toString == `"`); 88 } 89 90 unittest 91 { 92 //Test escaping of ' 93 const templ = `'`; 94 auto render = compile_temple!templ; 95 assert(render.toString == `'`); 96 } 97 98 unittest 99 { 100 auto render = compile_temple!`"%"`; 101 assert(render.toString == `"%"`); 102 } 103 104 unittest 105 { 106 // Test shorthand 107 const templ = ` 108 % if(true) { 109 Hello! 110 % } 111 `; 112 auto render = compile_temple!(templ); 113 assert(isSameRender(render.toString, "Hello!")); 114 } 115 116 unittest 117 { 118 // Test shorthand string eval 119 const templ = ` 120 % if(true) { 121 %= "foo" 122 % } 123 `; 124 auto render = compile_temple!(templ); 125 //static assert(false); 126 assert(isSameRender(render.toString, "foo")); 127 } 128 unittest 129 { 130 // Test shorthand only after newline 131 const templ = `foo%bar`; 132 auto render = compile_temple!(templ); 133 assert(render.toString == "foo%bar"); 134 } 135 136 unittest 137 { 138 // Ditto 139 auto render = compile_temple!`<%= "foo%bar" %>`; 140 assert(render.toString == "foo%bar"); 141 } 142 143 unittest 144 { 145 auto context = new TempleContext(); 146 context.foo = 123; 147 context.bar = "test"; 148 149 auto render = compile_temple!`<%= var("foo") %> <%= var("bar") %>`; 150 assert(render.toString(context) == "123 test"); 151 } 152 153 unittest 154 { 155 // Loading templates from a file 156 auto render = compile_temple_file!"test1.emd"; 157 auto compare = readText("test/test1.emd.txt"); 158 assert(isSameRender(render.toString, compare)); 159 } 160 161 unittest 162 { 163 auto render = compile_temple_file!"test2.emd"; 164 auto compare = readText("test/test2.emd.txt"); 165 166 auto ctx = new TempleContext(); 167 ctx.name = "dymk"; 168 ctx.will_work = true; 169 170 assert(isSameRender(render.toString(ctx), compare)); 171 } 172 173 unittest 174 { 175 auto render = compile_temple_file!"test3_nester.emd"; 176 auto compare = readText("test/test3.emd.txt"); 177 assert(isSameRender(render.toString, compare)); 178 } 179 180 unittest 181 { 182 auto render = compile_temple_file!"test4_root.emd"; 183 auto compare = readText("test/test4.emd.txt"); 184 185 auto ctx = new TempleContext(); 186 ctx.var1 = "this_is_var1"; 187 188 assert(isSameRender(render.toString(ctx), compare)); 189 } 190 191 unittest 192 { 193 auto parent = compile_temple!"before <%= yield %> after"; 194 auto partial = compile_temple!"between"; 195 196 assert(isSameRender(parent.layout(&partial), "before between after")); 197 } 198 199 unittest 200 { 201 auto parent = compile_temple!"before <%= yield %> after"; 202 auto partial = compile_temple!"between"; 203 204 assert(isSameRender(parent.layout(&partial), "before between after")); 205 } 206 207 unittest 208 { 209 auto parent = compile_temple_file!"test5_layout.emd"; 210 auto partial1 = compile_temple_file!"test5_partial1.emd"; 211 auto partial2 = compile_temple_file!"test5_partial2.emd"; 212 213 auto p1 = parent.layout(&partial1); 214 auto p2 = parent.layout(&partial2); 215 216 assert(isSameRender(p1, readText("test/test5_partial1.emd.txt"))); 217 assert(isSameRender(p2, readText("test/test5_partial2.emd.txt"))); 218 } 219 220 // Layouts and contexts 221 unittest 222 { 223 auto parent = compile_temple_file!"test6_layout.emd"; 224 auto partial = compile_temple_file!"test6_partial.emd"; 225 226 auto context = new TempleContext(); 227 context.name = "dymk"; 228 context.uni = "UCSD"; 229 context.age = 19; 230 231 assert(isSameRender(parent.layout(&partial), context, readText("test/test6_partial.emd.txt"))); 232 } 233 234 // opDispatch variable getting 235 unittest 236 { 237 auto render = compile_temple!"<%= var.foo %>"; 238 239 auto context = new TempleContext(); 240 context.foo = "Hello, world"; 241 242 assert(isSameRender(render, context, "Hello, world")); 243 } 244 245 unittest 246 { 247 // 22 Nov, 2014: Disabled this bit, because DMD now ICEs when 248 // evaluating the erronious template (but not before spitting out 249 // a lot of errors). This will have to do for finding out that a templtae 250 // has a lot of errors in it. 251 // Uncomment to view the line numbers inserted into the template 252 //TODO: check if this works in future DMD releases 253 //auto render = compile_temple_file!"test7_error.emd"; 254 //assert(!__traits(compiles, { 255 // auto t = compile_temple_file!"test7_error.emd"; 256 //})); 257 } 258 259 unittest 260 { 261 import temple.func_string_gen; 262 // Test returning early from templates 263 //auto str = ` 264 auto render = compile_temple!` 265 one 266 % auto blah = true; 267 % if(blah) { 268 two 269 % return; 270 % } 271 three 272 `; 273 274 //writeln(__temple_gen_temple_func_string(str, "Inline")); 275 assert(isSameRender(render.toString, 276 `one 277 two`)); 278 }