OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 | 4 @TestOn('vm') |
5 library code_transformers.test.assets_test; | 5 library code_transformers.test.assets_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:barback/barback.dart'; | 9 import 'package:barback/barback.dart'; |
10 import 'package:code_transformers/assets.dart'; | 10 import 'package:code_transformers/assets.dart'; |
11 import 'package:code_transformers/tests.dart'; | 11 import 'package:transformer_test/utils.dart'; |
12 import 'package:unittest/compact_vm_config.dart'; | 12 import 'package:test/test.dart'; |
13 import 'package:unittest/unittest.dart'; | |
14 | 13 |
15 main() { | 14 main() { |
16 useCompactVMConfiguration(); | |
17 | |
18 group('uriToAssetId', uriToAssetIdTests); | 15 group('uriToAssetId', uriToAssetIdTests); |
19 group('assetIdToUri', assetIdToUriTests); | 16 group('assetIdToUri', assetIdToUriTests); |
20 } | 17 } |
21 | 18 |
22 void assetIdToUriTests() { | 19 void assetIdToUriTests() { |
23 void testAssetIdToUri(String name, AssetId assetId, | 20 void testAssetIdToUri(String name, AssetId assetId, |
24 {String result, AssetId from, String message}) { | 21 {String result, AssetId from, String message}) { |
25 test(name, () { | 22 test(name, () { |
26 var transformer = new Validator((transform) { | 23 var transformer = new Validator((transform) { |
27 var uriOut = assetIdToUri(assetId, | 24 var uriOut = assetIdToUri(assetId, |
28 logger: transform.logger, span: null, from: from); | 25 logger: transform.logger, span: null, from: from); |
29 expect(uriOut, result); | 26 expect(uriOut, result); |
30 }); | 27 }); |
31 var messages = []; | 28 var messages = <String>[]; |
32 if (message != null) messages.add(message); | 29 if (message != null) messages.add(message); |
33 | 30 |
34 return applyTransformers([[transformer]], | 31 return applyTransformers([ |
35 inputs: {assetId.toString(): ''}, messages: messages); | 32 [transformer] |
| 33 ], inputs: { |
| 34 assetId.toString(): '' |
| 35 }, messages: messages); |
36 }); | 36 }); |
37 } | 37 } |
38 | 38 |
39 testAssetIdToUri('resolves relative URIs', new AssetId('a', 'web/main.dart'), | 39 testAssetIdToUri('resolves relative URIs', new AssetId('a', 'web/main.dart'), |
40 result: 'main.dart', from: new AssetId('a', 'web/foo.dart')); | 40 result: 'main.dart', from: new AssetId('a', 'web/foo.dart')); |
41 | 41 |
42 testAssetIdToUri('resolves relative URIs in subfolders', new AssetId('a', 'web
/foo/main.dart'), | 42 testAssetIdToUri('resolves relative URIs in subfolders', |
| 43 new AssetId('a', 'web/foo/main.dart'), |
43 result: 'foo/main.dart', from: new AssetId('a', 'web/foo.dart')); | 44 result: 'foo/main.dart', from: new AssetId('a', 'web/foo.dart')); |
44 | 45 |
45 testAssetIdToUri('resolves package: URIs', new AssetId('foo', 'lib/foo.dart'), | 46 testAssetIdToUri('resolves package: URIs', new AssetId('foo', 'lib/foo.dart'), |
46 result: 'package:foo/foo.dart'); | 47 result: 'package:foo/foo.dart'); |
47 | 48 |
48 testAssetIdToUri( | 49 testAssetIdToUri( |
49 'resolves package: URIs from libs', new AssetId('foo', 'lib/foo.dart'), | 50 '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 result: 'package:foo/foo.dart', from: new AssetId('a', 'lib/main.dart')); |
51 | 52 |
52 testAssetIdToUri( | 53 testAssetIdToUri( |
53 'resolves packages paths', new AssetId('foo', 'lib/foo.dart'), | 54 'resolves packages paths', new AssetId('foo', 'lib/foo.dart'), |
54 from: new AssetId('a', 'web/main.dart'), result: 'package:foo/foo.dart'); | 55 from: new AssetId('a', 'web/main.dart'), result: 'package:foo/foo.dart'); |
55 | 56 |
56 testAssetIdToUri('resolves relative packages paths', | 57 testAssetIdToUri('resolves relative packages paths', |
57 new AssetId('foo', 'lib/src/bar.dart'), | 58 new AssetId('foo', 'lib/src/bar.dart'), |
58 from: new AssetId('foo', 'lib/foo.dart'), | 59 from: new AssetId('foo', 'lib/foo.dart'), |
59 result: 'package:foo/src/bar.dart'); | 60 result: 'package:foo/src/bar.dart'); |
60 | 61 |
61 testAssetIdToUri('does not allow non-lib assets without specifying `from`', | 62 testAssetIdToUri('does not allow non-lib assets without specifying `from`', |
62 new AssetId('foo', 'not-lib/foo.dart'), | 63 new AssetId('foo', 'not-lib/foo.dart'), |
63 message: 'warning: Cannot create URI for foo|not-lib/foo.dart without ' | 64 message: 'warning: Cannot create URI for foo|not-lib/foo.dart without ' |
64 'specifying where to import it from.'); | 65 'specifying where to import it from.'); |
65 | 66 |
66 testAssetIdToUri('does not allow non-lib, non-relative assets', | 67 testAssetIdToUri('does not allow non-lib, non-relative assets', |
67 new AssetId('foo', 'not-lib/foo.dart'), | 68 new AssetId('foo', 'not-lib/foo.dart'), |
68 from: new AssetId('bar', 'lib/bar.dart'), | 69 from: new AssetId('bar', 'lib/bar.dart'), |
69 message: 'warning: Not possible to import foo|not-lib/foo.dart from ' | 70 message: 'warning: Not possible to import foo|not-lib/foo.dart from ' |
70 'bar|lib/bar.dart'); | 71 'bar|lib/bar.dart'); |
71 } | 72 } |
72 | 73 |
73 void uriToAssetIdTests() { | 74 void uriToAssetIdTests() { |
74 void testAssetUri(String name, {AssetId source, String uri, AssetId result, | 75 void testAssetUri(String name, |
75 String message, bool errorOnAbsolute: true}) { | 76 {AssetId source, |
| 77 String uri, |
| 78 AssetId result, |
| 79 String message, |
| 80 bool errorOnAbsolute: true}) { |
76 test(name, () { | 81 test(name, () { |
77 var transformer = new Validator((transform) { | 82 var transformer = new Validator((transform) { |
78 var assetId = uriToAssetId(source, uri, transform.logger, null, | 83 var assetId = uriToAssetId(source, uri, transform.logger, null, |
79 errorOnAbsolute: errorOnAbsolute); | 84 errorOnAbsolute: errorOnAbsolute); |
80 expect(assetId, result); | 85 expect(assetId, result); |
81 }); | 86 }); |
82 var messages = []; | 87 var messages = <String>[]; |
83 if (message != null) messages.add(message); | 88 if (message != null) messages.add(message); |
84 | 89 |
85 return applyTransformers([[transformer]], | 90 return applyTransformers([ |
86 inputs: {source.toString(): ''}, messages: messages); | 91 [transformer] |
| 92 ], inputs: { |
| 93 source.toString(): '' |
| 94 }, messages: messages); |
87 }); | 95 }); |
88 } | 96 } |
89 | 97 |
90 testAssetUri('resolves relative URIs', | 98 testAssetUri('resolves relative URIs', |
91 source: new AssetId('a', 'web/main.dart'), | 99 source: new AssetId('a', 'web/main.dart'), |
92 uri: 'foo.dart', | 100 uri: 'foo.dart', |
93 result: new AssetId('a', 'web/foo.dart')); | 101 result: new AssetId('a', 'web/foo.dart')); |
94 | 102 |
95 testAssetUri('resolves package: URIs', | 103 testAssetUri('resolves package: URIs', |
96 source: new AssetId('a', 'web/main.dart'), | 104 source: new AssetId('a', 'web/main.dart'), |
(...skipping 12 matching lines...) Expand all Loading... |
109 | 117 |
110 testAssetUri('resolves relative packages paths', | 118 testAssetUri('resolves relative packages paths', |
111 source: new AssetId('a', 'web/main.dart'), | 119 source: new AssetId('a', 'web/main.dart'), |
112 uri: '../lib/foo.dart', | 120 uri: '../lib/foo.dart', |
113 result: new AssetId('a', 'lib/foo.dart')); | 121 result: new AssetId('a', 'lib/foo.dart')); |
114 | 122 |
115 testAssetUri('does not allow packages from non-dart lib files', | 123 testAssetUri('does not allow packages from non-dart lib files', |
116 source: new AssetId('a', 'lib/index.html'), | 124 source: new AssetId('a', 'lib/index.html'), |
117 uri: 'packages/foo/bar', | 125 uri: 'packages/foo/bar', |
118 message: 'warning: Invalid URL to reach to another package: ' | 126 message: 'warning: Invalid URL to reach to another package: ' |
119 'packages/foo/bar. Path reaching to other packages must first ' | 127 'packages/foo/bar. Path reaching to other packages must first ' |
120 'reach up all the way to the packages directory. For example, try ' | 128 'reach up all the way to the packages directory. For example, try ' |
121 'changing the URL to: ../../packages/foo/bar'); | 129 'changing the URL to: ../../packages/foo/bar'); |
122 | 130 |
123 testAssetUri('allows relative packages from non-dart lib files', | 131 testAssetUri('allows relative packages from non-dart lib files', |
124 source: new AssetId('a', 'lib/index.html'), | 132 source: new AssetId('a', 'lib/index.html'), |
125 uri: '../../packages/foo/bar', | 133 uri: '../../packages/foo/bar', |
126 result: new AssetId('foo', 'lib/bar')); | 134 result: new AssetId('foo', 'lib/bar')); |
127 | 135 |
128 testAssetUri('does not allow package: imports from non-dart files', | 136 testAssetUri('does not allow package: imports from non-dart files', |
129 source: new AssetId('a', 'lib/index.html'), | 137 source: new AssetId('a', 'lib/index.html'), |
130 uri: 'package:foo/bar.dart', | 138 uri: 'package:foo/bar.dart', |
131 message: 'warning: absolute paths not allowed: "package:foo/bar.dart"'); | 139 message: 'warning: absolute paths not allowed: "package:foo/bar.dart"'); |
(...skipping 12 matching lines...) Expand all Loading... |
144 | 152 |
145 class Validator extends Transformer { | 153 class Validator extends Transformer { |
146 final Function validation; | 154 final Function validation; |
147 | 155 |
148 Validator(this.validation); | 156 Validator(this.validation); |
149 | 157 |
150 Future apply(Transform transform) { | 158 Future apply(Transform transform) { |
151 return new Future.value(validation(transform)); | 159 return new Future.value(validation(transform)); |
152 } | 160 } |
153 } | 161 } |
OLD | NEW |