PageRenderTime 31ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/training-web/vendor/bundle/gems/sass-3.2.9/test/sass/script_test.rb

https://bitbucket.org/ohimmelreich/asalia-training
Ruby | 591 lines | 582 code | 8 blank | 1 comment | 0 complexity | 0e06fd5962083e9f6cebc4cfc62d08c9 MD5 | raw file
  1. #!/usr/bin/env ruby
  2. require File.dirname(__FILE__) + '/../test_helper'
  3. require 'sass/engine'
  4. module Sass::Script::Functions::UserFunctions
  5. def assert_options(val)
  6. val.options[:foo]
  7. Sass::Script::String.new("Options defined!")
  8. end
  9. def arg_error
  10. assert_options
  11. end
  12. end
  13. module Sass::Script::Functions
  14. include Sass::Script::Functions::UserFunctions
  15. end
  16. class SassScriptTest < Test::Unit::TestCase
  17. include Sass::Script
  18. def test_color_checks_input
  19. assert_raise_message(ArgumentError, "Blue value -1 must be between 0 and 255") {Color.new([1, 2, -1])}
  20. assert_raise_message(ArgumentError, "Red value 256 must be between 0 and 255") {Color.new([256, 2, 3])}
  21. end
  22. def test_color_checks_rgba_input
  23. assert_raise_message(ArgumentError, "Alpha channel 1.1 must be between 0 and 1") {Color.new([1, 2, 3, 1.1])}
  24. assert_raise_message(ArgumentError, "Alpha channel -0.1 must be between 0 and 1") {Color.new([1, 2, 3, -0.1])}
  25. end
  26. def test_string_escapes
  27. assert_equal "'", resolve("\"'\"")
  28. assert_equal '"', resolve("\"\\\"\"")
  29. assert_equal "\\\\", resolve("\"\\\\\"")
  30. assert_equal "\\02fa", resolve("\"\\02fa\"")
  31. assert_equal "'", resolve("'\\''")
  32. assert_equal '"', resolve("'\"'")
  33. assert_equal "\\\\", resolve("'\\\\'")
  34. assert_equal "\\02fa", resolve("'\\02fa'")
  35. end
  36. def test_color_names
  37. assert_equal "white", resolve("white")
  38. assert_equal "white", resolve("#ffffff")
  39. assert_equal "#fffffe", resolve("white - #000001")
  40. end
  41. def test_rgba_color_literals
  42. assert_equal Sass::Script::Color.new([1, 2, 3, 0.75]), eval("rgba(1, 2, 3, 0.75)")
  43. assert_equal "rgba(1, 2, 3, 0.75)", resolve("rgba(1, 2, 3, 0.75)")
  44. assert_equal Sass::Script::Color.new([1, 2, 3, 0]), eval("rgba(1, 2, 3, 0)")
  45. assert_equal "rgba(1, 2, 3, 0)", resolve("rgba(1, 2, 3, 0)")
  46. assert_equal Sass::Script::Color.new([1, 2, 3]), eval("rgba(1, 2, 3, 1)")
  47. assert_equal Sass::Script::Color.new([1, 2, 3, 1]), eval("rgba(1, 2, 3, 1)")
  48. assert_equal "#010203", resolve("rgba(1, 2, 3, 1)")
  49. assert_equal "white", resolve("rgba(255, 255, 255, 1)")
  50. end
  51. def test_rgba_color_math
  52. assert_equal "rgba(50, 50, 100, 0.35)", resolve("rgba(1, 1, 2, 0.35) * rgba(50, 50, 50, 0.35)")
  53. assert_equal "rgba(52, 52, 52, 0.25)", resolve("rgba(2, 2, 2, 0.25) + rgba(50, 50, 50, 0.25)")
  54. assert_raise_message(Sass::SyntaxError, "Alpha channels must be equal: rgba(1, 2, 3, 0.15) + rgba(50, 50, 50, 0.75)") do
  55. resolve("rgba(1, 2, 3, 0.15) + rgba(50, 50, 50, 0.75)")
  56. end
  57. assert_raise_message(Sass::SyntaxError, "Alpha channels must be equal: #123456 * rgba(50, 50, 50, 0.75)") do
  58. resolve("#123456 * rgba(50, 50, 50, 0.75)")
  59. end
  60. assert_raise_message(Sass::SyntaxError, "Alpha channels must be equal: rgba(50, 50, 50, 0.75) / #123456") do
  61. resolve("rgba(50, 50, 50, 0.75) / #123456")
  62. end
  63. end
  64. def test_rgba_number_math
  65. assert_equal "rgba(49, 49, 49, 0.75)", resolve("rgba(50, 50, 50, 0.75) - 1")
  66. assert_equal "rgba(100, 100, 100, 0.75)", resolve("rgba(50, 50, 50, 0.75) * 2")
  67. end
  68. def test_rgba_rounding
  69. assert_equal "rgba(10, 1, 0, 0.12346)", resolve("rgba(10.0, 1.23456789, 0.0, 0.1234567)")
  70. end
  71. def test_compressed_colors
  72. assert_equal "#123456", resolve("#123456", :style => :compressed)
  73. assert_equal "rgba(1,2,3,0.5)", resolve("rgba(1, 2, 3, 0.5)", :style => :compressed)
  74. assert_equal "#123", resolve("#112233", :style => :compressed)
  75. assert_equal "#000", resolve("black", :style => :compressed)
  76. assert_equal "red", resolve("#f00", :style => :compressed)
  77. assert_equal "blue", resolve("#00f", :style => :compressed)
  78. assert_equal "navy", resolve("#000080", :style => :compressed)
  79. assert_equal "navy #fff", resolve("#000080 white", :style => :compressed)
  80. assert_equal "This color is #fff", resolve('"This color is #{ white }"', :style => :compressed)
  81. end
  82. def test_compressed_comma
  83. # assert_equal "foo,bar,baz", resolve("foo, bar, baz", :style => :compressed)
  84. # assert_equal "foo,#baf,baz", resolve("foo, #baf, baz", :style => :compressed)
  85. assert_equal "foo,#baf,red", resolve("foo, #baf, #f00", :style => :compressed)
  86. end
  87. def test_implicit_strings
  88. assert_equal Sass::Script::String.new("foo"), eval("foo")
  89. assert_equal Sass::Script::String.new("foo/bar"), eval("foo/bar")
  90. end
  91. def test_basic_interpolation
  92. assert_equal "foo3bar", resolve("foo\#{1 + 2}bar")
  93. assert_equal "foo3 bar", resolve("foo\#{1 + 2} bar")
  94. assert_equal "foo 3bar", resolve("foo \#{1 + 2}bar")
  95. assert_equal "foo 3 bar", resolve("foo \#{1 + 2} bar")
  96. assert_equal "foo 35 bar", resolve("foo \#{1 + 2}\#{2 + 3} bar")
  97. assert_equal "foo 3 5 bar", resolve("foo \#{1 + 2} \#{2 + 3} bar")
  98. assert_equal "3bar", resolve("\#{1 + 2}bar")
  99. assert_equal "foo3", resolve("foo\#{1 + 2}")
  100. assert_equal "3", resolve("\#{1 + 2}")
  101. end
  102. def test_interpolation_in_function
  103. assert_equal 'flabnabbit(1foo)', resolve('flabnabbit(#{1 + "foo"})')
  104. assert_equal 'flabnabbit(foo 1foobaz)', resolve('flabnabbit(foo #{1 + "foo"}baz)')
  105. assert_equal('flabnabbit(foo 1foo2bar baz)',
  106. resolve('flabnabbit(foo #{1 + "foo"}#{2 + "bar"} baz)'))
  107. end
  108. def test_interpolation_near_operators
  109. assert_equal '3 , 7', resolve('#{1 + 2} , #{3 + 4}')
  110. assert_equal '3, 7', resolve('#{1 + 2}, #{3 + 4}')
  111. assert_equal '3 ,7', resolve('#{1 + 2} ,#{3 + 4}')
  112. assert_equal '3,7', resolve('#{1 + 2},#{3 + 4}')
  113. assert_equal '3, 7, 11', resolve('#{1 + 2}, #{3 + 4}, #{5 + 6}')
  114. assert_equal '3, 7, 11', resolve('3, #{3 + 4}, 11')
  115. assert_equal '3, 7, 11', resolve('3, 7, #{5 + 6}')
  116. assert_equal '3 / 7', resolve('3 / #{3 + 4}')
  117. assert_equal '3 /7', resolve('3 /#{3 + 4}')
  118. assert_equal '3/ 7', resolve('3/ #{3 + 4}')
  119. assert_equal '3/7', resolve('3/#{3 + 4}')
  120. assert_equal '3 * 7', resolve('#{1 + 2} * 7')
  121. assert_equal '3* 7', resolve('#{1 + 2}* 7')
  122. assert_equal '3 *7', resolve('#{1 + 2} *7')
  123. assert_equal '3*7', resolve('#{1 + 2}*7')
  124. assert_equal '-3', resolve('-#{1 + 2}')
  125. assert_equal '- 3', resolve('- #{1 + 2}')
  126. assert_equal '5 + 3 * 7', resolve('5 + #{1 + 2} * #{3 + 4}')
  127. assert_equal '5 +3 * 7', resolve('5 +#{1 + 2} * #{3 + 4}')
  128. assert_equal '5+3 * 7', resolve('5+#{1 + 2} * #{3 + 4}')
  129. assert_equal '3 * 7 + 5', resolve('#{1 + 2} * #{3 + 4} + 5')
  130. assert_equal '3 * 7+ 5', resolve('#{1 + 2} * #{3 + 4}+ 5')
  131. assert_equal '3 * 7+5', resolve('#{1 + 2} * #{3 + 4}+5')
  132. assert_equal '5/3 + 7', resolve('5 / (#{1 + 2} + #{3 + 4})')
  133. assert_equal '5/3 + 7', resolve('5 /(#{1 + 2} + #{3 + 4})')
  134. assert_equal '5/3 + 7', resolve('5 /( #{1 + 2} + #{3 + 4} )')
  135. assert_equal '3 + 7/5', resolve('(#{1 + 2} + #{3 + 4}) / 5')
  136. assert_equal '3 + 7/5', resolve('(#{1 + 2} + #{3 + 4})/ 5')
  137. assert_equal '3 + 7/5', resolve('( #{1 + 2} + #{3 + 4} )/ 5')
  138. assert_equal '3 + 5', resolve('#{1 + 2} + 2 + 3')
  139. assert_equal '3 +5', resolve('#{1 + 2} +2 + 3')
  140. end
  141. def test_string_interpolation
  142. assert_equal "foo bar, baz bang", resolve('"foo #{"bar"}, #{"baz"} bang"')
  143. assert_equal "foo bar baz bang", resolve('"foo #{"#{"ba" + "r"} baz"} bang"')
  144. assert_equal 'foo #{bar baz} bang', resolve('"foo \#{#{"ba" + "r"} baz} bang"')
  145. assert_equal 'foo #{baz bang', resolve('"foo #{"\#{" + "baz"} bang"')
  146. assert_equal "foo2bar", resolve('\'foo#{1 + 1}bar\'')
  147. assert_equal "foo2bar", resolve('"foo#{1 + 1}bar"')
  148. assert_equal "foo1bar5baz4bang", resolve('\'foo#{1 + "bar#{2 + 3}baz" + 4}bang\'')
  149. end
  150. def test_rule_interpolation
  151. assert_equal(<<CSS, render(<<SASS))
  152. foo bar baz bang {
  153. a: b; }
  154. CSS
  155. foo \#{"\#{"ba" + "r"} baz"} bang
  156. a: b
  157. SASS
  158. assert_equal(<<CSS, render(<<SASS))
  159. foo [bar="\#{bar baz}"] bang {
  160. a: b; }
  161. CSS
  162. foo [bar="\\\#{\#{"ba" + "r"} baz}"] bang
  163. a: b
  164. SASS
  165. assert_equal(<<CSS, render(<<SASS))
  166. foo [bar="\#{baz"] bang {
  167. a: b; }
  168. CSS
  169. foo [bar="\#{"\\\#{" + "baz"}"] bang
  170. a: b
  171. SASS
  172. end
  173. def test_inaccessible_functions
  174. assert_equal "send(to_s)", resolve("send(to_s)", :line => 2)
  175. assert_equal "public_instance_methods()", resolve("public_instance_methods()")
  176. end
  177. def test_adding_functions_directly_to_functions_module
  178. assert !Functions.callable?('nonexistant')
  179. Functions.class_eval { def nonexistant; end }
  180. assert Functions.callable?('nonexistant')
  181. Functions.send :remove_method, :nonexistant
  182. end
  183. def test_default_functions
  184. assert_equal "url(12)", resolve("url(12)")
  185. assert_equal 'blam("foo")', resolve('blam("foo")')
  186. end
  187. def test_function_results_have_options
  188. assert_equal "Options defined!", resolve("assert_options(abs(1))")
  189. assert_equal "Options defined!", resolve("assert_options(round(1.2))")
  190. end
  191. def test_funcall_requires_no_whitespace_before_lparen
  192. assert_equal "no-repeat 15px", resolve("no-repeat (7px + 8px)")
  193. assert_equal "no-repeat(15px)", resolve("no-repeat(7px + 8px)")
  194. end
  195. def test_dynamic_url
  196. assert_equal "url(foo-bar)", resolve("url($foo)", {}, env('foo' => Sass::Script::String.new("foo-bar")))
  197. assert_equal "url(foo-bar baz)", resolve("url($foo $bar)", {}, env('foo' => Sass::Script::String.new("foo-bar"), 'bar' => Sass::Script::String.new("baz")))
  198. assert_equal "url(foo baz)", resolve("url(foo $bar)", {}, env('bar' => Sass::Script::String.new("baz")))
  199. assert_equal "url(foo bar)", resolve("url(foo bar)")
  200. end
  201. def test_url_with_interpolation
  202. assert_equal "url(http://sass-lang.com/images/foo-bar)", resolve("url(http://sass-lang.com/images/\#{foo-bar})")
  203. assert_equal 'url("http://sass-lang.com/images/foo-bar")', resolve("url('http://sass-lang.com/images/\#{foo-bar}')")
  204. assert_equal 'url("http://sass-lang.com/images/foo-bar")', resolve('url("http://sass-lang.com/images/#{foo-bar}")')
  205. assert_unquoted "url(http://sass-lang.com/images/\#{foo-bar})"
  206. end
  207. def test_hyphenated_variables
  208. assert_equal("a-b", resolve("$a-b", {}, env("a-b" => Sass::Script::String.new("a-b"))))
  209. end
  210. def test_ruby_equality
  211. assert_equal eval('"foo"'), eval('"foo"')
  212. assert_equal eval('1'), eval('1.0')
  213. assert_equal eval('1 2 3.0'), eval('1 2 3')
  214. assert_equal eval('1, 2, 3.0'), eval('1, 2, 3')
  215. assert_equal eval('(1 2), (3, 4), (5 6)'), eval('(1 2), (3, 4), (5 6)')
  216. assert_not_equal eval('1, 2, 3'), eval('1 2 3')
  217. assert_not_equal eval('1'), eval('"1"')
  218. end
  219. def test_booleans
  220. assert_equal "true", resolve("true")
  221. assert_equal "false", resolve("false")
  222. end
  223. def test_null
  224. assert_equal "", resolve("null")
  225. end
  226. def test_boolean_ops
  227. assert_equal "true", resolve("true and true")
  228. assert_equal "true", resolve("false or true")
  229. assert_equal "true", resolve("true or false")
  230. assert_equal "true", resolve("true or true")
  231. assert_equal "false", resolve("false or false")
  232. assert_equal "false", resolve("false and true")
  233. assert_equal "false", resolve("true and false")
  234. assert_equal "false", resolve("false and false")
  235. assert_equal "true", resolve("not false")
  236. assert_equal "false", resolve("not true")
  237. assert_equal "true", resolve("not not true")
  238. assert_equal "1", resolve("false or 1")
  239. assert_equal "false", resolve("false and 1")
  240. assert_equal "2", resolve("2 or 3")
  241. assert_equal "3", resolve("2 and 3")
  242. assert_equal "true", resolve("null or true")
  243. assert_equal "true", resolve("true or null")
  244. assert_equal "", resolve("null or null")
  245. assert_equal "", resolve("null and true")
  246. assert_equal "", resolve("true and null")
  247. assert_equal "", resolve("null and null")
  248. assert_equal "true", resolve("not null")
  249. assert_equal "1", resolve("null or 1")
  250. assert_equal "", resolve("null and 1")
  251. end
  252. def test_arithmetic_ops
  253. assert_equal "2", resolve("1 + 1")
  254. assert_equal "0", resolve("1 - 1")
  255. assert_equal "8", resolve("2 * 4")
  256. assert_equal "0.5", resolve("(2 / 4)")
  257. assert_equal "2", resolve("(4 / 2)")
  258. assert_equal "-1", resolve("-1")
  259. end
  260. def test_string_ops
  261. assert_equal '"foo" "bar"', resolve('"foo" "bar"')
  262. assert_equal "true 1", resolve('true 1')
  263. assert_equal '"foo", "bar"', resolve("'foo' , 'bar'")
  264. assert_equal "true, 1", resolve('true , 1')
  265. assert_equal "foobar", resolve('"foo" + "bar"')
  266. assert_equal "true1", resolve('true + 1')
  267. assert_equal '"foo"-"bar"', resolve("'foo' - 'bar'")
  268. assert_equal "true-1", resolve('true - 1')
  269. assert_equal '"foo"/"bar"', resolve('"foo" / "bar"')
  270. assert_equal "true/1", resolve('true / 1')
  271. assert_equal '-"bar"', resolve("- 'bar'")
  272. assert_equal "-true", resolve('- true')
  273. assert_equal '/"bar"', resolve('/ "bar"')
  274. assert_equal "/true", resolve('/ true')
  275. end
  276. def test_relational_ops
  277. assert_equal "false", resolve("1 > 2")
  278. assert_equal "false", resolve("2 > 2")
  279. assert_equal "true", resolve("3 > 2")
  280. assert_equal "false", resolve("1 >= 2")
  281. assert_equal "true", resolve("2 >= 2")
  282. assert_equal "true", resolve("3 >= 2")
  283. assert_equal "true", resolve("1 < 2")
  284. assert_equal "false", resolve("2 < 2")
  285. assert_equal "false", resolve("3 < 2")
  286. assert_equal "true", resolve("1 <= 2")
  287. assert_equal "true", resolve("2 <= 2")
  288. assert_equal "false", resolve("3 <= 2")
  289. end
  290. def test_null_ops
  291. assert_raise_message(Sass::SyntaxError,
  292. 'Invalid null operation: "null plus 1".') {eval("null + 1")}
  293. assert_raise_message(Sass::SyntaxError,
  294. 'Invalid null operation: "null minus 1".') {eval("null - 1")}
  295. assert_raise_message(Sass::SyntaxError,
  296. 'Invalid null operation: "null times 1".') {eval("null * 1")}
  297. assert_raise_message(Sass::SyntaxError,
  298. 'Invalid null operation: "null div 1".') {eval("null / 1")}
  299. assert_raise_message(Sass::SyntaxError,
  300. 'Invalid null operation: "null mod 1".') {eval("null % 1")}
  301. assert_raise_message(Sass::SyntaxError,
  302. 'Invalid null operation: "1 plus null".') {eval("1 + null")}
  303. assert_raise_message(Sass::SyntaxError,
  304. 'Invalid null operation: "1 minus null".') {eval("1 - null")}
  305. assert_raise_message(Sass::SyntaxError,
  306. 'Invalid null operation: "1 times null".') {eval("1 * null")}
  307. assert_raise_message(Sass::SyntaxError,
  308. 'Invalid null operation: "1 div null".') {eval("1 / null")}
  309. assert_raise_message(Sass::SyntaxError,
  310. 'Invalid null operation: "1 mod null".') {eval("1 % null")}
  311. assert_raise_message(Sass::SyntaxError,
  312. 'Invalid null operation: "1 gt null".') {eval("1 > null")}
  313. assert_raise_message(Sass::SyntaxError,
  314. 'Invalid null operation: "null lt 1".') {eval("null < 1")}
  315. assert_raise_message(Sass::SyntaxError,
  316. 'Invalid null operation: "null plus null".') {eval("null + null")}
  317. assert_raise_message(Sass::SyntaxError,
  318. 'Invalid null operation: ""foo" plus null".') {eval("foo + null")}
  319. end
  320. def test_equals
  321. assert_equal("true", resolve('"foo" == $foo', {},
  322. env("foo" => Sass::Script::String.new("foo"))))
  323. assert_equal "true", resolve("1 == 1.0")
  324. assert_equal "true", resolve("false != true")
  325. assert_equal "false", resolve("1em == 1px")
  326. assert_equal "false", resolve("12 != 12")
  327. assert_equal "true", resolve("(foo bar baz) == (foo bar baz)")
  328. assert_equal "true", resolve("(foo, bar, baz) == (foo, bar, baz)")
  329. assert_equal "true", resolve('((1 2), (3, 4), (5 6)) == ((1 2), (3, 4), (5 6))')
  330. assert_equal "true", resolve('((1 2), (3 4)) == (1 2, 3 4)')
  331. assert_equal "false", resolve('((1 2) 3) == (1 2 3)')
  332. assert_equal "false", resolve('(1 (2 3)) == (1 2 3)')
  333. assert_equal "false", resolve('((1, 2) (3, 4)) == (1, 2 3, 4)')
  334. assert_equal "false", resolve('(1 2 3) == (1, 2, 3)')
  335. assert_equal "true", resolve('null == null')
  336. assert_equal "false", resolve('"null" == null')
  337. assert_equal "false", resolve('0 == null')
  338. assert_equal "false", resolve('() == null')
  339. assert_equal "false", resolve('null != null')
  340. assert_equal "true", resolve('"null" != null')
  341. assert_equal "true", resolve('0 != null')
  342. assert_equal "true", resolve('() != null')
  343. end
  344. def test_operation_precedence
  345. assert_equal "false true", resolve("true and false false or true")
  346. assert_equal "true", resolve("false and true or true and true")
  347. assert_equal "true", resolve("1 == 2 or 3 == 3")
  348. assert_equal "true", resolve("1 < 2 == 3 >= 3")
  349. assert_equal "true", resolve("1 + 3 > 4 - 2")
  350. assert_equal "11", resolve("1 + 2 * 3 + 4")
  351. end
  352. def test_functions
  353. assert_equal "#80ff80", resolve("hsl(120, 100%, 75%)")
  354. assert_equal "#81ff81", resolve("hsl(120, 100%, 75%) + #010001")
  355. end
  356. def test_operator_unit_conversion
  357. assert_equal "1.1cm", resolve("1cm + 1mm")
  358. assert_equal "2in", resolve("1in + 96px")
  359. assert_equal "true", resolve("2mm < 1cm")
  360. assert_equal "true", resolve("10mm == 1cm")
  361. assert_equal "true", resolve("1 == 1cm")
  362. assert_equal "true", resolve("1.1cm == 11mm")
  363. end
  364. def test_operations_have_options
  365. assert_equal "Options defined!", resolve("assert_options(1 + 1)")
  366. assert_equal "Options defined!", resolve("assert_options('bar' + 'baz')")
  367. end
  368. def test_slash_compiles_literally_when_left_alone
  369. assert_equal "1px/2px", resolve("1px/2px")
  370. assert_equal "1px/2px/3px/4px", resolve("1px/2px/3px/4px")
  371. assert_equal "1px/2px redpx bluepx", resolve("1px/2px redpx bluepx")
  372. assert_equal "foo 1px/2px/3px bar", resolve("foo 1px/2px/3px bar")
  373. end
  374. def test_slash_divides_with_parens
  375. assert_equal "0.5", resolve("(1px/2px)")
  376. assert_equal "0.5", resolve("(1px)/2px")
  377. assert_equal "0.5", resolve("1px/(2px)")
  378. end
  379. def test_slash_divides_with_other_arithmetic
  380. assert_equal "0.5px", resolve("1px*1px/2px")
  381. assert_equal "0.5px", resolve("1px/2px*1px")
  382. assert_equal "0.5", resolve("0+1px/2px")
  383. assert_equal "0.5", resolve("1px/2px+0")
  384. end
  385. def test_slash_divides_with_variable
  386. assert_equal "0.5", resolve("$var/2px", {}, env("var" => eval("1px")))
  387. assert_equal "0.5", resolve("1px/$var", {}, env("var" => eval("2px")))
  388. assert_equal "0.5", resolve("$var", {}, env("var" => eval("1px/2px")))
  389. end
  390. def test_colors_with_wrong_number_of_digits
  391. assert_raise_message(Sass::SyntaxError,
  392. "Colors must have either three or six digits: '#0'") {eval("#0")}
  393. assert_raise_message(Sass::SyntaxError,
  394. "Colors must have either three or six digits: '#12'") {eval("#12")}
  395. assert_raise_message(Sass::SyntaxError,
  396. "Colors must have either three or six digits: '#abcd'") {eval("#abcd")}
  397. assert_raise_message(Sass::SyntaxError,
  398. "Colors must have either three or six digits: '#abcdE'") {eval("#abcdE")}
  399. assert_raise_message(Sass::SyntaxError,
  400. "Colors must have either three or six digits: '#abcdEFA'") {eval("#abcdEFA")}
  401. end
  402. def test_case_insensitive_color_names
  403. assert_equal "blue", resolve("BLUE")
  404. assert_equal "red", resolve("rEd")
  405. assert_equal "#7f4000", resolve("mix(GrEeN, ReD)")
  406. end
  407. def test_empty_list
  408. assert_equal "1 2 3", resolve("1 2 () 3")
  409. assert_equal "1 2 3", resolve("1 2 3 ()")
  410. assert_equal "1 2 3", resolve("() 1 2 3")
  411. assert_raise_message(Sass::SyntaxError, "() isn't a valid CSS value.") {resolve("()")}
  412. assert_raise_message(Sass::SyntaxError, "() isn't a valid CSS value.") {resolve("nth(append((), ()), 1)")}
  413. end
  414. def test_list_with_nulls
  415. assert_equal "1, 2, 3", resolve("1, 2, null, 3")
  416. assert_equal "1 2 3", resolve("1 2 null 3")
  417. assert_equal "1, 2, 3", resolve("1, 2, 3, null")
  418. assert_equal "1 2 3", resolve("1 2 3 null")
  419. assert_equal "1, 2, 3", resolve("null, 1, 2, 3")
  420. assert_equal "1 2 3", resolve("null 1 2 3")
  421. end
  422. def test_deep_argument_error_not_unwrapped
  423. # JRuby (as of 1.6.7.2) offers no way of distinguishing between
  424. # argument errors caused by programming errors in a function and
  425. # argument errors explicitly thrown within that function.
  426. return if RUBY_PLATFORM =~ /java/
  427. # Don't validate the message; it's different on Rubinius.
  428. assert_raise(ArgumentError) {resolve("arg-error()")}
  429. end
  430. def test_shallow_argument_error_unwrapped
  431. assert_raise_message(Sass::SyntaxError, "wrong number of arguments (1 for 0) for `arg-error'") {resolve("arg-error(1)")}
  432. end
  433. def test_boolean_ops_short_circuit
  434. assert_equal "false", resolve("$ie and $ie <= 7", {}, env('ie' => Sass::Script::Bool.new(false)))
  435. assert_equal "true", resolve("$ie or $undef", {}, env('ie' => Sass::Script::Bool.new(true)))
  436. end
  437. # Regression Tests
  438. def test_funcall_has_higher_precedence_than_color_name
  439. assert_equal "teal(12)", resolve("teal(12)")
  440. assert_equal "tealbang(12)", resolve("tealbang(12)")
  441. assert_equal "teal-bang(12)", resolve("teal-bang(12)")
  442. assert_equal "teal\\+bang(12)", resolve("teal\\+bang(12)")
  443. end
  444. def test_interpolation_after_hash
  445. assert_equal "#2", resolve('"##{1 + 1}"')
  446. end
  447. def test_misplaced_comma_in_funcall
  448. assert_raise_message(Sass::SyntaxError,
  449. 'Invalid CSS after "foo(bar, ": expected function argument, was ")"') {eval('foo(bar, )')}
  450. end
  451. def test_color_prefixed_identifier
  452. assert_equal "tealbang", resolve("tealbang")
  453. assert_equal "teal-bang", resolve("teal-bang")
  454. end
  455. def test_op_prefixed_identifier
  456. assert_equal "notbang", resolve("notbang")
  457. assert_equal "not-bang", resolve("not-bang")
  458. assert_equal "or-bang", resolve("or-bang")
  459. assert_equal "and-bang", resolve("and-bang")
  460. end
  461. private
  462. def resolve(str, opts = {}, environment = env)
  463. munge_filename opts
  464. val = eval(str, opts, environment)
  465. assert_kind_of Sass::Script::Literal, val
  466. val.is_a?(Sass::Script::String) ? val.value : val.to_s
  467. end
  468. def assert_unquoted(str, opts = {}, environment = env)
  469. munge_filename opts
  470. val = eval(str, opts, environment)
  471. assert_kind_of Sass::Script::String, val
  472. assert_equal :identifier, val.type
  473. end
  474. def assert_quoted(str, opts = {}, environment = env)
  475. munge_filename opts
  476. val = eval(str, opts, environment)
  477. assert_kind_of Sass::Script::String, val
  478. assert_equal :string, val.type
  479. end
  480. def eval(str, opts = {}, environment = env)
  481. munge_filename opts
  482. Sass::Script.parse(str, opts.delete(:line) || 1,
  483. opts.delete(:offset) || 0, opts).
  484. perform(Sass::Environment.new(environment, opts))
  485. end
  486. def render(sass, options = {})
  487. munge_filename options
  488. Sass::Engine.new(sass, options).render
  489. end
  490. def env(hash = {})
  491. env = Sass::Environment.new
  492. hash.each {|k, v| env.set_var(k, v)}
  493. env
  494. end
  495. def test_number_printing
  496. assert_equal "1", eval("1")
  497. assert_equal "1", eval("1.0")
  498. assert_equal "1.121", eval("1.1214")
  499. assert_equal "1.122", eval("1.1215")
  500. assert_equal "Infinity", eval("1.0/0.0")
  501. assert_equal "-Infinity", eval("-1.0/0.0")
  502. assert_equal "NaN", eval("0.0/0.0")
  503. end
  504. end