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