merbjedi-haml 2.1.0

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