OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, 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 mixin_test; | |
6 | |
7 import 'package:unittest/unittest.dart'; | |
8 import 'testing.dart'; | |
9 | |
10 var options = ['--warnings_as_errors', '--no-colors', 'memory']; | |
Jennifer Messerly
2013/09/18 06:44:15
extra space here?
nweiz
2013/09/18 22:40:54
"final", not "var".
terry
2013/10/09 03:40:33
Done.
| |
11 | |
12 void topLevelMixin() { | |
nweiz
2013/09/18 22:40:54
Why are you defining a separate function for each
| |
13 var errors = []; | |
14 final input1 = r''' | |
nweiz
2013/09/18 22:40:54
Why is this [input1]? There's no other [input] var
terry
2013/10/09 03:40:33
Probably other test at one point. 1 is gone.
On 20
| |
15 @mixin silly-links { | |
16 a { | |
17 color: blue; | |
18 background-color: red; | |
19 } | |
20 } | |
21 | |
22 @include silly-links; | |
23 '''; | |
nweiz
2013/09/18 22:40:54
These no-indentation strings are difficult to read
terry
2013/10/09 03:40:33
I'll consider it but I fine pre-processing to be a
| |
24 | |
25 final generated1 = r'''a { | |
26 color: #00f; | |
27 background-color: #f00; | |
28 }'''; | |
29 | |
30 final stylesheet1 = compileCss(input1, errors: errors, opts: options); | |
nweiz
2013/09/18 22:40:54
Using an out parameter like [errors] here is a cod
terry
2013/10/09 03:40:33
It's something we've liked so far for tooling.
On
| |
31 | |
32 expect(stylesheet1 != null, true); | |
nweiz
2013/09/18 22:40:54
This could be written as "expect(stylesheet, isNot
| |
33 expect(errors.isEmpty, true, reason: errors.toString()); | |
nweiz
2013/09/18 22:40:54
expect(errors, isEmpty, ...)
| |
34 expect(prettyPrint(stylesheet1), generated1); | |
35 | |
36 // TODO(terry): Test using a declaration @mixin as a top-level @include. | |
nweiz
2013/09/18 22:40:54
Why is this a TODO and not an actual test?
terry
2013/10/09 03:40:33
comment old.
On 2013/09/18 22:40:54, nweiz wrote:
| |
37 } | |
38 | |
39 void topLevelMixin2Includes() { | |
40 final errors = []; | |
41 final input = r''' | |
42 @mixin a { | |
43 a { | |
44 color: blue; | |
45 background-color: red; | |
46 } | |
47 } | |
48 @mixin b { | |
49 span { | |
50 color: black; | |
51 background-color: orange; | |
52 } | |
53 } | |
54 @include a; | |
55 @include b; | |
56 '''; | |
57 | |
58 final generated = r'''a { | |
59 color: #00f; | |
60 background-color: #f00; | |
61 } | |
62 span { | |
63 color: #000; | |
64 background-color: #ffa500; | |
65 }'''; | |
66 | |
67 final stylesheet = compileCss(input, errors: errors, opts: options); | |
68 | |
69 expect(stylesheet != null, true); | |
70 expect(errors.isEmpty, true, reason: errors.toString()); | |
71 expect(prettyPrint(stylesheet), generated); | |
72 } | |
73 | |
74 void topLevelMixinMultiRulesets() { | |
nweiz
2013/09/18 22:40:54
What do multiple rulesets have to do with this tes
terry
2013/10/09 03:40:33
Tests top-level mixins that includes another mixin
| |
75 final errors = []; | |
76 final input = r''' | |
77 @mixin a { | |
78 a { | |
79 color: blue; | |
80 background-color: red; | |
81 } | |
82 } | |
83 @mixin b { | |
84 span { | |
85 color: black; | |
86 background-color: orange; | |
87 } | |
88 @include c; | |
89 } | |
90 @mixin c { | |
91 #foo-id { | |
92 border-top: 1px solid red; | |
93 border-bottom: 2px solid green; | |
94 } | |
95 } | |
96 @include a; | |
97 @include b; | |
98 '''; | |
99 | |
100 final generated = r'''a { | |
101 color: #00f; | |
102 background-color: #f00; | |
103 } | |
104 span { | |
105 color: #000; | |
106 background-color: #ffa500; | |
107 } | |
108 #foo-id { | |
109 border-top: 1px solid #f00; | |
110 border-bottom: 2px solid #008000; | |
111 }'''; | |
112 | |
113 final stylesheet = compileCss(input, errors: errors, opts: options); | |
114 | |
115 expect(stylesheet != null, true); | |
116 expect(errors.isEmpty, true, reason: errors.toString()); | |
117 expect(prettyPrint(stylesheet), generated); | |
118 } | |
119 | |
120 void topLevelMixinMultiRulesets2() { | |
nweiz
2013/09/18 22:40:54
I have no idea what this test is testing. Is it ju
terry
2013/10/09 03:40:33
1, 2 and 3 nesting levels of includes.
On 2013/09/
| |
121 final errors = []; | |
122 final input = r''' | |
123 @mixin a { | |
124 a { | |
125 color: blue; | |
126 background-color: red; | |
127 } | |
128 } | |
129 @mixin b { | |
130 @include a; | |
131 span { | |
132 color: black; | |
133 background-color: orange; | |
134 } | |
135 @include c; | |
136 @include d; | |
137 } | |
138 @mixin c { | |
139 #foo-id { | |
140 border-top: 1px solid red; | |
141 border-bottom: 2px solid green; | |
142 } | |
143 } | |
144 @mixin d { | |
145 a:hover { | |
146 cursor: arrow; | |
147 } | |
148 @include e | |
149 } | |
150 @mixin e { | |
151 #split-bar:visited { | |
152 color: gray; | |
153 } | |
154 @include f; | |
155 } | |
156 @mixin f { | |
157 #split-bar div { | |
158 border: 1px solid lightgray; | |
159 } | |
160 } | |
161 @include b; | |
162 '''; | |
163 | |
164 final generated = r'''a { | |
165 color: #00f; | |
166 background-color: #f00; | |
167 } | |
168 span { | |
169 color: #000; | |
170 background-color: #ffa500; | |
171 } | |
172 #foo-id { | |
173 border-top: 1px solid #f00; | |
174 border-bottom: 2px solid #008000; | |
175 } | |
176 a:hover { | |
177 cursor: arrow; | |
178 } | |
179 #split-bar:visited { | |
180 color: #808080; | |
181 } | |
182 #split-bar div { | |
183 border: 1px solid #d3d3d3; | |
184 }'''; | |
185 | |
186 final stylesheet = compileCss(input, errors: errors, opts: options); | |
187 | |
188 expect(stylesheet != null, true); | |
189 expect(errors.isEmpty, true, reason: errors.toString()); | |
190 expect(prettyPrint(stylesheet), generated); | |
191 } | |
192 | |
193 void topLevelMixinSelectors() { | |
nweiz
2013/09/18 22:40:54
All the previous tests had selectors. Saying that
terry
2013/10/09 03:40:33
testing with combinators and selector groups.
On 2
| |
194 final errors = []; | |
195 final input = r''' | |
196 @mixin a { | |
197 a, b { | |
198 color: blue; | |
199 background-color: red; | |
200 } | |
201 div > span { | |
202 color: black; | |
203 background-color: orange; | |
204 } | |
205 } | |
206 | |
207 @include a; | |
208 '''; | |
209 | |
210 final generated = r'''a, b { | |
211 color: #00f; | |
212 background-color: #f00; | |
213 } | |
214 div > span { | |
215 color: #000; | |
216 background-color: #ffa500; | |
217 }'''; | |
218 | |
219 final stylesheet = compileCss(input, errors: errors, opts: options); | |
220 | |
221 expect(stylesheet != null, true); | |
222 expect(errors.isEmpty, true, reason: errors.toString()); | |
223 expect(prettyPrint(stylesheet), generated); | |
224 } | |
225 | |
226 void declSimpleMixin() { | |
227 final errors = []; | |
228 final input = r''' | |
229 @mixin div-border { | |
230 border: 2px dashed red; | |
231 } | |
232 div { | |
233 @include div-border; | |
234 } | |
235 '''; | |
236 | |
237 final generated = r'''div { | |
238 border: 2px dashed #f00; | |
239 }'''; | |
240 | |
241 var stylesheet = compileCss(input, errors: errors, opts: options); | |
242 | |
243 expect(stylesheet != null, true); | |
244 expect(errors.isEmpty, true, reason: errors.toString()); | |
245 expect(prettyPrint(stylesheet), generated); | |
246 } | |
247 | |
248 void declMixin2Includes() { | |
249 final errors = []; | |
250 final input = r''' | |
251 @mixin div-border { | |
252 border: 2px dashed red; | |
253 } | |
254 @mixin div-color { | |
255 color: blue; | |
256 } | |
257 div { | |
258 @include div-border; | |
259 @include div-color; | |
260 } | |
261 '''; | |
262 | |
263 final generated = r'''div { | |
264 border: 2px dashed #f00; | |
265 color: #00f; | |
266 }'''; | |
267 | |
268 var stylesheet = compileCss(input, errors: errors, opts: options); | |
269 | |
270 expect(stylesheet != null, true); | |
271 expect(errors.isEmpty, true, reason: errors.toString()); | |
272 expect(prettyPrint(stylesheet), generated); | |
273 } | |
274 | |
275 void declMixinNestedIncludes() { | |
nweiz
2013/09/18 22:40:54
As above, "div-border" is unrelated to what this t
terry
2013/10/09 03:40:33
As test implies for than one level of testing.
On
| |
276 final errors = []; | |
277 final input = r''' | |
278 @mixin div-border { | |
279 border: 2px dashed red; | |
280 } | |
281 @mixin div-color { | |
282 @include div-padding; | |
283 color: blue; | |
284 @include div-margin; | |
285 } | |
286 @mixin div-padding { | |
287 padding: .5em; | |
288 } | |
289 @mixin div-margin { | |
290 margin: 5px; | |
291 } | |
292 div { | |
293 @include div-border; | |
294 @include div-color; | |
295 } | |
296 '''; | |
297 | |
298 final generated = r'''div { | |
299 border: 2px dashed #f00; | |
300 padding: .5em; | |
301 color: #00f; | |
302 margin: 5px; | |
303 }'''; | |
304 | |
305 var stylesheet = compileCss(input, errors: errors, opts: options); | |
306 | |
307 expect(stylesheet != null, true); | |
308 expect(errors.isEmpty, true, reason: errors.toString()); | |
309 expect(prettyPrint(stylesheet), generated); | |
310 } | |
311 | |
312 void declMixinNestedIncludes2() { | |
nweiz
2013/09/18 22:40:54
I can't understand what this test tests that the p
terry
2013/10/09 03:40:33
nesting with other selectors pseudo.
On 2013/09/18
| |
313 final errors = []; | |
314 final input = r''' | |
315 @mixin div-border { | |
316 border: 2px dashed red; | |
317 } | |
318 @mixin div-color { | |
319 @include div-padding; | |
320 @include div-margin; | |
321 } | |
322 @mixin div-padding { | |
323 padding: .5em; | |
324 } | |
325 @mixin div-margin { | |
326 margin: 5px; | |
327 } | |
328 div { | |
329 @include div-border; | |
330 @include div-color; | |
331 } | |
332 '''; | |
333 | |
334 final generated = r'''div { | |
335 border: 2px dashed #f00; | |
336 padding: .5em; | |
337 margin: 5px; | |
338 }'''; | |
339 | |
340 var stylesheet = compileCss(input, errors: errors, opts: options); | |
341 | |
342 expect(stylesheet != null, true); | |
343 expect(errors.isEmpty, true, reason: errors.toString()); | |
344 expect(prettyPrint(stylesheet), generated); | |
345 } | |
346 | |
347 void mixinArg() { | |
348 final errors = []; | |
349 final input = r''' | |
350 @mixin div-border { | |
351 border: 2px dashed red; | |
352 } | |
353 | |
354 @mixin div-inside(@dist) { | |
nweiz
2013/09/18 22:40:54
If your arguments are going to use this syntax, yo
terry
2013/10/09 03:40:33
Vars are now different in CSS (var-) but we're sup
| |
355 margin-left: var(dist); | |
356 | |
357 } | |
358 | |
359 div { | |
360 @include left(10px); | |
361 @include div-border; | |
362 } | |
363 '''; | |
364 | |
365 final generated = r''' '''; | |
366 | |
367 var stylesheet = compileCss(input, errors: errors, opts: options); | |
368 | |
369 expect(stylesheet != null, true); | |
370 expect(errors.isEmpty, true, reason: errors.toString()); | |
371 expect(prettyPrint(stylesheet), generated); | |
372 | |
373 // TODO(terry): Test using a top-level @mixin as a declaration @include. | |
374 } | |
375 | |
376 void mixinArgs() { | |
377 final errors = []; | |
378 var input = r''' | |
379 @mixin box-shadow(@shadows...) { | |
380 -moz-box-shadow: var(shadows); | |
381 -webkit-box-shadow: var(shadows; | |
382 box-shadow: var(shadows); | |
383 } | |
384 | |
385 .shadows { | |
386 @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999); | |
387 }'''; | |
388 | |
389 var generated = r''' | |
390 .shadowed { | |
391 -moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999; | |
392 -webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999; | |
393 box-shadow: 0px 4px 5px #666, 2px 6px 10px #999; | |
394 } | |
395 '''; | |
396 | |
397 var stylesheet = compileCss(input, errors: errors, opts: options); | |
398 | |
399 expect(stylesheet != null, true); | |
400 expect(errors.isEmpty, true, reason: errors.toString()); | |
401 expect(prettyPrint(stylesheet), generated); | |
402 } | |
403 | |
404 void mixinArgs_2() { | |
405 final errors = []; | |
406 var input = r''' | |
407 @mixin colors(@text, @background, @border) { | |
408 color: var(text); | |
409 background-color: var(background); | |
410 border-color: var(border); | |
411 } | |
412 | |
413 @values: #ff0000, #00ff00, #0000ff; | |
414 .primary { | |
415 @include colors(var(values)); | |
416 } | |
417 '''; | |
418 | |
419 var generated = r''' | |
420 .primary { | |
421 color: #ff0000; | |
422 background-color: #00ff00; | |
423 border-color: #0000ff; | |
424 } | |
425 '''; | |
426 | |
427 var stylesheet = compileCss(input, errors: errors, opts: options); | |
428 | |
429 expect(stylesheet != null, true); | |
430 expect(errors.isEmpty, true, reason: errors.toString()); | |
431 expect(prettyPrint(stylesheet), generated); | |
432 } | |
433 | |
434 void mixinArgs_var() { | |
435 final errors = []; | |
436 var input = r''' | |
437 @mixin wrapped-stylish-mixin(@args...) { | |
438 font-weight: bold; | |
439 @include stylish-mixin(var(args)); | |
440 } | |
441 | |
442 .stylish { | |
443 // The @width argument will get passed on to "stylish-mixin" as a keyword | |
444 @include wrapped-stylish-mixin(#00ff00, @width: 100px); | |
445 } | |
446 '''; | |
447 | |
448 var generated = r''' | |
449 '''; | |
450 | |
451 var stylesheet = compileCss(input, errors: errors, opts: options); | |
452 | |
453 expect(stylesheet != null, true); | |
454 expect(errors.isEmpty, true, reason: errors.toString()); | |
455 expect(prettyPrint(stylesheet), generated); | |
456 } | |
457 | |
458 void mixin_content_toplevel() { | |
459 void mixinArgs_var() { | |
460 final errors = []; | |
461 var input = r''' | |
462 @mixin apply-to-ie6-only { | |
463 * html { | |
464 @content; | |
465 } | |
466 } | |
467 @include apply-to-ie6-only { | |
468 #logo { | |
469 background-image: url(/logo.gif); | |
470 } | |
471 } | |
472 '''; | |
473 | |
474 var generated = r'''* html #logo { | |
475 background-image: url(/logo.gif); | |
476 }'''; | |
477 | |
478 var stylesheet = compileCss(input, errors: errors, opts: options); | |
479 | |
480 expect(stylesheet != null, true); | |
481 expect(errors.isEmpty, true, reason: errors.toString()); | |
482 expect(prettyPrint(stylesheet), generated); | |
483 } | |
484 } | |
485 | |
486 void mixin_content() { | |
487 void mixinArgs_var() { | |
488 final errors = []; | |
489 var input = r''' | |
490 @mixin keyframes { | |
491 @-moz-keyframes { @content; } | |
492 @-webkit-keyframes { @content; } | |
493 } | |
494 | |
495 @include keyframes { | |
496 0% { opacity: 0; } | |
497 100% { opacity: 1; } | |
498 } | |
499 '''; | |
500 | |
501 var generated = r'''@-moz-keyframes { | |
502 0% { opacity: 0; } | |
503 100% { opacity: 1; } | |
504 } | |
505 | |
506 @-webkit-keyframes { | |
507 0% { opacity: 0; } | |
508 100% { opacity: 1; } | |
509 }'''; | |
510 | |
511 var stylesheet = compileCss(input, errors: errors, opts: options); | |
512 | |
513 expect(stylesheet != null, true); | |
514 expect(errors.isEmpty, true, reason: errors.toString()); | |
515 expect(prettyPrint(stylesheet), generated); | |
516 } | |
517 } | |
518 | |
519 void mixin_content_var() { | |
520 void mixinArgs_var() { | |
521 final errors = []; | |
522 var input = r''' | |
523 @color: white; | |
524 @mixin colors(@color: blue) { | |
525 background-color: var(color); | |
526 @content; | |
527 border-color: var(color); | |
528 } | |
529 .colors { | |
530 @include colors { color: var(color); } | |
531 } | |
532 '''; | |
533 | |
534 var generated = r'''.colors { | |
535 background-color: blue; | |
536 color: white; | |
537 border-color: blue; | |
538 } | |
539 '''; | |
540 | |
541 var stylesheet = compileCss(input, errors: errors, opts: options); | |
542 | |
543 expect(stylesheet != null, true); | |
544 expect(errors.isEmpty, true, reason: errors.toString()); | |
545 expect(prettyPrint(stylesheet), generated); | |
546 } | |
547 } | |
548 | |
549 void badDeclarationInclude() { | |
nweiz
2013/09/18 22:40:54
This shouldn't cause an error.
terry
2013/10/09 03:40:33
Seems weird that top-level will close a declaratio
| |
550 final errors = []; | |
551 final input = r''' | |
552 @mixin a { | |
553 span { | |
554 border: 2px dashed red; | |
555 @include b; | |
556 } | |
557 } | |
558 | |
559 @mixin b { | |
560 #foo-id { | |
561 color: red; | |
562 } | |
563 } | |
564 '''; | |
565 | |
566 var stylesheet = compileCss(input, errors: errors, opts: options); | |
567 | |
568 expect(stylesheet != null, true); | |
569 expect(errors.isNotEmpty, true, reason: errors.toString()); | |
570 expect(errors.length, 1, reason: errors.toString()); | |
571 var error = errors[0]; | |
572 expect(error.message, | |
573 'Trying to use top-level ruleset in a declaration - @include b'); | |
574 expect(error.span.end.offset, 60); | |
575 } | |
576 | |
577 void badTopInclude() { | |
nweiz
2013/09/18 22:40:54
This *should* cause an error.
terry
2013/10/09 03:40:33
Currently, we really report everything as a warnin
| |
578 final errors = []; | |
579 final input = r''' | |
580 @mixin a { | |
581 span { | |
582 border: 2px dashed red; | |
583 } | |
584 @include b; | |
585 } | |
586 | |
587 @mixin b { | |
588 color: red; | |
589 } | |
590 | |
591 @include a; | |
592 '''; | |
593 | |
594 var generated = r'''span { | |
595 border: 2px dashed #f00; | |
596 }'''; | |
597 | |
598 var stylesheet = compileCss(input, errors: errors, opts: options); | |
599 expect(stylesheet != null, true); | |
600 expect(errors.isEmpty, true, reason: errors.toString()); | |
601 expect(prettyPrint(stylesheet), generated); | |
602 } | |
603 | |
604 void cycleDetect() { | |
605 final errors = []; | |
606 final input = r''' | |
607 @mixin a { | |
608 @include b; | |
609 } | |
610 @mixin b { | |
611 span { | |
612 border: 2px dashed red; | |
613 } | |
614 @include a; | |
615 } | |
616 @include b; | |
617 '''; | |
618 | |
619 var stylesheet = compileCss(input, errors: errors, opts: options); | |
620 | |
621 expect(stylesheet != null, true); | |
622 expect(errors.isNotEmpty, true, reason: errors.toString()); | |
nweiz
2013/09/18 22:40:54
expect(errors, isNotEmpty)
[reason] here doesn't
terry
2013/10/09 03:40:33
test is gone.
On 2013/09/18 22:40:54, nweiz wrote:
| |
623 expect(errors.length, 2, reason: errors.toString()); | |
624 var error = errors[0]; | |
625 expect(error.message, 'mixin cycle detected @mixin a'); | |
nweiz
2013/09/18 22:40:54
Test the type of the error, not the message. Testi
| |
626 expect(error.span.end.offset, 24); | |
627 error = errors[1]; | |
628 expect(error.message, 'mixin cycle detected @mixin b'); | |
629 expect(error.span.end.offset, 92); | |
630 } | |
631 | |
632 void cycleNesting() { | |
633 final errors = []; | |
634 final input = r''' | |
635 @mixin a { | |
636 @include b; | |
637 } | |
638 @mixin b { | |
639 border: 2px dashed red; | |
640 @include a; | |
641 } | |
642 div { | |
643 @include b; | |
644 } | |
645 '''; | |
646 | |
647 var stylesheet = compileCss(input, errors: errors, opts: options); | |
648 | |
649 expect(stylesheet != null, true); | |
650 expect(errors.isNotEmpty, true, reason: errors.toString()); | |
651 expect(errors.length, 2, reason: errors.toString()); | |
652 var error = errors[0]; | |
653 expect(error.message, 'mixin cycle detected @mixin a'); | |
654 expect(error.span.end.offset, 24); | |
655 error = errors[1]; | |
656 expect(error.message, 'mixin cycle detected @mixin b'); | |
657 expect(error.span.end.offset, 77); | |
658 } | |
659 | |
660 main() { | |
661 test('top-level mixin', topLevelMixin); | |
662 test('top-level mixin 2 @includes', topLevelMixin2Includes); | |
nweiz
2013/09/18 22:40:54
"2" -> "two", here and elsewhere. Also, at least u
terry
2013/10/09 03:40:33
Done.
| |
663 test('top-level multi rulesets', topLevelMixinMultiRulesets); | |
664 test('top-level multi rulesets and nesting', topLevelMixinMultiRulesets2); | |
665 test('top-level selector groups', topLevelMixinSelectors); | |
666 | |
667 test('declaration mixin', declSimpleMixin); | |
668 test('declaration mixin 2 @includes', declMixin2Includes); | |
669 test('declaration mixin includes', declMixinNestedIncludes); | |
670 test('declaration mixin includes #2', declMixinNestedIncludes2); | |
nweiz
2013/09/18 22:40:54
Adding "#2" here doesn't tell me anything about th
terry
2013/10/09 03:40:33
Done.
| |
671 | |
672 test('detect cycle', cycleDetect); | |
673 test('detect cycle nesting', cycleNesting); | |
674 test('detect bad top-level as declaration', badDeclarationInclude); | |
675 test('detect bad declaration as top-level', badTopInclude); | |
676 | |
677 // TODO(Terry): More tests to enable. | |
nweiz
2013/09/18 22:40:54
Don't include tests that don't work. You can add t
terry
2013/10/09 03:40:33
Done.
| |
678 // test('mixin arg', mixinArg); | |
679 // test('mixin args', mixinArgs); | |
680 // test('mixin args fanout', mixinArgs_2); | |
681 // test('mixin args with variables', mixinArgs_var); | |
682 // test('mixin content top-level', mixin_content_toplevel); | |
683 // test('mixin content', mixin_content); | |
684 // test('mixin content with variables', mixin_content_var); | |
685 } | |
OLD | NEW |