OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 |
5 library pubspec_test; | 5 library pubspec_test; |
6 | 6 |
7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 | 8 |
9 import '../lib/src/pubspec.dart'; | 9 import '../lib/src/pubspec.dart'; |
10 import '../lib/src/source.dart'; | 10 import '../lib/src/source.dart'; |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 main() { | 26 main() { |
27 initConfig(); | 27 initConfig(); |
28 group('parse()', () { | 28 group('parse()', () { |
29 var sources = new SourceRegistry(); | 29 var sources = new SourceRegistry(); |
30 sources.register(new MockSource()); | 30 sources.register(new MockSource()); |
31 | 31 |
32 var throwsPubspecException = | 32 var throwsPubspecException = |
33 throwsA(new isInstanceOf<PubspecException>('PubspecException')); | 33 throwsA(new isInstanceOf<PubspecException>('PubspecException')); |
34 | 34 |
35 expectPubspecException(String contents, fn(Pubspec pubspec)) { | 35 expectPubspecException(String contents, fn(Pubspec pubspec), |
| 36 [String expectedContains]) { |
| 37 var expectation = throwsPubspecException; |
| 38 if (expectedContains != null) { |
| 39 expectation = throwsA(allOf( |
| 40 new isInstanceOf<PubspecException>('PubspecException'), |
| 41 predicate((error) => error.message.contains(expectedContains)))); |
| 42 } |
| 43 |
36 var pubspec = new Pubspec.parse(contents, sources); | 44 var pubspec = new Pubspec.parse(contents, sources); |
37 expect(() => fn(pubspec), throwsPubspecException); | 45 expect(() => fn(pubspec), expectation); |
38 } | 46 } |
39 | 47 |
40 test("doesn't eagerly throw an error for an invalid field", () { | 48 test("doesn't eagerly throw an error for an invalid field", () { |
41 // Shouldn't throw an error. | 49 // Shouldn't throw an error. |
42 new Pubspec.parse('version: not a semver', sources); | 50 new Pubspec.parse('version: not a semver', sources); |
43 }); | 51 }); |
44 | 52 |
45 test("eagerly throws an error if the pubspec name doesn't match the " | 53 test("eagerly throws an error if the pubspec name doesn't match the " |
46 "expected name", () { | 54 "expected name", () { |
47 expect(() => new Pubspec.parse("name: foo", sources, expectedName: 'bar'), | 55 expect(() => new Pubspec.parse("name: foo", sources, expectedName: 'bar'), |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 216 |
209 test("throws if version is not a string", () { | 217 test("throws if version is not a string", () { |
210 expectPubspecException('version: 1.0', (pubspec) => pubspec.version); | 218 expectPubspecException('version: 1.0', (pubspec) => pubspec.version); |
211 }); | 219 }); |
212 | 220 |
213 test("throws if version is not a version", () { | 221 test("throws if version is not a version", () { |
214 expectPubspecException('version: not version', | 222 expectPubspecException('version: not version', |
215 (pubspec) => pubspec.version); | 223 (pubspec) => pubspec.version); |
216 }); | 224 }); |
217 | 225 |
| 226 test("throws if transformers isn't a list", () { |
| 227 expectPubspecException('transformers: "not list"', |
| 228 (pubspec) => pubspec.transformers, |
| 229 '"transformers" field must be a list'); |
| 230 }); |
| 231 |
218 test("throws if a transformer isn't a string or map", () { | 232 test("throws if a transformer isn't a string or map", () { |
219 expectPubspecException('transformers: 12', | |
220 (pubspec) => pubspec.transformers); | |
221 expectPubspecException('transformers: [12]', | 233 expectPubspecException('transformers: [12]', |
222 (pubspec) => pubspec.transformers); | 234 (pubspec) => pubspec.transformers, |
| 235 '"transformers" field must be a string or map'); |
223 }); | 236 }); |
224 | 237 |
225 test("throws if a transformer's configuration isn't a map", () { | 238 test("throws if a transformer's configuration isn't a map", () { |
226 expectPubspecException('transformers: {pkg: 12}', | 239 expectPubspecException('transformers: [{pkg: 12}]', |
227 (pubspec) => pubspec.transformers); | 240 (pubspec) => pubspec.transformers, |
| 241 '"transformers.pkg" field must be a map'); |
228 }); | 242 }); |
229 | 243 |
230 test("throws if a transformer's configuration contains a top-level key " | 244 test("throws if a transformer's configuration contains a top-level key " |
231 "beginning with a dollar sign", () { | 245 "beginning with a dollar sign", () { |
232 expectPubspecException('transformers: {pkg: {\$key: value}}', | 246 expectPubspecException(''' |
233 (pubspec) => pubspec.transformers); | 247 name: pkg |
| 248 transformers: [{pkg: {\$key: "value"}}]''', |
| 249 (pubspec) => pubspec.transformers, |
| 250 '"transformers.pkg" field cannot contain reserved field "\$key"'); |
234 }); | 251 }); |
235 | 252 |
236 test("doesn't throw if a transformer's configuration contains a " | 253 test("doesn't throw if a transformer's configuration contains a " |
237 "non-top-level key beginning with a dollar sign", () { | 254 "non-top-level key beginning with a dollar sign", () { |
238 expectPubspecException('transformers: {pkg: {\$key: value}}', | 255 var pubspec = new Pubspec.parse(''' |
239 (pubspec) => pubspec.transformers); | 256 name: pkg |
| 257 transformers: |
| 258 - pkg: {outer: {\$inner: value}} |
| 259 ''', sources); |
| 260 |
| 261 var pkg = pubspec.transformers[0].single; |
| 262 expect(pkg.configuration["outer"]["\$inner"], equals("value")); |
| 263 }); |
| 264 |
| 265 test("throws if a transformer is not from a dependency", () { |
| 266 expectPubspecException(''' |
| 267 name: pkg |
| 268 transformers: [foo] |
| 269 ''', |
| 270 (pubspec) => pubspec.transformers, |
| 271 '"transformers.foo" refers to a package that\'s not a dependency.'); |
| 272 }); |
| 273 |
| 274 test("allows a transformer from a normal dependency", () { |
| 275 var pubspec = new Pubspec.parse(''' |
| 276 name: pkg |
| 277 dependencies: |
| 278 foo: |
| 279 mock: ok |
| 280 transformers: |
| 281 - foo''', sources); |
| 282 |
| 283 expect(pubspec.transformers[0].single.package, equals("foo")); |
| 284 }); |
| 285 |
| 286 test("allows a transformer from a dev dependency", () { |
| 287 var pubspec = new Pubspec.parse(''' |
| 288 name: pkg |
| 289 dev_dependencies: |
| 290 foo: |
| 291 mock: ok |
| 292 transformers: |
| 293 - foo''', sources); |
| 294 |
| 295 expect(pubspec.transformers[0].single.package, equals("foo")); |
| 296 }); |
| 297 |
| 298 test("allows a transformer from a dependency override", () { |
| 299 var pubspec = new Pubspec.parse(''' |
| 300 name: pkg |
| 301 dependency_overrides: |
| 302 foo: |
| 303 mock: ok |
| 304 transformers: |
| 305 - foo''', sources); |
| 306 |
| 307 expect(pubspec.transformers[0].single.package, equals("foo")); |
240 }); | 308 }); |
241 | 309 |
242 test("allows comment-only files", () { | 310 test("allows comment-only files", () { |
243 var pubspec = new Pubspec.parse(''' | 311 var pubspec = new Pubspec.parse(''' |
244 # No external dependencies yet | 312 # No external dependencies yet |
245 # Including for completeness | 313 # Including for completeness |
246 # ...and hoping the spec expands to include details about author, version, etc | 314 # ...and hoping the spec expands to include details about author, version, etc |
247 # See http://www.dartlang.org/docs/pub-package-manager/ for details | 315 # See http://www.dartlang.org/docs/pub-package-manager/ for details |
248 ''', sources); | 316 ''', sources); |
249 expect(pubspec.version, equals(Version.none)); | 317 expect(pubspec.version, equals(Version.none)); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 (pubspec) => pubspec.environment); | 352 (pubspec) => pubspec.environment); |
285 }); | 353 }); |
286 | 354 |
287 test("throws if the sdk isn't a valid version constraint", () { | 355 test("throws if the sdk isn't a valid version constraint", () { |
288 expectPubspecException('environment: {sdk: "oopies"}', | 356 expectPubspecException('environment: {sdk: "oopies"}', |
289 (pubspec) => pubspec.environment); | 357 (pubspec) => pubspec.environment); |
290 }); | 358 }); |
291 }); | 359 }); |
292 }); | 360 }); |
293 } | 361 } |
OLD | NEW |