PageRenderTime 39ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ohimmelreich/asalia-training
Ruby | 1130 lines | 1004 code | 99 blank | 27 comment | 97 complexity | fa1c7c6218a4f6af1ecbd0cc238691bd MD5 | raw file
  1. #!/usr/bin/env ruby
  2. require 'test/unit'
  3. require File.dirname(__FILE__) + '/../test_helper'
  4. require 'sass/script'
  5. module Sass::Script::Functions
  6. def no_kw_args
  7. Sass::Script::String.new("no-kw-args")
  8. end
  9. def only_var_args(*args)
  10. Sass::Script::String.new("only-var-args("+args.map{|a| a.plus(Sass::Script::Number.new(1)).to_s }.join(", ")+")")
  11. end
  12. declare :only_var_args, [], :var_args => true
  13. def only_kw_args(kwargs)
  14. Sass::Script::String.new("only-kw-args(" + kwargs.keys.map {|a| a.to_s}.sort.join(", ") + ")")
  15. end
  16. declare :only_kw_args, [], :var_kwargs => true
  17. end
  18. module Sass::Script::Functions::UserFunctions
  19. def call_options_on_new_literal
  20. str = Sass::Script::String.new("foo")
  21. str.options[:foo]
  22. str
  23. end
  24. def user_defined
  25. Sass::Script::String.new("I'm a user-defined string!")
  26. end
  27. def _preceding_underscore
  28. Sass::Script::String.new("I'm another user-defined string!")
  29. end
  30. end
  31. module Sass::Script::Functions
  32. include Sass::Script::Functions::UserFunctions
  33. end
  34. class SassFunctionTest < Test::Unit::TestCase
  35. # Tests taken from:
  36. # http://www.w3.org/Style/CSS/Test/CSS3/Color/20070927/html4/t040204-hsl-h-rotating-b.htm
  37. # http://www.w3.org/Style/CSS/Test/CSS3/Color/20070927/html4/t040204-hsl-values-b.htm
  38. File.read(File.dirname(__FILE__) + "/data/hsl-rgb.txt").split("\n\n").each do |chunk|
  39. hsls, rgbs = chunk.strip.split("====")
  40. hsls.strip.split("\n").zip(rgbs.strip.split("\n")) do |hsl, rgb|
  41. hsl_method = "test_hsl: #{hsl} = #{rgb}"
  42. unless method_defined?(hsl_method)
  43. define_method(hsl_method) do
  44. assert_equal(evaluate(rgb), evaluate(hsl))
  45. end
  46. end
  47. rgb_to_hsl_method = "test_rgb_to_hsl: #{rgb} = #{hsl}"
  48. unless method_defined?(rgb_to_hsl_method)
  49. define_method(rgb_to_hsl_method) do
  50. rgb_color = perform(rgb)
  51. hsl_color = perform(hsl)
  52. white = hsl_color.lightness == 100
  53. black = hsl_color.lightness == 0
  54. grayscale = white || black || hsl_color.saturation == 0
  55. assert_in_delta(hsl_color.hue, rgb_color.hue, 0.0001,
  56. "Hues should be equal") unless grayscale
  57. assert_in_delta(hsl_color.saturation, rgb_color.saturation, 0.0001,
  58. "Saturations should be equal") unless white || black
  59. assert_in_delta(hsl_color.lightness, rgb_color.lightness, 0.0001,
  60. "Lightnesses should be equal")
  61. end
  62. end
  63. end
  64. end
  65. def test_hsl_kwargs
  66. assert_equal "#33cccc", evaluate("hsl($hue: 180, $saturation: 60%, $lightness: 50%)")
  67. end
  68. def test_hsl_checks_bounds
  69. assert_error_message("Saturation -114 must be between 0% and 100% for `hsl'", "hsl(10, -114, 12)");
  70. assert_error_message("Lightness 256% must be between 0% and 100% for `hsl'", "hsl(10, 10, 256%)");
  71. end
  72. def test_hsl_checks_types
  73. assert_error_message("\"foo\" is not a number for `hsl'", "hsl(\"foo\", 10, 12)");
  74. assert_error_message("\"foo\" is not a number for `hsl'", "hsl(10, \"foo\", 12)");
  75. assert_error_message("\"foo\" is not a number for `hsl'", "hsl(10, 10, \"foo\")");
  76. end
  77. def test_hsla
  78. assert_equal "rgba(51, 204, 204, 0.4)", evaluate("hsla(180, 60%, 50%, 0.4)")
  79. assert_equal "#33cccc", evaluate("hsla(180, 60%, 50%, 1)")
  80. assert_equal "rgba(51, 204, 204, 0)", evaluate("hsla(180, 60%, 50%, 0)")
  81. assert_equal "rgba(51, 204, 204, 0.4)", evaluate("hsla($hue: 180, $saturation: 60%, $lightness: 50%, $alpha: 0.4)")
  82. end
  83. def test_hsla_checks_bounds
  84. assert_error_message("Saturation -114 must be between 0% and 100% for `hsla'", "hsla(10, -114, 12, 1)");
  85. assert_error_message("Lightness 256% must be between 0% and 100% for `hsla'", "hsla(10, 10, 256%, 0)");
  86. assert_error_message("Alpha channel -0.1 must be between 0 and 1 for `hsla'", "hsla(10, 10, 10, -0.1)");
  87. assert_error_message("Alpha channel 1.1 must be between 0 and 1 for `hsla'", "hsla(10, 10, 10, 1.1)");
  88. end
  89. def test_hsla_checks_types
  90. assert_error_message("\"foo\" is not a number for `hsla'", "hsla(\"foo\", 10, 12, 0.3)");
  91. assert_error_message("\"foo\" is not a number for `hsla'", "hsla(10, \"foo\", 12, 0)");
  92. assert_error_message("\"foo\" is not a number for `hsla'", "hsla(10, 10, \"foo\", 1)");
  93. assert_error_message("\"foo\" is not a number for `hsla'", "hsla(10, 10, 10, \"foo\")");
  94. end
  95. def test_percentage
  96. assert_equal("50%", evaluate("percentage(.5)"))
  97. assert_equal("100%", evaluate("percentage(1)"))
  98. assert_equal("25%", evaluate("percentage(25px / 100px)"))
  99. assert_equal("50%", evaluate("percentage($value: 0.5)"))
  100. end
  101. def test_percentage_checks_types
  102. assert_error_message("25px is not a unitless number for `percentage'", "percentage(25px)")
  103. assert_error_message("#cccccc is not a unitless number for `percentage'", "percentage(#ccc)")
  104. assert_error_message("\"string\" is not a unitless number for `percentage'", %Q{percentage("string")})
  105. end
  106. def test_round
  107. assert_equal("5", evaluate("round(4.8)"))
  108. assert_equal("5px", evaluate("round(4.8px)"))
  109. assert_equal("5px", evaluate("round(5.49px)"))
  110. assert_equal("5px", evaluate("round($value: 5.49px)"))
  111. assert_error_message("#cccccc is not a number for `round'", "round(#ccc)")
  112. end
  113. def test_floor
  114. assert_equal("4", evaluate("floor(4.8)"))
  115. assert_equal("4px", evaluate("floor(4.8px)"))
  116. assert_equal("4px", evaluate("floor($value: 4.8px)"))
  117. assert_error_message("\"foo\" is not a number for `floor'", "floor(\"foo\")")
  118. end
  119. def test_ceil
  120. assert_equal("5", evaluate("ceil(4.1)"))
  121. assert_equal("5px", evaluate("ceil(4.8px)"))
  122. assert_equal("5px", evaluate("ceil($value: 4.8px)"))
  123. assert_error_message("\"a\" is not a number for `ceil'", "ceil(\"a\")")
  124. end
  125. def test_abs
  126. assert_equal("5", evaluate("abs(-5)"))
  127. assert_equal("5px", evaluate("abs(-5px)"))
  128. assert_equal("5", evaluate("abs(5)"))
  129. assert_equal("5px", evaluate("abs(5px)"))
  130. assert_equal("5px", evaluate("abs($value: 5px)"))
  131. assert_error_message("#aaaaaa is not a number for `abs'", "abs(#aaa)")
  132. end
  133. def test_min
  134. #assert_equal("1", evaluate("min(1, 2, 3)"))
  135. assert_equal("1", evaluate("min(3px, 2px, 1)"))
  136. assert_equal("4em", evaluate("min(4em)"))
  137. assert_equal("10cm", evaluate("min(10cm, 6in)"))
  138. assert_error_message("#aaaaaa is not a number for `min'", "min(#aaa)")
  139. assert_error_message("Incompatible units: 'px' and 'em'.", "min(3em, 4em, 1px)")
  140. end
  141. def test_max
  142. assert_equal("3", evaluate("max(1, 2, 3)"))
  143. assert_equal("3", evaluate("max(3, 2px, 1px)"))
  144. assert_equal("4em", evaluate("max(4em)"))
  145. assert_equal("6in", evaluate("max(10cm, 6in)"))
  146. assert_error_message("#aaaaaa is not a number for `max'", "max(#aaa)")
  147. assert_error_message("Incompatible units: 'px' and 'em'.", "max(3em, 4em, 1px)")
  148. end
  149. def test_rgb
  150. assert_equal("#123456", evaluate("rgb(18, 52, 86)"))
  151. assert_equal("#beaded", evaluate("rgb(190, 173, 237)"))
  152. assert_equal("springgreen", evaluate("rgb(0, 255, 127)"))
  153. assert_equal("springgreen", evaluate("rgb($red: 0, $green: 255, $blue: 127)"))
  154. end
  155. def test_rgb_percent
  156. assert_equal("#123456", evaluate("rgb(7.1%, 20.4%, 34%)"))
  157. assert_equal("#beaded", evaluate("rgb(74.7%, 173, 93%)"))
  158. assert_equal("#beaded", evaluate("rgb(190, 68%, 237)"))
  159. assert_equal("springgreen", evaluate("rgb(0%, 100%, 50%)"))
  160. end
  161. def test_rgb_tests_bounds
  162. assert_error_message("Color value 256 must be between 0 and 255 for `rgb'",
  163. "rgb(256, 1, 1)")
  164. assert_error_message("Color value 256 must be between 0 and 255 for `rgb'",
  165. "rgb(1, 256, 1)")
  166. assert_error_message("Color value 256 must be between 0 and 255 for `rgb'",
  167. "rgb(1, 1, 256)")
  168. assert_error_message("Color value 256 must be between 0 and 255 for `rgb'",
  169. "rgb(1, 256, 257)")
  170. assert_error_message("Color value -1 must be between 0 and 255 for `rgb'",
  171. "rgb(-1, 1, 1)")
  172. end
  173. def test_rgb_test_percent_bounds
  174. assert_error_message("Color value 100.1% must be between 0% and 100% for `rgb'",
  175. "rgb(100.1%, 0, 0)")
  176. assert_error_message("Color value -0.1% must be between 0% and 100% for `rgb'",
  177. "rgb(0, -0.1%, 0)")
  178. assert_error_message("Color value 101% must be between 0% and 100% for `rgb'",
  179. "rgb(0, 0, 101%)")
  180. end
  181. def test_rgb_tests_types
  182. assert_error_message("\"foo\" is not a number for `rgb'", "rgb(\"foo\", 10, 12)");
  183. assert_error_message("\"foo\" is not a number for `rgb'", "rgb(10, \"foo\", 12)");
  184. assert_error_message("\"foo\" is not a number for `rgb'", "rgb(10, 10, \"foo\")");
  185. end
  186. def test_rgba
  187. assert_equal("rgba(18, 52, 86, 0.5)", evaluate("rgba(18, 52, 86, 0.5)"))
  188. assert_equal("#beaded", evaluate("rgba(190, 173, 237, 1)"))
  189. assert_equal("rgba(0, 255, 127, 0)", evaluate("rgba(0, 255, 127, 0)"))
  190. assert_equal("rgba(0, 255, 127, 0)", evaluate("rgba($red: 0, $green: 255, $blue: 127, $alpha: 0)"))
  191. end
  192. def test_rgba_tests_bounds
  193. assert_error_message("Color value 256 must be between 0 and 255 for `rgba'",
  194. "rgba(256, 1, 1, 0.3)")
  195. assert_error_message("Color value 256 must be between 0 and 255 for `rgba'",
  196. "rgba(1, 256, 1, 0.3)")
  197. assert_error_message("Color value 256 must be between 0 and 255 for `rgba'",
  198. "rgba(1, 1, 256, 0.3)")
  199. assert_error_message("Color value 256 must be between 0 and 255 for `rgba'",
  200. "rgba(1, 256, 257, 0.3)")
  201. assert_error_message("Color value -1 must be between 0 and 255 for `rgba'",
  202. "rgba(-1, 1, 1, 0.3)")
  203. assert_error_message("Alpha channel -0.2 must be between 0 and 1 for `rgba'",
  204. "rgba(1, 1, 1, -0.2)")
  205. assert_error_message("Alpha channel 1.2 must be between 0 and 1 for `rgba'",
  206. "rgba(1, 1, 1, 1.2)")
  207. end
  208. def test_rgba_tests_types
  209. assert_error_message("\"foo\" is not a number for `rgba'", "rgba(\"foo\", 10, 12, 0.2)");
  210. assert_error_message("\"foo\" is not a number for `rgba'", "rgba(10, \"foo\", 12, 0.1)");
  211. assert_error_message("\"foo\" is not a number for `rgba'", "rgba(10, 10, \"foo\", 0)");
  212. assert_error_message("\"foo\" is not a number for `rgba'", "rgba(10, 10, 10, \"foo\")");
  213. end
  214. def test_rgba_with_color
  215. assert_equal "rgba(16, 32, 48, 0.5)", evaluate("rgba(#102030, 0.5)")
  216. assert_equal "rgba(0, 0, 255, 0.5)", evaluate("rgba(blue, 0.5)")
  217. assert_equal "rgba(0, 0, 255, 0.5)", evaluate("rgba($color: blue, $alpha: 0.5)")
  218. end
  219. def test_rgba_with_color_tests_types
  220. assert_error_message("\"foo\" is not a color for `rgba'", "rgba(\"foo\", 0.2)");
  221. assert_error_message("\"foo\" is not a number for `rgba'", "rgba(blue, \"foo\")");
  222. end
  223. def test_rgba_tests_num_args
  224. assert_error_message("wrong number of arguments (0 for 4) for `rgba'", "rgba()");
  225. assert_error_message("wrong number of arguments (1 for 4) for `rgba'", "rgba(blue)");
  226. assert_error_message("wrong number of arguments (3 for 4) for `rgba'", "rgba(1, 2, 3)");
  227. assert_error_message("wrong number of arguments (5 for 4) for `rgba'", "rgba(1, 2, 3, 0.4, 5)");
  228. end
  229. def test_red
  230. assert_equal("18", evaluate("red(#123456)"))
  231. assert_equal("18", evaluate("red($color: #123456)"))
  232. end
  233. def test_red_exception
  234. assert_error_message("12 is not a color for `red'", "red(12)")
  235. end
  236. def test_green
  237. assert_equal("52", evaluate("green(#123456)"))
  238. assert_equal("52", evaluate("green($color: #123456)"))
  239. end
  240. def test_green_exception
  241. assert_error_message("12 is not a color for `green'", "green(12)")
  242. end
  243. def test_blue
  244. assert_equal("86", evaluate("blue(#123456)"))
  245. assert_equal("86", evaluate("blue($color: #123456)"))
  246. end
  247. def test_blue_exception
  248. assert_error_message("12 is not a color for `blue'", "blue(12)")
  249. end
  250. def test_hue
  251. assert_equal("18deg", evaluate("hue(hsl(18, 50%, 20%))"))
  252. assert_equal("18deg", evaluate("hue($color: hsl(18, 50%, 20%))"))
  253. end
  254. def test_hue_exception
  255. assert_error_message("12 is not a color for `hue'", "hue(12)")
  256. end
  257. def test_saturation
  258. assert_equal("52%", evaluate("saturation(hsl(20, 52%, 20%))"))
  259. assert_equal("52%", evaluate("saturation(hsl(20, 52, 20%))"))
  260. assert_equal("52%", evaluate("saturation($color: hsl(20, 52, 20%))"))
  261. end
  262. def test_saturation_exception
  263. assert_error_message("12 is not a color for `saturation'", "saturation(12)")
  264. end
  265. def test_lightness
  266. assert_equal("86%", evaluate("lightness(hsl(120, 50%, 86%))"))
  267. assert_equal("86%", evaluate("lightness(hsl(120, 50%, 86))"))
  268. assert_equal("86%", evaluate("lightness($color: hsl(120, 50%, 86))"))
  269. end
  270. def test_lightness_exception
  271. assert_error_message("12 is not a color for `lightness'", "lightness(12)")
  272. end
  273. def test_alpha
  274. assert_equal("1", evaluate("alpha(#123456)"))
  275. assert_equal("0.34", evaluate("alpha(rgba(0, 1, 2, 0.34))"))
  276. assert_equal("0", evaluate("alpha(hsla(0, 1, 2, 0))"))
  277. assert_equal("0", evaluate("alpha($color: hsla(0, 1, 2, 0))"))
  278. end
  279. def test_alpha_exception
  280. assert_error_message("12 is not a color for `alpha'", "alpha(12)")
  281. end
  282. def test_opacity
  283. assert_equal("1", evaluate("opacity(#123456)"))
  284. assert_equal("0.34", evaluate("opacity(rgba(0, 1, 2, 0.34))"))
  285. assert_equal("0", evaluate("opacity(hsla(0, 1, 2, 0))"))
  286. assert_equal("0", evaluate("opacity($color: hsla(0, 1, 2, 0))"))
  287. assert_equal("opacity(20%)", evaluate("opacity(20%)"))
  288. end
  289. def test_opacity_exception
  290. assert_error_message("\"foo\" is not a color for `opacity'", "opacity(foo)")
  291. end
  292. def test_opacify
  293. assert_equal("rgba(0, 0, 0, 0.75)", evaluate("opacify(rgba(0, 0, 0, 0.5), 0.25)"))
  294. assert_equal("rgba(0, 0, 0, 0.3)", evaluate("opacify(rgba(0, 0, 0, 0.2), 0.1)"))
  295. assert_equal("rgba(0, 0, 0, 0.7)", evaluate("fade-in(rgba(0, 0, 0, 0.2), 0.5px)"))
  296. assert_equal("black", evaluate("fade_in(rgba(0, 0, 0, 0.2), 0.8)"))
  297. assert_equal("black", evaluate("opacify(rgba(0, 0, 0, 0.2), 1)"))
  298. assert_equal("rgba(0, 0, 0, 0.2)", evaluate("opacify(rgba(0, 0, 0, 0.2), 0%)"))
  299. assert_equal("rgba(0, 0, 0, 0.2)", evaluate("opacify($color: rgba(0, 0, 0, 0.2), $amount: 0%)"))
  300. assert_equal("rgba(0, 0, 0, 0.2)", evaluate("fade-in($color: rgba(0, 0, 0, 0.2), $amount: 0%)"))
  301. end
  302. def test_opacify_tests_bounds
  303. assert_error_message("Amount -0.001 must be between 0 and 1 for `opacify'",
  304. "opacify(rgba(0, 0, 0, 0.2), -0.001)")
  305. assert_error_message("Amount 1.001 must be between 0 and 1 for `opacify'",
  306. "opacify(rgba(0, 0, 0, 0.2), 1.001)")
  307. end
  308. def test_opacify_tests_types
  309. assert_error_message("\"foo\" is not a color for `opacify'", "opacify(\"foo\", 10%)")
  310. assert_error_message("\"foo\" is not a number for `opacify'", "opacify(#fff, \"foo\")")
  311. end
  312. def test_transparentize
  313. assert_equal("rgba(0, 0, 0, 0.3)", evaluate("transparentize(rgba(0, 0, 0, 0.5), 0.2)"))
  314. assert_equal("rgba(0, 0, 0, 0.1)", evaluate("transparentize(rgba(0, 0, 0, 0.2), 0.1)"))
  315. assert_equal("rgba(0, 0, 0, 0.2)", evaluate("fade-out(rgba(0, 0, 0, 0.5), 0.3px)"))
  316. assert_equal("rgba(0, 0, 0, 0)", evaluate("fade_out(rgba(0, 0, 0, 0.2), 0.2)"))
  317. assert_equal("rgba(0, 0, 0, 0)", evaluate("transparentize(rgba(0, 0, 0, 0.2), 1)"))
  318. assert_equal("rgba(0, 0, 0, 0.2)", evaluate("transparentize(rgba(0, 0, 0, 0.2), 0)"))
  319. assert_equal("rgba(0, 0, 0, 0.2)", evaluate("transparentize($color: rgba(0, 0, 0, 0.2), $amount: 0)"))
  320. assert_equal("rgba(0, 0, 0, 0.2)", evaluate("fade-out($color: rgba(0, 0, 0, 0.2), $amount: 0)"))
  321. end
  322. def test_transparentize_tests_bounds
  323. assert_error_message("Amount -0.001 must be between 0 and 1 for `transparentize'",
  324. "transparentize(rgba(0, 0, 0, 0.2), -0.001)")
  325. assert_error_message("Amount 1.001 must be between 0 and 1 for `transparentize'",
  326. "transparentize(rgba(0, 0, 0, 0.2), 1.001)")
  327. end
  328. def test_transparentize_tests_types
  329. assert_error_message("\"foo\" is not a color for `transparentize'", "transparentize(\"foo\", 10%)")
  330. assert_error_message("\"foo\" is not a number for `transparentize'", "transparentize(#fff, \"foo\")")
  331. end
  332. def test_lighten
  333. assert_equal("#4d4d4d", evaluate("lighten(hsl(0, 0, 0), 30%)"))
  334. assert_equal("#ee0000", evaluate("lighten(#800, 20%)"))
  335. assert_equal("white", evaluate("lighten(#fff, 20%)"))
  336. assert_equal("white", evaluate("lighten(#800, 100%)"))
  337. assert_equal("#880000", evaluate("lighten(#800, 0%)"))
  338. assert_equal("rgba(238, 0, 0, 0.5)", evaluate("lighten(rgba(136, 0, 0, 0.5), 20%)"))
  339. assert_equal("rgba(238, 0, 0, 0.5)", evaluate("lighten($color: rgba(136, 0, 0, 0.5), $amount: 20%)"))
  340. end
  341. def test_lighten_tests_bounds
  342. assert_error_message("Amount -0.001 must be between 0% and 100% for `lighten'",
  343. "lighten(#123, -0.001)")
  344. assert_error_message("Amount 100.001 must be between 0% and 100% for `lighten'",
  345. "lighten(#123, 100.001)")
  346. end
  347. def test_lighten_tests_types
  348. assert_error_message("\"foo\" is not a color for `lighten'", "lighten(\"foo\", 10%)")
  349. assert_error_message("\"foo\" is not a number for `lighten'", "lighten(#fff, \"foo\")")
  350. end
  351. def test_darken
  352. assert_equal("#ff6a00", evaluate("darken(hsl(25, 100, 80), 30%)"))
  353. assert_equal("#220000", evaluate("darken(#800, 20%)"))
  354. assert_equal("black", evaluate("darken(#000, 20%)"))
  355. assert_equal("black", evaluate("darken(#800, 100%)"))
  356. assert_equal("#880000", evaluate("darken(#800, 0%)"))
  357. assert_equal("rgba(34, 0, 0, 0.5)", evaluate("darken(rgba(136, 0, 0, 0.5), 20%)"))
  358. assert_equal("rgba(34, 0, 0, 0.5)", evaluate("darken($color: rgba(136, 0, 0, 0.5), $amount: 20%)"))
  359. end
  360. def test_darken_tests_bounds
  361. assert_error_message("Amount -0.001 must be between 0% and 100% for `darken'",
  362. "darken(#123, -0.001)")
  363. assert_error_message("Amount 100.001 must be between 0% and 100% for `darken'",
  364. "darken(#123, 100.001)")
  365. end
  366. def test_darken_tests_types
  367. assert_error_message("\"foo\" is not a color for `darken'", "darken(\"foo\", 10%)")
  368. assert_error_message("\"foo\" is not a number for `darken'", "darken(#fff, \"foo\")")
  369. end
  370. def test_saturate
  371. assert_equal("#d9f2d9", evaluate("saturate(hsl(120, 30, 90), 20%)"))
  372. assert_equal("#9e3f3f", evaluate("saturate(#855, 20%)"))
  373. assert_equal("black", evaluate("saturate(#000, 20%)"))
  374. assert_equal("white", evaluate("saturate(#fff, 20%)"))
  375. assert_equal("#33ff33", evaluate("saturate(#8a8, 100%)"))
  376. assert_equal("#88aa88", evaluate("saturate(#8a8, 0%)"))
  377. assert_equal("rgba(158, 63, 63, 0.5)", evaluate("saturate(rgba(136, 85, 85, 0.5), 20%)"))
  378. assert_equal("rgba(158, 63, 63, 0.5)", evaluate("saturate($color: rgba(136, 85, 85, 0.5), $amount: 20%)"))
  379. assert_equal("saturate(50%)", evaluate("saturate(50%)"))
  380. end
  381. def test_saturate_tests_bounds
  382. assert_error_message("Amount -0.001 must be between 0% and 100% for `saturate'",
  383. "saturate(#123, -0.001)")
  384. assert_error_message("Amount 100.001 must be between 0% and 100% for `saturate'",
  385. "saturate(#123, 100.001)")
  386. end
  387. def test_saturate_tests_types
  388. assert_error_message("\"foo\" is not a color for `saturate'", "saturate(\"foo\", 10%)")
  389. assert_error_message("\"foo\" is not a number for `saturate'", "saturate(#fff, \"foo\")")
  390. end
  391. def test_desaturate
  392. assert_equal("#e3e8e3", evaluate("desaturate(hsl(120, 30, 90), 20%)"))
  393. assert_equal("#726b6b", evaluate("desaturate(#855, 20%)"))
  394. assert_equal("black", evaluate("desaturate(#000, 20%)"))
  395. assert_equal("white", evaluate("desaturate(#fff, 20%)"))
  396. assert_equal("#999999", evaluate("desaturate(#8a8, 100%)"))
  397. assert_equal("#88aa88", evaluate("desaturate(#8a8, 0%)"))
  398. assert_equal("rgba(114, 107, 107, 0.5)", evaluate("desaturate(rgba(136, 85, 85, 0.5), 20%)"))
  399. assert_equal("rgba(114, 107, 107, 0.5)", evaluate("desaturate($color: rgba(136, 85, 85, 0.5), $amount: 20%)"))
  400. end
  401. def test_desaturate_tests_bounds
  402. assert_error_message("Amount -0.001 must be between 0% and 100% for `desaturate'",
  403. "desaturate(#123, -0.001)")
  404. assert_error_message("Amount 100.001 must be between 0% and 100% for `desaturate'",
  405. "desaturate(#123, 100.001)")
  406. end
  407. def test_desaturate_tests_types
  408. assert_error_message("\"foo\" is not a color for `desaturate'", "desaturate(\"foo\", 10%)")
  409. assert_error_message("\"foo\" is not a number for `desaturate'", "desaturate(#fff, \"foo\")")
  410. end
  411. def test_adjust_hue
  412. assert_equal("#deeded", evaluate("adjust-hue(hsl(120, 30, 90), 60deg)"))
  413. assert_equal("#ededde", evaluate("adjust-hue(hsl(120, 30, 90), -60deg)"))
  414. assert_equal("#886a11", evaluate("adjust-hue(#811, 45deg)"))
  415. assert_equal("black", evaluate("adjust-hue(#000, 45deg)"))
  416. assert_equal("white", evaluate("adjust-hue(#fff, 45deg)"))
  417. assert_equal("#88aa88", evaluate("adjust-hue(#8a8, 360deg)"))
  418. assert_equal("#88aa88", evaluate("adjust-hue(#8a8, 0deg)"))
  419. assert_equal("rgba(136, 106, 17, 0.5)", evaluate("adjust-hue(rgba(136, 17, 17, 0.5), 45deg)"))
  420. assert_equal("rgba(136, 106, 17, 0.5)", evaluate("adjust-hue($color: rgba(136, 17, 17, 0.5), $degrees: 45deg)"))
  421. end
  422. def test_adjust_hue_tests_types
  423. assert_error_message("\"foo\" is not a color for `adjust-hue'", "adjust-hue(\"foo\", 10%)")
  424. assert_error_message("\"foo\" is not a number for `adjust-hue'", "adjust-hue(#fff, \"foo\")")
  425. end
  426. def test_adjust_color
  427. # HSL
  428. assert_equal(evaluate("hsl(180, 30, 90)"),
  429. evaluate("adjust-color(hsl(120, 30, 90), $hue: 60deg)"))
  430. assert_equal(evaluate("hsl(120, 50, 90)"),
  431. evaluate("adjust-color(hsl(120, 30, 90), $saturation: 20%)"))
  432. assert_equal(evaluate("hsl(120, 30, 60)"),
  433. evaluate("adjust-color(hsl(120, 30, 90), $lightness: -30%)"))
  434. # RGB
  435. assert_equal(evaluate("rgb(15, 20, 30)"),
  436. evaluate("adjust-color(rgb(10, 20, 30), $red: 5)"))
  437. assert_equal(evaluate("rgb(10, 15, 30)"),
  438. evaluate("adjust-color(rgb(10, 20, 30), $green: -5)"))
  439. assert_equal(evaluate("rgb(10, 20, 40)"),
  440. evaluate("adjust-color(rgb(10, 20, 30), $blue: 10)"))
  441. # Alpha
  442. assert_equal(evaluate("hsla(120, 30, 90, 0.65)"),
  443. evaluate("adjust-color(hsl(120, 30, 90), $alpha: -0.35)"))
  444. assert_equal(evaluate("rgba(10, 20, 30, 0.9)"),
  445. evaluate("adjust-color(rgba(10, 20, 30, 0.4), $alpha: 0.5)"))
  446. # HSL composability
  447. assert_equal(evaluate("hsl(180, 20, 90)"),
  448. evaluate("adjust-color(hsl(120, 30, 90), $hue: 60deg, $saturation: -10%)"))
  449. assert_equal(evaluate("hsl(180, 20, 95)"),
  450. evaluate("adjust-color(hsl(120, 30, 90), $hue: 60deg, $saturation: -10%, $lightness: 5%)"))
  451. assert_equal(evaluate("hsla(120, 20, 95, 0.3)"),
  452. evaluate("adjust-color(hsl(120, 30, 90), $saturation: -10%, $lightness: 5%, $alpha: -0.7)"))
  453. # RGB composability
  454. assert_equal(evaluate("rgb(15, 20, 29)"),
  455. evaluate("adjust-color(rgb(10, 20, 30), $red: 5, $blue: -1)"))
  456. assert_equal(evaluate("rgb(15, 45, 29)"),
  457. evaluate("adjust-color(rgb(10, 20, 30), $red: 5, $green: 25, $blue: -1)"))
  458. assert_equal(evaluate("rgba(10, 25, 29, 0.7)"),
  459. evaluate("adjust-color(rgb(10, 20, 30), $green: 5, $blue: -1, $alpha: -0.3)"))
  460. # HSL range restriction
  461. assert_equal(evaluate("hsl(120, 30, 90)"),
  462. evaluate("adjust-color(hsl(120, 30, 90), $hue: 720deg)"))
  463. assert_equal(evaluate("hsl(120, 0, 90)"),
  464. evaluate("adjust-color(hsl(120, 30, 90), $saturation: -90%)"))
  465. assert_equal(evaluate("hsl(120, 30, 100)"),
  466. evaluate("adjust-color(hsl(120, 30, 90), $lightness: 30%)"))
  467. # RGB range restriction
  468. assert_equal(evaluate("rgb(255, 20, 30)"),
  469. evaluate("adjust-color(rgb(10, 20, 30), $red: 250)"))
  470. assert_equal(evaluate("rgb(10, 0, 30)"),
  471. evaluate("adjust-color(rgb(10, 20, 30), $green: -30)"))
  472. assert_equal(evaluate("rgb(10, 20, 0)"),
  473. evaluate("adjust-color(rgb(10, 20, 30), $blue: -40)"))
  474. end
  475. def test_adjust_color_tests_types
  476. assert_error_message("\"foo\" is not a color for `adjust-color'", "adjust-color(foo, $hue: 10)")
  477. # HSL
  478. assert_error_message("$hue: \"foo\" is not a number for `adjust-color'",
  479. "adjust-color(blue, $hue: foo)")
  480. assert_error_message("$saturation: \"foo\" is not a number for `adjust-color'",
  481. "adjust-color(blue, $saturation: foo)")
  482. assert_error_message("$lightness: \"foo\" is not a number for `adjust-color'",
  483. "adjust-color(blue, $lightness: foo)")
  484. # RGB
  485. assert_error_message("$red: \"foo\" is not a number for `adjust-color'",
  486. "adjust-color(blue, $red: foo)")
  487. assert_error_message("$green: \"foo\" is not a number for `adjust-color'",
  488. "adjust-color(blue, $green: foo)")
  489. assert_error_message("$blue: \"foo\" is not a number for `adjust-color'",
  490. "adjust-color(blue, $blue: foo)")
  491. # Alpha
  492. assert_error_message("$alpha: \"foo\" is not a number for `adjust-color'",
  493. "adjust-color(blue, $alpha: foo)")
  494. end
  495. def test_adjust_color_tests_arg_range
  496. # HSL
  497. assert_error_message("$saturation: Amount 101% must be between -100% and 100% for `adjust-color'",
  498. "adjust-color(blue, $saturation: 101%)")
  499. assert_error_message("$saturation: Amount -101% must be between -100% and 100% for `adjust-color'",
  500. "adjust-color(blue, $saturation: -101%)")
  501. assert_error_message("$lightness: Amount 101% must be between -100% and 100% for `adjust-color'",
  502. "adjust-color(blue, $lightness: 101%)")
  503. assert_error_message("$lightness: Amount -101% must be between -100% and 100% for `adjust-color'",
  504. "adjust-color(blue, $lightness: -101%)")
  505. # RGB
  506. assert_error_message("$red: Amount 256 must be between -255 and 255 for `adjust-color'",
  507. "adjust-color(blue, $red: 256)")
  508. assert_error_message("$red: Amount -256 must be between -255 and 255 for `adjust-color'",
  509. "adjust-color(blue, $red: -256)")
  510. assert_error_message("$green: Amount 256 must be between -255 and 255 for `adjust-color'",
  511. "adjust-color(blue, $green: 256)")
  512. assert_error_message("$green: Amount -256 must be between -255 and 255 for `adjust-color'",
  513. "adjust-color(blue, $green: -256)")
  514. assert_error_message("$blue: Amount 256 must be between -255 and 255 for `adjust-color'",
  515. "adjust-color(blue, $blue: 256)")
  516. assert_error_message("$blue: Amount -256 must be between -255 and 255 for `adjust-color'",
  517. "adjust-color(blue, $blue: -256)")
  518. # Alpha
  519. assert_error_message("$alpha: Amount 1.1 must be between -1 and 1 for `adjust-color'",
  520. "adjust-color(blue, $alpha: 1.1)")
  521. assert_error_message("$alpha: Amount -1.1 must be between -1 and 1 for `adjust-color'",
  522. "adjust-color(blue, $alpha: -1.1)")
  523. end
  524. def test_adjust_color_argument_errors
  525. assert_error_message("Unknown argument $hoo (260deg) for `adjust-color'",
  526. "adjust-color(blue, $hoo: 260deg)")
  527. assert_error_message("Cannot specify HSL and RGB values for a color at the same time for `adjust-color'",
  528. "adjust-color(blue, $hue: 120deg, $red: 10)");
  529. assert_error_message("10px is not a keyword argument for `adjust_color'",
  530. "adjust-color(blue, 10px)")
  531. assert_error_message("10px is not a keyword argument for `adjust_color'",
  532. "adjust-color(blue, 10px, 20px)")
  533. assert_error_message("10px is not a keyword argument for `adjust_color'",
  534. "adjust-color(blue, 10px, $hue: 180deg)")
  535. end
  536. def test_scale_color
  537. # HSL
  538. assert_equal(evaluate("hsl(120, 51, 90)"),
  539. evaluate("scale-color(hsl(120, 30, 90), $saturation: 30%)"))
  540. assert_equal(evaluate("hsl(120, 30, 76.5)"),
  541. evaluate("scale-color(hsl(120, 30, 90), $lightness: -15%)"))
  542. # RGB
  543. assert_equal(evaluate("rgb(157, 20, 30)"),
  544. evaluate("scale-color(rgb(10, 20, 30), $red: 60%)"))
  545. assert_equal(evaluate("rgb(10, 38.8, 30)"),
  546. evaluate("scale-color(rgb(10, 20, 30), $green: 8%)"))
  547. assert_equal(evaluate("rgb(10, 20, 20)"),
  548. evaluate("scale-color(rgb(10, 20, 30), $blue: -(1/3)*100%)"))
  549. # Alpha
  550. assert_equal(evaluate("hsla(120, 30, 90, 0.86)"),
  551. evaluate("scale-color(hsl(120, 30, 90), $alpha: -14%)"))
  552. assert_equal(evaluate("rgba(10, 20, 30, 0.82)"),
  553. evaluate("scale-color(rgba(10, 20, 30, 0.8), $alpha: 10%)"))
  554. # HSL composability
  555. assert_equal(evaluate("hsl(120, 51, 76.5)"),
  556. evaluate("scale-color(hsl(120, 30, 90), $saturation: 30%, $lightness: -15%)"))
  557. assert_equal(evaluate("hsla(120, 51, 90, 0.2)"),
  558. evaluate("scale-color(hsl(120, 30, 90), $saturation: 30%, $alpha: -80%)"))
  559. # RGB composability
  560. assert_equal(evaluate("rgb(157, 38.8, 30)"),
  561. evaluate("scale-color(rgb(10, 20, 30), $red: 60%, $green: 8%)"))
  562. assert_equal(evaluate("rgb(157, 38.8, 20)"),
  563. evaluate("scale-color(rgb(10, 20, 30), $red: 60%, $green: 8%, $blue: -(1/3)*100%)"))
  564. assert_equal(evaluate("rgba(10, 38.8, 20, 0.55)"),
  565. evaluate("scale-color(rgba(10, 20, 30, 0.5), $green: 8%, $blue: -(1/3)*100%, $alpha: 10%)"))
  566. # Extremes
  567. assert_equal(evaluate("hsl(120, 100, 90)"),
  568. evaluate("scale-color(hsl(120, 30, 90), $saturation: 100%)"))
  569. assert_equal(evaluate("hsl(120, 30, 90)"),
  570. evaluate("scale-color(hsl(120, 30, 90), $saturation: 0%)"))
  571. assert_equal(evaluate("hsl(120, 0, 90)"),
  572. evaluate("scale-color(hsl(120, 30, 90), $saturation: -100%)"))
  573. end
  574. def test_scale_color_tests_types
  575. assert_error_message("\"foo\" is not a color for `scale-color'", "scale-color(foo, $red: 10%)")
  576. # HSL
  577. assert_error_message("$saturation: \"foo\" is not a number for `scale-color'",
  578. "scale-color(blue, $saturation: foo)")
  579. assert_error_message("$lightness: \"foo\" is not a number for `scale-color'",
  580. "scale-color(blue, $lightness: foo)")
  581. # RGB
  582. assert_error_message("$red: \"foo\" is not a number for `scale-color'",
  583. "scale-color(blue, $red: foo)")
  584. assert_error_message("$green: \"foo\" is not a number for `scale-color'",
  585. "scale-color(blue, $green: foo)")
  586. assert_error_message("$blue: \"foo\" is not a number for `scale-color'",
  587. "scale-color(blue, $blue: foo)")
  588. # Alpha
  589. assert_error_message("$alpha: \"foo\" is not a number for `scale-color'",
  590. "scale-color(blue, $alpha: foo)")
  591. end
  592. def test_scale_color_argument_errors
  593. # Range
  594. assert_error_message("$saturation: Amount 101% must be between -100% and 100% for `scale-color'",
  595. "scale-color(blue, $saturation: 101%)")
  596. assert_error_message("$red: Amount -101% must be between -100% and 100% for `scale-color'",
  597. "scale-color(blue, $red: -101%)")
  598. assert_error_message("$alpha: Amount -101% must be between -100% and 100% for `scale-color'",
  599. "scale-color(blue, $alpha: -101%)")
  600. # Unit
  601. assert_error_message("$saturation: Amount 80 must be a % (e.g. 80%) for `scale-color'",
  602. "scale-color(blue, $saturation: 80)")
  603. assert_error_message("$alpha: Amount 0.5 must be a % (e.g. 0.5%) for `scale-color'",
  604. "scale-color(blue, $alpha: 0.5)")
  605. # Unknown argument
  606. assert_error_message("Unknown argument $hue (80%) for `scale-color'", "scale-color(blue, $hue: 80%)")
  607. # Non-keyword arg
  608. assert_error_message("10px is not a keyword argument for `scale_color'", "scale-color(blue, 10px)")
  609. # HSL/RGB
  610. assert_error_message("Cannot specify HSL and RGB values for a color at the same time for `scale-color'",
  611. "scale-color(blue, $lightness: 10%, $red: 20%)");
  612. end
  613. def test_change_color
  614. # HSL
  615. assert_equal(evaluate("hsl(195, 30, 90)"),
  616. evaluate("change-color(hsl(120, 30, 90), $hue: 195deg)"))
  617. assert_equal(evaluate("hsl(120, 50, 90)"),
  618. evaluate("change-color(hsl(120, 30, 90), $saturation: 50%)"))
  619. assert_equal(evaluate("hsl(120, 30, 40)"),
  620. evaluate("change-color(hsl(120, 30, 90), $lightness: 40%)"))
  621. # RGB
  622. assert_equal(evaluate("rgb(123, 20, 30)"),
  623. evaluate("change-color(rgb(10, 20, 30), $red: 123)"))
  624. assert_equal(evaluate("rgb(10, 234, 30)"),
  625. evaluate("change-color(rgb(10, 20, 30), $green: 234)"))
  626. assert_equal(evaluate("rgb(10, 20, 198)"),
  627. evaluate("change-color(rgb(10, 20, 30), $blue: 198)"))
  628. # Alpha
  629. assert_equal(evaluate("rgba(10, 20, 30, 0.76)"),
  630. evaluate("change-color(rgb(10, 20, 30), $alpha: 0.76)"))
  631. # HSL composability
  632. assert_equal(evaluate("hsl(56, 30, 47)"),
  633. evaluate("change-color(hsl(120, 30, 90), $hue: 56deg, $lightness: 47%)"))
  634. assert_equal(evaluate("hsla(56, 30, 47, 0.9)"),
  635. evaluate("change-color(hsl(120, 30, 90), $hue: 56deg, $lightness: 47%, $alpha: 0.9)"))
  636. end
  637. def test_change_color_tests_types
  638. assert_error_message("\"foo\" is not a color for `change-color'", "change-color(foo, $red: 10%)")
  639. # HSL
  640. assert_error_message("$saturation: \"foo\" is not a number for `change-color'",
  641. "change-color(blue, $saturation: foo)")
  642. assert_error_message("$lightness: \"foo\" is not a number for `change-color'",
  643. "change-color(blue, $lightness: foo)")
  644. # RGB
  645. assert_error_message("$red: \"foo\" is not a number for `change-color'", "change-color(blue, $red: foo)")
  646. assert_error_message("$green: \"foo\" is not a number for `change-color'", "change-color(blue, $green: foo)")
  647. assert_error_message("$blue: \"foo\" is not a number for `change-color'", "change-color(blue, $blue: foo)")
  648. # Alpha
  649. assert_error_message("$alpha: \"foo\" is not a number for `change-color'", "change-color(blue, $alpha: foo)")
  650. end
  651. def test_change_color_argument_errors
  652. # Range
  653. assert_error_message("Saturation 101% must be between 0% and 100% for `change-color'",
  654. "change-color(blue, $saturation: 101%)")
  655. assert_error_message("Lightness 101% must be between 0% and 100% for `change-color'",
  656. "change-color(blue, $lightness: 101%)")
  657. assert_error_message("Red value -1 must be between 0 and 255 for `change-color'",
  658. "change-color(blue, $red: -1)")
  659. assert_error_message("Green value 256 must be between 0 and 255 for `change-color'",
  660. "change-color(blue, $green: 256)")
  661. assert_error_message("Blue value 500 must be between 0 and 255 for `change-color'",
  662. "change-color(blue, $blue: 500)")
  663. # Unknown argument
  664. assert_error_message("Unknown argument $hoo (80%) for `change-color'", "change-color(blue, $hoo: 80%)")
  665. # Non-keyword arg
  666. assert_error_message("10px is not a keyword argument for `change_color'", "change-color(blue, 10px)")
  667. # HSL/RGB
  668. assert_error_message("Cannot specify HSL and RGB values for a color at the same time for `change-color'",
  669. "change-color(blue, $lightness: 10%, $red: 120)");
  670. end
  671. def test_ie_hex_str
  672. assert_equal("#FFAA11CC", evaluate('ie-hex-str(#aa11cc)'))
  673. assert_equal("#FFAA11CC", evaluate('ie-hex-str(#a1c)'))
  674. assert_equal("#FFAA11CC", evaluate('ie-hex-str(#A1c)'))
  675. assert_equal("#80FF0000", evaluate('ie-hex-str(rgba(255, 0, 0, 0.5))'))
  676. end
  677. def test_mix
  678. assert_equal("#7f007f", evaluate("mix(#f00, #00f)"))
  679. assert_equal("#7f7f7f", evaluate("mix(#f00, #0ff)"))
  680. assert_equal("#7f9055", evaluate("mix(#f70, #0aa)"))
  681. assert_equal("#3f00bf", evaluate("mix(#f00, #00f, 25%)"))
  682. assert_equal("rgba(63, 0, 191, 0.75)", evaluate("mix(rgba(255, 0, 0, 0.5), #00f)"))
  683. assert_equal("red", evaluate("mix(#f00, #00f, 100%)"))
  684. assert_equal("blue", evaluate("mix(#f00, #00f, 0%)"))
  685. assert_equal("rgba(255, 0, 0, 0.5)", evaluate("mix(#f00, transparentize(#00f, 1))"))
  686. assert_equal("rgba(0, 0, 255, 0.5)", evaluate("mix(transparentize(#f00, 1), #00f)"))
  687. assert_equal("red", evaluate("mix(#f00, transparentize(#00f, 1), 100%)"))
  688. assert_equal("blue", evaluate("mix(transparentize(#f00, 1), #00f, 0%)"))
  689. assert_equal("rgba(0, 0, 255, 0)", evaluate("mix(#f00, transparentize(#00f, 1), 0%)"))
  690. assert_equal("rgba(255, 0, 0, 0)", evaluate("mix(transparentize(#f00, 1), #00f, 100%)"))
  691. assert_equal("rgba(255, 0, 0, 0)", evaluate("mix($color-1: transparentize(#f00, 1), $color-2: #00f, $weight: 100%)"))
  692. end
  693. def test_mix_tests_types
  694. assert_error_message("\"foo\" is not a color for `mix'", "mix(\"foo\", #f00, 10%)")
  695. assert_error_message("\"foo\" is not a color for `mix'", "mix(#f00, \"foo\", 10%)")
  696. assert_error_message("\"foo\" is not a number for `mix'", "mix(#f00, #baf, \"foo\")")
  697. end
  698. def test_mix_tests_bounds
  699. assert_error_message("Weight -0.001 must be between 0% and 100% for `mix'",
  700. "mix(#123, #456, -0.001)")
  701. assert_error_message("Weight 100.001 must be between 0% and 100% for `mix'",
  702. "mix(#123, #456, 100.001)")
  703. end
  704. def test_grayscale
  705. assert_equal("#bbbbbb", evaluate("grayscale(#abc)"))
  706. assert_equal("gray", evaluate("grayscale(#f00)"))
  707. assert_equal("gray", evaluate("grayscale(#00f)"))
  708. assert_equal("white", evaluate("grayscale(white)"))
  709. assert_equal("black", evaluate("grayscale(black)"))
  710. assert_equal("black", evaluate("grayscale($color: black)"))
  711. assert_equal("grayscale(2)", evaluate("grayscale(2)"))
  712. assert_equal("grayscale(-5px)", evaluate("grayscale(-5px)"))
  713. end
  714. def tets_grayscale_tests_types
  715. assert_error_message("\"foo\" is not a color for `grayscale'", "grayscale(\"foo\")")
  716. end
  717. def test_complement
  718. assert_equal("#ccbbaa", evaluate("complement(#abc)"))
  719. assert_equal("cyan", evaluate("complement(red)"))
  720. assert_equal("red", evaluate("complement(cyan)"))
  721. assert_equal("white", evaluate("complement(white)"))
  722. assert_equal("black", evaluate("complement(black)"))
  723. assert_equal("black", evaluate("complement($color: black)"))
  724. end
  725. def tets_complement_tests_types
  726. assert_error_message("\"foo\" is not a color for `complement'", "complement(\"foo\")")
  727. end
  728. def test_invert
  729. assert_equal("#112233", evaluate("invert(#edc)"))
  730. assert_equal("rgba(245, 235, 225, 0.5)", evaluate("invert(rgba(10, 20, 30, 0.5))"))
  731. assert_equal("invert(20%)", evaluate("invert(20%)"))
  732. end
  733. def test_invert_tests_types
  734. assert_error_message("\"foo\" is not a color for `invert'", "invert(\"foo\")")
  735. end
  736. def test_unquote
  737. assert_equal('foo', evaluate('unquote("foo")'))
  738. assert_equal('foo', evaluate('unquote(foo)'))
  739. assert_equal('foo', evaluate('unquote($string: foo)'))
  740. end
  741. def test_quote
  742. assert_equal('"foo"', evaluate('quote(foo)'))
  743. assert_equal('"foo"', evaluate('quote("foo")'))
  744. assert_equal('"foo"', evaluate('quote($string: "foo")'))
  745. end
  746. def test_quote_tests_type
  747. assert_error_message("#ff0000 is not a string for `quote'", "quote(#f00)")
  748. end
  749. def test_user_defined_function
  750. assert_equal("I'm a user-defined string!", evaluate("user_defined()"))
  751. end
  752. def test_user_defined_function_with_preceding_underscore
  753. assert_equal("I'm another user-defined string!", evaluate("_preceding_underscore()"))
  754. assert_equal("I'm another user-defined string!", evaluate("-preceding-underscore()"))
  755. end
  756. def test_options_on_new_literals_fails
  757. assert_error_message(<<MSG, "call-options-on-new-literal()")
  758. The #options attribute is not set on this Sass::Script::String.
  759. This error is probably occurring because #to_s was called
  760. on this literal within a custom Sass function without first
  761. setting the #option attribute.
  762. MSG
  763. end
  764. def test_type_of
  765. assert_equal("string", evaluate("type-of(\"asdf\")"))
  766. assert_equal("string", evaluate("type-of(asdf)"))
  767. assert_equal("number", evaluate("type-of(1px)"))
  768. assert_equal("bool", evaluate("type-of(true)"))
  769. assert_equal("color", evaluate("type-of(#fff)"))
  770. assert_equal("color", evaluate("type-of($value: #fff)"))
  771. assert_equal("null", evaluate("type-of(null)"))
  772. end
  773. def test_unit
  774. assert_equal(%Q{""}, evaluate("unit(100)"))
  775. assert_equal(%Q{"px"}, evaluate("unit(100px)"))
  776. assert_equal(%Q{"em*px"}, evaluate("unit(10px * 5em)"))
  777. assert_equal(%Q{"em*px"}, evaluate("unit(5em * 10px)"))
  778. assert_equal(%Q{"em/rem"}, evaluate("unit(10px * 5em / 30cm / 1rem)"))
  779. assert_equal(%Q{"em*vh/cm*rem"}, evaluate("unit(10vh * 5em / 30cm / 1rem)"))
  780. assert_equal(%Q{"px"}, evaluate("unit($number: 100px)"))
  781. assert_error_message("#ff0000 is not a number for `unit'", "unit(#f00)")
  782. end
  783. def test_unitless
  784. assert_equal(%Q{true}, evaluate("unitless(100)"))
  785. assert_equal(%Q{false}, evaluate("unitless(100px)"))
  786. assert_equal(%Q{false}, evaluate("unitless($number: 100px)"))
  787. assert_error_message("#ff0000 is not a number for `unitless'", "unitless(#f00)")
  788. end
  789. def test_comparable
  790. assert_equal(%Q{true}, evaluate("comparable(2px, 1px)"))
  791. assert_equal(%Q{true}, evaluate("comparable(10cm, 3mm)"))
  792. assert_equal(%Q{false}, evaluate("comparable(100px, 3em)"))
  793. assert_equal(%Q{false}, evaluate("comparable($number-1: 100px, $number-2: 3em)"))
  794. assert_error_message("#ff0000 is not a number for `comparable'", "comparable(#f00, 1px)")
  795. assert_error_message("#ff0000 is not a number for `comparable'", "comparable(1px, #f00)")
  796. end
  797. def test_length
  798. assert_equal("5", evaluate("length(1 2 3 4 5)"))
  799. assert_equal("4", evaluate("length((foo, bar, baz, bip))"))
  800. assert_equal("3", evaluate("length((foo, bar, baz bip))"))
  801. assert_equal("3", evaluate("length((foo, bar, (baz, bip)))"))
  802. assert_equal("1", evaluate("length(#f00)"))
  803. assert_equal("0", evaluate("length(())"))
  804. assert_equal("4", evaluate("length(1 2 () 3)"))
  805. end
  806. def test_nth
  807. assert_equal("1", evaluate("nth(1 2 3, 1)"))
  808. assert_equal("2", evaluate("nth(1 2 3, 2)"))
  809. assert_equal("3", evaluate("nth((1, 2, 3), 3)"))
  810. assert_equal("foo", evaluate("nth(foo, 1)"))
  811. assert_equal("bar baz", evaluate("nth(foo (bar baz) bang, 2)"))
  812. assert_error_message("List index 0 must be greater than or equal to 1 for `nth'", "nth(foo, 0)")
  813. assert_error_message("List index -10 must be greater than or equal to 1 for `nth'", "nth(foo, -10)")
  814. assert_error_message("List index 1.5 must be an integer for `nth'", "nth(foo, 1.5)")
  815. assert_error_message("List index is 5 but list is only 4 items long for `nth'", "nth(1 2 3 4, 5)")
  816. assert_error_message("List index is 2 but list is only 1 item long for `nth'", "nth(foo, 2)")
  817. assert_error_message("List index is 1 but list has no items for `nth'", "nth((), 1)")
  818. end
  819. def test_join
  820. assert_equal("1 2 3", evaluate("join(1 2, 3)"))
  821. assert_equal("1 2 3", evaluate("join(1, 2 3)"))
  822. assert_equal("1 2 3 4", evaluate("join(1 2, 3 4)"))
  823. assert_equal("true", evaluate("(1 2 3 4) == join(1 2, 3 4)"))
  824. assert_equal("false", evaluate("(1 2 (3 4)) == join(1 2, 3 4)"))
  825. assert_equal("1, 2, 3", evaluate("join((1, 2), 3)"))
  826. assert_equal("1, 2, 3", evaluate("join(1, (2, 3))"))
  827. assert_equal("1, 2, 3, 4", evaluate("join((1, 2), (3, 4))"))
  828. assert_equal("true", evaluate("(1, 2, 3, 4) == join((1, 2), (3, 4))"))
  829. assert_equal("false", evaluate("(1, 2, (3, 4)) == join((1, 2), (3, 4))"))
  830. assert_equal("1 2", evaluate("join(1, 2)"))
  831. assert_equal("1 2 3 4", evaluate("join(1 2, (3, 4))"))
  832. assert_equal("1, 2, 3, 4", evaluate("join((1, 2), 3 4)"))
  833. assert_equal("1 2", evaluate("join(1, 2, auto)"))
  834. assert_equal("1, 2, 3, 4", evaluate("join(1 2, 3 4, comma)"))
  835. assert_equal("1 2 3 4", evaluate("join((1, 2), (3, 4), space)"))
  836. assert_equal("1, 2", evaluate("join(1, 2, comma)"))
  837. assert_equal("1 2", evaluate("join(1 2, ())"))
  838. assert_equal("1, 2", evaluate("join((1, 2), ())"))
  839. assert_equal("true", evaluate("(1 2) == join(1 2, ())"))
  840. assert_equal("true", evaluate("(1, 2) == join((1, 2), ())"))
  841. assert_equal("false", evaluate("(1 2 ()) == join(1 2, ())"))
  842. assert_equal("false", evaluate("(1, 2, ()) == join((1, 2), ())"))
  843. assert_equal("1 2", evaluate("join((), 1 2)"))
  844. assert_equal("1, 2", evaluate("join((), (1, 2))"))
  845. assert_equal("true", evaluate("(1 2) == join((), 1 2)"))
  846. assert_equal("true", evaluate("(1, 2) == join((), (1, 2))"))
  847. assert_equal("false", evaluate("(1 2 ()) == join((), 1 2)"))
  848. assert_equal("false", evaluate("(1, 2, ()) == join((), (1, 2))"))
  849. assert_error_message("Separator name must be space, comma, or auto for `join'", "join(1, 2, baboon)")
  850. end
  851. def test_append
  852. assert_equal("1 2 3", evaluate("append(1 2, 3)"))
  853. assert_equal("1 2 3 4", evaluate("append(1 2, 3 4)"))
  854. assert_equal("false", evaluate("(1 2 3 4) == append(1 2, 3 4)"))
  855. assert_equal("true", evaluate("(1 2 (3 4)) == append(1 2, 3 4)"))
  856. assert_equal("1, 2, 3", evaluate("append((1, 2), 3)"))
  857. assert_equal("1, 2, 3, 4", evaluate("append((1, 2), (3, 4))"))
  858. assert_equal("false", evaluate("(1, 2, 3, 4) == append((1, 2), (3, 4))"))
  859. assert_equal("true", evaluate("(1, 2, (3, 4)) == append((1, 2), (3, 4))"))
  860. assert_equal("1 2", evaluate("append(1, 2)"))
  861. assert_equal("1 2 3, 4", evaluate("append(1 2, (3, 4))"))
  862. assert_equal("true", evaluate("(1 2 (3, 4)) == append(1 2, (3, 4))"))
  863. assert_equal("1, 2, 3 4", evaluate("append((1, 2), 3 4)"))
  864. assert_equal("true", evaluate("(1, 2, 3 4) == append((1, 2), 3 4)"))
  865. assert_equal("1 2", evaluate("append(1, 2, auto)"))
  866. assert_equal("1, 2, 3 4", evaluate("append(1 2, 3 4, comma)"))
  867. assert_equal("1 2 3, 4", evaluate("append((1, 2), (3, 4), space)"))
  868. assert_equal("1, 2", evaluate("append(1, 2, comma)"))
  869. assert_equal("1 2", evaluate("append(1 2, ())"))
  870. assert_equal("1, 2", evaluate("append((1, 2), ())"))
  871. assert_equal("true", evaluate("(1 2 ()) == append(1 2, ())"))
  872. assert_equal("true", evaluate("(1, 2, ()) == append((1, 2), ())"))
  873. assert_equal("1 2", evaluate("append((), 1 2)"))
  874. assert_equal("1, 2", evaluate("append((), (1, 2))"))
  875. assert_equal("false", evaluate("(1 2) == append((), 1 2)"))
  876. assert_equal("true", evaluate("(1 2) == nth(append((), 1 2), 1)"))
  877. assert_error_message("Separator name must be space, comma, or auto for `append'", "append(1, 2, baboon)")
  878. end
  879. def test_zip
  880. assert_equal("1 3 5, 2 4 6", evaluate("zip(1 2, 3 4, 5 6)"))
  881. assert_equal("1 4 7, 2 5 8", evaluate("zip(1 2 3, 4 5 6, 7 8)"))
  882. assert_equal("1 2 3", evaluate("zip(1, 2, 3)"))
  883. end
  884. def test_index
  885. assert_equal("1", evaluate("index(1px solid blue, 1px)"))
  886. assert_equal("2", evaluate("index(1px solid blue, solid)"))
  887. assert_equal("3", evaluate("index(1px solid blue, #00f)"))
  888. assert_equal("1", evaluate("index(1px, 1px)"))
  889. assert_equal("false", evaluate("index(1px solid blue, 1em)"))
  890. assert_equal("false", evaluate("index(1px solid blue, notfound)"))
  891. assert_equal("false", evaluate("index(1px, #00f)"))
  892. end
  893. def test_if
  894. assert_equal("1px", evaluate("if(true, 1px, 2px)"))
  895. assert_equal("2px", evaluate("if(false, 1px, 2px)"))
  896. assert_equal("2px", evaluate("if(null, 1px, 2px)"))
  897. end
  898. def test_counter
  899. assert_equal("counter(foo)", evaluate("counter(foo)"))
  900. assert_equal('counter(item,".")', evaluate('counter(item, ".")'))
  901. assert_equal('counter(item,".")', evaluate('counter(item,".")'))
  902. end
  903. def test_keyword_args_rgb
  904. assert_equal(%Q{white}, evaluate("rgb($red: 255, $green: 255, $blue: 255)"))
  905. end
  906. def test_keyword_args_rgba
  907. assert_equal(%Q{rgba(255, 255, 255, 0.5)}, evaluate("rgba($red: 255, $green: 255, $blue: 255, $alpha: 0.5)"))
  908. assert_equal(%Q{rgba(255, 255, 255, 0.5)}, evaluate("rgba($color: #fff, $alpha: 0.5)"))
  909. end
  910. def test_keyword_args_rgba_with_extra_args
  911. evaluate("rgba($red: 255, $green: 255, $blue: 255, $alpha: 0.5, $extra: error)")
  912. flunk("Expected exception")
  913. rescue Sass::SyntaxError => e
  914. assert_equal("Function rgba doesn't have an argument named $extra", e.message)
  915. end
  916. def test_keyword_args_must_have_signature
  917. evaluate("no-kw-args($fake: value)")
  918. flunk("Expected exception")
  919. rescue Sass::SyntaxError => e
  920. assert_equal("Function no_kw_args doesn't support keyword arguments", e.message)
  921. end
  922. def test_keyword_args_with_missing_argument
  923. evaluate("rgb($red: 255, $green: 255)")
  924. flunk("Expected exception")
  925. rescue Sass::SyntaxError => e
  926. assert_equal("Function rgb requires an argument named $blue", e.message)
  927. end
  928. def test_keyword_args_with_extra_argument
  929. evaluate("rgb($red: 255, $green: 255, $blue: 255, $purple: 255)")
  930. flunk("Expected exception")
  931. rescue Sass::SyntaxError => e
  932. assert_equal("Function rgb doesn't have an argument named $purple", e.message)
  933. end
  934. def test_keyword_args_with_positional_and_keyword_argument
  935. evaluate("rgb(255, 255, 255, $red: 255)")
  936. flunk("Expected exception")
  937. rescue Sass::SyntaxError => e
  938. assert_equal("Function rgb was passed argument $red both by position and by name", e.message)
  939. end
  940. def test_keyword_args_with_keyword_before_positional_argument
  941. evaluate("rgb($red: 255, 255, 255)")
  942. flunk("Expected exception")
  943. rescue Sass::SyntaxError => e
  944. assert_equal("Positional arguments must come before keyword arguments.", e.message)
  945. end
  946. def test_only_var_args
  947. assert_equal "only-var-args(2px, 3px, 4px)", evaluate("only-var-args(1px, 2px, 3px)")
  948. end
  949. def test_only_kw_args
  950. assert_equal "only-kw-args(a, b, c)", evaluate("only-kw-args($a: 1, $b: 2, $c: 3)")
  951. end
  952. ## Regression Tests
  953. def test_saturation_bounds
  954. assert_equal "#fbfdff", evaluate("hsl(hue(#fbfdff), saturation(#fbfdff), lightness(#fbfdff))")
  955. end
  956. private
  957. def evaluate(value)
  958. result = perform(value)
  959. assert_kind_of Sass::Script::Literal, result
  960. return result.to_s
  961. end
  962. def perform(value)
  963. Sass::Script::Parser.parse(value, 0, 0).perform(Sass::Environment.new)
  964. end
  965. def assert_error_message(message, value)
  966. evaluate(value)
  967. flunk("Error message expected but not raised: #{message}")
  968. rescue Sass::SyntaxError => e
  969. assert_equal(message, e.message)
  970. end
  971. end