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