/
engine_test.rb
executable file
·300 lines (263 loc) · 12 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
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../test_helper'
require 'sass/engine'
class SassEngineTest < Test::Unit::TestCase
# 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 + " => 'Constant arithmetic error: "1 +".',
"!a = 1 + 2 +" => 'Constant arithmetic error: "1 + 2 +".',
"!a = \"b" => 'Unterminated string: "\\"b".',
"!a = #aaa - a" => 'Undefined operation: "#aaaaaa minus a".',
"!a = #aaa / a" => 'Undefined operation: "#aaaaaa div a".',
"!a = #aaa * a" => 'Undefined operation: "#aaaaaa times a".',
"!a = #aaa % a" => 'Undefined operation: "#aaaaaa mod a".',
"!a = 1 - a" => 'Undefined operation: "1 minus a".',
"!a = 1 * a" => 'Undefined operation: "1 times a".',
"!a = 1 / a" => 'Undefined operation: "1 div a".',
"!a = 1 % a" => 'Undefined operation: "1 mod a".',
":" => 'Invalid attribute: ":".',
": a" => 'Invalid attribute: ": a".',
":= a" => 'Invalid attribute: ":= a".',
"a\n :b" => 'Invalid attribute: ":b ".',
"a\n :b: c" => 'Invalid attribute: ":b: c".',
"a\n :b:c d" => 'Invalid attribute: ":b:c d".',
"a\n :b=c d" => 'Invalid attribute: ":b=c d".',
"a\n :b c;" => 'Invalid attribute: ":b c;" (This isn\'t CSS!).',
"a\n b : c" => 'Invalid attribute: "b : c".',
"a\n b=c: d" => 'Invalid attribute: "b=c: d".',
":a" => 'Attributes aren\'t allowed at the root of a document.',
"!" => 'Invalid constant: "!".',
"!a" => 'Invalid constant: "!a".',
"! a" => 'Invalid constant: "! a".',
"!a b" => 'Invalid constant: "!a b".',
"a\n\t:b c" => <<END.strip,
A tab character was used for indentation. Sass must be indented using two spaces.
Are you sure you have soft tabs enabled in your editor?
END
"a\n :b c" => "1 space was used for indentation. Sass must be indented using two spaces.",
"a\n :b c" => "4 spaces were used for indentation. Sass must be indented using two spaces.",
"a\n :b c\n !d = 3" => "Constants may only be declared at the root of a document.",
"!a = 1b + 2c" => "Incompatible units: b and c.",
"& a\n :b c" => ["Base-level rules cannot contain the parent-selector-referencing character '&'.", 1],
"a\n :b\n c" => "Illegal nesting: Only attributes may be nested beneath attributes.",
"a,\n :b c" => "Rules can\'t end in commas.",
"a," => "Rules can\'t end in commas.",
"a,\n!b = c" => "Rules can\'t end in commas.",
"!a = b\n :c d\n" => "Illegal nesting: Nothing may be nested beneath constants.",
"@import foo.sass" => "File to import not found or unreadable: foo.sass.",
"@import templates/basic\n foo" => "Illegal nesting: Nothing may be nested beneath import directives.",
"foo\n @import templates/basic" => "Import directives may only be used at the root of a document.",
"!foo = bar baz !" => "Unterminated constant.",
"!foo = !(foo)" => "Invalid constant.",
"=foo\n :color red\n.bar\n +bang" => "Undefined mixin 'bang'.",
".bar\n =foo\n :color red\n" => "Mixins may only be defined at the root of a document.",
"=foo\n :color red\n.bar\n +foo\n :color red" => "Illegal nesting: Nothing may be nested beneath mixin directives.",
" 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],
# Regression tests
"a\n b:\n c\n d" => ["Illegal nesting: Only attributes may be nested beneath attributes.", 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 test_basic_render
renders_correctly "basic", { :style => :compact }
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_exceptions
EXCEPTION_MAP.each do |key, value|
begin
Sass::Engine.new(key).render
rescue Sass::SyntaxError => err
value = [value] unless value.is_a?(Array)
assert_equal(value.first, err.message, "Line: #{key}")
assert_equal(value[1] || key.split("\n").length, err.sass_line, "Line: #{key}")
assert_match(/\(sass\):[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
:attr 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_imported_exception
[1, 2].each do |i|
i = nil if i == 1
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)
else
assert(false, "Exception not raised for imported template: bork#{i}")
end
end
end
def test_css_import
assert_equal("@import url(./fonts.css) screen;", render("@import url(./fonts.css) screen"))
assert_equal("@import \"./fonts.css\" screen;", render("@import \"./fonts.css\" screen"))
end
def test_sass_import
renders_correctly "import", { :style => :compact, :load_paths => [File.dirname(__FILE__) + "/templates"] }
end
def test_default_function
assert_equal("foo {\n bar: url(foo.png); }\n", render("foo\n bar = url(foo.png)\n"));
assert_equal("foo {\n bar: url(); }\n", render("foo\n bar = url()\n"));
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))
end
def test_complex_multiline_selector
renders_correctly "multiline"
end
def test_colon_only
begin
render("a\n b: c", :attribute_syntax => :normal)
rescue Sass::SyntaxError => e
assert_equal("Illegal attribute syntax: can't use alternate syntax when :attribute_syntax => :normal is set.",
e.message)
else
assert(false, "SyntaxError not raised for :attribute_syntax => :normal")
end
begin
render("a\n :b c", :attribute_syntax => :alternate)
rescue Sass::SyntaxError => e
assert_equal("Illegal attribute syntax: can't use normal syntax when :attribute_syntax => :alternate is set.",
e.message)
else
assert(false, "SyntaxError not raised for :attribute_syntax => :alternate")
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;", 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_empty_first_line
assert_equal("#a {\n b: c; }\n", render("#a\n\n b: c"))
end
def test_escaped_rule
assert_equal(":focus {\n a: b; }\n", render("\\:focus\n a: b"))
assert_equal("a {\n b: c; }\n a :focus {\n d: e; }\n", render("\\a\n b: c\n \\:focus\n d: e"))
end
def test_cr_newline
assert_equal("foo {\n a: b;\n c: d;\n e: f; }\n", render("foo\r a: b\r\n c: d\n\r e: f"))
end
def test_or_eq
assert_equal("foo {\n a: b; }\n", render("!foo = b\n!foo ||= c\nfoo\n a = !foo"))
assert_equal("foo {\n a: b; }\n", render("!foo ||= b\nfoo\n a = !foo"))
end
def test_mixins
renders_correctly "mixins", { :style => :expanded }
end
def test_mixins_dont_interfere_with_sibling_combinator
assert_equal("foo + bar {\n a: b; }\n", render("foo\n + bar\n a: b"))
assert_equal("foo + bar {\n a: b; }\nfoo + baz {\n c: d; }\n",
render("foo\n +\n bar\n a: b\n baz\n c: d"))
end
private
def render(sass, options = {})
Sass::Engine.new(sass, options).render
end
def renders_correctly(name, options={})
sass_file = load_file(name, "sass")
css_file = load_file(name, "css")
css_result = Sass::Engine.new(sass_file, options).render
assert_equal css_file, css_result
end
def load_file(name, type = "sass")
@result = ''
File.new(File.dirname(__FILE__) + "/#{type == 'sass' ? 'templates' : 'results'}/#{name}.#{type}").each_line { |l| @result += l }
@result
end
end