OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 library web_components.test.build.import_crawler_test; |
| 5 |
| 6 import 'dart:async'; |
| 7 import 'package:barback/barback.dart'; |
| 8 import 'package:code_transformers/tests.dart'; |
| 9 import 'package:code_transformers/messages/build_logger.dart'; |
| 10 import 'package:html/dom.dart' show Document; |
| 11 import 'package:web_components/build/common.dart'; |
| 12 import 'package:web_components/build/import_crawler.dart'; |
| 13 import 'package:unittest/compact_vm_config.dart'; |
| 14 |
| 15 class _TestTransformer extends Transformer { |
| 16 final String _entryPoint; |
| 17 Map<AssetId, ImportData> documents; |
| 18 final bool _preParseDocument; |
| 19 |
| 20 _TestTransformer(this._entryPoint, [this._preParseDocument = false]); |
| 21 |
| 22 isPrimary(AssetId id) => id.path == _entryPoint; |
| 23 |
| 24 apply(Transform transform) { |
| 25 var primaryInput = transform.primaryInput; |
| 26 var logger = new BuildLogger(transform, primaryId: primaryInput.id); |
| 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(Transform transform, BuildLogger logger, |
| 38 [Document document]) { |
| 39 var primaryInput = transform.primaryInput; |
| 40 var crawler = new ImportCrawler(transform, primaryInput.id, logger, |
| 41 primaryDocument: document); |
| 42 return crawler.crawlImports().then((docs) { |
| 43 documents = docs; |
| 44 transform.addOutput(new Asset.fromString( |
| 45 new AssetId('a', 'web/result.txt'), '${documents.keys}')); |
| 46 }); |
| 47 } |
| 48 } |
| 49 |
| 50 main() { |
| 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 } |
| 56 |
| 57 runTests(List<List<Transformer>> phases) { |
| 58 testPhases('basic', phases, { |
| 59 'a|web/index.html': ''' |
| 60 <link rel="import" href="foo.html"> |
| 61 <link rel="import" href="packages/a/foo.html"> |
| 62 <link rel="import" href="packages/b/foo.html"> |
| 63 <link rel="import" href="packages/b/foo/bar.html"> |
| 64 <div>a|web/index.html</div> |
| 65 ''', |
| 66 'a|web/foo.html': '<div>a|web/foo.html</div>', |
| 67 'a|lib/foo.html': '<div>a|lib/foo.html</div>', |
| 68 'b|lib/foo.html': ''' |
| 69 <link rel="import" href="foo/bar.html"> |
| 70 <div>b|lib/foo.html</div> |
| 71 ''', |
| 72 'b|lib/foo/bar.html': '<div>b|lib/foo/bar.html</div>', |
| 73 }, { |
| 74 'a|web/result.txt': ''' |
| 75 (a|web/foo.html, a|lib/foo.html, b|lib/foo/bar.html, b|lib/foo.html, a|web
/index.html) |
| 76 ''', |
| 77 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 78 |
| 79 testPhases('cycle', phases, { |
| 80 'a|web/index.html': ''' |
| 81 <link rel="import" href="packages/a/foo.html"> |
| 82 <div>a|web/index.html</div> |
| 83 ''', |
| 84 'a|lib/foo.html': ''' |
| 85 <link rel="import" href="bar.html"> |
| 86 <div>a|lib/foo.html</div>''', |
| 87 'a|lib/bar.html': ''' |
| 88 <link rel="import" href="foo.html"> |
| 89 <div>a|lib/bar.html</div>''', |
| 90 }, { |
| 91 'a|web/result.txt': ''' |
| 92 (a|lib/bar.html, a|lib/foo.html, a|web/index.html) |
| 93 ''', |
| 94 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 95 |
| 96 testPhases('deep imports', phases, { |
| 97 'a|web/index.html': ''' |
| 98 <link rel="import" href="packages/a/foo.html"> |
| 99 <div>a|web/index.html</div> |
| 100 ''', |
| 101 'a|lib/foo.html': ''' |
| 102 <link rel="import" href="one/bar.html"> |
| 103 <div>a|lib/foo.html</div>''', |
| 104 'a|lib/one/bar.html': ''' |
| 105 <link rel="import" href="two/baz.html"> |
| 106 <div>a|lib/one/bar.html</div>''', |
| 107 'a|lib/one/two/baz.html': ''' |
| 108 <link rel="import" href="three/zap.html"> |
| 109 <div>a|lib/one/two/baz.html</div>''', |
| 110 'a|lib/one/two/three/zap.html': ''' |
| 111 <div>a|lib/one/two/three/zap.html</div>''', |
| 112 }, { |
| 113 'a|web/result.txt': |
| 114 '(a|lib/one/two/three/zap.html, a|lib/one/two/baz.html, ' |
| 115 'a|lib/one/bar.html, a|lib/foo.html, a|web/index.html)', |
| 116 }, [], StringFormatter.noNewlinesOrSurroundingWhitespace); |
| 117 } |
OLD | NEW |