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