OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #library('lock_file_test'); |
| 6 |
| 7 #import('../../../lib/unittest/unittest.dart'); |
| 8 #import('../../pub/lock_file.dart'); |
| 9 #import('../../pub/package.dart'); |
| 10 #import('../../pub/source.dart'); |
| 11 #import('../../pub/source_registry.dart'); |
| 12 #import('../../pub/utils.dart'); |
| 13 #import('../../pub/version.dart'); |
| 14 |
| 15 class MockSource extends Source { |
| 16 final String name = 'mock'; |
| 17 final bool shouldCache = false; |
| 18 |
| 19 void validateDescription(String description) => description.endsWith(' desc'); |
| 20 |
| 21 String packageName(PackageId id) { |
| 22 // Strip off ' desc'. |
| 23 return id.description.substring(0, id.description.length - 5); |
| 24 } |
| 25 } |
| 26 |
| 27 main() { |
| 28 var sources = new SourceRegistry(); |
| 29 var mockSource = new MockSource(); |
| 30 sources.register(mockSource); |
| 31 |
| 32 group('LockFile', () { |
| 33 group('parse()', () { |
| 34 test('returns an empty lockfile if the contents are empty', () { |
| 35 var lockFile = new LockFile.parse('', sources); |
| 36 expect(lockFile.packages.length).equals(0); |
| 37 }); |
| 38 |
| 39 test('returns an empty lockfile if the contents are whitespace', () { |
| 40 var lockFile = new LockFile.parse(' \t\n ', sources); |
| 41 expect(lockFile.packages.length).equals(0); |
| 42 }); |
| 43 |
| 44 test('parses a series of package descriptions', () { |
| 45 var lockFile = new LockFile.parse(''' |
| 46 packages: |
| 47 bar: |
| 48 version: 1.2.3 |
| 49 source: mock |
| 50 description: bar desc |
| 51 foo: |
| 52 version: 2.3.4 |
| 53 source: mock |
| 54 description: foo desc |
| 55 ''', sources); |
| 56 |
| 57 expect(lockFile.packages.length).equals(2); |
| 58 |
| 59 var bar = lockFile.packages['bar']; |
| 60 expect(bar.name).equals('bar'); |
| 61 expect(bar.version).equals(new Version(1, 2, 3)); |
| 62 expect(bar.source).equals(mockSource); |
| 63 expect(bar.description).equals('bar desc'); |
| 64 |
| 65 var foo = lockFile.packages['foo']; |
| 66 expect(foo.name).equals('foo'); |
| 67 expect(foo.version).equals(new Version(2, 3, 4)); |
| 68 expect(foo.source).equals(mockSource); |
| 69 expect(foo.description).equals('foo desc'); |
| 70 }); |
| 71 |
| 72 test("throws if the version is missing", () { |
| 73 throwsBadFormat(() { |
| 74 new LockFile.parse(''' |
| 75 packages: |
| 76 foo: |
| 77 source: mock |
| 78 description: foo desc |
| 79 ''', sources); |
| 80 }); |
| 81 }); |
| 82 |
| 83 test("throws if the version is invalid", () { |
| 84 throwsBadFormat(() { |
| 85 new LockFile.parse(''' |
| 86 packages: |
| 87 foo: |
| 88 version: vorpal |
| 89 source: mock |
| 90 description: foo desc |
| 91 ''', sources); |
| 92 }); |
| 93 }); |
| 94 |
| 95 test("throws if the source is missing", () { |
| 96 throwsBadFormat(() { |
| 97 new LockFile.parse(''' |
| 98 packages: |
| 99 foo: |
| 100 version: 1.2.3 |
| 101 description: foo desc |
| 102 ''', sources); |
| 103 }); |
| 104 }); |
| 105 |
| 106 test("throws if the source is unknown", () { |
| 107 throwsBadFormat(() { |
| 108 new LockFile.parse(''' |
| 109 packages: |
| 110 foo: |
| 111 version: 1.2.3 |
| 112 source: notreal |
| 113 description: foo desc |
| 114 ''', sources); |
| 115 }); |
| 116 }); |
| 117 |
| 118 test("throws if the description is missing", () { |
| 119 throwsBadFormat(() { |
| 120 new LockFile.parse(''' |
| 121 packages: |
| 122 foo: |
| 123 version: 1.2.3 |
| 124 source: mock |
| 125 ''', sources); |
| 126 }); |
| 127 }); |
| 128 |
| 129 test("throws if the description is invalid", () { |
| 130 throwsBadFormat(() { |
| 131 new LockFile.parse(''' |
| 132 packages: |
| 133 foo: |
| 134 version: 1.2.3 |
| 135 source: mock |
| 136 description: foo desc is bad |
| 137 ''', sources); |
| 138 }); |
| 139 }); |
| 140 |
| 141 test("throws if the source name doesn't match the given name", () { |
| 142 throwsBadFormat(() { |
| 143 new LockFile.parse(''' |
| 144 packages: |
| 145 foo: |
| 146 version: 1.2.3 |
| 147 source: mock |
| 148 description: notfoo desc |
| 149 ''', sources); |
| 150 }); |
| 151 }); |
| 152 |
| 153 test("ignores extra stuff in file", () { |
| 154 var lockFile = new LockFile.parse(''' |
| 155 extra: |
| 156 some: stuff |
| 157 packages: |
| 158 foo: |
| 159 bonus: not used |
| 160 version: 1.2.3 |
| 161 source: mock |
| 162 description: foo desc |
| 163 ''', sources); |
| 164 }); |
| 165 }); |
| 166 }); |
| 167 } |
| 168 |
| 169 throwsBadFormat(function) { |
| 170 expectThrow(function, (e) => e is FormatException); |
| 171 } |
OLD | NEW |