/
engine_test.rb
executable file
·3160 lines (2799 loc) · 72.4 KB
/
engine_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/../test_helper'
require File.dirname(__FILE__) + '/test_helper'
require 'sass/engine'
require 'stringio'
require 'mock_importer'
require 'pathname'
module Sass::Script::Functions::UserFunctions
def option(name)
Sass::Script::String.new(@options[name.value.to_sym].to_s)
end
def set_a_local_variable
environment.set_var('variable', Sass::Script::Number.new(5))
return Sass::Script::Null.new
end
end
class SassEngineTest < Test::Unit::TestCase
FAKE_FILE_NAME = __FILE__.gsub(/rb$/,"sass")
# A map of erroneous Sass documents to the error messages they should produce.
# The error messages may be arrays;
# if so, the second element should be the line number that should be reported for the error.
# If this isn't provided, the tests will assume the line number should be the last line of the document.
EXCEPTION_MAP = {
"$a: 1 + " => 'Invalid CSS after "1 +": expected expression (e.g. 1px, bold), was ""',
"$a: 1 + 2 +" => 'Invalid CSS after "1 + 2 +": expected expression (e.g. 1px, bold), was ""',
"$a: 1 + 2 + %" => 'Invalid CSS after "1 + 2 + ": expected expression (e.g. 1px, bold), was "%"',
"$a: foo(\"bar\"" => 'Invalid CSS after "foo("bar"": expected ")", was ""',
"$a: 1 }" => 'Invalid CSS after "1 ": expected expression (e.g. 1px, bold), was "}"',
"$a: 1 }foo\"" => 'Invalid CSS after "1 ": expected expression (e.g. 1px, bold), was "}foo""',
":" => 'Invalid property: ":".',
": a" => 'Invalid property: ": a".',
"a\n :b" => <<MSG,
Invalid property: ":b" (no value).
If ":b" should be a selector, use "\\:b" instead.
MSG
"a\n b:" => 'Invalid property: "b:" (no value).',
"a\n :b: c" => 'Invalid property: ":b: c".',
"a\n :b:c d" => 'Invalid property: ":b:c d".',
"a\n :b c;" => 'Invalid CSS after "c": expected expression (e.g. 1px, bold), was ";"',
"a\n b: c;" => 'Invalid CSS after "c": expected expression (e.g. 1px, bold), was ";"',
".foo ^bar\n a: b" => ['Invalid CSS after ".foo ": expected selector, was "^bar"', 1],
"a\n @extend .foo ^bar" => 'Invalid CSS after ".foo ": expected selector, was "^bar"',
"a\n @extend .foo .bar" => "Can't extend .foo .bar: can't extend nested selectors",
"a\n @extend >" => "Can't extend >: invalid selector",
"a\n @extend &.foo" => "Can't extend &.foo: can't extend parent selectors",
"a: b" => 'Properties are only allowed within rules, directives, mixin includes, or other properties.',
":a b" => 'Properties are only allowed within rules, directives, mixin includes, or other properties.',
"$" => 'Invalid variable: "$".',
"$a" => 'Invalid variable: "$a".',
"$ a" => 'Invalid variable: "$ a".',
"$a b" => 'Invalid variable: "$a b".',
"$a: 1b + 2c" => "Incompatible units: 'c' and 'b'.",
"$a: 1b < 2c" => "Incompatible units: 'c' and 'b'.",
"$a: 1b > 2c" => "Incompatible units: 'c' and 'b'.",
"$a: 1b <= 2c" => "Incompatible units: 'c' and 'b'.",
"$a: 1b >= 2c" => "Incompatible units: 'c' and 'b'.",
"a\n b: 1b * 2c" => "2b*c isn't a valid CSS value.",
"a\n b: 1b % 2c" => "Cannot modulo by a number with units: 2c.",
"$a: 2px + #ccc" => "Cannot add a number with units (2px) to a color (#cccccc).",
"$a: #ccc + 2px" => "Cannot add a number with units (2px) to a color (#cccccc).",
"& a\n :b c" => ["Base-level rules cannot contain the parent-selector-referencing character '&'.", 1],
"a\n :b\n c" => "Illegal nesting: Only properties may be nested beneath properties.",
"$a: b\n :c d\n" => "Illegal nesting: Nothing may be nested beneath variable declarations.",
"$a: b\n :c d\n" => "Illegal nesting: Nothing may be nested beneath variable declarations.",
"@import templates/basic\n foo" => "Illegal nesting: Nothing may be nested beneath import directives.",
"foo\n @import foo.css" => "CSS import directives may only be used at the root of a document.",
"@if true\n @import foo" => "Import directives may not be used within control directives or mixins.",
"@if true\n .foo\n @import foo" => "Import directives may not be used within control directives or mixins.",
"@mixin foo\n @import foo" => "Import directives may not be used within control directives or mixins.",
"@mixin foo\n .foo\n @import foo" => "Import directives may not be used within control directives or mixins.",
"@import foo;" => "Invalid @import: expected end of line, was \";\".",
'$foo: "bar" "baz" !' => %Q{Invalid CSS after ""bar" "baz" ": expected expression (e.g. 1px, bold), was "!"},
'$foo: "bar" "baz" $' => %Q{Invalid CSS after ""bar" "baz" ": expected expression (e.g. 1px, bold), was "$"}, #'
"=foo\n :color red\n.bar\n +bang" => "Undefined mixin 'bang'.",
"=foo\n :color red\n.bar\n +bang_bop" => "Undefined mixin 'bang_bop'.",
"=foo\n :color red\n.bar\n +bang-bop" => "Undefined mixin 'bang-bop'.",
".foo\n =foo\n :color red\n.bar\n +foo" => "Undefined mixin 'foo'.",
" a\n b: c" => ["Indenting at the beginning of the document is illegal.", 1],
" \n \n\t\n a\n b: c" => ["Indenting at the beginning of the document is illegal.", 4],
"a\n b: c\n b: c" => ["Inconsistent indentation: 1 space was used for indentation, but the rest of the document was indented using 2 spaces.", 3],
"a\n b: c\na\n b: c" => ["Inconsistent indentation: 1 space was used for indentation, but the rest of the document was indented using 2 spaces.", 4],
"a\n\t\tb: c\n\tb: c" => ["Inconsistent indentation: 1 tab was used for indentation, but the rest of the document was indented using 2 tabs.", 3],
"a\n b: c\n b: c" => ["Inconsistent indentation: 3 spaces were used for indentation, but the rest of the document was indented using 2 spaces.", 3],
"a\n b: c\n a\n d: e" => ["Inconsistent indentation: 3 spaces were used for indentation, but the rest of the document was indented using 2 spaces.", 4],
"a\n b: c\na\n d: e" => ["The line was indented 2 levels deeper than the previous line.", 4],
"a\n b: c\n a\n d: e" => ["The line was indented 3 levels deeper than the previous line.", 4],
"a\n \tb: c" => ["Indentation can't use both tabs and spaces.", 2],
"=a(" => 'Invalid CSS after "(": expected variable (e.g. $foo), was ""',
"=a(b)" => 'Invalid CSS after "(": expected variable (e.g. $foo), was "b)"',
"=a(,)" => 'Invalid CSS after "(": expected variable (e.g. $foo), was ",)"',
"=a($)" => 'Invalid CSS after "(": expected variable (e.g. $foo), was "$)"',
"=a($foo bar)" => 'Invalid CSS after "($foo ": expected ")", was "bar)"',
"=foo\n bar: baz\n+foo" => ["Properties are only allowed within rules, directives, mixin includes, or other properties.", 2],
"a-\#{$b\n c: d" => ['Invalid CSS after "a-#{$b": expected "}", was ""', 1],
"=a($b: 1, $c)" => "Required argument $c must come before any optional arguments.",
"=a($b: 1)\n a: $b\ndiv\n +a(1,2)" => "Mixin a takes 1 argument but 2 were passed.",
"=a($b: 1)\n a: $b\ndiv\n +a(1,$c: 3)" => "Mixin a doesn't have an argument named $c.",
"=a($b)\n a: $b\ndiv\n +a" => "Mixin a is missing argument $b.",
"@function foo()\n 1 + 2" => "Functions can only contain variable declarations and control directives.",
"@function foo()\n foo: bar" => "Functions can only contain variable declarations and control directives.",
"@function foo()\n foo: bar\n @return 3" => ["Functions can only contain variable declarations and control directives.", 2],
"@function foo\n @return 1" => ['Invalid CSS after "": expected "(", was ""', 1],
"@function foo(\n @return 1" => ['Invalid CSS after "(": expected variable (e.g. $foo), was ""', 1],
"@function foo(b)\n @return 1" => ['Invalid CSS after "(": expected variable (e.g. $foo), was "b)"', 1],
"@function foo(,)\n @return 1" => ['Invalid CSS after "(": expected variable (e.g. $foo), was ",)"', 1],
"@function foo($)\n @return 1" => ['Invalid CSS after "(": expected variable (e.g. $foo), was "$)"', 1],
"@function foo()\n @return" => 'Invalid @return: expected expression.',
"@function foo()\n @return 1\n $var: val" => 'Illegal nesting: Nothing may be nested beneath return directives.',
"@function foo($a)\n @return 1\na\n b: foo()" => 'Function foo is missing argument $a.',
"@function foo()\n @return 1\na\n b: foo(2)" => 'Function foo takes 0 arguments but 1 was passed.',
"@function foo()\n @return 1\na\n b: foo($a: 1)" => "Function foo doesn't have an argument named $a.",
"@function foo()\n @return 1\na\n b: foo($a: 1, $b: 2)" => "Function foo doesn't have the following arguments: $a, $b.",
"@return 1" => '@return may only be used within a function.',
"@if true\n @return 1" => '@return may only be used within a function.',
"@mixin foo\n @return 1\n@include foo" => ['@return may only be used within a function.', 2],
"@else\n a\n b: c" => ["@else must come after @if.", 1],
"@if false\n@else foo" => "Invalid else directive '@else foo': expected 'if <expr>'.",
"@if false\n@else if " => "Invalid else directive '@else if': expected 'if <expr>'.",
"a\n $b: 12\nc\n d: $b" => 'Undefined variable: "$b".',
"=foo\n $b: 12\nc\n +foo\n d: $b" => 'Undefined variable: "$b".',
"c\n d: $b-foo" => 'Undefined variable: "$b-foo".',
"c\n d: $b_foo" => 'Undefined variable: "$b_foo".',
'@for $a from "foo" to 1' => '"foo" is not an integer.',
'@for $a from 1 to "2"' => '"2" is not an integer.',
'@for $a from 1 to "foo"' => '"foo" is not an integer.',
'@for $a from 1 to 1.232323' => '1.23232 is not an integer.',
'@for $a from 1px to 3em' => "Incompatible units: 'em' and 'px'.",
'@if' => "Invalid if directive '@if': expected expression.",
'@while' => "Invalid while directive '@while': expected expression.",
'@debug' => "Invalid debug directive '@debug': expected expression.",
%Q{@debug "a message"\n "nested message"} => "Illegal nesting: Nothing may be nested beneath debug directives.",
'@warn' => "Invalid warn directive '@warn': expected expression.",
%Q{@warn "a message"\n "nested message"} => "Illegal nesting: Nothing may be nested beneath warn directives.",
"/* foo\n bar\n baz" => "Inconsistent indentation: previous line was indented by 4 spaces, but this line was indented by 2 spaces.",
'+foo(1 + 1: 2)' => 'Invalid CSS after "(1 + 1": expected comma, was ": 2)"',
'+foo($var: )' => 'Invalid CSS after "($var: ": expected mixin argument, was ")"',
'+foo($var: a, $var: b)' => 'Keyword argument "$var" passed more than once',
'+foo($var-var: a, $var_var: b)' => 'Keyword argument "$var-var" passed more than once',
'+foo($var_var: a, $var-var: b)' => 'Keyword argument "$var_var" passed more than once',
"a\n b: foo(1 + 1: 2)" => 'Invalid CSS after "foo(1 + 1": expected comma, was ": 2)"',
"a\n b: foo($var: )" => 'Invalid CSS after "foo($var: ": expected function argument, was ")"',
"a\n b: foo($var: a, $var: b)" => 'Keyword argument "$var" passed more than once',
"a\n b: foo($var-var: a, $var_var: b)" => 'Keyword argument "$var-var" passed more than once',
"a\n b: foo($var_var: a, $var-var: b)" => 'Keyword argument "$var_var" passed more than once',
"@if foo\n @extend .bar" => ["Extend directives may only be used within rules.", 2],
"$var: true\n@while $var\n @extend .bar\n $var: false" => ["Extend directives may only be used within rules.", 3],
"@for $i from 0 to 1\n @extend .bar" => ["Extend directives may only be used within rules.", 2],
"@mixin foo\n @extend .bar\n@include foo" => ["Extend directives may only be used within rules.", 2],
"foo\n &a\n b: c" => ["Invalid CSS after \"&\": expected \"{\", was \"a\"\n\n\"a\" may only be used at the beginning of a selector.", 2],
"foo\n &1\n b: c" => ["Invalid CSS after \"&\": expected \"{\", was \"1\"\n\n\"1\" may only be used at the beginning of a selector.", 2],
"foo %\n a: b" => ['Invalid CSS after "foo %": expected placeholder name, was ""', 1],
"=foo\n @content error" => "Invalid content directive. Trailing characters found: \"error\".",
"=foo\n @content\n b: c" => "Illegal nesting: Nothing may be nested beneath @content directives.",
"@content" => '@content may only be used within a mixin.',
"=simple\n .simple\n color: red\n+simple\n color: blue" => ['Mixin "simple" does not accept a content block.', 4],
"@import \"foo\" // bar" => "Invalid CSS after \"\"foo\" \": expected media query list, was \"// bar\"",
# Regression tests
"a\n b:\n c\n d" => ["Illegal nesting: Only properties may be nested beneath properties.", 3],
"& foo\n bar: baz\n blat: bang" => ["Base-level rules cannot contain the parent-selector-referencing character '&'.", 1],
"a\n b: c\n& foo\n bar: baz\n blat: bang" => ["Base-level rules cannot contain the parent-selector-referencing character '&'.", 3],
}
def teardown
clean_up_sassc
end
def test_basic_render
renders_correctly "basic", { :style => :compact }
end
def test_empty_render
assert_equal "", render("")
end
def test_multiple_calls_to_render
sass = Sass::Engine.new("a\n b: c")
assert_equal sass.render, sass.render
end
def test_alternate_styles
renders_correctly "expanded", { :style => :expanded }
renders_correctly "compact", { :style => :compact }
renders_correctly "nested", { :style => :nested }
renders_correctly "compressed", { :style => :compressed }
end
def test_compile
assert_equal "div { hello: world; }\n", Sass.compile("$who: world\ndiv\n hello: $who", :syntax => :sass, :style => :compact)
assert_equal "div { hello: world; }\n", Sass.compile("$who: world; div { hello: $who }", :style => :compact)
end
def test_compile_file
FileUtils.mkdir_p(absolutize("tmp"))
open(absolutize("tmp/test_compile_file.sass"), "w") {|f| f.write("$who: world\ndiv\n hello: $who")}
open(absolutize("tmp/test_compile_file.scss"), "w") {|f| f.write("$who: world; div { hello: $who }")}
assert_equal "div { hello: world; }\n", Sass.compile_file(absolutize("tmp/test_compile_file.sass"), :style => :compact)
assert_equal "div { hello: world; }\n", Sass.compile_file(absolutize("tmp/test_compile_file.scss"), :style => :compact)
ensure
FileUtils.rm_rf(absolutize("tmp"))
end
def test_compile_file_to_css_file
FileUtils.mkdir_p(absolutize("tmp"))
open(absolutize("tmp/test_compile_file.sass"), "w") {|f| f.write("$who: world\ndiv\n hello: $who")}
open(absolutize("tmp/test_compile_file.scss"), "w") {|f| f.write("$who: world; div { hello: $who }")}
Sass.compile_file(absolutize("tmp/test_compile_file.sass"), absolutize("tmp/test_compile_file_sass.css"), :style => :compact)
Sass.compile_file(absolutize("tmp/test_compile_file.scss"), absolutize("tmp/test_compile_file_scss.css"), :style => :compact)
assert_equal "div { hello: world; }\n", File.read(absolutize("tmp/test_compile_file_sass.css"))
assert_equal "div { hello: world; }\n", File.read(absolutize("tmp/test_compile_file_scss.css"))
ensure
FileUtils.rm_rf(absolutize("tmp"))
end
def test_flexible_tabulation
assert_equal("p {\n a: b; }\n p q {\n c: d; }\n",
render("p\n a: b\n q\n c: d\n"))
assert_equal("p {\n a: b; }\n p q {\n c: d; }\n",
render("p\n\ta: b\n\tq\n\t\tc: d\n"))
end
EXCEPTION_MAP.each do |key, value|
define_method("test_exception (#{key.inspect})") do
line = 10
begin
silence_warnings {Sass::Engine.new(key, :filename => FAKE_FILE_NAME, :line => line).render}
rescue Sass::SyntaxError => err
value = [value] unless value.is_a?(Array)
assert_equal(value.first.rstrip, err.message, "Line: #{key}")
assert_equal(FAKE_FILE_NAME, err.sass_filename)
assert_equal((value[1] || key.split("\n").length) + line - 1, err.sass_line, "Line: #{key}")
assert_match(/#{Regexp.escape(FAKE_FILE_NAME)}:[0-9]+/, err.backtrace[0], "Line: #{key}")
else
assert(false, "Exception not raised for\n#{key}")
end
end
end
def test_exception_line
to_render = <<SASS
rule
:prop val
// comment!
:broken
SASS
begin
Sass::Engine.new(to_render).render
rescue Sass::SyntaxError => err
assert_equal(5, err.sass_line)
else
assert(false, "Exception not raised for '#{to_render}'!")
end
end
def test_exception_location
to_render = <<SASS
rule
:prop val
// comment!
:broken
SASS
begin
Sass::Engine.new(to_render, :filename => FAKE_FILE_NAME, :line => (__LINE__-7)).render
rescue Sass::SyntaxError => err
assert_equal(FAKE_FILE_NAME, err.sass_filename)
assert_equal((__LINE__-6), err.sass_line)
else
assert(false, "Exception not raised for '#{to_render}'!")
end
end
def test_imported_exception
[1, 2, 3, 4].each do |i|
begin
Sass::Engine.new("@import bork#{i}", :load_paths => [File.dirname(__FILE__) + '/templates/']).render
rescue Sass::SyntaxError => err
assert_equal(2, err.sass_line)
assert_match(/(\/|^)bork#{i}\.sass$/, err.sass_filename)
assert_hash_has(err.sass_backtrace.first,
:filename => err.sass_filename, :line => err.sass_line)
assert_nil(err.sass_backtrace[1][:filename])
assert_equal(1, err.sass_backtrace[1][:line])
assert_match(/(\/|^)bork#{i}\.sass:2$/, err.backtrace.first)
assert_equal("(sass):1", err.backtrace[1])
else
assert(false, "Exception not raised for imported template: bork#{i}")
end
end
end
def test_double_imported_exception
[1, 2, 3, 4].each do |i|
begin
Sass::Engine.new("@import nested_bork#{i}", :load_paths => [File.dirname(__FILE__) + '/templates/']).render
rescue Sass::SyntaxError => err
assert_equal(2, err.sass_line)
assert_match(/(\/|^)bork#{i}\.sass$/, err.sass_filename)
assert_hash_has(err.sass_backtrace.first,
:filename => err.sass_filename, :line => err.sass_line)
assert_match(/(\/|^)nested_bork#{i}\.sass$/, err.sass_backtrace[1][:filename])
assert_equal(2, err.sass_backtrace[1][:line])
assert_nil(err.sass_backtrace[2][:filename])
assert_equal(1, err.sass_backtrace[2][:line])
assert_match(/(\/|^)bork#{i}\.sass:2$/, err.backtrace.first)
assert_match(/(\/|^)nested_bork#{i}\.sass:2$/, err.backtrace[1])
assert_equal("(sass):1", err.backtrace[2])
else
assert(false, "Exception not raised for imported template: bork#{i}")
end
end
end
def test_selector_tracing
actual_css = render(<<-SCSS, :syntax => :scss, :trace_selectors => true)
@mixin mixed {
.mixed { color: red; }
}
.context {
@include mixed;
}
SCSS
assert_equal(<<CSS,actual_css)
/* on line 2 of test_selector_tracing_inline.scss, in `mixed'
from line 5 of test_selector_tracing_inline.scss */
.context .mixed {
color: red; }
CSS
end
def test_mixin_exception
render(<<SASS)
=error-mixin($a)
color: $a * 1em * 1px
=outer-mixin($a)
+error-mixin($a)
.error
+outer-mixin(12)
SASS
assert(false, "Exception not raised")
rescue Sass::SyntaxError => err
assert_equal(2, err.sass_line)
assert_equal(filename_for_test, err.sass_filename)
assert_equal("error-mixin", err.sass_mixin)
assert_hash_has(err.sass_backtrace.first, :line => err.sass_line,
:filename => err.sass_filename, :mixin => err.sass_mixin)
assert_hash_has(err.sass_backtrace[1], :line => 5,
:filename => filename_for_test, :mixin => "outer-mixin")
assert_hash_has(err.sass_backtrace[2], :line => 8,
:filename => filename_for_test, :mixin => nil)
assert_equal("#{filename_for_test}:2:in `error-mixin'", err.backtrace.first)
assert_equal("#{filename_for_test}:5:in `outer-mixin'", err.backtrace[1])
assert_equal("#{filename_for_test}:8", err.backtrace[2])
end
def test_mixin_callsite_exception
render(<<SASS)
=one-arg-mixin($a)
color: $a
=outer-mixin($a)
+one-arg-mixin($a, 12)
.error
+outer-mixin(12)
SASS
assert(false, "Exception not raised")
rescue Sass::SyntaxError => err
assert_hash_has(err.sass_backtrace.first, :line => 5,
:filename => filename_for_test, :mixin => "one-arg-mixin")
assert_hash_has(err.sass_backtrace[1], :line => 5,
:filename => filename_for_test, :mixin => "outer-mixin")
assert_hash_has(err.sass_backtrace[2], :line => 8,
:filename => filename_for_test, :mixin => nil)
end
def test_mixin_exception_cssize
render(<<SASS)
=parent-ref-mixin
& foo
a: b
=outer-mixin
+parent-ref-mixin
+outer-mixin
SASS
assert(false, "Exception not raised")
rescue Sass::SyntaxError => err
assert_hash_has(err.sass_backtrace.first, :line => 2,
:filename => filename_for_test, :mixin => "parent-ref-mixin")
assert_hash_has(err.sass_backtrace[1], :line => 6,
:filename => filename_for_test, :mixin => "outer-mixin")
assert_hash_has(err.sass_backtrace[2], :line => 8,
:filename => filename_for_test, :mixin => nil)
end
def test_mixin_and_import_exception
Sass::Engine.new("@import nested_mixin_bork", :load_paths => [File.dirname(__FILE__) + '/templates/']).render
assert(false, "Exception not raised")
rescue Sass::SyntaxError => err
assert_match(/(\/|^)nested_mixin_bork\.sass$/, err.sass_backtrace.first[:filename])
assert_hash_has(err.sass_backtrace.first, :mixin => "error-mixin", :line => 4)
assert_match(/(\/|^)mixin_bork\.sass$/, err.sass_backtrace[1][:filename])
assert_hash_has(err.sass_backtrace[1], :mixin => "outer-mixin", :line => 2)
assert_match(/(\/|^)mixin_bork\.sass$/, err.sass_backtrace[2][:filename])
assert_hash_has(err.sass_backtrace[2], :mixin => nil, :line => 5)
assert_match(/(\/|^)nested_mixin_bork\.sass$/, err.sass_backtrace[3][:filename])
assert_hash_has(err.sass_backtrace[3], :mixin => nil, :line => 6)
assert_hash_has(err.sass_backtrace[4], :filename => nil, :mixin => nil, :line => 1)
end
def test_basic_mixin_loop_exception
render <<SASS
@mixin foo
@include foo
@include foo
SASS
assert(false, "Exception not raised")
rescue Sass::SyntaxError => err
assert_equal("An @include loop has been found: foo includes itself", err.message)
assert_hash_has(err.sass_backtrace[0], :mixin => "foo", :line => 2)
end
def test_double_mixin_loop_exception
render <<SASS
@mixin foo
@include bar
@mixin bar
@include foo
@include foo
SASS
assert(false, "Exception not raised")
rescue Sass::SyntaxError => err
assert_equal(<<MESSAGE.rstrip, err.message)
An @include loop has been found:
foo includes bar
bar includes foo
MESSAGE
assert_hash_has(err.sass_backtrace[0], :mixin => "bar", :line => 4)
assert_hash_has(err.sass_backtrace[1], :mixin => "foo", :line => 2)
end
def test_deep_mixin_loop_exception
render <<SASS
@mixin foo
@include bar
@mixin bar
@include baz
@mixin baz
@include foo
@include foo
SASS
assert(false, "Exception not raised")
rescue Sass::SyntaxError => err
assert_equal(<<MESSAGE.rstrip, err.message)
An @include loop has been found:
foo includes bar
bar includes baz
baz includes foo
MESSAGE
assert_hash_has(err.sass_backtrace[0], :mixin => "baz", :line => 8)
assert_hash_has(err.sass_backtrace[1], :mixin => "bar", :line => 5)
assert_hash_has(err.sass_backtrace[2], :mixin => "foo", :line => 2)
end
def test_basic_import_loop_exception
import = filename_for_test
importer = MockImporter.new
importer.add_import(import, "@import '#{import}'")
engine = Sass::Engine.new("@import '#{import}'", :filename => import,
:load_paths => [importer])
assert_raise_message(Sass::SyntaxError, <<ERR.rstrip) {engine.render}
An @import loop has been found: #{import} imports itself
ERR
end
def test_double_import_loop_exception
importer = MockImporter.new
importer.add_import("foo", "@import 'bar'")
importer.add_import("bar", "@import 'foo'")
engine = Sass::Engine.new('@import "foo"', :filename => filename_for_test,
:load_paths => [importer])
assert_raise_message(Sass::SyntaxError, <<ERR.rstrip) {engine.render}
An @import loop has been found:
#{filename_for_test} imports foo
foo imports bar
bar imports foo
ERR
end
def test_deep_import_loop_exception
importer = MockImporter.new
importer.add_import("foo", "@import 'bar'")
importer.add_import("bar", "@import 'baz'")
importer.add_import("baz", "@import 'foo'")
engine = Sass::Engine.new('@import "foo"', :filename => filename_for_test,
:load_paths => [importer])
assert_raise_message(Sass::SyntaxError, <<ERR.rstrip) {engine.render}
An @import loop has been found:
#{filename_for_test} imports foo
foo imports bar
bar imports baz
baz imports foo
ERR
end
def test_exception_css_with_offset
opts = {:full_exception => true, :line => 362}
render(("a\n b: c\n" * 10) + "d\n e:\n" + ("f\n g: h\n" * 10), opts)
rescue Sass::SyntaxError => e
assert_equal(<<CSS, Sass::SyntaxError.exception_to_css(e, opts).split("\n")[0..15].join("\n"))
/*
Syntax error: Invalid property: "e:" (no value).
on line 383 of test_exception_css_with_offset_inline.sass
378: a
379: b: c
380: a
381: b: c
382: d
383: e:
384: f
385: g: h
386: f
387: g: h
388: f
CSS
else
assert(false, "Exception not raised for test_exception_css_with_offset")
end
def test_exception_css_with_mixins
opts = {:full_exception => true}
render(<<SASS, opts)
=error-mixin($a)
color: $a * 1em * 1px
=outer-mixin($a)
+error-mixin($a)
.error
+outer-mixin(12)
SASS
rescue Sass::SyntaxError => e
assert_equal(<<CSS, Sass::SyntaxError.exception_to_css(e, opts).split("\n")[0..13].join("\n"))
/*
Syntax error: 12em*px isn't a valid CSS value.
on line 2 of test_exception_css_with_mixins_inline.sass, in `error-mixin'
from line 5 of test_exception_css_with_mixins_inline.sass, in `outer-mixin'
from line 8 of test_exception_css_with_mixins_inline.sass
1: =error-mixin($a)
2: color: $a * 1em * 1px
3:
4: =outer-mixin($a)
5: +error-mixin($a)
6:
7: .error
CSS
else
assert(false, "Exception not raised")
end
def test_cssize_exception_css
opts = {:full_exception => true}
render(<<SASS, opts)
.filler
stuff: "stuff!"
a: b
.more.filler
a: b
SASS
rescue Sass::SyntaxError => e
assert_equal(<<CSS, Sass::SyntaxError.exception_to_css(e, opts).split("\n")[0..11].join("\n"))
/*
Syntax error: Properties are only allowed within rules, directives, mixin includes, or other properties.
on line 4 of test_cssize_exception_css_inline.sass
1: .filler
2: stuff: "stuff!"
3:
4: a: b
5:
6: .more.filler
7: a: b
CSS
else
assert(false, "Exception not raised")
end
def test_css_import
assert_equal("@import url(./fonts.css);\n", render("@import \"./fonts.css\""))
end
def test_http_import
assert_equal("@import url(http://fonts.googleapis.com/css?family=Droid+Sans);\n",
render("@import \"http://fonts.googleapis.com/css?family=Droid+Sans\""))
end
def test_import_with_interpolation
assert_equal(<<CSS, render(<<SASS))
@import url("http://fonts.googleapis.com/css?family=Droid+Sans");
CSS
$family: unquote("Droid+Sans")
@import url("http://fonts.googleapis.com/css?family=\#{$family}")
SASS
end
def test_import_with_dynamic_media_query
assert_equal(<<CSS, render(<<SASS))
@import "foo" print and (-webkit-min-device-pixel-ratio-foo: 25);
CSS
$media: print
$key: -webkit-min-device-pixel-ratio
$value: 20
@import "foo" \#{$media} and ($key + "-foo": $value + 5)
SASS
end
def test_url_import
assert_equal("@import url(fonts.sass);\n", render("@import url(fonts.sass)"))
end
def test_sass_import
sassc_file = sassc_path("importee")
assert !File.exists?(sassc_file)
renders_correctly "import", { :style => :compact, :load_paths => [File.dirname(__FILE__) + "/templates"] }
assert File.exists?(sassc_file)
end
def test_sass_pathname_import
sassc_file = sassc_path("importee")
assert !File.exists?(sassc_file)
renders_correctly("import",
:style => :compact,
:load_paths => [Pathname.new(File.dirname(__FILE__) + "/templates")])
assert File.exists?(sassc_file)
end
def test_import_from_global_load_paths
importer = MockImporter.new
importer.add_import("imported", "div{color:red}")
Sass.load_paths << importer
assert_equal "div {\n color: red; }\n", Sass::Engine.new('@import "imported"').render
ensure
Sass.load_paths.clear
end
def test_nonexistent_import
assert_raise_message(Sass::SyntaxError, <<ERR.rstrip) do
File to import not found or unreadable: nonexistent.sass.
Load path: #{Dir.pwd}
ERR
render("@import nonexistent.sass")
end
end
def test_nonexistent_extensionless_import
assert_raise_message(Sass::SyntaxError, <<ERR.rstrip) do
File to import not found or unreadable: nonexistent.
Load path: #{Dir.pwd}
ERR
render("@import nonexistent")
end
end
def test_no_cache
assert !File.exists?(sassc_path("importee"))
renders_correctly("import", {
:style => :compact, :cache => false,
:load_paths => [File.dirname(__FILE__) + "/templates"],
})
assert !File.exists?(sassc_path("importee"))
end
def test_import_in_rule
assert_equal(<<CSS, render(<<SASS, :load_paths => [File.dirname(__FILE__) + '/templates/']))
.foo #foo {
background-color: #bbaaff; }
.bar {
a: b; }
.bar #foo {
background-color: #bbaaff; }
CSS
.foo
@import partial
.bar
a: b
@import partial
SASS
end
def test_units
renders_correctly "units"
end
def test_default_function
assert_equal(<<CSS, render(<<SASS))
foo {
bar: url("foo.png"); }
CSS
foo
bar: url("foo.png")
SASS
assert_equal("foo {\n bar: url(); }\n", render("foo\n bar: url()\n"));
end
def test_string_minus
assert_equal("foo {\n bar: baz-boom-bat; }\n", render(%Q{foo\n bar: baz-boom-bat}))
assert_equal("foo {\n bar: -baz-boom; }\n", render(%Q{foo\n bar: -baz-boom}))
end
def test_string_div
assert_equal("foo {\n bar: baz/boom/bat; }\n", render(%Q{foo\n bar: baz/boom/bat}))
assert_equal("foo {\n bar: /baz/boom; }\n", render(%Q{foo\n bar: /baz/boom}))
end
def test_basic_multiline_selector
assert_equal("#foo #bar,\n#baz #boom {\n foo: bar; }\n",
render("#foo #bar,\n#baz #boom\n :foo bar"))
assert_equal("#foo #bar,\n#foo #baz {\n foo: bar; }\n",
render("#foo\n #bar,\n #baz\n :foo bar"))
assert_equal("#foo,\n#bar {\n foo: bar; }\n #foo #baz,\n #bar #baz {\n foo: bar; }\n",
render("#foo,\n#bar\n :foo bar\n #baz\n :foo bar"))
assert_equal("#foo #bar, #baz #boom { foo: bar; }\n",
render("#foo #bar,\n#baz #boom\n :foo bar", :style => :compact))
assert_equal("#foo #bar,#baz #boom{foo:bar}\n",
render("#foo #bar,\n#baz #boom\n :foo bar", :style => :compressed))
assert_equal("#foo #bar,\n#baz #boom {\n foo: bar; }\n",
render("#foo #bar,,\n,#baz #boom,\n :foo bar"))
assert_equal("#bip #bop {\n foo: bar; }\n",
render("#bip #bop,, ,\n :foo bar"))
end
def test_complex_multiline_selector
renders_correctly "multiline"
end
def test_colon_only
begin
render("a\n b: c", :property_syntax => :old)
rescue Sass::SyntaxError => e
assert_equal("Illegal property syntax: can't use new syntax when :property_syntax => :old is set.",
e.message)
assert_equal(2, e.sass_line)
else
assert(false, "SyntaxError not raised for :property_syntax => :old")
end
begin
render("a\n :b c", :property_syntax => :new)
assert_equal(2, e.sass_line)
rescue Sass::SyntaxError => e
assert_equal("Illegal property syntax: can't use old syntax when :property_syntax => :new is set.",
e.message)
else
assert(false, "SyntaxError not raised for :property_syntax => :new")
end
end
def test_pseudo_elements
assert_equal(<<CSS, render(<<SASS))
::first-line {
size: 10em; }
CSS
::first-line
size: 10em
SASS
end
def test_directive
assert_equal("@a b;\n", render("@a b"))
assert_equal("@a {\n b: c; }\n", render("@a\n :b c"))
assert_equal("@a { b: c; }\n", render("@a\n :b c", :style => :compact))
assert_equal("@a {\n b: c;\n}\n", render("@a\n :b c", :style => :expanded))
assert_equal("@a{b:c}\n", render("@a\n :b c", :style => :compressed))
assert_equal("@a {\n b: c;\n d: e; }\n",
render("@a\n :b c\n :d e"))
assert_equal("@a { b: c; d: e; }\n",
render("@a\n :b c\n :d e", :style => :compact))
assert_equal("@a {\n b: c;\n d: e;\n}\n",
render("@a\n :b c\n :d e", :style => :expanded))
assert_equal("@a{b:c;d:e}\n",
render("@a\n :b c\n :d e", :style => :compressed))
assert_equal("@a {\n #b {\n c: d; } }\n",
render("@a\n #b\n :c d"))
assert_equal("@a { #b { c: d; } }\n",
render("@a\n #b\n :c d", :style => :compact))
assert_equal("@a {\n #b {\n c: d;\n }\n}\n",
render("@a\n #b\n :c d", :style => :expanded))
assert_equal("@a{#b{c:d}}\n",
render("@a\n #b\n :c d", :style => :compressed))
assert_equal("@a {\n #b {\n a: b; }\n #b #c {\n d: e; } }\n",
render("@a\n #b\n :a b\n #c\n :d e"))
assert_equal("@a { #b { a: b; }\n #b #c { d: e; } }\n",
render("@a\n #b\n :a b\n #c\n :d e", :style => :compact))
assert_equal("@a {\n #b {\n a: b;\n }\n #b #c {\n d: e;\n }\n}\n",
render("@a\n #b\n :a b\n #c\n :d e", :style => :expanded))
assert_equal("@a{#b{a:b}#b #c{d:e}}\n",
render("@a\n #b\n :a b\n #c\n :d e", :style => :compressed))
assert_equal("@a {\n #foo,\n #bar {\n b: c; } }\n",
render("@a\n #foo, \n #bar\n :b c"))
assert_equal("@a { #foo, #bar { b: c; } }\n",
render("@a\n #foo, \n #bar\n :b c", :style => :compact))
assert_equal("@a {\n #foo,\n #bar {\n b: c;\n }\n}\n",
render("@a\n #foo, \n #bar\n :b c", :style => :expanded))
assert_equal("@a{#foo,#bar{b:c}}\n",
render("@a\n #foo, \n #bar\n :b c", :style => :compressed))
to_render = <<END
@a
:b c
#d
:e f
:g h
END
rendered = <<END
@a { b: c;
#d { e: f; }
g: h; }
END
assert_equal(rendered, render(to_render, :style => :compact))
assert_equal("@a{b:c;#d{e:f}g:h}\n", render(to_render, :style => :compressed))
end
def test_property_hacks
assert_equal(<<CSS, render(<<SASS))
foo {
_name: val;
*name: val;
#name: val;
.name: val;
name/**/: val;
name/*\\**/: val;
name: val; }
CSS
foo
_name: val
*name: val
#name: val
.name: val
name/**/: val
name/*\\**/: val
name: val
SASS
end
def test_properties_with_space_after_colon
assert_equal <<CSS, render(<<SASS)
foo {
bar: baz;
bizz: bap; }
CSS
foo
bar : baz
bizz : bap
SASS
end
def test_line_annotations
assert_equal(<<CSS, render(<<SASS, :line_comments => true, :style => :compact))
/* line 2, test_line_annotations_inline.sass */
foo bar { foo: bar; }
/* line 5, test_line_annotations_inline.sass */
foo baz { blip: blop; }
/* line 9, test_line_annotations_inline.sass */
floodle { flop: blop; }
/* line 18, test_line_annotations_inline.sass */
bup { mix: on; }
/* line 15, test_line_annotations_inline.sass */
bup mixin { moop: mup; }
/* line 22, test_line_annotations_inline.sass */
bip hop, skip hop { a: b; }
CSS
foo
bar
foo: bar
baz
blip: blop
floodle
flop: blop
=mxn
mix: on
mixin
moop: mup
bup
+mxn
bip, skip
hop
a: b
SASS
end
def test_line_annotations_with_filename
renders_correctly "line_numbers", :line_comments => true, :load_paths => [File.dirname(__FILE__) + "/templates"]
end
def test_debug_info
esc_file_name = Sass::SCSS::RX.escape_ident(Sass::Util.scope("test_debug_info_inline.sass"))
assert_equal(<<CSS, render(<<SASS, :debug_info => true, :style => :compact))
@media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\000032}}
foo bar { foo: bar; }
@media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\000035}}
foo baz { blip: blop; }
@media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\000039}}
floodle { flop: blop; }
@media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\0000318}}
bup { mix: on; }
@media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\0000315}}
bup mixin { moop: mup; }
@media -sass-debug-info{filename{font-family:file\\:\\/\\/#{esc_file_name}}line{font-family:\\0000322}}
bip hop, skip hop { a: b; }
CSS
foo
bar
foo: bar
baz
blip: blop
floodle
flop: blop
=mxn
mix: on
mixin
moop: mup
bup
+mxn
bip, skip
hop
a: b
SASS
end
def test_debug_info_without_filename
assert_equal(<<CSS, Sass::Engine.new(<<SASS, :debug_info => true).render)