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 group('uriToAssetId', uriToAssetIdTests); |
| 19 group('assetIdToUri', assetIdToUriTests); |
| 20 } |
| 21 |
| 22 void assetIdToUriTests() { |
| 23 void testAssetIdToUri(String name, AssetId assetId, |
| 24 {String result, AssetId from, String message}) { |
| 25 test(name, () { |
| 26 var transformer = new Validator((transform) { |
| 27 var uriOut = assetIdToUri(assetId, |
| 28 logger: transform.logger, span: null, from: from); |
| 29 expect(uriOut, result); |
| 30 }); |
| 31 var messages = []; |
| 32 if (message != null) messages.add(message); |
| 33 |
| 34 return applyTransformers([[transformer]], |
| 35 inputs: {assetId.toString(): ''}, messages: messages); |
| 36 }); |
| 37 } |
| 38 |
| 39 testAssetIdToUri('resolves relative URIs', new AssetId('a', 'web/main.dart'), |
| 40 result: 'main.dart', from: new AssetId('a', 'web/foo.dart')); |
| 41 |
| 42 testAssetIdToUri('resolves relative URIs in subfolders', new AssetId('a', 'web
/foo/main.dart'), |
| 43 result: 'foo/main.dart', from: new AssetId('a', 'web/foo.dart')); |
| 44 |
| 45 testAssetIdToUri('resolves package: URIs', new AssetId('foo', 'lib/foo.dart'), |
| 46 result: 'package:foo/foo.dart'); |
| 47 |
| 48 testAssetIdToUri( |
| 49 'resolves package: URIs from libs', new AssetId('foo', 'lib/foo.dart'), |
| 50 result: 'package:foo/foo.dart', from: new AssetId('a', 'lib/main.dart')); |
| 51 |
| 52 testAssetIdToUri( |
| 53 'resolves packages paths', new AssetId('foo', 'lib/foo.dart'), |
| 54 from: new AssetId('a', 'web/main.dart'), result: 'package:foo/foo.dart'); |
| 55 |
| 56 testAssetIdToUri('resolves relative packages paths', |
| 57 new AssetId('foo', 'lib/src/bar.dart'), |
| 58 from: new AssetId('foo', 'lib/foo.dart'), |
| 59 result: 'package:foo/src/bar.dart'); |
| 60 |
| 61 testAssetIdToUri('does not allow non-lib assets without specifying `from`', |
| 62 new AssetId('foo', 'not-lib/foo.dart'), |
| 63 message: 'warning: Cannot create URI for foo|not-lib/foo.dart without ' |
| 64 'specifying where to import it from.'); |
| 65 |
| 66 testAssetIdToUri('does not allow non-lib, non-relative assets', |
| 67 new AssetId('foo', 'not-lib/foo.dart'), |
| 68 from: new AssetId('bar', 'lib/bar.dart'), |
| 69 message: 'warning: Not possible to import foo|not-lib/foo.dart from ' |
| 70 'bar|lib/bar.dart'); |
| 71 } |
| 72 |
| 73 void uriToAssetIdTests() { |
| 74 void testAssetUri(String name, {AssetId source, String uri, AssetId result, |
| 75 String message, bool errorOnAbsolute: true}) { |
| 76 test(name, () { |
| 77 var transformer = new Validator((transform) { |
| 78 var assetId = uriToAssetId(source, uri, transform.logger, null, |
| 79 errorOnAbsolute: errorOnAbsolute); |
| 80 expect(assetId, result); |
| 81 }); |
| 82 var messages = []; |
| 83 if (message != null) messages.add(message); |
| 84 |
| 85 return applyTransformers([[transformer]], |
| 86 inputs: {source.toString(): ''}, messages: messages); |
| 87 }); |
| 88 } |
| 89 |
| 90 testAssetUri('resolves relative URIs', |
| 91 source: new AssetId('a', 'web/main.dart'), |
| 92 uri: 'foo.dart', |
| 93 result: new AssetId('a', 'web/foo.dart')); |
| 94 |
| 95 testAssetUri('resolves package: URIs', |
| 96 source: new AssetId('a', 'web/main.dart'), |
| 97 uri: 'package:foo/foo.dart', |
| 98 result: new AssetId('foo', 'lib/foo.dart')); |
| 99 |
| 100 testAssetUri('resolves package: URIs from libs', |
| 101 source: new AssetId('a', 'lib/main.dart'), |
| 102 uri: 'package:foo/foo.dart', |
| 103 result: new AssetId('foo', 'lib/foo.dart')); |
| 104 |
| 105 testAssetUri('resolves packages paths', |
| 106 source: new AssetId('a', 'web/main.dart'), |
| 107 uri: 'packages/foo/foo.dart', |
| 108 result: new AssetId('foo', 'lib/foo.dart')); |
| 109 |
| 110 testAssetUri('resolves relative packages paths', |
| 111 source: new AssetId('a', 'web/main.dart'), |
| 112 uri: '../lib/foo.dart', |
| 113 result: new AssetId('a', 'lib/foo.dart')); |
| 114 |
| 115 testAssetUri('does not allow packages from non-dart lib files', |
| 116 source: new AssetId('a', 'lib/index.html'), |
| 117 uri: 'packages/foo/bar', |
| 118 message: 'warning: Invalid URL to reach to another package: ' |
| 119 'packages/foo/bar. Path reaching to other packages must first ' |
| 120 'reach up all the way to the packages directory. For example, try ' |
| 121 'changing the URL to: ../../packages/foo/bar'); |
| 122 |
| 123 testAssetUri('allows relative packages from non-dart lib files', |
| 124 source: new AssetId('a', 'lib/index.html'), |
| 125 uri: '../../packages/foo/bar', |
| 126 result: new AssetId('foo', 'lib/bar')); |
| 127 |
| 128 testAssetUri('does not allow package: imports from non-dart files', |
| 129 source: new AssetId('a', 'lib/index.html'), |
| 130 uri: 'package:foo/bar.dart', |
| 131 message: 'warning: absolute paths not allowed: "package:foo/bar.dart"'); |
| 132 |
| 133 testAssetUri('does not allow absolute /packages by default', |
| 134 source: new AssetId('a', 'lib/index.html'), |
| 135 uri: '/packages/foo/bar.dart', |
| 136 message: 'warning: absolute paths not allowed: "/packages/foo/bar.dart"'); |
| 137 |
| 138 testAssetUri('can suppress error on absolute /packages ', |
| 139 source: new AssetId('a', 'lib/index.html'), |
| 140 uri: '/packages/foo/bar.dart', |
| 141 errorOnAbsolute: false, |
| 142 result: null); |
| 143 } |
| 144 |
| 145 class Validator extends Transformer { |
| 146 final Function validation; |
| 147 |
| 148 Validator(this.validation); |
| 149 |
| 150 Future apply(Transform transform) { |
| 151 return new Future.value(validation(transform)); |
| 152 } |
| 153 } |
OLD | NEW |