OLD | NEW |
(Empty) | |
| 1 import 'dart:async'; |
| 2 import 'dart:io'; |
| 3 import 'package:mustache/mustache.dart' as mustache; |
| 4 import 'dart:convert'; |
| 5 |
| 6 var verbose = false; |
| 7 var testName = null; |
| 8 var fileName = null; //'test/spec/sections.json'; |
| 9 |
| 10 main(List<String> args) { |
| 11 if (!args.isEmpty) { |
| 12 for (var arg in args) { |
| 13 if (arg == '--verbose' || arg == '-v') { |
| 14 verbose = true; |
| 15 } else if (arg.startsWith('-')) { |
| 16 print('Unknown argument: $arg'); |
| 17 print('Usage: dart test/mustache_spec_test.dart
[--verbose] [test name]'); |
| 18 return; |
| 19 } else { |
| 20 testName = arg; |
| 21 verbose = true; |
| 22 } |
| 23 } |
| 24 } |
| 25 |
| 26 var specs = new Directory('test/spec') |
| 27 .listSync() |
| 28 .where((f) => f is File |
| 29 && f.path.endsWith('.json') |
| 30 && (fileName == null || f.path == fileName))
; |
| 31 |
| 32 Future.forEach(specs, |
| 33 (file) => file |
| 34 .readAsString() |
| 35 .then((s) => JSON.decode(s)) |
| 36 .then((spec) { |
| 37 print('Specification: ${file.path}'); |
| 38 spec['tests'].forEach((map) => runTest( |
| 39 map['name'], |
| 40 map['desc'], |
| 41 map['data'], |
| 42 map['template'], |
| 43 map['expected'])); |
| 44 print(''); |
| 45 })); |
| 46 } |
| 47 |
| 48 runTest(String name, String desc, Map data, String template, String expected) { |
| 49 if (testName != null && name != testName) |
| 50 return; |
| 51 |
| 52 var output; |
| 53 var exception; |
| 54 var trace; |
| 55 try { |
| 56 output = mustache.parse(template, lenient: true).renderString(da
ta, lenient: true); |
| 57 } catch (ex, stacktrace) { |
| 58 exception = ex; |
| 59 trace = stacktrace; |
| 60 } |
| 61 |
| 62 // if (output != null) |
| 63 // output = output.replaceAll('\n', '\\n'); |
| 64 |
| 65 // if (expected != null) |
| 66 // expected = expected.replaceAll('\n', '\\n'); |
| 67 |
| 68 var passed = output == expected; |
| 69 var result = passed ? 'Pass' : 'Fail'; |
| 70 print(' $result $name'); |
| 71 if (verbose && !passed) { |
| 72 print(desc); |
| 73 print(' Template: $template'); |
| 74 print(' Data: ${JSON.encode(data)}'); |
| 75 print(' Expected: $expected'); |
| 76 print(' Output: $output'); |
| 77 if (exception != null) print(' Exception: $exception'); |
| 78 if (trace != null) print(trace); |
| 79 print(''); |
| 80 print(''); |
| 81 } |
| 82 } |
| 83 |
OLD | NEW |