| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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 | |
| 5 library code_transformers.test.assets_test; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:barback/barback.dart'; | |
| 10 import 'package:code_transformers/assets.dart'; | |
| 11 import 'package:code_transformers/tests.dart'; | |
| 12 import 'package:unittest/compact_vm_config.dart'; | |
| 13 import 'package:unittest/unittest.dart'; | |
| 14 | |
| 15 main() { | |
| 16 useCompactVMConfiguration(); | |
| 17 | |
| 18 | |
| 19 void testAssetUri(String name, | |
| 20 {AssetId source, String uri, AssetId result, String message, | |
| 21 bool errorOnAbsolute: true}) { | |
| 22 test(name, () { | |
| 23 var transformer = new Validator((transform) { | |
| 24 var assetId = uriToAssetId(source, uri, transform.logger, null, | |
| 25 errorOnAbsolute: errorOnAbsolute); | |
| 26 expect(assetId, result); | |
| 27 }); | |
| 28 var messages = []; | |
| 29 if (message != null) messages.add(message); | |
| 30 | |
| 31 return applyTransformers( | |
| 32 [[transformer]], | |
| 33 inputs: { | |
| 34 source.toString(): '' | |
| 35 }, | |
| 36 messages: messages); | |
| 37 }); | |
| 38 } | |
| 39 | |
| 40 group('uriToAssetId', () { | |
| 41 testAssetUri('resolves relative URIs', | |
| 42 source: new AssetId('a', 'web/main.dart'), | |
| 43 uri: 'foo.dart', | |
| 44 result: new AssetId('a', 'web/foo.dart')); | |
| 45 | |
| 46 testAssetUri('resolves package: URIs', | |
| 47 source: new AssetId('a', 'web/main.dart'), | |
| 48 uri: 'package:foo/foo.dart', | |
| 49 result: new AssetId('foo', 'lib/foo.dart')); | |
| 50 | |
| 51 testAssetUri('resolves package: URIs from libs', | |
| 52 source: new AssetId('a', 'lib/main.dart'), | |
| 53 uri: 'package:foo/foo.dart', | |
| 54 result: new AssetId('foo', 'lib/foo.dart')); | |
| 55 | |
| 56 testAssetUri('resolves packages paths', | |
| 57 source: new AssetId('a', 'web/main.dart'), | |
| 58 uri: 'packages/foo/foo.dart', | |
| 59 result: new AssetId('foo', 'lib/foo.dart')); | |
| 60 | |
| 61 testAssetUri('resolves relative packages paths', | |
| 62 source: new AssetId('a', 'web/main.dart'), | |
| 63 uri: 'packages/foo/foo.dart', | |
| 64 result: new AssetId('foo', 'lib/foo.dart')); | |
| 65 | |
| 66 testAssetUri('does not allow packages from non-dart lib files', | |
| 67 source: new AssetId('a', 'lib/index.html'), | |
| 68 uri: 'packages/foo/bar', | |
| 69 message: 'warning: Invalid URL to reach to another package: ' | |
| 70 'packages/foo/bar. Path reaching to other packages must first ' | |
| 71 'reach up all the way to the packages directory. For example, try ' | |
| 72 'changing the URL to: ../../packages/foo/bar'); | |
| 73 | |
| 74 testAssetUri('allows relative packages from non-dart lib files', | |
| 75 source: new AssetId('a', 'lib/index.html'), | |
| 76 uri: '../../packages/foo/bar', | |
| 77 result: new AssetId('foo', 'lib/bar')); | |
| 78 | |
| 79 testAssetUri('does not allow package: imports from non-dart files', | |
| 80 source: new AssetId('a', 'lib/index.html'), | |
| 81 uri: 'package:foo/bar.dart', | |
| 82 message: 'warning: absolute paths not allowed: "package:foo/bar.dart"'); | |
| 83 | |
| 84 testAssetUri('does not allow absolute /packages by default', | |
| 85 source: new AssetId('a', 'lib/index.html'), | |
| 86 uri: '/packages/foo/bar.dart', | |
| 87 message: | |
| 88 'warning: absolute paths not allowed: "/packages/foo/bar.dart"'); | |
| 89 | |
| 90 testAssetUri('can suppress error on absolute /packages ', | |
| 91 source: new AssetId('a', 'lib/index.html'), | |
| 92 uri: '/packages/foo/bar.dart', | |
| 93 errorOnAbsolute: false, | |
| 94 result: null); | |
| 95 }); | |
| 96 } | |
| 97 | |
| 98 class Validator extends Transformer { | |
| 99 final Function validation; | |
| 100 | |
| 101 Validator(this.validation); | |
| 102 | |
| 103 Future apply(Transform transform) { | |
| 104 return new Future.value(validation(transform)); | |
| 105 } | |
| 106 } | |
| OLD | NEW |