OLD | NEW |
(Empty) | |
| 1 import '../../../pkg/unittest/lib/unittest.dart'; |
| 2 import '../lib/html_to_json.dart' as html_to_json; |
| 3 import '../lib/json_to_html.dart' as json_to_html; |
| 4 import 'dart:json'; |
| 5 import 'dart:io'; |
| 6 |
| 7 void main() { |
| 8 var scriptPath = new Path(new Options().script).directoryPath.toString(); |
| 9 |
| 10 test('HTML Doc to JSON', () { |
| 11 var htmlPath = new Path('$scriptPath/test_data/html_to_json'); |
| 12 var jsonPath = new Path('$scriptPath/test_output/html_to_json_test.json'); |
| 13 |
| 14 var convertFuture = html_to_json.convert(htmlPath, jsonPath); |
| 15 |
| 16 convertFuture.then(expectAsync1((anyErrors) { |
| 17 var output = new File.fromPath(jsonPath); |
| 18 |
| 19 var goldenFile = new File( |
| 20 '$scriptPath/test_data/html_to_json/' |
| 21 'html_to_json_test_golden_output.json'); |
| 22 |
| 23 expect(anyErrors, false, reason:'The conversion completed with errors.'); |
| 24 expect(output.readAsStringSync(), goldenFile.readAsStringSync()); |
| 25 })); |
| 26 }); |
| 27 |
| 28 test('JSON to HTML Doc', () { |
| 29 var preHtmlPath = new Path('$scriptPath/test_data/json_to_html'); |
| 30 var goldenHtmlPath = new Path('$scriptPath/test_data/html_to_json'); |
| 31 var htmlPath = new Path('$scriptPath/test_output'); |
| 32 var jsonPath = new Path('$scriptPath/test_output/html_to_json_test.json'); |
| 33 |
| 34 var copyFuture = _copyFiles(preHtmlPath, htmlPath); |
| 35 |
| 36 copyFuture.then(expectAsync1((_) { |
| 37 var convertFuture = json_to_html.convert(htmlPath, jsonPath); |
| 38 |
| 39 convertFuture.then((anyErrors) { |
| 40 expect(anyErrors, false, |
| 41 reason:'The conversion completed with errors.'); |
| 42 |
| 43 _compareFilesInDirectories(goldenHtmlPath, htmlPath); |
| 44 |
| 45 }); |
| 46 })); |
| 47 }); |
| 48 } |
| 49 |
| 50 void _compareFilesInDirectories(Path path1, Path path2) { |
| 51 final dir1 = new Directory.fromPath(path1); |
| 52 final dir2 = new Directory.fromPath(path2); |
| 53 final lister1 = dir1.list(recursive: false); |
| 54 final lister2 = dir2.list(recursive: false); |
| 55 |
| 56 // True once one of the listers is finished. |
| 57 var oneFinished = false; |
| 58 |
| 59 var list1 = <String, File>{}; |
| 60 |
| 61 lister1.onFile = (String path) { |
| 62 if (path.endsWith('.dart')) { |
| 63 list1.putIfAbsent(new Path(path).filename, () => new File(path)); |
| 64 } |
| 65 }; |
| 66 |
| 67 lister1.onDone = (_) { |
| 68 lister2.onFile = (String path) { |
| 69 if (path.endsWith('.dart')) { |
| 70 expect(list1[new Path(path).filename].readAsStringSync(), |
| 71 new File(path).readAsStringSync()); |
| 72 } |
| 73 }; |
| 74 }; |
| 75 } |
| 76 |
| 77 Future _copyFiles(Path fromDir, Path toDir) { |
| 78 // First copy the files into a new place to keep the old files. |
| 79 final completer = new Completer(); |
| 80 final htmlDir = new Directory.fromPath(fromDir); |
| 81 final lister = htmlDir.list(recursive: false); |
| 82 |
| 83 lister.onFile = (String path) { |
| 84 final name = new Path.fromNative(path).filename; |
| 85 |
| 86 // Ignore private classes. |
| 87 if (name.startsWith('_')) return; |
| 88 |
| 89 // Ignore non-dart files. |
| 90 if (!name.endsWith('.dart')) return; |
| 91 |
| 92 File file = new File(path); |
| 93 File newFile = new File.fromPath(toDir.append(name)); |
| 94 |
| 95 var outputStream = newFile.openOutputStream(); |
| 96 outputStream.writeString(file.readAsStringSync()); |
| 97 }; |
| 98 |
| 99 lister.onDone = (_) { |
| 100 completer.complete(null); |
| 101 }; |
| 102 return completer.future; |
| 103 } |
OLD | NEW |