OLD | NEW |
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 pub.test.preprocess_test; | 1 library pub.test.preprocess_test; |
6 | |
7 import 'package:unittest/unittest.dart'; | 2 import 'package:unittest/unittest.dart'; |
8 | |
9 import '../lib/src/preprocess.dart'; | 3 import '../lib/src/preprocess.dart'; |
10 import '../lib/src/version.dart'; | 4 import '../lib/src/version.dart'; |
11 import 'test_pub.dart'; | 5 import 'test_pub.dart'; |
12 | |
13 main() { | 6 main() { |
14 initConfig(); | 7 initConfig(); |
15 | |
16 test("does nothing on a file without preprocessor directives", () { | 8 test("does nothing on a file without preprocessor directives", () { |
17 var text = ''' | 9 var text = ''' |
18 some text | 10 some text |
19 // normal comment | 11 // normal comment |
20 // # | 12 // # |
21 //# not beginning of line | 13 //# not beginning of line |
22 '''; | 14 '''; |
23 | |
24 expect(_preprocess(text), equals(text)); | 15 expect(_preprocess(text), equals(text)); |
25 }); | 16 }); |
26 | |
27 test("allows bare insert directive", () { | 17 test("allows bare insert directive", () { |
28 expect(_preprocess('//> foo'), equals('foo')); | 18 expect(_preprocess('//> foo'), equals('foo')); |
29 }); | 19 }); |
30 | |
31 test("allows empty insert directive", () { | 20 test("allows empty insert directive", () { |
32 expect(_preprocess(''' | 21 expect(_preprocess(''' |
33 //> foo | 22 //> foo |
34 //> | 23 //> |
35 //> bar | 24 //> bar |
36 '''), equals('foo\n\nbar\n')); | 25 '''), equals('foo\n\nbar\n')); |
37 }); | 26 }); |
38 | |
39 group("if", () { | 27 group("if", () { |
40 group("with a version range", () { | 28 group("with a version range", () { |
41 test("removes sections with non-matching versions", () { | 29 test("removes sections with non-matching versions", () { |
42 expect(_preprocess(''' | 30 expect(_preprocess(''' |
43 before | 31 before |
44 //# if barback <1.0.0 | 32 //# if barback <1.0.0 |
45 inside | 33 inside |
46 //# end | 34 //# end |
47 after | 35 after |
48 '''), equals(''' | 36 '''), equals(''' |
49 before | 37 before |
50 after | 38 after |
51 ''')); | 39 ''')); |
52 }); | 40 }); |
53 | |
54 test("doesn't insert section with non-matching versions", () { | 41 test("doesn't insert section with non-matching versions", () { |
55 expect(_preprocess(''' | 42 expect(_preprocess(''' |
56 before | 43 before |
57 //# if barback <1.0.0 | 44 //# if barback <1.0.0 |
58 //> inside | 45 //> inside |
59 //# end | 46 //# end |
60 after | 47 after |
61 '''), equals(''' | 48 '''), equals(''' |
62 before | 49 before |
63 after | 50 after |
64 ''')); | 51 ''')); |
65 }); | 52 }); |
66 | |
67 test("doesn't remove sections with matching versions", () { | 53 test("doesn't remove sections with matching versions", () { |
68 expect(_preprocess(''' | 54 expect(_preprocess(''' |
69 before | 55 before |
70 //# if barback >1.0.0 | 56 //# if barback >1.0.0 |
71 inside | 57 inside |
72 //# end | 58 //# end |
73 after | 59 after |
74 '''), equals(''' | 60 '''), equals(''' |
75 before | 61 before |
76 inside | 62 inside |
77 after | 63 after |
78 ''')); | 64 ''')); |
79 }); | 65 }); |
80 | |
81 test("inserts sections with matching versions", () { | 66 test("inserts sections with matching versions", () { |
82 expect(_preprocess(''' | 67 expect(_preprocess(''' |
83 before | 68 before |
84 //# if barback >1.0.0 | 69 //# if barback >1.0.0 |
85 //> inside | 70 //> inside |
86 //# end | 71 //# end |
87 after | 72 after |
88 '''), equals(''' | 73 '''), equals(''' |
89 before | 74 before |
90 inside | 75 inside |
91 after | 76 after |
92 ''')); | 77 ''')); |
93 }); | 78 }); |
94 | |
95 test("allows multi-element version ranges", () { | 79 test("allows multi-element version ranges", () { |
96 expect(_preprocess(''' | 80 expect(_preprocess(''' |
97 before | 81 before |
98 //# if barback >=1.0.0 <2.0.0 | 82 //# if barback >=1.0.0 <2.0.0 |
99 inside 1 | 83 inside 1 |
100 //# end | 84 //# end |
101 //# if barback >=0.9.0 <1.0.0 | 85 //# if barback >=0.9.0 <1.0.0 |
102 inside 2 | 86 inside 2 |
103 //# end | 87 //# end |
104 after | 88 after |
105 '''), equals(''' | 89 '''), equals(''' |
106 before | 90 before |
107 inside 1 | 91 inside 1 |
108 after | 92 after |
109 ''')); | 93 ''')); |
110 }); | 94 }); |
111 }); | 95 }); |
112 | |
113 group("with a package name", () { | 96 group("with a package name", () { |
114 test("removes sections for a nonexistent package", () { | 97 test("removes sections for a nonexistent package", () { |
115 expect(_preprocess(''' | 98 expect(_preprocess(''' |
116 before | 99 before |
117 //# if fblthp | 100 //# if fblthp |
118 inside | 101 inside |
119 //# end | 102 //# end |
120 after | 103 after |
121 '''), equals(''' | 104 '''), equals(''' |
122 before | 105 before |
123 after | 106 after |
124 ''')); | 107 ''')); |
125 }); | 108 }); |
126 | |
127 test("doesn't insert sections for a nonexistent package", () { | 109 test("doesn't insert sections for a nonexistent package", () { |
128 expect(_preprocess(''' | 110 expect(_preprocess(''' |
129 before | 111 before |
130 //# if fblthp | 112 //# if fblthp |
131 //> inside | 113 //> inside |
132 //# end | 114 //# end |
133 after | 115 after |
134 '''), equals(''' | 116 '''), equals(''' |
135 before | 117 before |
136 after | 118 after |
137 ''')); | 119 ''')); |
138 }); | 120 }); |
139 | |
140 test("doesn't remove sections with an existent package", () { | 121 test("doesn't remove sections with an existent package", () { |
141 expect(_preprocess(''' | 122 expect(_preprocess(''' |
142 before | 123 before |
143 //# if barback | 124 //# if barback |
144 inside | 125 inside |
145 //# end | 126 //# end |
146 after | 127 after |
147 '''), equals(''' | 128 '''), equals(''' |
148 before | 129 before |
149 inside | 130 inside |
150 after | 131 after |
151 ''')); | 132 ''')); |
152 }); | 133 }); |
153 | |
154 test("inserts sections with an existent package", () { | 134 test("inserts sections with an existent package", () { |
155 expect(_preprocess(''' | 135 expect(_preprocess(''' |
156 before | 136 before |
157 //# if barback | 137 //# if barback |
158 //> inside | 138 //> inside |
159 //# end | 139 //# end |
160 after | 140 after |
161 '''), equals(''' | 141 '''), equals(''' |
162 before | 142 before |
163 inside | 143 inside |
164 after | 144 after |
165 ''')); | 145 ''')); |
166 }); | 146 }); |
167 }); | 147 }); |
168 }); | 148 }); |
169 | |
170 group("else", () { | 149 group("else", () { |
171 test("removes non-matching sections", () { | 150 test("removes non-matching sections", () { |
172 expect(_preprocess(''' | 151 expect(_preprocess(''' |
173 before | 152 before |
174 //# if barback >1.0.0 | 153 //# if barback >1.0.0 |
175 inside 1 | 154 inside 1 |
176 //# else | 155 //# else |
177 inside 2 | 156 inside 2 |
178 //# end | 157 //# end |
179 after | 158 after |
180 '''), equals(''' | 159 '''), equals(''' |
181 before | 160 before |
182 inside 1 | 161 inside 1 |
183 after | 162 after |
184 ''')); | 163 ''')); |
185 }); | 164 }); |
186 | |
187 test("doesn't insert non-matching sections", () { | 165 test("doesn't insert non-matching sections", () { |
188 expect(_preprocess(''' | 166 expect(_preprocess(''' |
189 before | 167 before |
190 //# if barback >1.0.0 | 168 //# if barback >1.0.0 |
191 inside 1 | 169 inside 1 |
192 //# else | 170 //# else |
193 //> inside 2 | 171 //> inside 2 |
194 //# end | 172 //# end |
195 after | 173 after |
196 '''), equals(''' | 174 '''), equals(''' |
197 before | 175 before |
198 inside 1 | 176 inside 1 |
199 after | 177 after |
200 ''')); | 178 ''')); |
201 }); | 179 }); |
202 | |
203 test("doesn't remove matching sections", () { | 180 test("doesn't remove matching sections", () { |
204 expect(_preprocess(''' | 181 expect(_preprocess(''' |
205 before | 182 before |
206 //# if barback <1.0.0 | 183 //# if barback <1.0.0 |
207 inside 1 | 184 inside 1 |
208 //# else | 185 //# else |
209 inside 2 | 186 inside 2 |
210 //# end | 187 //# end |
211 after | 188 after |
212 '''), equals(''' | 189 '''), equals(''' |
213 before | 190 before |
214 inside 2 | 191 inside 2 |
215 after | 192 after |
216 ''')); | 193 ''')); |
217 }); | 194 }); |
218 | |
219 test("inserts matching sections", () { | 195 test("inserts matching sections", () { |
220 expect(_preprocess(''' | 196 expect(_preprocess(''' |
221 before | 197 before |
222 //# if barback <1.0.0 | 198 //# if barback <1.0.0 |
223 inside 1 | 199 inside 1 |
224 //# else | 200 //# else |
225 //> inside 2 | 201 //> inside 2 |
226 //# end | 202 //# end |
227 after | 203 after |
228 '''), equals(''' | 204 '''), equals(''' |
229 before | 205 before |
230 inside 2 | 206 inside 2 |
231 after | 207 after |
232 ''')); | 208 ''')); |
233 }); | 209 }); |
234 }); | 210 }); |
235 | |
236 group("errors", () { | 211 group("errors", () { |
237 test("disallows unknown statements", () { | 212 test("disallows unknown statements", () { |
238 expect(() => _preprocess('//# foo bar\n//# end'), throwsFormatException); | 213 expect(() => _preprocess('//# foo bar\n//# end'), throwsFormatException); |
239 }); | 214 }); |
240 | |
241 test("disallows insert directive without space", () { | 215 test("disallows insert directive without space", () { |
242 expect(() => _preprocess('//>foo'), throwsFormatException); | 216 expect(() => _preprocess('//>foo'), throwsFormatException); |
243 }); | 217 }); |
244 | |
245 group("if", () { | 218 group("if", () { |
246 test("disallows if with no arguments", () { | 219 test("disallows if with no arguments", () { |
247 expect(() => _preprocess('//# if\n//# end'), throwsFormatException); | 220 expect(() => _preprocess('//# if\n//# end'), throwsFormatException); |
248 }); | 221 }); |
249 | |
250 test("disallows if with no package", () { | 222 test("disallows if with no package", () { |
251 expect(() => _preprocess('//# if <=1.0.0\n//# end'), | 223 expect( |
| 224 () => _preprocess('//# if <=1.0.0\n//# end'), |
252 throwsFormatException); | 225 throwsFormatException); |
253 }); | 226 }); |
254 | |
255 test("disallows invalid version constraint", () { | 227 test("disallows invalid version constraint", () { |
256 expect(() => _preprocess('//# if barback >=1.0\n//# end'), | 228 expect( |
| 229 () => _preprocess('//# if barback >=1.0\n//# end'), |
257 throwsFormatException); | 230 throwsFormatException); |
258 }); | 231 }); |
259 | |
260 test("disallows dangling end", () { | 232 test("disallows dangling end", () { |
261 expect(() => _preprocess('//# end'), | 233 expect(() => _preprocess('//# end'), throwsFormatException); |
| 234 }); |
| 235 test("disallows if without end", () { |
| 236 expect( |
| 237 () => _preprocess('//# if barback >=1.0.0'), |
262 throwsFormatException); | 238 throwsFormatException); |
263 }); | 239 }); |
264 | |
265 test("disallows if without end", () { | |
266 expect(() => _preprocess('//# if barback >=1.0.0'), | |
267 throwsFormatException); | |
268 }); | |
269 | |
270 test("disallows nested if", () { | 240 test("disallows nested if", () { |
271 expect(() => _preprocess(''' | 241 expect(() => _preprocess(''' |
272 //# if barback >=1.0.0 | 242 //# if barback >=1.0.0 |
273 //# if barback >= 1.5.0 | 243 //# if barback >= 1.5.0 |
274 //# end | 244 //# end |
275 //# end | 245 //# end |
276 '''), | 246 '''), throwsFormatException); |
277 throwsFormatException); | |
278 }); | 247 }); |
279 }); | 248 }); |
280 | |
281 group("else", () { | 249 group("else", () { |
282 test("disallows else without if", () { | 250 test("disallows else without if", () { |
283 expect(() => _preprocess('//# else\n//# end'), throwsFormatException); | 251 expect(() => _preprocess('//# else\n//# end'), throwsFormatException); |
284 }); | 252 }); |
285 | |
286 test("disallows else without end", () { | 253 test("disallows else without end", () { |
287 expect(() => _preprocess('//# if barback >=1.0.0\n//# else'), | 254 expect( |
| 255 () => _preprocess('//# if barback >=1.0.0\n//# else'), |
288 throwsFormatException); | 256 throwsFormatException); |
289 }); | 257 }); |
290 | |
291 test("disallows else with an argument", () { | 258 test("disallows else with an argument", () { |
292 expect(() => _preprocess(''' | 259 expect(() => _preprocess(''' |
293 //# if barback >=1.0.0 | 260 //# if barback >=1.0.0 |
294 //# else barback <0.5.0 | 261 //# else barback <0.5.0 |
295 //# end | 262 //# end |
296 '''), throwsFormatException); | 263 '''), throwsFormatException); |
297 }); | 264 }); |
298 }); | 265 }); |
299 }); | 266 }); |
300 } | 267 } |
301 | 268 String _preprocess(String input) => preprocess(input, { |
302 String _preprocess(String input) => | 269 'barback': new Version.parse("1.2.3") |
303 preprocess(input, {'barback': new Version.parse("1.2.3")}, 'source/url'); | 270 }, 'source/url'); |
OLD | NEW |