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

/vendor/bundle/ruby/2.3.0/gems/sass-3.2.19/test/sass/util_test.rb

https://gitlab.com/tt2016/horse_analysis
Ruby | 382 lines | 341 code | 40 blank | 1 comment | 8 complexity | e14f895bbf833c06ecce1de2f5418418 MD5 | raw file
  1. #!/usr/bin/env ruby
  2. require File.dirname(__FILE__) + '/../test_helper'
  3. require 'pathname'
  4. require 'tmpdir'
  5. class UtilTest < Test::Unit::TestCase
  6. include Sass::Util
  7. def test_scope
  8. assert(File.exist?(scope("Rakefile")))
  9. end
  10. def test_to_hash
  11. assert_equal({
  12. :foo => 1,
  13. :bar => 2,
  14. :baz => 3
  15. }, to_hash([[:foo, 1], [:bar, 2], [:baz, 3]]))
  16. end
  17. def test_map_keys
  18. assert_equal({
  19. "foo" => 1,
  20. "bar" => 2,
  21. "baz" => 3
  22. }, map_keys({:foo => 1, :bar => 2, :baz => 3}) {|k| k.to_s})
  23. end
  24. def test_map_vals
  25. assert_equal({
  26. :foo => "1",
  27. :bar => "2",
  28. :baz => "3"
  29. }, map_vals({:foo => 1, :bar => 2, :baz => 3}) {|k| k.to_s})
  30. end
  31. def test_map_hash
  32. assert_equal({
  33. "foo" => "1",
  34. "bar" => "2",
  35. "baz" => "3"
  36. }, map_hash({:foo => 1, :bar => 2, :baz => 3}) {|k, v| [k.to_s, v.to_s]})
  37. end
  38. def test_powerset
  39. return unless Set[Set[]] == Set[Set[]] # There's a bug in Ruby 1.8.6 that breaks nested set equality
  40. assert_equal([[].to_set].to_set,
  41. powerset([]))
  42. assert_equal([[].to_set, [1].to_set].to_set,
  43. powerset([1]))
  44. assert_equal([[].to_set, [1].to_set, [2].to_set, [1, 2].to_set].to_set,
  45. powerset([1, 2]))
  46. assert_equal([[].to_set, [1].to_set, [2].to_set, [3].to_set,
  47. [1, 2].to_set, [2, 3].to_set, [1, 3].to_set, [1, 2, 3].to_set].to_set,
  48. powerset([1, 2, 3]))
  49. end
  50. def test_restrict
  51. assert_equal(0.5, restrict(0.5, 0..1))
  52. assert_equal(1, restrict(2, 0..1))
  53. assert_equal(1.3, restrict(2, 0..1.3))
  54. assert_equal(0, restrict(-1, 0..1))
  55. end
  56. def test_merge_adjacent_strings
  57. assert_equal(["foo bar baz", :bang, "biz bop", 12],
  58. merge_adjacent_strings(["foo ", "bar ", "baz", :bang, "biz", " bop", 12]))
  59. str = "foo"
  60. assert_equal(["foo foo foo", :bang, "foo foo", 12],
  61. merge_adjacent_strings([str, " ", str, " ", str, :bang, str, " ", str, 12]))
  62. end
  63. def test_intersperse
  64. assert_equal(["foo", " ", "bar", " ", "baz"],
  65. intersperse(%w[foo bar baz], " "))
  66. assert_equal([], intersperse([], " "))
  67. end
  68. def test_substitute
  69. assert_equal(["foo", "bar", "baz", 3, 4],
  70. substitute([1, 2, 3, 4], [1, 2], ["foo", "bar", "baz"]))
  71. assert_equal([1, "foo", "bar", "baz", 4],
  72. substitute([1, 2, 3, 4], [2, 3], ["foo", "bar", "baz"]))
  73. assert_equal([1, 2, "foo", "bar", "baz"],
  74. substitute([1, 2, 3, 4], [3, 4], ["foo", "bar", "baz"]))
  75. assert_equal([1, "foo", "bar", "baz", 2, 3, 4],
  76. substitute([1, 2, 2, 2, 3, 4], [2, 2], ["foo", "bar", "baz"]))
  77. end
  78. def test_strip_string_array
  79. assert_equal(["foo ", " bar ", " baz"],
  80. strip_string_array([" foo ", " bar ", " baz "]))
  81. assert_equal([:foo, " bar ", " baz"],
  82. strip_string_array([:foo, " bar ", " baz "]))
  83. assert_equal(["foo ", " bar ", :baz],
  84. strip_string_array([" foo ", " bar ", :baz]))
  85. end
  86. def test_paths
  87. assert_equal([[1, 3, 5], [2, 3, 5], [1, 4, 5], [2, 4, 5]],
  88. paths([[1, 2], [3, 4], [5]]))
  89. assert_equal([[]], paths([]))
  90. assert_equal([[1, 2, 3]], paths([[1], [2], [3]]))
  91. end
  92. def test_lcs
  93. assert_equal([1, 2, 3], lcs([1, 2, 3], [1, 2, 3]))
  94. assert_equal([], lcs([], [1, 2, 3]))
  95. assert_equal([], lcs([1, 2, 3], []))
  96. assert_equal([1, 2, 3], lcs([5, 1, 4, 2, 3, 17], [0, 0, 1, 2, 6, 3]))
  97. assert_equal([1], lcs([1, 2, 3, 4], [4, 3, 2, 1]))
  98. assert_equal([1, 2], lcs([1, 2, 3, 4], [3, 4, 1, 2]))
  99. end
  100. def test_lcs_with_block
  101. assert_equal(["1", "2", "3"],
  102. lcs([1, 4, 2, 5, 3], [1, 2, 3]) {|a, b| a == b && a.to_s})
  103. assert_equal([-4, 2, 8],
  104. lcs([-5, 3, 2, 8], [-4, 1, 8]) {|a, b| (a - b).abs <= 1 && [a, b].max})
  105. end
  106. def test_group_by_to_a
  107. assert_equal([[1, [1, 3, 5, 7]], [0, [2, 4, 6, 8]]],
  108. group_by_to_a(1..8) {|i| i % 2})
  109. assert_equal([[1, [1, 4, 7, 10]], [2, [2, 5, 8, 11]], [0, [3, 6, 9, 12]]],
  110. group_by_to_a(1..12) {|i| i % 3})
  111. end
  112. def test_subsequence
  113. assert(subsequence?([1, 2, 3], [1, 2, 3]))
  114. assert(subsequence?([1, 2, 3], [1, :a, 2, :b, 3]))
  115. assert(subsequence?([1, 2, 3], [:a, 1, :b, :c, 2, :d, 3, :e, :f]))
  116. assert(!subsequence?([1, 2, 3], [1, 2]))
  117. assert(!subsequence?([1, 2, 3], [1, 3, 2]))
  118. assert(!subsequence?([1, 2, 3], [3, 2, 1]))
  119. end
  120. def test_silence_warnings
  121. old_stderr, $stderr = $stderr, StringIO.new
  122. warn "Out"
  123. assert_equal("Out\n", $stderr.string)
  124. silence_warnings {warn "In"}
  125. assert_equal("Out\n", $stderr.string)
  126. ensure
  127. $stderr = old_stderr
  128. end
  129. def test_sass_warn
  130. assert_warning("Foo!") {sass_warn "Foo!"}
  131. end
  132. def test_silence_sass_warnings
  133. old_stderr, $stderr = $stderr, StringIO.new
  134. silence_sass_warnings {warn "Out"}
  135. assert_equal("Out\n", $stderr.string)
  136. silence_sass_warnings {sass_warn "In"}
  137. assert_equal("Out\n", $stderr.string)
  138. ensure
  139. $stderr = old_stderr
  140. end
  141. def test_has
  142. assert(has?(:instance_method, String, :chomp!))
  143. assert(has?(:private_instance_method, Sass::Engine, :parse_interp))
  144. end
  145. def test_enum_with_index
  146. assert_equal(%w[foo0 bar1 baz2],
  147. enum_with_index(%w[foo bar baz]).map {|s, i| "#{s}#{i}"})
  148. end
  149. def test_enum_cons
  150. assert_equal(%w[foobar barbaz],
  151. enum_cons(%w[foo bar baz], 2).map {|s1, s2| "#{s1}#{s2}"})
  152. end
  153. def test_extract
  154. arr = [1, 2, 3, 4, 5]
  155. assert_equal([1, 3, 5], extract!(arr) {|e| e % 2 == 1})
  156. assert_equal([2, 4], arr)
  157. end
  158. def test_ord
  159. assert_equal(102, ord("f"))
  160. assert_equal(98, ord("bar"))
  161. end
  162. def test_flatten
  163. assert_equal([1, 2, 3], flatten([1, 2, 3], 0))
  164. assert_equal([1, 2, 3], flatten([1, 2, 3], 1))
  165. assert_equal([1, 2, 3], flatten([1, 2, 3], 2))
  166. assert_equal([[1, 2], 3], flatten([[1, 2], 3], 0))
  167. assert_equal([1, 2, 3], flatten([[1, 2], 3], 1))
  168. assert_equal([1, 2, 3], flatten([[1, 2], 3], 2))
  169. assert_equal([[[1], 2], [3], 4], flatten([[[1], 2], [3], 4], 0))
  170. assert_equal([[1], 2, 3, 4], flatten([[[1], 2], [3], 4], 1))
  171. assert_equal([1, 2, 3, 4], flatten([[[1], 2], [3], 4], 2))
  172. end
  173. def test_set_hash
  174. assert(set_hash(Set[1, 2, 3]) == set_hash(Set[3, 2, 1]))
  175. assert(set_hash(Set[1, 2, 3]) == set_hash(Set[1, 2, 3]))
  176. s1 = Set[]
  177. s1 << 1
  178. s1 << 2
  179. s1 << 3
  180. s2 = Set[]
  181. s2 << 3
  182. s2 << 2
  183. s2 << 1
  184. assert(set_hash(s1) == set_hash(s2))
  185. end
  186. def test_set_eql
  187. assert(set_eql?(Set[1, 2, 3], Set[3, 2, 1]))
  188. assert(set_eql?(Set[1, 2, 3], Set[1, 2, 3]))
  189. s1 = Set[]
  190. s1 << 1
  191. s1 << 2
  192. s1 << 3
  193. s2 = Set[]
  194. s2 << 3
  195. s2 << 2
  196. s2 << 1
  197. assert(set_eql?(s1, s2))
  198. end
  199. def test_extract_and_inject_values
  200. test = lambda {|arr| assert_equal(arr, with_extracted_values(arr) {|str| str})}
  201. test[['foo bar']]
  202. test[['foo {12} bar']]
  203. test[['foo {{12} bar']]
  204. test[['foo {{1', 12, '2} bar']]
  205. test[['foo 1', 2, '{3', 4, 5, 6, '{7}', 8]]
  206. test[['foo 1', [2, 3, 4], ' bar']]
  207. test[['foo ', 1, "\n bar\n", [2, 3, 4], "\n baz"]]
  208. end
  209. def nested_caller_info_fn
  210. caller_info
  211. end
  212. def double_nested_caller_info_fn
  213. nested_caller_info_fn
  214. end
  215. def test_caller_info
  216. assert_equal(["/tmp/foo.rb", 12, "fizzle"], caller_info("/tmp/foo.rb:12: in `fizzle'"))
  217. assert_equal(["/tmp/foo.rb", 12, nil], caller_info("/tmp/foo.rb:12"))
  218. assert_equal(["(sass)", 12, "blah"], caller_info("(sass):12: in `blah'"))
  219. assert_equal(["", 12, "boop"], caller_info(":12: in `boop'"))
  220. assert_equal(["/tmp/foo.rb", -12, "fizzle"], caller_info("/tmp/foo.rb:-12: in `fizzle'"))
  221. assert_equal(["/tmp/foo.rb", 12, "fizzle"], caller_info("/tmp/foo.rb:12: in `fizzle {}'"))
  222. info = nested_caller_info_fn
  223. assert_equal(__FILE__, info[0])
  224. assert_equal("test_caller_info", info[2])
  225. info = proc {nested_caller_info_fn}.call
  226. assert_equal(__FILE__, info[0])
  227. assert_match(/^(block in )?test_caller_info$/, info[2])
  228. info = double_nested_caller_info_fn
  229. assert_equal(__FILE__, info[0])
  230. assert_equal("double_nested_caller_info_fn", info[2])
  231. info = proc {double_nested_caller_info_fn}.call
  232. assert_equal(__FILE__, info[0])
  233. assert_equal("double_nested_caller_info_fn", info[2])
  234. end
  235. def test_version_gt
  236. assert_version_gt("2.0.0", "1.0.0")
  237. assert_version_gt("1.1.0", "1.0.0")
  238. assert_version_gt("1.0.1", "1.0.0")
  239. assert_version_gt("1.0.0", "1.0.0.rc")
  240. assert_version_gt("1.0.0.1", "1.0.0.rc")
  241. assert_version_gt("1.0.0.rc", "0.9.9")
  242. assert_version_gt("1.0.0.beta", "1.0.0.alpha")
  243. assert_version_eq("1.0.0", "1.0.0")
  244. assert_version_eq("1.0.0", "1.0.0.0")
  245. end
  246. def assert_version_gt(v1, v2)
  247. #assert(version_gt(v1, v2), "Expected #{v1} > #{v2}")
  248. assert(!version_gt(v2, v1), "Expected #{v2} < #{v1}")
  249. end
  250. def assert_version_eq(v1, v2)
  251. assert(!version_gt(v1, v2), "Expected #{v1} = #{v2}")
  252. assert(!version_gt(v2, v1), "Expected #{v2} = #{v1}")
  253. end
  254. class FooBar
  255. def foo
  256. Sass::Util.abstract(self)
  257. end
  258. end
  259. def test_abstract
  260. assert_raise_message(NotImplementedError,
  261. "UtilTest::FooBar must implement #foo") {FooBar.new.foo}
  262. end
  263. def test_atomic_writes
  264. # when using normal writes, this test fails about 90% of the time.
  265. filename = File.join(Dir.tmpdir, "test_atomic")
  266. 5.times do
  267. writes_to_perform = %w(1 2 3 4 5 6 7 8 9).map {|i| "#{i}\n" * 100_000}
  268. threads = writes_to_perform.map do |to_write|
  269. Thread.new do
  270. # To see this test fail with a normal write,
  271. # change to the standard file open mechanism:
  272. # open(filename, "w") do |f|
  273. atomic_create_and_write_file(filename) do |f|
  274. f.write(to_write)
  275. end
  276. end
  277. end
  278. loop do
  279. contents = File.exist?(filename) ? File.read(filename) : nil
  280. next if contents.nil? || contents.size == 0
  281. unless writes_to_perform.include?(contents)
  282. if contents.size != writes_to_perform.first.size
  283. fail "Incomplete write detected: was #{contents.size} characters, " +
  284. "should have been #{writes_to_perform.first.size}"
  285. else
  286. fail "Corrupted read/write detected"
  287. end
  288. end
  289. break if threads.all? {|t| !t.alive?}
  290. end
  291. threads.each {|t| t.join}
  292. end
  293. end
  294. def test_atomic_write_permissions
  295. atomic_filename = File.join(Dir.tmpdir, "test_atomic_perms.atomic")
  296. normal_filename = File.join(Dir.tmpdir, "test_atomic_perms.normal")
  297. atomic_create_and_write_file(atomic_filename) {|f| f.write("whatever\n") }
  298. open(normal_filename, "wb") {|f| f.write("whatever\n") }
  299. assert_equal File.stat(normal_filename).mode.to_s(8), File.stat(atomic_filename).mode.to_s(8)
  300. ensure
  301. File.unlink(atomic_filename) rescue nil
  302. File.unlink(normal_filename) rescue nil
  303. end
  304. def test_atomic_writes_respect_umask
  305. atomic_filename = File.join(Dir.tmpdir, "test_atomic_perms.atomic")
  306. atomic_create_and_write_file(atomic_filename) do |f|
  307. f.write("whatever\n")
  308. end
  309. assert_equal 0, File.stat(atomic_filename).mode & File.umask
  310. ensure
  311. File.unlink(atomic_filename)
  312. end
  313. class FakeError < RuntimeError; end
  314. def test_atomic_writes_handles_exceptions
  315. filename = File.join(Dir.tmpdir, "test_atomic_exception")
  316. FileUtils.rm_f(filename)
  317. tmp_filename = nil
  318. assert_raises FakeError do
  319. atomic_create_and_write_file(filename) do |f|
  320. tmp_filename = f.path
  321. raise FakeError.new "Borken"
  322. end
  323. end
  324. assert !File.exist?(filename)
  325. assert !File.exist?(tmp_filename)
  326. end
  327. end