OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 library web_components.test.build.import_crawler_test; | 4 library web_components.test.build.import_crawler_test; |
5 | 5 |
| 6 import 'dart:async'; |
6 import 'package:barback/barback.dart'; | 7 import 'package:barback/barback.dart'; |
7 import 'package:code_transformers/tests.dart'; | 8 import 'package:code_transformers/tests.dart'; |
8 import 'package:code_transformers/messages/build_logger.dart'; | 9 import 'package:code_transformers/messages/build_logger.dart'; |
| 10 import 'package:html5lib/dom.dart' show Document; |
| 11 import 'package:web_components/build/common.dart'; |
9 import 'package:web_components/build/import_crawler.dart'; | 12 import 'package:web_components/build/import_crawler.dart'; |
10 import 'package:unittest/compact_vm_config.dart'; | 13 import 'package:unittest/compact_vm_config.dart'; |
11 | 14 |
12 class _TestTransformer extends Transformer { | 15 class _TestTransformer extends Transformer { |
13 final String _entryPoint; | 16 final String _entryPoint; |
14 Map<AssetId, ImportData> documents; | 17 Map<AssetId, ImportData> documents; |
| 18 final bool _preParseDocument; |
15 | 19 |
16 _TestTransformer(this._entryPoint); | 20 _TestTransformer(this._entryPoint, [this._preParseDocument = false]); |
17 | 21 |
18 isPrimary(AssetId id) => id.path == _entryPoint; | 22 isPrimary(AssetId id) => id.path == _entryPoint; |
19 | 23 |
20 apply(Transform transform) { | 24 apply(Transform transform) { |
21 var primaryInput = transform.primaryInput; | 25 var primaryInput = transform.primaryInput; |
22 var logger = new BuildLogger(transform, primaryId: primaryInput.id); | 26 var logger = new BuildLogger(transform, primaryId: primaryInput.id); |
23 var crawler = new ImportCrawler(transform, primaryInput.id, logger); | 27 if (_preParseDocument) { |
| 28 return primaryInput.readAsString().then((html) { |
| 29 var document = parseHtml(html, primaryInput.id.path); |
| 30 return crawlDocument(transform, logger, document); |
| 31 }); |
| 32 } else { |
| 33 return crawlDocument(transform, logger); |
| 34 } |
| 35 } |
| 36 |
| 37 Future crawlDocument( |
| 38 Transform transform, BuildLogger logger, [Document document]) { |
| 39 var primaryInput = transform.primaryInput; |
| 40 var crawler = new ImportCrawler( |
| 41 transform, primaryInput.id, logger, primaryDocument: document); |
24 return crawler.crawlImports().then((docs) { | 42 return crawler.crawlImports().then((docs) { |
25 documents = docs; | 43 documents = docs; |
26 transform.addOutput(new Asset.fromString( | 44 transform.addOutput(new Asset.fromString( |
27 new AssetId('a', 'web/result.txt'), '${documents.keys}')); | 45 new AssetId('a', 'web/result.txt'), '${documents.keys}')); |
28 }); | 46 }); |
29 } | 47 } |
30 } | 48 } |
31 | 49 |
32 main() { | 50 main() { |
33 useCompactVMConfiguration(); | 51 useCompactVMConfiguration(); |
| 52 runTests([[new _TestTransformer('web/index.html')]]); |
| 53 // Test with a preparsed original document as well. |
| 54 runTests([[new _TestTransformer('web/index.html', true)]]); |
| 55 } |
34 | 56 |
35 testPhases('basic', [[new _TestTransformer('web/index.html')]], { | 57 runTests(List<List<Transformer>> phases) { |
| 58 testPhases('basic', phases, { |
36 'a|web/index.html': ''' | 59 'a|web/index.html': ''' |
37 <link rel="import" href="foo.html"> | 60 <link rel="import" href="foo.html"> |
38 <link rel="import" href="packages/a/foo.html"> | 61 <link rel="import" href="packages/a/foo.html"> |
39 <link rel="import" href="packages/b/foo.html"> | 62 <link rel="import" href="packages/b/foo.html"> |
40 <link rel="import" href="packages/b/foo/bar.html"> | 63 <link rel="import" href="packages/b/foo/bar.html"> |
41 <div>a|web/index.html</div> | 64 <div>a|web/index.html</div> |
42 ''', | 65 ''', |
43 'a|web/foo.html': '<div>a|web/foo.html</div>', | 66 'a|web/foo.html': '<div>a|web/foo.html</div>', |
44 'a|lib/foo.html': '<div>a|lib/foo.html</div>', | 67 'a|lib/foo.html': '<div>a|lib/foo.html</div>', |
45 'b|lib/foo.html': ''' | 68 'b|lib/foo.html': ''' |
46 <link rel="import" href="foo/bar.html"> | 69 <link rel="import" href="foo/bar.html"> |
47 <div>b|lib/foo.html</div> | 70 <div>b|lib/foo.html</div> |
48 ''', | 71 ''', |
49 'b|lib/foo/bar.html': '<div>b|lib/foo/bar.html</div>', | 72 'b|lib/foo/bar.html': '<div>b|lib/foo/bar.html</div>', |
50 }, { | 73 }, { |
51 'a|web/result.txt': ''' | 74 'a|web/result.txt': ''' |
52 (a|web/foo.html, a|lib/foo.html, b|lib/foo/bar.html, b|lib/foo.html, a|web
/index.html) | 75 (a|web/foo.html, a|lib/foo.html, b|lib/foo/bar.html, b|lib/foo.html, a|web
/index.html) |
53 ''', | 76 ''', |
54 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); | 77 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
55 | 78 |
56 testPhases('cycle', [[new _TestTransformer('web/index.html')]], { | 79 testPhases('cycle', phases, { |
57 'a|web/index.html': ''' | 80 'a|web/index.html': ''' |
58 <link rel="import" href="packages/a/foo.html"> | 81 <link rel="import" href="packages/a/foo.html"> |
59 <div>a|web/index.html</div> | 82 <div>a|web/index.html</div> |
60 ''', | 83 ''', |
61 'a|lib/foo.html': ''' | 84 'a|lib/foo.html': ''' |
62 <link rel="import" href="bar.html"> | 85 <link rel="import" href="bar.html"> |
63 <div>a|lib/foo.html</div>''', | 86 <div>a|lib/foo.html</div>''', |
64 'a|lib/bar.html': ''' | 87 'a|lib/bar.html': ''' |
65 <link rel="import" href="foo.html"> | 88 <link rel="import" href="foo.html"> |
66 <div>a|lib/bar.html</div>''', | 89 <div>a|lib/bar.html</div>''', |
67 }, { | 90 }, { |
68 'a|web/result.txt': ''' | 91 'a|web/result.txt': ''' |
69 (a|lib/bar.html, a|lib/foo.html, a|web/index.html) | 92 (a|lib/bar.html, a|lib/foo.html, a|web/index.html) |
70 ''', | 93 ''', |
71 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); | 94 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
72 | 95 |
73 testPhases('deep imports', [[new _TestTransformer('web/index.html')]], { | 96 testPhases('deep imports', phases, { |
74 'a|web/index.html': ''' | 97 'a|web/index.html': ''' |
75 <link rel="import" href="packages/a/foo.html"> | 98 <link rel="import" href="packages/a/foo.html"> |
76 <div>a|web/index.html</div> | 99 <div>a|web/index.html</div> |
77 ''', | 100 ''', |
78 'a|lib/foo.html': ''' | 101 'a|lib/foo.html': ''' |
79 <link rel="import" href="one/bar.html"> | 102 <link rel="import" href="one/bar.html"> |
80 <div>a|lib/foo.html</div>''', | 103 <div>a|lib/foo.html</div>''', |
81 'a|lib/one/bar.html': ''' | 104 'a|lib/one/bar.html': ''' |
82 <link rel="import" href="two/baz.html"> | 105 <link rel="import" href="two/baz.html"> |
83 <div>a|lib/one/bar.html</div>''', | 106 <div>a|lib/one/bar.html</div>''', |
84 'a|lib/one/two/baz.html': ''' | 107 'a|lib/one/two/baz.html': ''' |
85 <link rel="import" href="three/zap.html"> | 108 <link rel="import" href="three/zap.html"> |
86 <div>a|lib/one/two/baz.html</div>''', | 109 <div>a|lib/one/two/baz.html</div>''', |
87 'a|lib/one/two/three/zap.html': ''' | 110 'a|lib/one/two/three/zap.html': ''' |
88 <div>a|lib/one/two/three/zap.html</div>''', | 111 <div>a|lib/one/two/three/zap.html</div>''', |
89 }, { | 112 }, { |
90 'a|web/result.txt': | 113 'a|web/result.txt': |
91 '(a|lib/one/two/three/zap.html, a|lib/one/two/baz.html, ' | 114 '(a|lib/one/two/three/zap.html, a|lib/one/two/baz.html, ' |
92 'a|lib/one/bar.html, a|lib/foo.html, a|web/index.html)', | 115 'a|lib/one/bar.html, a|lib/foo.html, a|web/index.html)', |
93 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); | 116 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
94 } | 117 } |
OLD | NEW |