PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/minitest/test_minitest_unit.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 1454 lines | 1267 code | 169 blank | 18 comment | 3 complexity | 72d41ebcca83dde45420485f9ed0da57 MD5 | raw file
  1. ######################################################################
  2. # This file is imported from the minitest project.
  3. # DO NOT make modifications in this repo. They _will_ be reverted!
  4. # File a patch instead and assign it to Ryan Davis.
  5. ######################################################################
  6. require 'stringio'
  7. require 'pathname'
  8. require 'minitest/unit'
  9. MiniTest::Unit.autorun
  10. module MyModule; end
  11. class AnError < StandardError; include MyModule; end
  12. class ImmutableString < String; def inspect; super.freeze; end; end
  13. class TestMiniTestUnit < MiniTest::Unit::TestCase
  14. pwd = Pathname.new(File.expand_path(Dir.pwd))
  15. basedir = Pathname.new(File.expand_path(MiniTest::MINI_DIR)) + 'mini'
  16. basedir = basedir.relative_path_from(pwd).to_s
  17. MINITEST_BASE_DIR = basedir[/\A\./] ? basedir : "./#{basedir}"
  18. BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:161:in `each'",
  19. "#{MINITEST_BASE_DIR}/test.rb:158:in `each'",
  20. "#{MINITEST_BASE_DIR}/test.rb:139:in `run'",
  21. "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"]
  22. def assert_report expected = nil
  23. expected ||= "Run options: --seed 42
  24. # Running tests:
  25. .
  26. Finished tests in 0.00
  27. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
  28. "
  29. output = @output.string.sub(/Finished tests in .*/, "Finished tests in 0.00")
  30. output.sub!(/Loaded suite .*/, 'Loaded suite blah')
  31. output.sub!(/^(\s+)(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+:/o, '\1FILE:LINE:')
  32. output.sub!(/\[(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+\]/o, '[FILE:LINE]')
  33. assert_equal(expected, output)
  34. end
  35. def setup
  36. srand 42
  37. MiniTest::Unit::TestCase.reset
  38. @tu = MiniTest::Unit.new
  39. @output = StringIO.new("")
  40. MiniTest::Unit.runner = nil # protect the outer runner from the inner tests
  41. MiniTest::Unit.output = @output
  42. end
  43. def teardown
  44. MiniTest::Unit.output = $stdout
  45. Object.send :remove_const, :ATestCase if defined? ATestCase
  46. end
  47. def test_class_puke_with_assertion_failed
  48. exception = MiniTest::Assertion.new "Oh no!"
  49. exception.set_backtrace ["unhappy"]
  50. assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception)
  51. assert_equal 1, @tu.failures
  52. assert_match(/^Failure.*Oh no!/m, @tu.report.first)
  53. assert_match("method_name(SomeClass) [unhappy]", @tu.report.first)
  54. end
  55. def test_class_puke_with_assertion_failed_and_long_backtrace
  56. bt = (["test/test_some_class.rb:615:in `method_name'",
  57. "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'",
  58. "test/test_some_class.rb:615:in `each'",
  59. "test/test_some_class.rb:614:in `test_method_name'",
  60. "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  61. BT_MIDDLE +
  62. ["#{MINITEST_BASE_DIR}/test.rb:29"])
  63. bt = util_expand_bt bt
  64. ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first
  65. exception = MiniTest::Assertion.new "Oh no!"
  66. exception.set_backtrace bt
  67. assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)
  68. assert_equal 1, @tu.failures
  69. assert_match(/^Failure.*Oh no!/m, @tu.report.first)
  70. assert_match("test_method_name(TestSomeClass) [#{ex_location}]", @tu.report.first)
  71. end
  72. def test_class_puke_with_assertion_failed_and_user_defined_assertions
  73. bt = (["lib/test/my/util.rb:16:in `another_method_name'",
  74. "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'",
  75. "lib/test/my/util.rb:15:in `block in assert_something'",
  76. "lib/test/my/util.rb:14:in `each'",
  77. "lib/test/my/util.rb:14:in `assert_something'",
  78. "test/test_some_class.rb:615:in `each'",
  79. "test/test_some_class.rb:614:in `test_method_name'",
  80. "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  81. BT_MIDDLE +
  82. ["#{MINITEST_BASE_DIR}/test.rb:29"])
  83. bt = util_expand_bt bt
  84. ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first
  85. exception = MiniTest::Assertion.new "Oh no!"
  86. exception.set_backtrace bt
  87. assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)
  88. assert_equal 1, @tu.failures
  89. assert_match(/^Failure.*Oh no!/m, @tu.report.first)
  90. assert_match("test_method_name(TestSomeClass) [#{ex_location}]", @tu.report.first)
  91. end
  92. def test_class_puke_with_failure_and_flunk_in_backtrace
  93. exception = begin
  94. MiniTest::Unit::TestCase.new('fake tc').flunk
  95. rescue MiniTest::Assertion => failure
  96. failure
  97. end
  98. assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception)
  99. refute @tu.report.any?{|line| line =~ /in .flunk/}
  100. end
  101. def test_class_puke_with_flunk_and_user_defined_assertions
  102. bt = (["lib/test/my/util.rb:16:in `flunk'",
  103. "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'",
  104. "lib/test/my/util.rb:15:in `block in assert_something'",
  105. "lib/test/my/util.rb:14:in `each'",
  106. "lib/test/my/util.rb:14:in `assert_something'",
  107. "test/test_some_class.rb:615:in `each'",
  108. "test/test_some_class.rb:614:in `test_method_name'",
  109. "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  110. BT_MIDDLE +
  111. ["#{MINITEST_BASE_DIR}/test.rb:29"])
  112. bt = util_expand_bt bt
  113. ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first
  114. exception = MiniTest::Assertion.new "Oh no!"
  115. exception.set_backtrace bt
  116. assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)
  117. assert_equal 1, @tu.failures
  118. assert_match(/^Failure.*Oh no!/m, @tu.report.first)
  119. assert_match("test_method_name(TestSomeClass) [#{ex_location}]", @tu.report.first)
  120. end
  121. def test_class_puke_with_non_failure_exception
  122. exception = Exception.new("Oh no again!")
  123. assert_equal 'E', @tu.puke('SomeClass', 'method_name', exception)
  124. assert_equal 1, @tu.errors
  125. assert_match(/^Exception.*Oh no again!/m, @tu.report.first)
  126. end
  127. def test_filter_backtrace
  128. # this is a semi-lame mix of relative paths.
  129. # I cheated by making the autotest parts not have ./
  130. bt = (["lib/autotest.rb:571:in `add_exception'",
  131. "test/test_autotest.rb:62:in `test_add_exception'",
  132. "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  133. BT_MIDDLE +
  134. ["#{MINITEST_BASE_DIR}/test.rb:29",
  135. "test/test_autotest.rb:422"])
  136. bt = util_expand_bt bt
  137. ex = ["lib/autotest.rb:571:in `add_exception'",
  138. "test/test_autotest.rb:62:in `test_add_exception'"]
  139. ex = util_expand_bt ex
  140. fu = MiniTest::filter_backtrace(bt)
  141. assert_equal ex, fu
  142. end
  143. def test_filter_backtrace_all_unit
  144. bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  145. BT_MIDDLE +
  146. ["#{MINITEST_BASE_DIR}/test.rb:29"])
  147. ex = bt.clone
  148. fu = MiniTest::filter_backtrace(bt)
  149. assert_equal ex, fu
  150. end
  151. def test_filter_backtrace_unit_starts
  152. bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  153. BT_MIDDLE +
  154. ["#{MINITEST_BASE_DIR}/mini/test.rb:29",
  155. "-e:1"])
  156. bt = util_expand_bt bt
  157. ex = ["-e:1"]
  158. fu = MiniTest::filter_backtrace(bt)
  159. assert_equal ex, fu
  160. end
  161. def test_run_error
  162. tc = Class.new(MiniTest::Unit::TestCase) do
  163. def test_something
  164. assert true
  165. end
  166. def test_error
  167. raise "unhandled exception"
  168. end
  169. end
  170. Object.const_set(:ATestCase, tc)
  171. @tu.run %w[--seed 42]
  172. expected = "Run options: --seed 42
  173. # Running tests:
  174. E.
  175. Finished tests in 0.00
  176. 1) Error:
  177. test_error(ATestCase):
  178. RuntimeError: unhandled exception
  179. FILE:LINE:in `test_error'
  180. 2 tests, 1 assertions, 0 failures, 1 errors, 0 skips
  181. "
  182. assert_report expected
  183. end
  184. def test_run_error_teardown
  185. tc = Class.new(MiniTest::Unit::TestCase) do
  186. def test_something
  187. assert true
  188. end
  189. def teardown
  190. raise "unhandled exception"
  191. end
  192. end
  193. Object.const_set(:ATestCase, tc)
  194. @tu.run %w[--seed 42]
  195. expected = "Run options: --seed 42
  196. # Running tests:
  197. E
  198. Finished tests in 0.00
  199. 1) Error:
  200. test_something(ATestCase):
  201. RuntimeError: unhandled exception
  202. FILE:LINE:in `teardown'
  203. 1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
  204. "
  205. assert_report expected
  206. end
  207. def test_run_failing
  208. tc = Class.new(MiniTest::Unit::TestCase) do
  209. def test_something
  210. assert true
  211. end
  212. def test_failure
  213. assert false
  214. end
  215. end
  216. Object.const_set(:ATestCase, tc)
  217. @tu.run %w[--seed 42]
  218. expected = "Run options: --seed 42
  219. # Running tests:
  220. F.
  221. Finished tests in 0.00
  222. 1) Failure:
  223. test_failure(ATestCase) [FILE:LINE]:
  224. Failed assertion, no message given.
  225. 2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
  226. "
  227. assert_report expected
  228. end
  229. def test_run_failing_filtered
  230. tc = Class.new(MiniTest::Unit::TestCase) do
  231. def test_something
  232. assert true
  233. end
  234. def test_failure
  235. assert false
  236. end
  237. end
  238. Object.const_set(:ATestCase, tc)
  239. @tu.run %w[--name /some|thing/ --seed 42]
  240. expected = "Run options: --name \"/some|thing/\" --seed 42
  241. # Running tests:
  242. .
  243. Finished tests in 0.00
  244. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
  245. "
  246. assert_report expected
  247. end
  248. def test_run_passing
  249. tc = Class.new(MiniTest::Unit::TestCase) do
  250. def test_something
  251. assert true
  252. end
  253. end
  254. Object.const_set(:ATestCase, tc)
  255. @tu.run %w[--seed 42]
  256. assert_report
  257. end
  258. def test_run_skip
  259. tc = Class.new(MiniTest::Unit::TestCase) do
  260. def test_something
  261. assert true
  262. end
  263. def test_skip
  264. skip "not yet"
  265. end
  266. end
  267. Object.const_set(:ATestCase, tc)
  268. @tu.run %w[--seed 42]
  269. expected = "Run options: --seed 42
  270. # Running tests:
  271. S.
  272. Finished tests in 0.00
  273. 2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
  274. "
  275. assert_report expected
  276. end
  277. def test_run_skip_verbose
  278. tc = Class.new(MiniTest::Unit::TestCase) do
  279. def test_something
  280. assert true
  281. end
  282. def test_skip
  283. skip "not yet"
  284. end
  285. end
  286. Object.const_set(:ATestCase, tc)
  287. @tu.run %w[--seed 42 --verbose]
  288. expected = "Run options: --seed 42 --verbose
  289. # Running tests:
  290. ATestCase#test_skip = 0.00 s = S
  291. ATestCase#test_something = 0.00 s = .
  292. Finished tests in 0.00
  293. 1) Skipped:
  294. test_skip(ATestCase) [FILE:LINE]:
  295. not yet
  296. 2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
  297. "
  298. assert_report expected
  299. end
  300. def test_default_runner_is_minitest_unit
  301. assert_instance_of MiniTest::Unit, MiniTest::Unit.runner
  302. end
  303. def test_run_with_other_runner
  304. runner = Class.new(MiniTest::Unit) do
  305. # Run once before each suite
  306. def _run_suite(suite, type)
  307. begin
  308. suite.before_suite
  309. super(suite, type)
  310. end
  311. end
  312. end
  313. tc = Class.new(MiniTest::Unit::TestCase) do
  314. def self.before_suite
  315. MiniTest::Unit.output.puts "Running #{self.name} tests"
  316. @@foo = 1
  317. end
  318. def test_something
  319. assert_equal 1, @@foo
  320. end
  321. def test_something_else
  322. assert_equal 1, @@foo
  323. end
  324. end
  325. Object.const_set(:ATestCase, tc)
  326. MiniTest::Unit.runner = runner.new
  327. @tu.run %w[--seed 42]
  328. # We should only see 'running ATestCase tests' once
  329. expected = "Run options: --seed 42
  330. # Running tests:
  331. Running ATestCase tests
  332. ..
  333. Finished tests in 0.00
  334. 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
  335. "
  336. assert_report expected
  337. end
  338. def with_overridden_include
  339. Class.class_eval do
  340. def inherited_with_hacks klass
  341. throw :inherited_hook
  342. end
  343. alias inherited_without_hacks inherited
  344. alias inherited inherited_with_hacks
  345. alias IGNORE_ME! inherited # 1.8 bug. god I love venture bros
  346. end
  347. yield
  348. ensure
  349. Class.class_eval do
  350. alias inherited inherited_without_hacks
  351. undef_method :inherited_with_hacks
  352. undef_method :inherited_without_hacks
  353. end
  354. refute_respond_to Class, :inherited_with_hacks
  355. refute_respond_to Class, :inherited_without_hacks
  356. end
  357. def test_inherited_hook_plays_nice_with_others
  358. with_overridden_include do
  359. assert_throws :inherited_hook do
  360. Class.new MiniTest::Unit::TestCase
  361. end
  362. end
  363. end
  364. def test_setup_hooks
  365. call_order = []
  366. tc = Class.new(MiniTest::Unit::TestCase) do
  367. define_method :setup do
  368. super()
  369. call_order << :method
  370. end
  371. define_method :test2 do
  372. call_order << :test2
  373. end
  374. define_method :test1 do
  375. call_order << :test1
  376. end
  377. end
  378. tc.add_setup_hook lambda { call_order << :proc }
  379. argument = nil
  380. tc.add_setup_hook do |arg|
  381. argument = arg
  382. call_order << :block
  383. end
  384. @tu.run %w[--seed 42]
  385. assert_kind_of tc, argument
  386. expected = [:method, :proc, :block, :test1,
  387. :method, :proc, :block, :test2]
  388. assert_equal expected, call_order
  389. end
  390. def test_teardown_hooks
  391. call_order = []
  392. tc = Class.new(MiniTest::Unit::TestCase) do
  393. define_method :teardown do
  394. super()
  395. call_order << :method
  396. end
  397. define_method :test2 do
  398. call_order << :test2
  399. end
  400. define_method :test1 do
  401. call_order << :test1
  402. end
  403. end
  404. tc.add_teardown_hook lambda { call_order << :proc }
  405. argument = nil
  406. tc.add_teardown_hook do |arg|
  407. argument = arg
  408. call_order << :block
  409. end
  410. @tu.run %w[--seed 42]
  411. assert_kind_of tc, argument
  412. expected = [:test1, :block, :proc, :method,
  413. :test2, :block, :proc, :method]
  414. assert_equal expected, call_order
  415. end
  416. def test_setup_and_teardown_hooks_survive_inheritance
  417. call_order = []
  418. parent = Class.new(MiniTest::Unit::TestCase) do
  419. define_method :setup do
  420. super()
  421. call_order << :setup_method
  422. end
  423. define_method :teardown do
  424. super()
  425. call_order << :teardown_method
  426. end
  427. define_method :test_something do
  428. call_order << :test
  429. end
  430. end
  431. parent.add_setup_hook { call_order << :setup_hook }
  432. parent.add_teardown_hook { call_order << :teardown_hook }
  433. _ = Class.new parent
  434. parent.add_setup_hook { call_order << :setup_after }
  435. parent.add_teardown_hook { call_order << :teardown_after }
  436. @tu.run %w[--seed 42]
  437. # Once for the parent class, once for the child
  438. expected = [:setup_method, :setup_hook, :setup_after, :test,
  439. :teardown_after, :teardown_hook, :teardown_method] * 2
  440. assert_equal expected, call_order
  441. end
  442. def util_expand_bt bt
  443. if RUBY_VERSION =~ /^1\.9/ then
  444. bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f }
  445. else
  446. bt
  447. end
  448. end
  449. end
  450. class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
  451. def setup
  452. MiniTest::Unit::TestCase.reset
  453. @tc = MiniTest::Unit::TestCase.new 'fake tc'
  454. @zomg = "zomg ponies!"
  455. @assertion_count = 1
  456. end
  457. def teardown
  458. assert_equal(@assertion_count, @tc._assertions,
  459. "expected #{@assertion_count} assertions to be fired during the test, not #{@tc._assertions}") if @tc._assertions
  460. Object.send :remove_const, :ATestCase if defined? ATestCase
  461. end
  462. def test_assert
  463. @assertion_count = 2
  464. @tc.assert_equal true, @tc.assert(true), "returns true on success"
  465. end
  466. def test_assert__triggered
  467. util_assert_triggered "Failed assertion, no message given." do
  468. @tc.assert false
  469. end
  470. end
  471. def test_assert__triggered_message
  472. util_assert_triggered @zomg do
  473. @tc.assert false, @zomg
  474. end
  475. end
  476. def test_assert_block
  477. @tc.assert_block do
  478. true
  479. end
  480. end
  481. def test_assert_block_triggered
  482. util_assert_triggered "blah.\nExpected block to return true value." do
  483. @tc.assert_block "blah" do
  484. false
  485. end
  486. end
  487. end
  488. def test_assert_empty
  489. @assertion_count = 2
  490. @tc.assert_empty []
  491. end
  492. def test_assert_empty_triggered
  493. @assertion_count = 2
  494. util_assert_triggered "Expected [1] to be empty." do
  495. @tc.assert_empty [1]
  496. end
  497. end
  498. def test_assert_equal
  499. @tc.assert_equal 1, 1
  500. end
  501. def test_assert_equal_different_diff_deactivated
  502. without_diff do
  503. util_assert_triggered util_msg("haha" * 10, "blah" * 10) do
  504. o1 = "haha" * 10
  505. o2 = "blah" * 10
  506. @tc.assert_equal o1, o2
  507. end
  508. end
  509. end
  510. def test_assert_equal_different_hex
  511. c = Class.new do
  512. def initialize s; @name = s; end
  513. end
  514. o1 = c.new "a"
  515. o2 = c.new "b"
  516. msg = "--- expected
  517. +++ actual
  518. @@ -1 +1 @@
  519. -#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"a\">
  520. +#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"b\">
  521. ".gsub(/^ +/, "")
  522. util_assert_triggered msg do
  523. @tc.assert_equal o1, o2
  524. end
  525. end
  526. def test_assert_equal_different_hex_invisible
  527. o1 = Object.new
  528. o2 = Object.new
  529. msg = "No visible difference.
  530. You should look at your implementation of Object#==.
  531. #<Object:0xXXXXXX>".gsub(/^ +/, "")
  532. util_assert_triggered msg do
  533. @tc.assert_equal o1, o2
  534. end
  535. end
  536. def test_assert_equal_different_long
  537. msg = "--- expected
  538. +++ actual
  539. @@ -1 +1 @@
  540. -\"hahahahahahahahahahahahahahahahahahahaha\"
  541. +\"blahblahblahblahblahblahblahblahblahblah\"
  542. ".gsub(/^ +/, "")
  543. util_assert_triggered msg do
  544. o1 = "haha" * 10
  545. o2 = "blah" * 10
  546. @tc.assert_equal o1, o2
  547. end
  548. end
  549. def test_assert_equal_different_long_invisible
  550. msg = "No visible difference.
  551. You should look at your implementation of String#==.
  552. \"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "")
  553. util_assert_triggered msg do
  554. o1 = "blah" * 10
  555. o2 = "blah" * 10
  556. def o1.== o
  557. false
  558. end
  559. @tc.assert_equal o1, o2
  560. end
  561. end
  562. def test_assert_equal_different_long_msg
  563. msg = "message.
  564. --- expected
  565. +++ actual
  566. @@ -1 +1 @@
  567. -\"hahahahahahahahahahahahahahahahahahahaha\"
  568. +\"blahblahblahblahblahblahblahblahblahblah\"
  569. ".gsub(/^ +/, "")
  570. util_assert_triggered msg do
  571. o1 = "haha" * 10
  572. o2 = "blah" * 10
  573. @tc.assert_equal o1, o2, "message"
  574. end
  575. end
  576. def test_assert_equal_different_short
  577. util_assert_triggered util_msg(1, 2) do
  578. @tc.assert_equal 1, 2
  579. end
  580. end
  581. def test_assert_equal_different_short_msg
  582. util_assert_triggered util_msg(1, 2, "message") do
  583. @tc.assert_equal 1, 2, "message"
  584. end
  585. end
  586. def test_assert_equal_different_short_multiline
  587. msg = "--- expected\n+++ actual\n@@ -1,2 +1,2 @@\n \"a\n-b\"\n+c\"\n"
  588. util_assert_triggered msg do
  589. @tc.assert_equal "a\nb", "a\nc"
  590. end
  591. end
  592. def test_assert_in_delta
  593. @tc.assert_in_delta 0.0, 1.0 / 1000, 0.1
  594. end
  595. def test_assert_in_delta_triggered
  596. util_assert_triggered 'Expected 0.0 - 0.001 (0.001) to be < 1.0e-06.' do
  597. @tc.assert_in_delta 0.0, 1.0 / 1000, 0.000001
  598. end
  599. end
  600. def test_assert_in_epsilon
  601. @assertion_count = 8
  602. @tc.assert_in_epsilon 10000, 9991
  603. @tc.assert_in_epsilon 9991, 10000
  604. @tc.assert_in_epsilon 1.0, 1.001
  605. @tc.assert_in_epsilon 1.001, 1.0
  606. @tc.assert_in_epsilon 10000, 9999.1, 0.0001
  607. @tc.assert_in_epsilon 9999.1, 10000, 0.0001
  608. @tc.assert_in_epsilon 1.0, 1.0001, 0.0001
  609. @tc.assert_in_epsilon 1.0001, 1.0, 0.0001
  610. end
  611. def test_assert_in_epsilon_triggered
  612. util_assert_triggered 'Expected 10000 - 9990 (10) to be < 9.99.' do
  613. @tc.assert_in_epsilon 10000, 9990
  614. end
  615. end
  616. def test_assert_includes
  617. @assertion_count = 2
  618. @tc.assert_includes [true], true
  619. end
  620. def test_assert_includes_triggered
  621. @assertion_count = 3
  622. e = @tc.assert_raises MiniTest::Assertion do
  623. @tc.assert_includes [true], false
  624. end
  625. expected = "Expected [true] to include false."
  626. assert_equal expected, e.message
  627. end
  628. def test_assert_instance_of
  629. @tc.assert_instance_of String, "blah"
  630. end
  631. def test_assert_instance_of_triggered
  632. util_assert_triggered 'Expected "blah" to be an instance of Array, not String.' do
  633. @tc.assert_instance_of Array, "blah"
  634. end
  635. end
  636. def test_assert_kind_of
  637. @tc.assert_kind_of String, "blah"
  638. end
  639. def test_assert_kind_of_triggered
  640. util_assert_triggered 'Expected "blah" to be a kind of Array, not String.' do
  641. @tc.assert_kind_of Array, "blah"
  642. end
  643. end
  644. def test_assert_match
  645. @assertion_count = 2
  646. @tc.assert_match(/\w+/, "blah blah blah")
  647. end
  648. def test_assert_match_object
  649. @assertion_count = 2
  650. pattern = Object.new
  651. def pattern.=~(other) true end
  652. @tc.assert_match pattern, 5
  653. end
  654. def test_assert_match_object_triggered
  655. @assertion_count = 2
  656. pattern = Object.new
  657. def pattern.=~(other) false end
  658. def pattern.inspect; "[Object]" end
  659. util_assert_triggered 'Expected [Object] to match 5.' do
  660. @tc.assert_match pattern, 5
  661. end
  662. end
  663. def test_assert_match_triggered
  664. @assertion_count = 2
  665. util_assert_triggered 'Expected /\d+/ to match "blah blah blah".' do
  666. @tc.assert_match(/\d+/, "blah blah blah")
  667. end
  668. end
  669. def test_assert_nil
  670. @tc.assert_nil nil
  671. end
  672. def test_assert_nil_triggered
  673. util_assert_triggered 'Expected 42 to be nil.' do
  674. @tc.assert_nil 42
  675. end
  676. end
  677. def test_assert_operator
  678. @tc.assert_operator 2, :>, 1
  679. end
  680. def test_assert_operator_triggered
  681. util_assert_triggered "Expected 2 to be < 1." do
  682. @tc.assert_operator 2, :<, 1
  683. end
  684. end
  685. def test_assert_output_both
  686. @assertion_count = 2
  687. @tc.assert_output "yay", "blah" do
  688. print "yay"
  689. $stderr.print "blah"
  690. end
  691. end
  692. def test_assert_output_err
  693. @tc.assert_output nil, "blah" do
  694. $stderr.print "blah"
  695. end
  696. end
  697. def test_assert_output_neither
  698. @assertion_count = 0
  699. @tc.assert_output do
  700. # do nothing
  701. end
  702. end
  703. def test_assert_output_out
  704. @tc.assert_output "blah" do
  705. print "blah"
  706. end
  707. end
  708. def test_assert_output_triggered_both
  709. util_assert_triggered util_msg("yay", "boo", "In stdout") do
  710. @tc.assert_output "yay", "blah" do
  711. print "boo"
  712. $stderr.print "blah blah"
  713. end
  714. end
  715. end
  716. def test_assert_output_triggered_err
  717. util_assert_triggered util_msg("blah", "blah blah", "In stderr") do
  718. @tc.assert_output nil, "blah" do
  719. $stderr.print "blah blah"
  720. end
  721. end
  722. end
  723. def test_assert_output_triggered_out
  724. util_assert_triggered util_msg("blah", "blah blah", "In stdout") do
  725. @tc.assert_output "blah" do
  726. print "blah blah"
  727. end
  728. end
  729. end
  730. def test_assert_raises
  731. @tc.assert_raises RuntimeError do
  732. raise "blah"
  733. end
  734. end
  735. ##
  736. # *sigh* This is quite an odd scenario, but it is from real (albeit
  737. # ugly) test code in ruby-core:
  738. #
  739. # http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29259
  740. def test_assert_raises_skip
  741. @assertion_count = 0
  742. util_assert_triggered "skipped", MiniTest::Skip do
  743. @tc.assert_raises ArgumentError do
  744. begin
  745. raise "blah"
  746. rescue
  747. skip "skipped"
  748. end
  749. end
  750. end
  751. end
  752. def test_assert_raises_module
  753. @tc.assert_raises MyModule do
  754. raise AnError
  755. end
  756. end
  757. def test_assert_raises_triggered_different
  758. e = assert_raises MiniTest::Assertion do
  759. @tc.assert_raises RuntimeError do
  760. raise SyntaxError, "icky"
  761. end
  762. end
  763. expected = "[RuntimeError] exception expected, not
  764. Class: <SyntaxError>
  765. Message: <\"icky\">
  766. ---Backtrace---
  767. FILE:LINE:in `test_assert_raises_triggered_different'
  768. ---------------"
  769. actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')
  770. actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION =~ /^1\.9/
  771. assert_equal expected, actual
  772. end
  773. def test_assert_raises_triggered_different_msg
  774. e = assert_raises MiniTest::Assertion do
  775. @tc.assert_raises RuntimeError, "XXX" do
  776. raise SyntaxError, "icky"
  777. end
  778. end
  779. expected = "XXX
  780. [RuntimeError] exception expected, not
  781. Class: <SyntaxError>
  782. Message: <\"icky\">
  783. ---Backtrace---
  784. FILE:LINE:in `test_assert_raises_triggered_different_msg'
  785. ---------------"
  786. actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')
  787. actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION =~ /^1\.9/
  788. assert_equal expected, actual
  789. end
  790. def test_assert_raises_triggered_none
  791. e = assert_raises MiniTest::Assertion do
  792. @tc.assert_raises MiniTest::Assertion do
  793. # do nothing
  794. end
  795. end
  796. expected = "MiniTest::Assertion expected but nothing was raised."
  797. assert_equal expected, e.message
  798. end
  799. def test_assert_raises_triggered_none_msg
  800. e = assert_raises MiniTest::Assertion do
  801. @tc.assert_raises MiniTest::Assertion, "XXX" do
  802. # do nothing
  803. end
  804. end
  805. expected = "XXX\nMiniTest::Assertion expected but nothing was raised."
  806. assert_equal expected, e.message
  807. end
  808. def test_assert_raises_triggered_subclass
  809. e = assert_raises MiniTest::Assertion do
  810. @tc.assert_raises StandardError do
  811. raise AnError
  812. end
  813. end
  814. expected = "[StandardError] exception expected, not
  815. Class: <AnError>
  816. Message: <\"AnError\">
  817. ---Backtrace---
  818. FILE:LINE:in `test_assert_raises_triggered_subclass'
  819. ---------------"
  820. actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')
  821. actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION =~ /^1\.9/
  822. assert_equal expected, actual
  823. end
  824. def test_assert_respond_to
  825. @tc.assert_respond_to "blah", :empty?
  826. end
  827. def test_assert_respond_to_triggered
  828. util_assert_triggered 'Expected "blah" (String) to respond to #rawr!.' do
  829. @tc.assert_respond_to "blah", :rawr!
  830. end
  831. end
  832. def test_assert_same
  833. @assertion_count = 3
  834. o = "blah"
  835. @tc.assert_same 1, 1
  836. @tc.assert_same :blah, :blah
  837. @tc.assert_same o, o
  838. end
  839. def test_assert_same_triggered
  840. @assertion_count = 2
  841. util_assert_triggered 'Expected 2 (oid=N) to be the same as 1 (oid=N).' do
  842. @tc.assert_same 1, 2
  843. end
  844. s1 = "blah"
  845. s2 = "blah"
  846. util_assert_triggered 'Expected "blah" (oid=N) to be the same as "blah" (oid=N).' do
  847. @tc.assert_same s1, s2
  848. end
  849. end
  850. def test_assert_send
  851. @tc.assert_send [1, :<, 2]
  852. end
  853. def test_assert_send_bad
  854. util_assert_triggered "Expected 1.>(*[2]) to return true." do
  855. @tc.assert_send [1, :>, 2]
  856. end
  857. end
  858. def test_assert_silent
  859. @assertion_count = 2
  860. @tc.assert_silent do
  861. # do nothing
  862. end
  863. end
  864. def test_assert_silent_triggered_err
  865. @assertion_count = 2
  866. util_assert_triggered util_msg("", "blah blah", "In stderr") do
  867. @tc.assert_silent do
  868. $stderr.print "blah blah"
  869. end
  870. end
  871. end
  872. def test_assert_silent_triggered_out
  873. util_assert_triggered util_msg("", "blah blah", "In stdout") do
  874. @tc.assert_silent do
  875. print "blah blah"
  876. end
  877. end
  878. end
  879. def test_assert_throws
  880. @tc.assert_throws(:blah) do
  881. throw :blah
  882. end
  883. end
  884. def test_assert_throws_different
  885. util_assert_triggered 'Expected :blah to have been thrown, not :not_blah.' do
  886. @tc.assert_throws(:blah) do
  887. throw :not_blah
  888. end
  889. end
  890. end
  891. def test_assert_throws_unthrown
  892. util_assert_triggered 'Expected :blah to have been thrown.' do
  893. @tc.assert_throws(:blah) do
  894. # do nothing
  895. end
  896. end
  897. end
  898. def test_capture_io
  899. @assertion_count = 0
  900. orig_verbose = $VERBOSE
  901. $VERBOSE = false
  902. out, err = capture_io do
  903. puts 'hi'
  904. warn 'bye!'
  905. end
  906. assert_equal "hi\n", out
  907. assert_equal "bye!\n", err
  908. ensure
  909. $VERBOSE = orig_verbose
  910. end
  911. def test_class_asserts_match_refutes
  912. @assertion_count = 0
  913. methods = MiniTest::Assertions.public_instance_methods
  914. methods.map! { |m| m.to_s } if Symbol === methods.first
  915. ignores = %w(assert_block assert_no_match assert_not_equal
  916. assert_not_nil assert_not_same assert_nothing_raised
  917. assert_nothing_thrown assert_output assert_raise
  918. assert_raises assert_send assert_silent assert_throws)
  919. asserts = methods.grep(/^assert/).sort - ignores
  920. refutes = methods.grep(/^refute/).sort - ignores
  921. assert_empty refutes.map { |n| n.sub(/^refute/, 'assert') } - asserts
  922. assert_empty asserts.map { |n| n.sub(/^assert/, 'refute') } - refutes
  923. end
  924. def test_class_inherited
  925. @assertion_count = 0
  926. Object.const_set(:ATestCase, Class.new(MiniTest::Unit::TestCase))
  927. assert_equal [ATestCase], MiniTest::Unit::TestCase.test_suites
  928. end
  929. def test_class_test_suites
  930. @assertion_count = 0
  931. Object.const_set(:ATestCase, Class.new(MiniTest::Unit::TestCase))
  932. assert_equal 1, MiniTest::Unit::TestCase.test_suites.size
  933. assert_equal [ATestCase], MiniTest::Unit::TestCase.test_suites
  934. end
  935. def test_flunk
  936. util_assert_triggered 'Epic Fail!' do
  937. @tc.flunk
  938. end
  939. end
  940. def test_flunk_message
  941. util_assert_triggered @zomg do
  942. @tc.flunk @zomg
  943. end
  944. end
  945. def test_message
  946. @assertion_count = 0
  947. assert_equal "blah2.", @tc.message { "blah2" }.call
  948. assert_equal "blah2.", @tc.message("") { "blah2" }.call
  949. assert_equal "blah1.\nblah2.", @tc.message(:blah1) { "blah2" }.call
  950. assert_equal "blah1.\nblah2.", @tc.message("blah1") { "blah2" }.call
  951. end
  952. def test_pass
  953. @tc.pass
  954. end
  955. def test_prints
  956. printer = Class.new { extend MiniTest::Assertions }
  957. @tc.assert_equal '"test"', printer.mu_pp(ImmutableString.new 'test')
  958. end
  959. def test_refute
  960. @assertion_count = 2
  961. @tc.assert_equal false, @tc.refute(false), "returns false on success"
  962. end
  963. def test_refute_empty
  964. @assertion_count = 2
  965. @tc.refute_empty [1]
  966. end
  967. def test_refute_empty_triggered
  968. @assertion_count = 2
  969. util_assert_triggered "Expected [] to not be empty." do
  970. @tc.refute_empty []
  971. end
  972. end
  973. def test_refute_equal
  974. @tc.refute_equal "blah", "yay"
  975. end
  976. def test_refute_equal_triggered
  977. util_assert_triggered 'Expected "blah" to not be equal to "blah".' do
  978. @tc.refute_equal "blah", "blah"
  979. end
  980. end
  981. def test_refute_in_delta
  982. @tc.refute_in_delta 0.0, 1.0 / 1000, 0.000001
  983. end
  984. def test_refute_in_delta_triggered
  985. util_assert_triggered 'Expected 0.0 - 0.001 (0.001) to not be < 0.1.' do
  986. @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1
  987. end
  988. end
  989. def test_refute_in_epsilon
  990. @tc.refute_in_epsilon 10000, 9990
  991. end
  992. def test_refute_in_epsilon_triggered
  993. util_assert_triggered 'Expected 10000 - 9991 (9) to not be < 10.0.' do
  994. @tc.refute_in_epsilon 10000, 9991
  995. fail
  996. end
  997. end
  998. def test_refute_includes
  999. @assertion_count = 2
  1000. @tc.refute_includes [true], false
  1001. end
  1002. def test_refute_includes_triggered
  1003. @assertion_count = 3
  1004. e = @tc.assert_raises MiniTest::Assertion do
  1005. @tc.refute_includes [true], true
  1006. end
  1007. expected = "Expected [true] to not include true."
  1008. assert_equal expected, e.message
  1009. end
  1010. def test_refute_instance_of
  1011. @tc.refute_instance_of Array, "blah"
  1012. end
  1013. def test_refute_instance_of_triggered
  1014. util_assert_triggered 'Expected "blah" to not be an instance of String.' do
  1015. @tc.refute_instance_of String, "blah"
  1016. end
  1017. end
  1018. def test_refute_kind_of
  1019. @tc.refute_kind_of Array, "blah"
  1020. end
  1021. def test_refute_kind_of_triggered
  1022. util_assert_triggered 'Expected "blah" to not be a kind of String.' do
  1023. @tc.refute_kind_of String, "blah"
  1024. end
  1025. end
  1026. def test_refute_match
  1027. @assertion_count = 2
  1028. @tc.refute_match(/\d+/, "blah blah blah")
  1029. end
  1030. def test_refute_match_object
  1031. @assertion_count = 2
  1032. @tc.refute_match Object.new, 5 # default #=~ returns false
  1033. end
  1034. def test_refute_match_object_triggered
  1035. @assertion_count = 2
  1036. pattern = Object.new
  1037. def pattern.=~(other) true end
  1038. def pattern.inspect; "[Object]" end
  1039. util_assert_triggered 'Expected [Object] to not match 5.' do
  1040. @tc.refute_match pattern, 5
  1041. end
  1042. end
  1043. def test_refute_match_triggered
  1044. @assertion_count = 2
  1045. util_assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do
  1046. @tc.refute_match(/\w+/, "blah blah blah")
  1047. end
  1048. end
  1049. def test_refute_nil
  1050. @tc.refute_nil 42
  1051. end
  1052. def test_refute_nil_triggered
  1053. util_assert_triggered 'Expected nil to not be nil.' do
  1054. @tc.refute_nil nil
  1055. end
  1056. end
  1057. def test_refute_operator
  1058. @tc.refute_operator 2, :<, 1
  1059. end
  1060. def test_refute_operator_triggered
  1061. util_assert_triggered "Expected 2 to not be > 1." do
  1062. @tc.refute_operator 2, :>, 1
  1063. end
  1064. end
  1065. def test_refute_respond_to
  1066. @tc.refute_respond_to "blah", :rawr!
  1067. end
  1068. def test_refute_respond_to_triggered
  1069. util_assert_triggered 'Expected "blah" to not respond to empty?.' do
  1070. @tc.refute_respond_to "blah", :empty?
  1071. end
  1072. end
  1073. def test_refute_same
  1074. @tc.refute_same 1, 2
  1075. end
  1076. def test_refute_same_triggered
  1077. util_assert_triggered 'Expected 1 (oid=N) to not be the same as 1 (oid=N).' do
  1078. @tc.refute_same 1, 1
  1079. end
  1080. end
  1081. def test_skip
  1082. @assertion_count = 0
  1083. util_assert_triggered "haha!", MiniTest::Skip do
  1084. @tc.skip "haha!"
  1085. end
  1086. end
  1087. def test_test_methods_random
  1088. @assertion_count = 0
  1089. sample_test_case = Class.new(MiniTest::Unit::TestCase) do
  1090. def test_test1; assert "does not matter" end
  1091. def test_test2; assert "does not matter" end
  1092. def test_test3; assert "does not matter" end
  1093. end
  1094. srand 42
  1095. expected = %w(test_test2 test_test1 test_test3)
  1096. assert_equal expected, sample_test_case.test_methods
  1097. end
  1098. def test_test_methods_sorted
  1099. @assertion_count = 0
  1100. sample_test_case = Class.new(MiniTest::Unit::TestCase) do
  1101. def self.test_order; :sorted end
  1102. def test_test3; assert "does not matter" end
  1103. def test_test2; assert "does not matter" end
  1104. def test_test1; assert "does not matter" end
  1105. end
  1106. expected = %w(test_test1 test_test2 test_test3)
  1107. assert_equal expected, sample_test_case.test_methods
  1108. end
  1109. def util_assert_triggered expected, klass = MiniTest::Assertion
  1110. e = assert_raises(klass) do
  1111. yield
  1112. end
  1113. msg = e.message.sub(/(---Backtrace---).*/m, '\1')
  1114. msg.gsub!(/\(oid=[-0-9]+\)/, '(oid=N)')
  1115. assert_equal expected, msg
  1116. end
  1117. def util_msg exp, act, msg = nil
  1118. s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}"
  1119. s = "#{msg}.\n#{s}" if msg
  1120. s
  1121. end
  1122. def without_diff
  1123. old_diff = MiniTest::Assertions.diff
  1124. MiniTest::Assertions.diff = nil
  1125. yield
  1126. ensure
  1127. MiniTest::Assertions.diff = old_diff
  1128. end
  1129. end