gohanlonllc-haml 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. data/README.rdoc +332 -0
  2. data/Rakefile +250 -0
  3. data/VERSION.yml +4 -0
  4. data/bin/css2sass +7 -0
  5. data/bin/haml +9 -0
  6. data/bin/html2haml +7 -0
  7. data/bin/sass +8 -0
  8. data/lib/haml/buffer.rb +255 -0
  9. data/lib/haml/engine.rb +268 -0
  10. data/lib/haml/error.rb +22 -0
  11. data/lib/haml/exec.rb +403 -0
  12. data/lib/haml/filters.rb +275 -0
  13. data/lib/haml/helpers/action_view_extensions.rb +45 -0
  14. data/lib/haml/helpers/action_view_mods.rb +181 -0
  15. data/lib/haml/helpers.rb +488 -0
  16. data/lib/haml/html.rb +219 -0
  17. data/lib/haml/precompiler.rb +904 -0
  18. data/lib/haml/shared.rb +47 -0
  19. data/lib/haml/template/patch.rb +58 -0
  20. data/lib/haml/template/plugin.rb +72 -0
  21. data/lib/haml/template.rb +42 -0
  22. data/lib/haml/util.rb +93 -0
  23. data/lib/haml/version.rb +46 -0
  24. data/lib/haml.rb +1044 -0
  25. data/lib/sass/css.rb +337 -0
  26. data/lib/sass/engine.rb +439 -0
  27. data/lib/sass/environment.rb +48 -0
  28. data/lib/sass/error.rb +42 -0
  29. data/lib/sass/files.rb +100 -0
  30. data/lib/sass/plugin/merb.rb +57 -0
  31. data/lib/sass/plugin/rails.rb +25 -0
  32. data/lib/sass/plugin.rb +203 -0
  33. data/lib/sass/repl.rb +51 -0
  34. data/lib/sass/script/bool.rb +13 -0
  35. data/lib/sass/script/color.rb +97 -0
  36. data/lib/sass/script/funcall.rb +29 -0
  37. data/lib/sass/script/functions.rb +134 -0
  38. data/lib/sass/script/lexer.rb +148 -0
  39. data/lib/sass/script/literal.rb +82 -0
  40. data/lib/sass/script/number.rb +231 -0
  41. data/lib/sass/script/operation.rb +30 -0
  42. data/lib/sass/script/parser.rb +142 -0
  43. data/lib/sass/script/string.rb +9 -0
  44. data/lib/sass/script/unary_operation.rb +21 -0
  45. data/lib/sass/script/variable.rb +20 -0
  46. data/lib/sass/script.rb +38 -0
  47. data/lib/sass/tree/attr_node.rb +72 -0
  48. data/lib/sass/tree/comment_node.rb +39 -0
  49. data/lib/sass/tree/debug_node.rb +22 -0
  50. data/lib/sass/tree/directive_node.rb +51 -0
  51. data/lib/sass/tree/file_node.rb +27 -0
  52. data/lib/sass/tree/for_node.rb +29 -0
  53. data/lib/sass/tree/if_node.rb +32 -0
  54. data/lib/sass/tree/mixin_def_node.rb +18 -0
  55. data/lib/sass/tree/mixin_node.rb +35 -0
  56. data/lib/sass/tree/node.rb +117 -0
  57. data/lib/sass/tree/rule_node.rb +161 -0
  58. data/lib/sass/tree/variable_node.rb +24 -0
  59. data/lib/sass/tree/while_node.rb +21 -0
  60. data/lib/sass.rb +1070 -0
  61. data/rails/init.rb +1 -0
  62. data/test/benchmark.rb +99 -0
  63. data/test/haml/engine_test.rb +795 -0
  64. data/test/haml/helper_test.rb +233 -0
  65. data/test/haml/html2haml_test.rb +108 -0
  66. data/test/haml/markaby/standard.mab +52 -0
  67. data/test/haml/mocks/article.rb +6 -0
  68. data/test/haml/results/content_for_layout.xhtml +15 -0
  69. data/test/haml/results/eval_suppressed.xhtml +9 -0
  70. data/test/haml/results/filters.xhtml +62 -0
  71. data/test/haml/results/helpers.xhtml +93 -0
  72. data/test/haml/results/helpful.xhtml +10 -0
  73. data/test/haml/results/just_stuff.xhtml +68 -0
  74. data/test/haml/results/list.xhtml +12 -0
  75. data/test/haml/results/nuke_inner_whitespace.xhtml +40 -0
  76. data/test/haml/results/nuke_outer_whitespace.xhtml +148 -0
  77. data/test/haml/results/original_engine.xhtml +20 -0
  78. data/test/haml/results/partial_layout.xhtml +5 -0
  79. data/test/haml/results/partials.xhtml +21 -0
  80. data/test/haml/results/render_layout.xhtml +3 -0
  81. data/test/haml/results/silent_script.xhtml +74 -0
  82. data/test/haml/results/standard.xhtml +162 -0
  83. data/test/haml/results/tag_parsing.xhtml +23 -0
  84. data/test/haml/results/very_basic.xhtml +5 -0
  85. data/test/haml/results/whitespace_handling.xhtml +89 -0
  86. data/test/haml/rhtml/_av_partial_1.rhtml +12 -0
  87. data/test/haml/rhtml/_av_partial_2.rhtml +8 -0
  88. data/test/haml/rhtml/action_view.rhtml +62 -0
  89. data/test/haml/rhtml/standard.rhtml +54 -0
  90. data/test/haml/template_test.rb +204 -0
  91. data/test/haml/templates/_av_partial_1.haml +9 -0
  92. data/test/haml/templates/_av_partial_1_ugly.haml +9 -0
  93. data/test/haml/templates/_av_partial_2.haml +5 -0
  94. data/test/haml/templates/_av_partial_2_ugly.haml +5 -0
  95. data/test/haml/templates/_layout.erb +3 -0
  96. data/test/haml/templates/_layout_for_partial.haml +3 -0
  97. data/test/haml/templates/_partial.haml +8 -0
  98. data/test/haml/templates/_text_area.haml +3 -0
  99. data/test/haml/templates/action_view.haml +47 -0
  100. data/test/haml/templates/action_view_ugly.haml +47 -0
  101. data/test/haml/templates/breakage.haml +8 -0
  102. data/test/haml/templates/content_for_layout.haml +10 -0
  103. data/test/haml/templates/eval_suppressed.haml +11 -0
  104. data/test/haml/templates/filters.haml +66 -0
  105. data/test/haml/templates/helpers.haml +95 -0
  106. data/test/haml/templates/helpful.haml +11 -0
  107. data/test/haml/templates/just_stuff.haml +83 -0
  108. data/test/haml/templates/list.haml +12 -0
  109. data/test/haml/templates/nuke_inner_whitespace.haml +32 -0
  110. data/test/haml/templates/nuke_outer_whitespace.haml +144 -0
  111. data/test/haml/templates/original_engine.haml +17 -0
  112. data/test/haml/templates/partial_layout.haml +3 -0
  113. data/test/haml/templates/partialize.haml +1 -0
  114. data/test/haml/templates/partials.haml +12 -0
  115. data/test/haml/templates/render_layout.haml +2 -0
  116. data/test/haml/templates/silent_script.haml +40 -0
  117. data/test/haml/templates/standard.haml +42 -0
  118. data/test/haml/templates/standard_ugly.haml +42 -0
  119. data/test/haml/templates/tag_parsing.haml +21 -0
  120. data/test/haml/templates/very_basic.haml +4 -0
  121. data/test/haml/templates/whitespace_handling.haml +87 -0
  122. data/test/haml/util_test.rb +92 -0
  123. data/test/linked_rails.rb +12 -0
  124. data/test/sass/css2sass_test.rb +215 -0
  125. data/test/sass/engine_test.rb +773 -0
  126. data/test/sass/functions_test.rb +109 -0
  127. data/test/sass/more_results/more1.css +9 -0
  128. data/test/sass/more_results/more1_with_line_comments.css +26 -0
  129. data/test/sass/more_results/more_import.css +29 -0
  130. data/test/sass/more_templates/_more_partial.sass +2 -0
  131. data/test/sass/more_templates/more1.sass +23 -0
  132. data/test/sass/more_templates/more_import.sass +11 -0
  133. data/test/sass/plugin_test.rb +214 -0
  134. data/test/sass/results/alt.css +4 -0
  135. data/test/sass/results/basic.css +9 -0
  136. data/test/sass/results/compact.css +5 -0
  137. data/test/sass/results/complex.css +87 -0
  138. data/test/sass/results/compressed.css +1 -0
  139. data/test/sass/results/expanded.css +19 -0
  140. data/test/sass/results/import.css +29 -0
  141. data/test/sass/results/line_numbers.css +49 -0
  142. data/test/sass/results/mixins.css +95 -0
  143. data/test/sass/results/multiline.css +24 -0
  144. data/test/sass/results/nested.css +22 -0
  145. data/test/sass/results/parent_ref.css +13 -0
  146. data/test/sass/results/script.css +16 -0
  147. data/test/sass/results/subdir/nested_subdir/nested_subdir.css +1 -0
  148. data/test/sass/results/subdir/subdir.css +3 -0
  149. data/test/sass/results/units.css +11 -0
  150. data/test/sass/script_test.rb +250 -0
  151. data/test/sass/templates/_partial.sass +2 -0
  152. data/test/sass/templates/alt.sass +16 -0
  153. data/test/sass/templates/basic.sass +23 -0
  154. data/test/sass/templates/bork.sass +2 -0
  155. data/test/sass/templates/bork2.sass +2 -0
  156. data/test/sass/templates/compact.sass +17 -0
  157. data/test/sass/templates/complex.sass +309 -0
  158. data/test/sass/templates/compressed.sass +15 -0
  159. data/test/sass/templates/expanded.sass +17 -0
  160. data/test/sass/templates/import.sass +11 -0
  161. data/test/sass/templates/importee.sass +19 -0
  162. data/test/sass/templates/line_numbers.sass +13 -0
  163. data/test/sass/templates/mixins.sass +76 -0
  164. data/test/sass/templates/multiline.sass +20 -0
  165. data/test/sass/templates/nested.sass +25 -0
  166. data/test/sass/templates/parent_ref.sass +25 -0
  167. data/test/sass/templates/script.sass +101 -0
  168. data/test/sass/templates/subdir/nested_subdir/_nested_partial.sass +2 -0
  169. data/test/sass/templates/subdir/nested_subdir/nested_subdir.sass +3 -0
  170. data/test/sass/templates/subdir/subdir.sass +6 -0
  171. data/test/sass/templates/units.sass +11 -0
  172. data/test/test_helper.rb +27 -0
  173. metadata +241 -0
@@ -0,0 +1,773 @@
1
+ #!/usr/bin/env ruby
2
+ require File.dirname(__FILE__) + '/../test_helper'
3
+ require 'sass/engine'
4
+ require 'stringio'
5
+
6
+ class SassEngineTest < Test::Unit::TestCase
7
+ # A map of erroneous Sass documents to the error messages they should produce.
8
+ # The error messages may be arrays;
9
+ # if so, the second element should be the line number that should be reported for the error.
10
+ # If this isn't provided, the tests will assume the line number should be the last line of the document.
11
+ EXCEPTION_MAP = {
12
+ "!a = 1 + " => 'Expected expression, was end of text.',
13
+ "!a = 1 + 2 +" => 'Expected expression, was end of text.',
14
+ "!a = 1 + 2 + %" => 'Expected expression, was mod token.',
15
+ "!a = foo(\"bar\"" => 'Expected rparen token, was end of text.',
16
+ "!a = 1 }" => 'Unexpected end_interpolation token.',
17
+ "!a = 1 }foo\"" => 'Unexpected end_interpolation token.',
18
+ ":" => 'Invalid attribute: ":".',
19
+ ": a" => 'Invalid attribute: ": a".',
20
+ ":= a" => 'Invalid attribute: ":= a".',
21
+ "a\n :b" => 'Invalid attribute: ":b ".',
22
+ "a\n :b: c" => 'Invalid attribute: ":b: c".',
23
+ "a\n :b:c d" => 'Invalid attribute: ":b:c d".',
24
+ "a\n :b=c d" => 'Invalid attribute: ":b=c d".',
25
+ "a\n :b c;" => 'Invalid attribute: ":b c;" (This isn\'t CSS!).',
26
+ "a\n b : c" => 'Invalid attribute: "b : c".',
27
+ "a\n b=c: d" => 'Invalid attribute: "b=c: d".',
28
+ ":a" => 'Attributes aren\'t allowed at the root of a document.',
29
+ "!" => 'Invalid variable: "!".',
30
+ "!a" => 'Invalid variable: "!a".',
31
+ "! a" => 'Invalid variable: "! a".',
32
+ "!a b" => 'Invalid variable: "!a b".',
33
+ "!a = 1b + 2c" => "Incompatible units: 'c' and 'b'.",
34
+ "a\n :b= 1b * 2c" => "2b*c isn't a valid CSS value.",
35
+ "a\n :b= 1b % 2c" => "Cannot modulo by a number with units: 2c.",
36
+ "!a = 2px + #ccc" => "Cannot add a number with units (2px) to a color (#cccccc).",
37
+ "!a = #ccc + 2px" => "Cannot add a number with units (2px) to a color (#cccccc).",
38
+ "& a\n :b c" => ["Base-level rules cannot contain the parent-selector-referencing character '&'.", 1],
39
+ "a\n :b\n c" => "Illegal nesting: Only attributes may be nested beneath attributes.",
40
+ "a,\n :b c" => ["Rules can\'t end in commas.", 1],
41
+ "a," => "Rules can\'t end in commas.",
42
+ "a,\n!b = 1" => ["Rules can\'t end in commas.", 1],
43
+ "!a = b\n :c d\n" => "Illegal nesting: Nothing may be nested beneath variable declarations.",
44
+ "@import foo.sass" => "File to import not found or unreadable: foo.sass.",
45
+ "@import templates/basic\n foo" => "Illegal nesting: Nothing may be nested beneath import directives.",
46
+ "foo\n @import templates/basic" => "Import directives may only be used at the root of a document.",
47
+ "foo\n @import #{File.dirname(__FILE__)}/templates/basic" => "Import directives may only be used at the root of a document.",
48
+ %Q{!foo = "bar" "baz" !} => %Q{Syntax error in '"bar" "baz" !' at character 20.},
49
+ "=foo\n :color red\n.bar\n +bang" => "Undefined mixin 'bang'.",
50
+ ".bar\n =foo\n :color red\n" => ["Mixins may only be defined at the root of a document.", 2],
51
+ "=foo\n :color red\n.bar\n +foo\n :color red" => "Illegal nesting: Nothing may be nested beneath mixin directives.",
52
+ " a\n b: c" => ["Indenting at the beginning of the document is illegal.", 1],
53
+ " \n \n\t\n a\n b: c" => ["Indenting at the beginning of the document is illegal.", 4],
54
+ "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],
55
+ "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],
56
+ "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],
57
+ "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],
58
+ "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],
59
+ "a\n b: c\na\n d: e" => ["The line was indented 2 levels deeper than the previous line.", 4],
60
+ "a\n b: c\n a\n d: e" => ["The line was indented 3 levels deeper than the previous line.", 4],
61
+ "a\n \tb: c" => ["Indentation can't use both tabs and spaces.", 2],
62
+ "=a(" => 'Invalid mixin "a(".',
63
+ "=a(b)" => 'Mixin argument "b" must begin with an exclamation point (!).',
64
+ "=a(,)" => "Mixin arguments can't be empty.",
65
+ "=a(!)" => "Mixin arguments can't be empty.",
66
+ "=a(!foo bar)" => "Invalid variable \"!foo bar\".",
67
+ "=foo\n bar: baz\n+foo" => ["Attributes aren't allowed at the root of a document.", 2],
68
+ "a-\#{!b\n c: d" => ["Expected end_interpolation token, was end of text.", 1],
69
+ "=a(!b = 1, !c)" => "Required arguments must not follow optional arguments \"!c\".",
70
+ "=a(!b = 1)\n :a= !b\ndiv\n +a(1,2)" => "Mixin a takes 1 argument but 2 were passed.",
71
+ "=a(!b)\n :a= !b\ndiv\n +a" => "Mixin a is missing parameter !b.",
72
+ "@else\n a\n b: c" => ["@else must come after @if.", 1],
73
+ "@if false\n@else foo" => "Invalid else directive '@else foo': expected 'if <expr>'.",
74
+ "@if false\n@else if " => "Invalid else directive '@else if': expected 'if <expr>'.",
75
+ "a\n !b = 12\nc\n d = !b" => 'Undefined variable: "!b".',
76
+ "=foo\n !b = 12\nc\n +foo\n d = !b" => 'Undefined variable: "!b".',
77
+ '@for !a from 1 to "foo"' => '"foo" is not an integer.',
78
+ '@for !a from 1 to 1.232323' => '1.232 is not an integer.',
79
+ '@if' => "Invalid if directive '@if': expected expression.",
80
+ '@while' => "Invalid while directive '@while': expected expression.",
81
+ '@debug' => "Invalid debug directive '@debug': expected expression.",
82
+
83
+ # Regression tests
84
+ "a\n b:\n c\n d" => ["Illegal nesting: Only attributes may be nested beneath attributes.", 3],
85
+ "& foo\n bar: baz\n blat: bang" => ["Base-level rules cannot contain the parent-selector-referencing character '&'.", 1],
86
+ "a\n b: c\n& foo\n bar: baz\n blat: bang" => ["Base-level rules cannot contain the parent-selector-referencing character '&'.", 3],
87
+ }
88
+
89
+ def teardown
90
+ clean_up_sassc
91
+ end
92
+
93
+ def test_basic_render
94
+ renders_correctly "basic", { :style => :compact }
95
+ end
96
+
97
+ def test_empty_render
98
+ assert_equal "", render("")
99
+ end
100
+
101
+ def test_multiple_calls_to_render
102
+ sass = Sass::Engine.new("a\n b: c")
103
+ assert_equal sass.render, sass.render
104
+ end
105
+
106
+ def test_alternate_styles
107
+ renders_correctly "expanded", { :style => :expanded }
108
+ renders_correctly "compact", { :style => :compact }
109
+ renders_correctly "nested", { :style => :nested }
110
+ renders_correctly "compressed", { :style => :compressed }
111
+ end
112
+
113
+ def test_flexible_tabulation
114
+ assert_equal("p {\n a: b; }\n p q {\n c: d; }\n",
115
+ render("p\n a: b\n q\n c: d\n"))
116
+ assert_equal("p {\n a: b; }\n p q {\n c: d; }\n",
117
+ render("p\n\ta: b\n\tq\n\t\tc: d\n"))
118
+ end
119
+
120
+ def test_exceptions
121
+ EXCEPTION_MAP.each do |key, value|
122
+ line = 10
123
+ begin
124
+ Sass::Engine.new(key, :filename => __FILE__, :line => line).render
125
+ rescue Sass::SyntaxError => err
126
+ value = [value] unless value.is_a?(Array)
127
+
128
+ assert_equal(value.first, err.message, "Line: #{key}")
129
+ assert_equal(__FILE__, err.sass_filename)
130
+ assert_equal((value[1] || key.split("\n").length) + line - 1, err.sass_line, "Line: #{key}")
131
+ assert_match(/#{Regexp.escape(__FILE__)}:[0-9]+/, err.backtrace[0], "Line: #{key}")
132
+ else
133
+ assert(false, "Exception not raised for\n#{key}")
134
+ end
135
+ end
136
+ end
137
+
138
+ def test_exception_line
139
+ to_render = <<SASS
140
+ rule
141
+ :attr val
142
+ // comment!
143
+
144
+ :broken
145
+ SASS
146
+ begin
147
+ Sass::Engine.new(to_render).render
148
+ rescue Sass::SyntaxError => err
149
+ assert_equal(5, err.sass_line)
150
+ else
151
+ assert(false, "Exception not raised for '#{to_render}'!")
152
+ end
153
+ end
154
+
155
+ def test_exception_location
156
+ to_render = <<SASS
157
+ rule
158
+ :attr val
159
+ // comment!
160
+
161
+ :broken
162
+ SASS
163
+ begin
164
+ Sass::Engine.new(to_render, :filename => __FILE__, :line => (__LINE__-7)).render
165
+ rescue Sass::SyntaxError => err
166
+ assert_equal(__FILE__, err.sass_filename)
167
+ assert_equal((__LINE__-6), err.sass_line)
168
+ else
169
+ assert(false, "Exception not raised for '#{to_render}'!")
170
+ end
171
+ end
172
+
173
+ def test_imported_exception
174
+ [nil, 2].each do |i|
175
+ begin
176
+ Sass::Engine.new("@import bork#{i}", :load_paths => [File.dirname(__FILE__) + '/templates/']).render
177
+ rescue Sass::SyntaxError => err
178
+ assert_equal(2, err.sass_line)
179
+ assert_match(/bork#{i}\.sass$/, err.sass_filename)
180
+ else
181
+ assert(false, "Exception not raised for imported template: bork#{i}")
182
+ end
183
+ end
184
+ end
185
+
186
+ def test_css_import
187
+ assert_equal("@import url(./fonts.css) screen;", render("@import url(./fonts.css) screen"))
188
+ assert_equal("@import \"./fonts.css\" screen;", render("@import \"./fonts.css\" screen"))
189
+ end
190
+
191
+ def test_sass_import
192
+ assert !File.exists?(sassc_path("importee"))
193
+ renders_correctly "import", { :style => :compact, :load_paths => [File.dirname(__FILE__) + "/templates"] }
194
+ assert File.exists?(sassc_path("importee"))
195
+ end
196
+
197
+ def test_no_cache
198
+ assert !File.exists?(sassc_path("importee"))
199
+ renders_correctly("import", {
200
+ :style => :compact, :cache => false,
201
+ :load_paths => [File.dirname(__FILE__) + "/templates"],
202
+ })
203
+ assert !File.exists?(sassc_path("importee"))
204
+ end
205
+
206
+ def test_units
207
+ renders_correctly "units"
208
+ end
209
+
210
+ def test_default_function
211
+ assert_equal("foo {\n bar: url(foo.png); }\n", render(%Q{foo\n bar = url("foo.png")\n}));
212
+ assert_equal("foo {\n bar: url(); }\n", render("foo\n bar = url()\n"));
213
+ end
214
+
215
+ def test_string_minus
216
+ assert_equal("foo {\n bar: baz-boom-bat; }\n", render(%Q{foo\n bar = "baz"-"boom"-"bat"}))
217
+ assert_equal("foo {\n bar: -baz-boom; }\n", render(%Q{foo\n bar = -"baz"-"boom"}))
218
+ end
219
+
220
+ def test_string_div
221
+ assert_equal("foo {\n bar: baz/boom/bat; }\n", render(%Q{foo\n bar = "baz"/"boom"/"bat"}))
222
+ assert_equal("foo {\n bar: /baz/boom; }\n", render(%Q{foo\n bar = /"baz"/"boom"}))
223
+ end
224
+
225
+ def test_basic_multiline_selector
226
+ assert_equal("#foo #bar,\n#baz #boom {\n foo: bar; }\n",
227
+ render("#foo #bar,\n#baz #boom\n :foo bar"))
228
+ assert_equal("#foo #bar,\n#foo #baz {\n foo: bar; }\n",
229
+ render("#foo\n #bar,\n #baz\n :foo bar"))
230
+ assert_equal("#foo,\n#bar {\n foo: bar; }\n #foo #baz,\n #bar #baz {\n foo: bar; }\n",
231
+ render("#foo,\n#bar\n :foo bar\n #baz\n :foo bar"))
232
+ assert_equal("#foo #bar, #baz #boom { foo: bar; }\n",
233
+ render("#foo #bar,\n#baz #boom\n :foo bar", :style => :compact))
234
+
235
+ assert_equal("#foo #bar,#baz #boom{foo:bar}\n",
236
+ render("#foo #bar,\n#baz #boom\n :foo bar", :style => :compressed))
237
+ end
238
+
239
+ def test_complex_multiline_selector
240
+ renders_correctly "multiline"
241
+ end
242
+
243
+ def test_colon_only
244
+ begin
245
+ render("a\n b: c", :attribute_syntax => :normal)
246
+ rescue Sass::SyntaxError => e
247
+ assert_equal("Illegal attribute syntax: can't use alternate syntax when :attribute_syntax => :normal is set.",
248
+ e.message)
249
+ else
250
+ assert(false, "SyntaxError not raised for :attribute_syntax => :normal")
251
+ end
252
+
253
+ begin
254
+ render("a\n :b c", :attribute_syntax => :alternate)
255
+ rescue Sass::SyntaxError => e
256
+ assert_equal("Illegal attribute syntax: can't use normal syntax when :attribute_syntax => :alternate is set.",
257
+ e.message)
258
+ else
259
+ assert(false, "SyntaxError not raised for :attribute_syntax => :alternate")
260
+ end
261
+ end
262
+
263
+ def test_pseudo_elements
264
+ assert_equal(<<CSS, render(<<SASS))
265
+ ::first-line {
266
+ size: 10em; }
267
+ CSS
268
+ ::first-line
269
+ size: 10em
270
+ SASS
271
+ end
272
+
273
+ def test_directive
274
+ assert_equal("@a b;", render("@a b"))
275
+
276
+ assert_equal("@a {\n b: c; }\n", render("@a\n :b c"))
277
+ assert_equal("@a { b: c; }\n", render("@a\n :b c", :style => :compact))
278
+ assert_equal("@a {\n b: c;\n}\n", render("@a\n :b c", :style => :expanded))
279
+ assert_equal("@a{b:c}\n", render("@a\n :b c", :style => :compressed))
280
+
281
+ assert_equal("@a {\n b: c;\n d: e; }\n",
282
+ render("@a\n :b c\n :d e"))
283
+ assert_equal("@a { b: c; d: e; }\n",
284
+ render("@a\n :b c\n :d e", :style => :compact))
285
+ assert_equal("@a {\n b: c;\n d: e;\n}\n",
286
+ render("@a\n :b c\n :d e", :style => :expanded))
287
+ assert_equal("@a{b:c;d:e}\n",
288
+ render("@a\n :b c\n :d e", :style => :compressed))
289
+
290
+ assert_equal("@a {\n #b {\n c: d; } }\n",
291
+ render("@a\n #b\n :c d"))
292
+ assert_equal("@a { #b { c: d; } }\n",
293
+ render("@a\n #b\n :c d", :style => :compact))
294
+ assert_equal("@a {\n #b {\n c: d;\n }\n}\n",
295
+ render("@a\n #b\n :c d", :style => :expanded))
296
+ assert_equal("@a{#b{c:d}}\n",
297
+ render("@a\n #b\n :c d", :style => :compressed))
298
+
299
+ assert_equal("@a {\n #b {\n a: b; }\n #b #c {\n d: e; } }\n",
300
+ render("@a\n #b\n :a b\n #c\n :d e"))
301
+ assert_equal("@a { #b { a: b; }\n #b #c { d: e; } }\n",
302
+ render("@a\n #b\n :a b\n #c\n :d e", :style => :compact))
303
+ assert_equal("@a {\n #b {\n a: b;\n }\n #b #c {\n d: e;\n }\n}\n",
304
+ render("@a\n #b\n :a b\n #c\n :d e", :style => :expanded))
305
+ assert_equal("@a{#b{a:b}#b #c{d:e}}\n",
306
+ render("@a\n #b\n :a b\n #c\n :d e", :style => :compressed))
307
+
308
+ assert_equal("@a {\n #foo,\n #bar {\n b: c; } }\n",
309
+ render("@a\n #foo, \n #bar\n :b c"))
310
+ assert_equal("@a { #foo, #bar { b: c; } }\n",
311
+ render("@a\n #foo, \n #bar\n :b c", :style => :compact))
312
+ assert_equal("@a {\n #foo,\n #bar {\n b: c;\n }\n}\n",
313
+ render("@a\n #foo, \n #bar\n :b c", :style => :expanded))
314
+ assert_equal("@a{#foo,#bar{b:c}}\n",
315
+ render("@a\n #foo, \n #bar\n :b c", :style => :compressed))
316
+
317
+ to_render = <<END
318
+ @a
319
+ :b c
320
+ #d
321
+ :e f
322
+ :g h
323
+ END
324
+ rendered = <<END
325
+ @a { b: c;
326
+ #d { e: f; }
327
+ g: h; }
328
+ END
329
+ assert_equal(rendered, render(to_render, :style => :compact))
330
+
331
+ assert_equal("@a{b:c;#d{e:f}g:h}\n", render(to_render, :style => :compressed))
332
+ end
333
+
334
+ def test_line_annotations
335
+ assert_equal(<<CSS, render(<<SASS, :line_comments => true, :style => :compact))
336
+ /* line 2, test_line_annotations_inline.sass */
337
+ foo bar { foo: bar; }
338
+ /* line 5, test_line_annotations_inline.sass */
339
+ foo baz { blip: blop; }
340
+
341
+ /* line 9, test_line_annotations_inline.sass */
342
+ floodle { flop: blop; }
343
+
344
+ /* line 18, test_line_annotations_inline.sass */
345
+ bup { mix: on; }
346
+ /* line 15, test_line_annotations_inline.sass */
347
+ bup mixin { moop: mup; }
348
+
349
+ /* line 22, test_line_annotations_inline.sass */
350
+ bip hop, skip hop { a: b; }
351
+ CSS
352
+ foo
353
+ bar
354
+ foo: bar
355
+
356
+ baz
357
+ blip: blop
358
+
359
+
360
+ floodle
361
+
362
+ flop: blop
363
+
364
+ =mxn
365
+ mix: on
366
+ mixin
367
+ moop: mup
368
+
369
+ bup
370
+ +mxn
371
+
372
+ bip, skip
373
+ hop
374
+ a: b
375
+ SASS
376
+ end
377
+
378
+ def test_line_annotations_with_filename
379
+ renders_correctly "line_numbers", :line_comments => true, :load_paths => [File.dirname(__FILE__) + "/templates"]
380
+ end
381
+
382
+ def test_empty_first_line
383
+ assert_equal("#a {\n b: c; }\n", render("#a\n\n b: c"))
384
+ end
385
+
386
+ def test_escaped_rule
387
+ assert_equal(":focus {\n a: b; }\n", render("\\:focus\n a: b"))
388
+ assert_equal("a {\n b: c; }\n a :focus {\n d: e; }\n", render("\\a\n b: c\n \\:focus\n d: e"))
389
+ end
390
+
391
+ def test_cr_newline
392
+ 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"))
393
+ end
394
+
395
+ def test_or_eq
396
+ assert_equal("foo {\n a: b; }\n", render(%Q{!foo = "b"\n!foo ||= "c"\nfoo\n a = !foo}))
397
+ assert_equal("foo {\n a: b; }\n", render(%Q{!foo ||= "b"\nfoo\n a = !foo}))
398
+ end
399
+
400
+ def test_mixins
401
+ renders_correctly "mixins", { :style => :expanded }
402
+ end
403
+
404
+ def test_mixins_dont_interfere_with_sibling_combinator
405
+ assert_equal("foo + bar {\n a: b; }\nfoo + baz {\n c: d; }\n",
406
+ render("foo\n +\n bar\n a: b\n baz\n c: d"))
407
+ end
408
+
409
+ def test_mixin_args
410
+ assert_equal("blat {\n baz: hi; }\n", render(<<SASS))
411
+ =foo(!bar)
412
+ baz = !bar
413
+ blat
414
+ +foo(\"hi\")
415
+ SASS
416
+ assert_equal("blat {\n baz: 3; }\n", render(<<SASS))
417
+ =foo(!a, !b)
418
+ baz = !a + !b
419
+ blat
420
+ +foo(1, 2)
421
+ SASS
422
+ assert_equal("blat {\n baz: 4;\n baz: 3;\n baz: 5;\n bang: 3; }\n", render(<<SASS))
423
+ =foo(!c = (6 + 4) / 2)
424
+ baz = !c
425
+ !c = 3
426
+ blat
427
+ +foo(!c + 1)
428
+ +foo((!c + 3)/2)
429
+ +foo
430
+ bang = !c
431
+ SASS
432
+ end
433
+
434
+ def test_default_values_for_mixin_arguments
435
+ assert_equal("white {\n color: white; }\n\nblack {\n color: black; }\n", render(<<SASS))
436
+ =foo(!a = #FFF)
437
+ :color= !a
438
+ white
439
+ +foo
440
+ black
441
+ +foo(#000)
442
+ SASS
443
+ assert_equal(<<CSS, render(<<SASS))
444
+ one {
445
+ color: white;
446
+ padding: 1px;
447
+ margin: 4px; }
448
+
449
+ two {
450
+ color: white;
451
+ padding: 2px;
452
+ margin: 5px; }
453
+
454
+ three {
455
+ color: white;
456
+ padding: 2px;
457
+ margin: 3px; }
458
+ CSS
459
+ !a = 5px
460
+ =foo(!a, !b = 1px, !c = 3px + !b)
461
+ :color= !a
462
+ :padding= !b
463
+ :margin= !c
464
+ one
465
+ +foo(#fff)
466
+ two
467
+ +foo(#fff, 2px)
468
+ three
469
+ +foo(#fff, 2px, 3px)
470
+ SASS
471
+ end
472
+
473
+ def test_interpolation
474
+ assert_equal("a-1 {\n b-2-3: c-3; }\n", render(<<SASS))
475
+ !a = 1
476
+ !b = 2
477
+ !c = 3
478
+ a-\#{!a}
479
+ b-\#{!b}-\#{!c}: c-\#{!a + !b}
480
+ SASS
481
+ end
482
+
483
+ def test_if_directive
484
+ assert_equal("a {\n b: 1; }\n", render(<<SASS))
485
+ !var = true
486
+ a
487
+ @if !var
488
+ b: 1
489
+ @if not !var
490
+ b: 2
491
+ SASS
492
+ end
493
+
494
+ def test_for
495
+ assert_equal(<<CSS, render(<<SASS))
496
+ a-0 {
497
+ 2i: 0; }
498
+
499
+ a-1 {
500
+ 2i: 2; }
501
+
502
+ a-2 {
503
+ 2i: 4; }
504
+
505
+ a-3 {
506
+ 2i: 6; }
507
+
508
+ b-1 {
509
+ j-1: 0; }
510
+
511
+ b-2 {
512
+ j-1: 1; }
513
+
514
+ b-3 {
515
+ j-1: 2; }
516
+
517
+ b-4 {
518
+ j-1: 3; }
519
+ CSS
520
+ !a = 3
521
+ @for !i from 0 to !a + 1
522
+ a-\#{!i}
523
+ 2i = 2 * !i
524
+
525
+ @for !j from 1 through 4
526
+ b-\#{!j}
527
+ j-1 = !j - 1
528
+ SASS
529
+ end
530
+
531
+ def test_while
532
+ assert_equal(<<CSS, render(<<SASS))
533
+ a-5 {
534
+ blooble: gloop; }
535
+
536
+ a-4 {
537
+ blooble: gloop; }
538
+
539
+ a-3 {
540
+ blooble: gloop; }
541
+
542
+ a-2 {
543
+ blooble: gloop; }
544
+
545
+ a-1 {
546
+ blooble: gloop; }
547
+ CSS
548
+ !a = 5
549
+ @while !a != 0
550
+ a-\#{!a}
551
+ blooble: gloop
552
+ !a = !a - 1
553
+ SASS
554
+ end
555
+
556
+ def test_else
557
+ assert_equal(<<CSS, render(<<SASS))
558
+ a {
559
+ t1: t;
560
+ t2: t;
561
+ t3: t;
562
+ t4: t; }
563
+ CSS
564
+ a
565
+ @if true
566
+ t1: t
567
+ @else
568
+ f1: f
569
+
570
+ @if false
571
+ f2: f
572
+ @else
573
+ t2: t
574
+
575
+ @if false
576
+ f3: f1
577
+ @else if 1 + 1 == 3
578
+ f3: f2
579
+ @else
580
+ t3: t
581
+
582
+ @if false
583
+ f4: f1
584
+ @else if 1 + 1 == 2
585
+ t4: t
586
+ @else
587
+ f4: f2
588
+
589
+ @if false
590
+ f5: f1
591
+ @else if false
592
+ f5: f2
593
+ SASS
594
+ end
595
+
596
+ def test_variable_reassignment
597
+ assert_equal(<<CSS, render(<<SASS))
598
+ a {
599
+ b: 1;
600
+ c: 2; }
601
+ CSS
602
+ !a = 1
603
+ a
604
+ b = !a
605
+ !a = 2
606
+ c = !a
607
+ SASS
608
+ end
609
+
610
+ def test_variable_scope
611
+ assert_equal(<<CSS, render(<<SASS))
612
+ a {
613
+ b-1: c;
614
+ b-2: c;
615
+ d: 12; }
616
+
617
+ b {
618
+ d: 17; }
619
+ CSS
620
+ !i = 12
621
+ a
622
+ @for !i from 1 through 2
623
+ b-\#{!i}: c
624
+ d = !i
625
+
626
+ =foo
627
+ !i = 17
628
+
629
+ b
630
+ +foo
631
+ d = !i
632
+ SASS
633
+ end
634
+
635
+ def test_argument_error
636
+ assert_raise(Sass::SyntaxError) { render("a\n b = hsl(1)") }
637
+ end
638
+
639
+ def test_comments_at_the_top_of_a_document
640
+ render(<<SASS)
641
+ //
642
+ This is a comment that
643
+ continues to the second line.
644
+ foo
645
+ bar: baz
646
+ SASS
647
+ end
648
+
649
+ def test_loud_comments_containing_a_comment_close
650
+ actual_css = render(<<SASS)
651
+ /*
652
+ This is a comment that
653
+ continues to the second line. */
654
+ foo
655
+ bar: baz
656
+ SASS
657
+ assert_equal(<<CSS, actual_css)
658
+ /*
659
+ * This is a comment that
660
+ * continues to the second line. */
661
+ foo {
662
+ bar: baz; }
663
+ CSS
664
+ end
665
+
666
+ def test_quoted_colon
667
+ assert_equal(<<CSS, render(<<SASS))
668
+ a b[foo="bar: baz"] {
669
+ c: d; }
670
+ CSS
671
+ a
672
+ b[foo="bar: baz"]
673
+ c: d
674
+ SASS
675
+ end
676
+
677
+ def test_quoted_comma
678
+ assert_equal(<<CSS, render(<<SASS))
679
+ a b[foo="bar, baz"] {
680
+ c: d; }
681
+ CSS
682
+ a
683
+ b[foo="bar, baz"]
684
+ c: d
685
+ SASS
686
+ end
687
+
688
+ def test_quoted_ampersand
689
+ assert_equal(<<CSS, render(<<SASS))
690
+ a b[foo="bar & baz"] {
691
+ c: d; }
692
+ CSS
693
+ a
694
+ b[foo="bar & baz"]
695
+ c: d
696
+ SASS
697
+ end
698
+
699
+ # Regression tests
700
+
701
+ def test_comment_beneath_attr
702
+ assert_equal(<<RESULT, render(<<SOURCE))
703
+ .box {
704
+ border-style: solid; }
705
+ RESULT
706
+ .box
707
+ :border
708
+ //:color black
709
+ :style solid
710
+ SOURCE
711
+
712
+ assert_equal(<<RESULT, render(<<SOURCE))
713
+ .box {
714
+ /* :color black */
715
+ border-style: solid; }
716
+ RESULT
717
+ .box
718
+ :border
719
+ /*:color black
720
+ :style solid
721
+ SOURCE
722
+
723
+ assert_equal(<<RESULT, render(<<SOURCE, :style => :compressed))
724
+ .box{border-style:solid}
725
+ RESULT
726
+ .box
727
+ :border
728
+ /*:color black
729
+ :style solid
730
+ SOURCE
731
+ end
732
+
733
+ def test_compressed_comment_beneath_directive
734
+ assert_equal(<<RESULT, render(<<SOURCE, :style => :compressed))
735
+ @foo{a:b}
736
+ RESULT
737
+ @foo
738
+ a: b
739
+ /*b: c
740
+ SOURCE
741
+ end
742
+
743
+ private
744
+
745
+ def render(sass, options = {})
746
+ munge_filename options
747
+ Sass::Engine.new(sass, options).render
748
+ end
749
+
750
+ def renders_correctly(name, options={})
751
+ sass_file = load_file(name, "sass")
752
+ css_file = load_file(name, "css")
753
+ options[:filename] ||= filename(name, "sass")
754
+ options[:css_filename] ||= filename(name, "css")
755
+ css_result = Sass::Engine.new(sass_file, options).render
756
+ assert_equal css_file, css_result
757
+ end
758
+
759
+ def load_file(name, type = "sass")
760
+ @result = ''
761
+ File.new(filename(name, type)).each_line { |l| @result += l }
762
+ @result
763
+ end
764
+
765
+ def filename(name, type)
766
+ File.dirname(__FILE__) + "/#{type == 'sass' ? 'templates' : 'results'}/#{name}.#{type}"
767
+ end
768
+
769
+ def sassc_path(template)
770
+ sassc_path = File.join(File.dirname(__FILE__) + "/templates/#{template}.sass")
771
+ Sass::Files.send(:sassc_filename, sassc_path, Sass::Engine::DEFAULT_OPTIONS)
772
+ end
773
+ end