OLD | NEW |
(Empty) | |
| 1 library mustache_test; |
| 2 |
| 3 import 'package:unittest/unittest.dart'; |
| 4 import 'package:mustache/mustache.dart'; |
| 5 |
| 6 const MISMATCHED_TAG = 'Mismatched tag'; |
| 7 const UNEXPECTED_EOF = 'Unexpected end of input'; |
| 8 const BAD_VALUE_SECTION = 'Invalid value type for section'; |
| 9 const BAD_VALUE_INV_SECTION = 'Invalid value type for inverse section'; |
| 10 const BAD_TAG_NAME = 'Tag contained invalid characters in name'; |
| 11 const VALUE_NULL = 'Value was null or missing'; |
| 12 |
| 13 main() { |
| 14 group('Basic', () { |
| 15 test('Variable', () { |
| 16 var output = parse('_{{var}}_') |
| 17 .renderString({"var": "bob"}); |
| 18 expect(output, equals('_bob_')); |
| 19 }); |
| 20 test('Comment', () { |
| 21 var output = parse('_{{! i am a comment ! }}_').renderSt
ring({}); |
| 22 expect(output, equals('__')); |
| 23 }); |
| 24 }); |
| 25 group('Section', () { |
| 26 test('Map', () { |
| 27 var output = parse('{{#section}}_{{var}}_{{/section}}') |
| 28 .renderString({"section": {"var": "bob"}}); |
| 29 expect(output, equals('_bob_')); |
| 30 }); |
| 31 test('List', () { |
| 32 var output = parse('{{#section}}_{{var}}_{{/section}}') |
| 33 .renderString({"section": [{"var": "bob"}, {"var
": "jim"}]}); |
| 34 expect(output, equals('_bob__jim_')); |
| 35 }); |
| 36 test('Empty List', () { |
| 37 var output = parse('{{#section}}_{{var}}_{{/section}}') |
| 38 .renderString({"section": []}); |
| 39 expect(output, equals('')); |
| 40 }); |
| 41 test('False', () { |
| 42 var output = parse('{{#section}}_{{var}}_{{/section}}') |
| 43 .renderString({"section": false}); |
| 44 expect(output, equals('')); |
| 45 }); |
| 46 test('Invalid value', () { |
| 47 var ex = renderFail( |
| 48 '{{#section}}_{{var}}_{{/section}}', |
| 49 {"section": 42}); |
| 50 expect(ex is FormatException, isTrue); |
| 51 expect(ex.message, startsWith(BAD_VALUE_SECTION)); |
| 52 }); |
| 53 test('True', () { |
| 54 var output = parse('{{#section}}_ok_{{/section}}') |
| 55 .renderString({"section": true}); |
| 56 expect(output, equals('_ok_')); |
| 57 }); |
| 58 test('Nested', () { |
| 59 var output = parse('{{#section}}.{{var}}.{{#nested}}_{{n
estedvar}}_{{/nested}}.{{/section}}') |
| 60 .renderString({"section": { |
| 61 "var": "bob", |
| 62 "nested": [ |
| 63 {"nestedvar": "jim"}, |
| 64 {"nestedvar": "sally"} |
| 65 ] |
| 66 }}); |
| 67 expect(output, equals('.bob._jim__sally_.')); |
| 68 }); |
| 69 }); |
| 70 |
| 71 group('Inverse Section', () { |
| 72 test('Map', () { |
| 73 var output = parse('{{^section}}_{{var}}_{{/section}}') |
| 74 .renderString({"section": {"var": "bob"}}); |
| 75 expect(output, equals('')); |
| 76 }); |
| 77 test('List', () { |
| 78 var output = parse('{{^section}}_{{var}}_{{/section}}') |
| 79 .renderString({"section": [{"var": "bob"}, {"var
": "jim"}]}); |
| 80 expect(output, equals('')); |
| 81 }); |
| 82 test('Empty List', () { |
| 83 var output = parse('{{^section}}_ok_{{/section}}') |
| 84 .renderString({"section": []}); |
| 85 expect(output, equals('_ok_')); |
| 86 }); |
| 87 test('False', () { |
| 88 var output = parse('{{^section}}_ok_{{/section}}') |
| 89 .renderString({"section": false}); |
| 90 expect(output, equals('_ok_')); |
| 91 }); |
| 92 test('Invalid value', () { |
| 93 var ex = renderFail( |
| 94 '{{^section}}_{{var}}_{{/section}}', |
| 95 {"section": 42}); |
| 96 expect(ex is FormatException, isTrue); |
| 97 expect(ex.message, startsWith(BAD_VALUE_INV_SECTION)); |
| 98 }); |
| 99 test('True', () { |
| 100 var output = parse('{{^section}}_ok_{{/section}}') |
| 101 .renderString({"section": true}); |
| 102 expect(output, equals('')); |
| 103 }); |
| 104 }); |
| 105 |
| 106 group('Html escape', () { |
| 107 |
| 108 test('Escape at start', () { |
| 109 var output = parse('_{{var}}_') |
| 110 .renderString({"var": "&."}); |
| 111 expect(output, equals('_&._')); |
| 112 }); |
| 113 |
| 114 test('Escape at end', () { |
| 115 var output = parse('_{{var}}_') |
| 116 .renderString({"var": ".&"}); |
| 117 expect(output, equals('_.&_')); |
| 118 }); |
| 119 |
| 120 test('&', () { |
| 121 var output = parse('_{{var}}_') |
| 122 .renderString({"var": "&"}); |
| 123 expect(output, equals('_&_')); |
| 124 }); |
| 125 |
| 126 test('<', () { |
| 127 var output = parse('_{{var}}_') |
| 128 .renderString({"var": "<"}); |
| 129 expect(output, equals('_<_')); |
| 130 }); |
| 131 |
| 132 test('>', () { |
| 133 var output = parse('_{{var}}_') |
| 134 .renderString({"var": ">"}); |
| 135 expect(output, equals('_>_')); |
| 136 }); |
| 137 |
| 138 test('"', () { |
| 139 var output = parse('_{{var}}_') |
| 140 .renderString({"var": '"'}); |
| 141 expect(output, equals('_"_')); |
| 142 }); |
| 143 |
| 144 test("'", () { |
| 145 var output = parse('_{{var}}_') |
| 146 .renderString({"var": "'"}); |
| 147 expect(output, equals('_'_')); |
| 148 }); |
| 149 |
| 150 test("/", () { |
| 151 var output = parse('_{{var}}_') |
| 152 .renderString({"var": "/"}); |
| 153 expect(output, equals('_/_')); |
| 154 }); |
| 155 |
| 156 }); |
| 157 |
| 158 group('Invalid format', () { |
| 159 test('Mismatched tag', () { |
| 160 var source = '{{#section}}_{{var}}_{{/notsection}}'; |
| 161 var ex = renderFail(source, {"section": {"var": "bob"}})
; |
| 162 expectFail(ex, 1, 25, 'Mismatched tag'); |
| 163 }); |
| 164 |
| 165 test('Unexpected EOF', () { |
| 166 var source = '{{#section}}_{{var}}_{{/section'; |
| 167 var ex = renderFail(source, {"section": {"var": "bob"}})
; |
| 168 expectFail(ex, 1, source.length + 2, UNEXPECTED_EOF); |
| 169 }); |
| 170 |
| 171 test('Bad tag name, open section', () { |
| 172 var source = r'{{#section$%$^%}}_{{var}}_{{/section}}'; |
| 173 var ex = renderFail(source, {"section": {"var": "bob"}})
; |
| 174 expectFail(ex, null, null, BAD_TAG_NAME); |
| 175 }); |
| 176 |
| 177 test('Bad tag name, close section', () { |
| 178 var source = r'{{#section}}_{{var}}_{{/section$%$^%}}'; |
| 179 var ex = renderFail(source, {"section": {"var": "bob"}})
; |
| 180 expectFail(ex, null, null, BAD_TAG_NAME); |
| 181 }); |
| 182 |
| 183 test('Bad tag name, variable', () { |
| 184 var source = r'{{#section}}_{{var$%$^%}}_{{/section}}'; |
| 185 var ex = renderFail(source, {"section": {"var": "bob"}})
; |
| 186 expectFail(ex, null, null, BAD_TAG_NAME); |
| 187 }); |
| 188 |
| 189 }); |
| 190 |
| 191 group('Lenient', () { |
| 192 test('Odd section name', () { |
| 193 var output = parse(r'{{#section$%$^%}}_{{var}}_{{/sectio
n$%$^%}}', lenient: true) |
| 194 .renderString({r'section$%$^%': {'var': 'bob'}},
lenient: true); |
| 195 expect(output, equals('_bob_')); |
| 196 }); |
| 197 |
| 198 test('Odd variable name', () { |
| 199 var output = parse(r'{{#section}}_{{var$%$^%}}_{{/sectio
n}}', lenient: true) |
| 200 .renderString({'section': {r'var$%$^%': 'bob'}},
lenient: true); |
| 201 }); |
| 202 |
| 203 test('Null variable', () { |
| 204 var output = parse(r'{{#section}}_{{var}}_{{/section}}',
lenient: true) |
| 205 .renderString({'section': {'var': null}}, lenien
t: true); |
| 206 expect(output, equals('__')); |
| 207 }); |
| 208 |
| 209 test('Null section', () { |
| 210 var output = parse('{{#section}}_{{var}}_{{/section}}',
lenient: true) |
| 211 .renderString({"section": null}, lenient: true); |
| 212 expect(output, equals('')); |
| 213 }); |
| 214 |
| 215 // Known failure |
| 216 // test('Null inverse section', () { |
| 217 // var output = parse('{{^section}}_{{var}}_{{/section}}',
lenient: true) |
| 218 // .renderString({"section": null}, lenient: true); |
| 219 // expect(output, equals('')); |
| 220 // }); |
| 221 |
| 222 }); |
| 223 |
| 224 group('Escape tags', () { |
| 225 test('{{{ ... }}}', () { |
| 226 var output = parse('{{{blah}}}') |
| 227 .renderString({'blah': '&'}); |
| 228 expect(output, equals('&')); |
| 229 }); |
| 230 test('{{& ... }}', () { |
| 231 var output = parse('{{{blah}}}') |
| 232 .renderString({'blah': '&'}); |
| 233 expect(output, equals('&')); |
| 234 }); |
| 235 }); |
| 236 |
| 237 group('Patial tag', () { |
| 238 test('Unimplemented', () { |
| 239 var fn = () => parse('{{>partial}}').renderString({}); |
| 240 expect(fn, throwsUnimplementedError); |
| 241 }); |
| 242 }); |
| 243 |
| 244 group('Other', () { |
| 245 test('Standalone line', () { |
| 246 var val = parse('|\n{{#bob}}\n{{/bob}}\n|').renderString
({'bob': []}); |
| 247 expect(val, equals('|\n|')); |
| 248 }); |
| 249 }); |
| 250 |
| 251 group('Array indexing', () { |
| 252 test('Basic', () { |
| 253 var val = parse('{{array.1}}').renderString({'array': [1
, 2, 3]}); |
| 254 expect(val, equals('2')); |
| 255 }); |
| 256 test('RangeError', () { |
| 257 var error = renderFail('{{array.5}}', {'array': [1, 2, 3
]}); |
| 258 expect(error, isRangeError); |
| 259 }); |
| 260 }); |
| 261 |
| 262 group('Mirrors', () { |
| 263 test('Simple field', () { |
| 264 var output = parse('_{{bar}}_') |
| 265 .renderString(new Foo()..bar = 'bob'); |
| 266 expect(output, equals('_bob_')); |
| 267 }); |
| 268 }); |
| 269 } |
| 270 |
| 271 renderFail(source, values) { |
| 272 try { |
| 273 parse(source).renderString(values); |
| 274 return null; |
| 275 } catch (e) { |
| 276 return e; |
| 277 } |
| 278 } |
| 279 |
| 280 expectFail(ex, int line, int column, [String msgStartsWith]) { |
| 281 expect(ex, isFormatException); |
| 282 expect(ex is MustacheFormatException, isTrue); |
| 283 if (line != null) |
| 284 expect(ex.line, equals(line)); |
| 285 if (column != null) |
| 286 expect(ex.column, equals(column)); |
| 287 if (msgStartsWith != null) |
| 288 expect(ex.message, startsWith(msgStartsWith)); |
| 289 } |
| 290 |
| 291 class Foo { |
| 292 String bar; |
| 293 } |
OLD | NEW |