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