PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/plugins/haml/test/sass/engine_test.rb

https://github.com/kbingman/assets_extension
Ruby | 240 lines | 237 code | 2 blank | 1 comment | 0 complexity | 21c6f5369fc2d38f10f45bc9cd51b8cd MD5 | raw file
  1. #!/usr/bin/env ruby
  2. require 'test/unit'
  3. require File.dirname(__FILE__) + '/../../lib/sass'
  4. require 'sass/engine'
  5. class SassEngineTest < Test::Unit::TestCase
  6. EXCEPTION_MAP = {
  7. "!a = 1 + " => 'Constant arithmetic error: "1 +"',
  8. "!a = 1 + 2 +" => 'Constant arithmetic error: "1 + 2 +"',
  9. "!a = \"b" => 'Unterminated string: "\\"b"',
  10. "!a = #aaa - a" => 'Undefined operation: "#aaaaaa minus a"',
  11. "!a = #aaa / a" => 'Undefined operation: "#aaaaaa div a"',
  12. "!a = #aaa * a" => 'Undefined operation: "#aaaaaa times a"',
  13. "!a = #aaa % a" => 'Undefined operation: "#aaaaaa mod a"',
  14. "!a = 1 - a" => 'Undefined operation: "1 minus a"',
  15. "!a = 1 * a" => 'Undefined operation: "1 times a"',
  16. "!a = 1 / a" => 'Undefined operation: "1 div a"',
  17. "!a = 1 % a" => 'Undefined operation: "1 mod a"',
  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 constant: "!"',
  30. "!a" => 'Invalid constant: "!a"',
  31. "! a" => 'Invalid constant: "! a"',
  32. "!a b" => 'Invalid constant: "!a b"',
  33. "a\n\t:b c" => "Illegal Indentation: Only two space characters are allowed as tabulation.",
  34. "a\n :b c" => "Illegal Indentation: Only two space characters are allowed as tabulation.",
  35. "a\n :b c" => "Illegal Indentation: Only two space characters are allowed as tabulation.",
  36. "a\n :b c\n !d = 3" => "Constants may only be declared at the root of a document.",
  37. "!a = 1b + 2c" => "Incompatible units: b and c",
  38. "& a\n :b c" => "Base-level rules cannot contain the parent-selector-referencing character '&'",
  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.",
  41. "!a = b\n :c d\n" => "Illegal nesting: Nothing may be nested beneath constants.",
  42. "@import foo.sass" => "File to import not found or unreadable: foo.sass",
  43. "@import templates/basic\n foo" => "Illegal nesting: Nothing may be nested beneath import directives.",
  44. "foo\n @import templates/basic" => "Import directives may only be used at the root of a document.",
  45. "!foo = bar baz !" => "Unterminated constant.",
  46. "!foo = !(foo)" => "Invalid constant.",
  47. }
  48. def test_basic_render
  49. renders_correctly "basic", { :style => :compact }
  50. end
  51. def test_alternate_styles
  52. renders_correctly "expanded", { :style => :expanded }
  53. renders_correctly "compact", { :style => :compact }
  54. renders_correctly "nested", { :style => :nested }
  55. renders_correctly "compressed", { :style => :compressed }
  56. end
  57. def test_exceptions
  58. EXCEPTION_MAP.each do |key, value|
  59. begin
  60. Sass::Engine.new(key).render
  61. rescue Sass::SyntaxError => err
  62. assert_equal(value, err.message)
  63. assert(err.sass_line, "Line: #{key}")
  64. assert_match(/\(sass\):[0-9]+/, err.backtrace[0], "Line: #{key}")
  65. else
  66. assert(false, "Exception not raised for\n#{key}")
  67. end
  68. end
  69. end
  70. def test_exception_line
  71. to_render = "rule\n :attr val\n// comment!\n\n :broken\n"
  72. begin
  73. Sass::Engine.new(to_render).render
  74. rescue Sass::SyntaxError => err
  75. assert_equal(5, err.sass_line)
  76. else
  77. assert(false, "Exception not raised for '#{to_render}'!")
  78. end
  79. end
  80. def test_imported_exception
  81. [1, 2].each do |i|
  82. i = nil if i == 1
  83. begin
  84. Sass::Engine.new("@import bork#{i}", :load_paths => [File.dirname(__FILE__) + '/templates/']).render
  85. rescue Sass::SyntaxError => err
  86. assert_equal(2, err.sass_line)
  87. assert_match(/bork#{i}\.sass$/, err.sass_filename)
  88. else
  89. assert(false, "Exception not raised for imported template: bork#{i}")
  90. end
  91. end
  92. end
  93. def test_css_import
  94. assert_equal("@import url(./fonts.css) screen;", render("@import url(./fonts.css) screen"))
  95. assert_equal("@import \"./fonts.css\" screen;", render("@import \"./fonts.css\" screen"))
  96. end
  97. def test_default_function
  98. assert_equal("foo {\n bar: url(foo.png); }\n",
  99. render("foo\n bar = url(foo.png)\n"));
  100. end
  101. def test_multiline_selector
  102. assert_equal("#foo #bar,\n#baz #boom {\n foo: bar; }\n",
  103. render("#foo #bar,\n#baz #boom\n :foo bar"))
  104. assert_equal("#foo #bar,\n#foo #baz {\n foo: bar; }\n",
  105. render("#foo\n #bar,\n #baz\n :foo bar"))
  106. assert_equal("#foo #bar, #baz #boom { foo: bar; }\n",
  107. render("#foo #bar,\n#baz #boom\n :foo bar", :style => :compact))
  108. assert_equal("#foo #bar,#baz #boom{foo:bar}\n",
  109. render("#foo #bar,\n#baz #boom\n :foo bar", :style => :compressed))
  110. end
  111. def test_colon_only
  112. begin
  113. render("a\n b: c", :attribute_syntax => :normal)
  114. rescue Sass::SyntaxError => e
  115. assert_equal("Illegal attribute syntax: can't use alternate syntax when :attribute_syntax => :normal is set.",
  116. e.message)
  117. else
  118. assert(false, "SyntaxError not raised for :attribute_syntax => :normal")
  119. end
  120. begin
  121. render("a\n :b c", :attribute_syntax => :alternate)
  122. rescue Sass::SyntaxError => e
  123. assert_equal("Illegal attribute syntax: can't use normal syntax when :attribute_syntax => :alternate is set.",
  124. e.message)
  125. else
  126. assert(false, "SyntaxError not raised for :attribute_syntax => :alternate")
  127. end
  128. end
  129. def test_directive
  130. assert_equal("@a b;", render("@a b"))
  131. assert_equal("@a {\n b: c; }\n", render("@a\n :b c"))
  132. assert_equal("@a { b: c; }\n", render("@a\n :b c", :style => :compact))
  133. assert_equal("@a {\n b: c;\n}\n", render("@a\n :b c", :style => :expanded))
  134. assert_equal("@a{b:c}\n", render("@a\n :b c", :style => :compressed))
  135. assert_equal("@a {\n b: c;\n d: e; }\n",
  136. render("@a\n :b c\n :d e"))
  137. assert_equal("@a { b: c; d: e; }\n",
  138. render("@a\n :b c\n :d e", :style => :compact))
  139. assert_equal("@a {\n b: c;\n d: e;\n}\n",
  140. render("@a\n :b c\n :d e", :style => :expanded))
  141. assert_equal("@a{b:c;d:e}\n",
  142. render("@a\n :b c\n :d e", :style => :compressed))
  143. assert_equal("@a {\n #b {\n c: d; } }\n",
  144. render("@a\n #b\n :c d"))
  145. assert_equal("@a { #b { c: d; } }\n",
  146. render("@a\n #b\n :c d", :style => :compact))
  147. assert_equal("@a {\n #b {\n c: d;\n }\n}\n",
  148. render("@a\n #b\n :c d", :style => :expanded))
  149. assert_equal("@a{#b{c:d}}\n",
  150. render("@a\n #b\n :c d", :style => :compressed))
  151. assert_equal("@a {\n #b {\n a: b; }\n #b #c {\n d: e; } }\n",
  152. render("@a\n #b\n :a b\n #c\n :d e"))
  153. assert_equal("@a { #b { a: b; }\n #b #c { d: e; } }\n",
  154. render("@a\n #b\n :a b\n #c\n :d e", :style => :compact))
  155. assert_equal("@a {\n #b {\n a: b;\n }\n #b #c {\n d: e;\n }\n}\n",
  156. render("@a\n #b\n :a b\n #c\n :d e", :style => :expanded))
  157. assert_equal("@a{#b{a:b}#b #c{d:e}}\n",
  158. render("@a\n #b\n :a b\n #c\n :d e", :style => :compressed))
  159. assert_equal("@a {\n #foo,\n #bar {\n b: c; } }\n",
  160. render("@a\n #foo, \n #bar\n :b c"))
  161. assert_equal("@a { #foo, #bar { b: c; } }\n",
  162. render("@a\n #foo, \n #bar\n :b c", :style => :compact))
  163. assert_equal("@a {\n #foo,\n #bar {\n b: c;\n }\n}\n",
  164. render("@a\n #foo, \n #bar\n :b c", :style => :expanded))
  165. assert_equal("@a{#foo,#bar{b:c}}\n",
  166. render("@a\n #foo, \n #bar\n :b c", :style => :compressed))
  167. to_render = <<END
  168. @a
  169. :b c
  170. #d
  171. :e f
  172. :g h
  173. END
  174. rendered = <<END
  175. @a { b: c;
  176. #d { e: f; }
  177. g: h; }
  178. END
  179. assert_equal(rendered, render(to_render, :style => :compact))
  180. assert_equal("@a{b:c;#d{e:f}g:h}\n", render(to_render, :style => :compressed))
  181. end
  182. def test_empty_first_line
  183. assert_equal("#a {\n b: c; }\n", render("#a\n\n b: c"))
  184. end
  185. def test_escaped_rule
  186. assert_equal(":focus {\n a: b; }\n", render("\\:focus\n a: b"))
  187. assert_equal("a {\n b: c; }\n a :focus {\n d: e; }\n", render("\\a\n b: c\n \\:focus\n d: e"))
  188. end
  189. def test_cr_newline
  190. 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"))
  191. end
  192. def test_or_eq
  193. assert_equal("foo {\n a: b; }\n", render("!foo = b\n!foo ||= c\nfoo\n a = !foo"))
  194. assert_equal("foo {\n a: b; }\n", render("!foo ||= b\nfoo\n a = !foo"))
  195. end
  196. private
  197. def render(sass, options = {})
  198. Sass::Engine.new(sass, options).render
  199. end
  200. def renders_correctly(name, options={})
  201. sass_file = load_file(name, "sass")
  202. css_file = load_file(name, "css")
  203. css_result = Sass::Engine.new(sass_file, options).render
  204. assert_equal css_file, css_result
  205. end
  206. def load_file(name, type = "sass")
  207. @result = ''
  208. File.new(File.dirname(__FILE__) + "/#{type == 'sass' ? 'templates' : 'results'}/#{name}.#{type}").each_line { |l| @result += l }
  209. @result
  210. end
  211. end