PageRenderTime 36ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/minitest/test_minitest_unit.rb

http://github.com/ruby/ruby
Ruby | 1778 lines | 1573 code | 188 blank | 17 comment | 4 complexity | 7235834dd1c40f8c7775143dc787028a MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # encoding: utf-8
  2. # frozen_string_literal: false
  3. require 'pathname'
  4. require 'minitest/metametameta'
  5. module MyModule; end
  6. class AnError < StandardError; include MyModule; end
  7. class ImmutableString < String; def inspect; super.freeze; end; end
  8. class TestMiniTestUnit < MetaMetaMetaTestCase
  9. pwd = Pathname.new File.expand_path Dir.pwd
  10. basedir = Pathname.new(File.expand_path "lib/minitest") + 'mini'
  11. basedir = basedir.relative_path_from(pwd).to_s
  12. MINITEST_BASE_DIR = basedir[/\A\./] ? basedir : "./#{basedir}"
  13. BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:161:in `each'",
  14. "#{MINITEST_BASE_DIR}/test.rb:158:in `each'",
  15. "#{MINITEST_BASE_DIR}/test.rb:139:in `run'",
  16. "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"]
  17. def test_class_puke_with_assertion_failed
  18. exception = MiniTest::Assertion.new "Oh no!"
  19. exception.set_backtrace ["unhappy"]
  20. assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception)
  21. assert_equal 1, @tu.failures
  22. assert_match(/^Failure.*Oh no!/m, @tu.report.first)
  23. assert_match("SomeClass#method_name [unhappy]", @tu.report.first)
  24. end
  25. def test_class_puke_with_assertion_failed_and_long_backtrace
  26. bt = (["test/test_some_class.rb:615:in `method_name'",
  27. "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'",
  28. "test/test_some_class.rb:615:in `each'",
  29. "test/test_some_class.rb:614:in `test_method_name'",
  30. "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  31. BT_MIDDLE +
  32. ["#{MINITEST_BASE_DIR}/test.rb:29"])
  33. bt = util_expand_bt bt
  34. ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first
  35. exception = MiniTest::Assertion.new "Oh no!"
  36. exception.set_backtrace bt
  37. assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)
  38. assert_equal 1, @tu.failures
  39. assert_match(/^Failure.*Oh no!/m, @tu.report.first)
  40. assert_match("TestSomeClass#test_method_name [#{ex_location}]", @tu.report.first)
  41. end
  42. def test_class_puke_with_assertion_failed_and_user_defined_assertions
  43. bt = (["lib/test/my/util.rb:16:in `another_method_name'",
  44. "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'",
  45. "lib/test/my/util.rb:15:in `block in assert_something'",
  46. "lib/test/my/util.rb:14:in `each'",
  47. "lib/test/my/util.rb:14:in `assert_something'",
  48. "test/test_some_class.rb:615:in `each'",
  49. "test/test_some_class.rb:614:in `test_method_name'",
  50. "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  51. BT_MIDDLE +
  52. ["#{MINITEST_BASE_DIR}/test.rb:29"])
  53. bt = util_expand_bt bt
  54. ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first
  55. exception = MiniTest::Assertion.new "Oh no!"
  56. exception.set_backtrace bt
  57. assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)
  58. assert_equal 1, @tu.failures
  59. assert_match(/^Failure.*Oh no!/m, @tu.report.first)
  60. assert_match("TestSomeClass#test_method_name [#{ex_location}]", @tu.report.first)
  61. end
  62. def test_class_puke_with_failure_and_flunk_in_backtrace
  63. exception = begin
  64. MiniTest::Unit::TestCase.new('fake tc').flunk
  65. rescue MiniTest::Assertion => failure
  66. failure
  67. end
  68. assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception)
  69. refute @tu.report.any?{|line| line =~ /in .flunk/}
  70. end
  71. def test_class_puke_with_flunk_and_user_defined_assertions
  72. bt = (["lib/test/my/util.rb:16:in `flunk'",
  73. "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'",
  74. "lib/test/my/util.rb:15:in `block in assert_something'",
  75. "lib/test/my/util.rb:14:in `each'",
  76. "lib/test/my/util.rb:14:in `assert_something'",
  77. "test/test_some_class.rb:615:in `each'",
  78. "test/test_some_class.rb:614:in `test_method_name'",
  79. "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  80. BT_MIDDLE +
  81. ["#{MINITEST_BASE_DIR}/test.rb:29"])
  82. bt = util_expand_bt bt
  83. ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first
  84. exception = MiniTest::Assertion.new "Oh no!"
  85. exception.set_backtrace bt
  86. assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)
  87. assert_equal 1, @tu.failures
  88. assert_match(/^Failure.*Oh no!/m, @tu.report.first)
  89. assert_match("TestSomeClass#test_method_name [#{ex_location}]", @tu.report.first)
  90. end
  91. def test_class_puke_with_non_failure_exception
  92. exception = Exception.new("Oh no again!")
  93. assert_equal 'E', @tu.puke('SomeClass', 'method_name', exception)
  94. assert_equal 1, @tu.errors
  95. assert_match(/^Exception.*Oh no again!/m, @tu.report.first)
  96. end
  97. def test_filter_backtrace
  98. # this is a semi-lame mix of relative paths.
  99. # I cheated by making the autotest parts not have ./
  100. bt = (["lib/autotest.rb:571:in `add_exception'",
  101. "test/test_autotest.rb:62:in `test_add_exception'",
  102. "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  103. BT_MIDDLE +
  104. ["#{MINITEST_BASE_DIR}/test.rb:29",
  105. "test/test_autotest.rb:422"])
  106. bt = util_expand_bt bt
  107. ex = ["lib/autotest.rb:571:in `add_exception'",
  108. "test/test_autotest.rb:62:in `test_add_exception'"]
  109. ex = util_expand_bt ex
  110. fu = MiniTest::filter_backtrace(bt)
  111. assert_equal ex, fu
  112. end
  113. def test_filter_backtrace_all_unit
  114. bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  115. BT_MIDDLE +
  116. ["#{MINITEST_BASE_DIR}/test.rb:29"])
  117. ex = bt.clone
  118. fu = MiniTest::filter_backtrace(bt)
  119. assert_equal ex, fu
  120. end
  121. def test_filter_backtrace_unit_starts
  122. bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  123. BT_MIDDLE +
  124. ["#{MINITEST_BASE_DIR}/mini/test.rb:29",
  125. "-e:1"])
  126. bt = util_expand_bt bt
  127. ex = ["-e:1"]
  128. fu = MiniTest::filter_backtrace bt
  129. assert_equal ex, fu
  130. end
  131. def test_default_runner_is_minitest_unit
  132. assert_instance_of MiniTest::Unit, MiniTest::Unit.runner
  133. end
  134. def test_passed_eh_teardown_good
  135. test_class = Class.new MiniTest::Unit::TestCase do
  136. def teardown; assert true; end
  137. def test_omg; assert true; end
  138. end
  139. test = test_class.new :test_omg
  140. test.run @tu
  141. assert test.passed?
  142. end
  143. def test_passed_eh_teardown_skipped
  144. test_class = Class.new MiniTest::Unit::TestCase do
  145. def teardown; assert true; end
  146. def test_omg; skip "bork"; end
  147. end
  148. test = test_class.new :test_omg
  149. test.run @tu
  150. assert test.passed?
  151. end
  152. def test_passed_eh_teardown_flunked
  153. test_class = Class.new MiniTest::Unit::TestCase do
  154. def teardown; flunk; end
  155. def test_omg; assert true; end
  156. end
  157. test = test_class.new :test_omg
  158. test.run @tu
  159. refute test.passed?
  160. end
  161. def util_expand_bt bt
  162. bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f }
  163. end
  164. end
  165. class TestMiniTestUnitInherited < MetaMetaMetaTestCase
  166. def with_overridden_include
  167. Class.class_eval do
  168. def inherited_with_hacks klass
  169. throw :inherited_hook
  170. end
  171. alias inherited_without_hacks inherited
  172. alias inherited inherited_with_hacks
  173. alias IGNORE_ME! inherited # 1.8 bug. god I love venture bros
  174. end
  175. yield
  176. ensure
  177. Class.class_eval do
  178. alias inherited inherited_without_hacks
  179. undef_method :inherited_with_hacks
  180. undef_method :inherited_without_hacks
  181. end
  182. refute_respond_to Class, :inherited_with_hacks
  183. refute_respond_to Class, :inherited_without_hacks
  184. end
  185. def test_inherited_hook_plays_nice_with_others
  186. with_overridden_include do
  187. assert_throws :inherited_hook do
  188. Class.new MiniTest::Unit::TestCase
  189. end
  190. end
  191. end
  192. end
  193. class TestMiniTestRunner < MetaMetaMetaTestCase
  194. # do not parallelize this suite... it just can't handle it.
  195. def test_class_test_suites
  196. @assertion_count = 0
  197. tc = Class.new(MiniTest::Unit::TestCase)
  198. assert_equal 1, MiniTest::Unit::TestCase.test_suites.size
  199. assert_equal [tc], MiniTest::Unit::TestCase.test_suites
  200. end
  201. def test_run_test
  202. Class.new MiniTest::Unit::TestCase do
  203. attr_reader :foo
  204. def run_test name
  205. @foo = "hi mom!"
  206. super
  207. @foo = "okay"
  208. end
  209. def test_something
  210. assert_equal "hi mom!", foo
  211. end
  212. end
  213. expected = clean <<-EOM
  214. .
  215. Finished tests in 0.00
  216. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
  217. EOM
  218. assert_report expected
  219. end
  220. def test_run_error
  221. Class.new MiniTest::Unit::TestCase do
  222. def test_something
  223. assert true
  224. end
  225. def test_error
  226. raise "unhandled exception"
  227. end
  228. end
  229. expected = clean <<-EOM
  230. E.
  231. Finished tests in 0.00
  232. 1) Error:
  233. #<Class:0xXXX>#test_error:
  234. RuntimeError: unhandled exception
  235. FILE:LINE:in \`test_error\'
  236. 2 tests, 1 assertions, 0 failures, 1 errors, 0 skips
  237. EOM
  238. assert_report expected
  239. end
  240. def test_run_error_teardown
  241. Class.new MiniTest::Unit::TestCase do
  242. def test_something
  243. assert true
  244. end
  245. def teardown
  246. raise "unhandled exception"
  247. end
  248. end
  249. expected = clean <<-EOM
  250. E
  251. Finished tests in 0.00
  252. 1) Error:
  253. #<Class:0xXXX>#test_something:
  254. RuntimeError: unhandled exception
  255. FILE:LINE:in \`teardown\'
  256. 1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
  257. EOM
  258. assert_report expected
  259. end
  260. def test_run_failing
  261. Class.new MiniTest::Unit::TestCase do
  262. def test_something
  263. assert true
  264. end
  265. def test_failure
  266. assert false
  267. end
  268. end
  269. expected = clean <<-EOM
  270. F.
  271. Finished tests in 0.00
  272. 1) Failure:
  273. #<Class:0xXXX>#test_failure [FILE:LINE]:
  274. Failed assertion, no message given.
  275. 2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
  276. EOM
  277. assert_report expected
  278. end
  279. def test_run_failing_filtered
  280. Class.new MiniTest::Unit::TestCase do
  281. def test_something
  282. assert true
  283. end
  284. def test_failure
  285. assert false
  286. end
  287. end
  288. expected = clean <<-EOM
  289. .
  290. Finished tests in 0.00
  291. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
  292. EOM
  293. assert_report expected, %w[--name /some|thing/ --seed 42]
  294. end
  295. def assert_filtering name, expected, a = false
  296. args = %W[--name #{name} --seed 42]
  297. alpha = Class.new MiniTest::Unit::TestCase do
  298. define_method :test_something do
  299. assert a
  300. end
  301. end
  302. Object.const_set(:Alpha, alpha)
  303. beta = Class.new MiniTest::Unit::TestCase do
  304. define_method :test_something do
  305. assert true
  306. end
  307. end
  308. Object.const_set(:Beta, beta)
  309. assert_report expected, args
  310. ensure
  311. Object.send :remove_const, :Alpha
  312. Object.send :remove_const, :Beta
  313. end
  314. def test_run_filtered_including_suite_name
  315. expected = clean <<-EOM
  316. .
  317. Finished tests in 0.00
  318. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
  319. EOM
  320. assert_filtering "/Beta#test_something/", expected
  321. end
  322. def test_run_filtered_including_suite_name_string
  323. expected = clean <<-EOM
  324. .
  325. Finished tests in 0.00
  326. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
  327. EOM
  328. assert_filtering "Beta#test_something", expected
  329. end
  330. def test_run_filtered_string_method_only
  331. expected = clean <<-EOM
  332. ..
  333. Finished tests in 0.00
  334. 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
  335. EOM
  336. assert_filtering "test_something", expected, :pass
  337. end
  338. def test_run_passing
  339. Class.new MiniTest::Unit::TestCase do
  340. def test_something
  341. assert true
  342. end
  343. end
  344. expected = clean <<-EOM
  345. .
  346. Finished tests in 0.00
  347. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
  348. EOM
  349. assert_report expected
  350. end
  351. def test_run_skip
  352. Class.new MiniTest::Unit::TestCase do
  353. def test_something
  354. assert true
  355. end
  356. def test_skip
  357. skip "not yet"
  358. end
  359. end
  360. expected = clean <<-EOM
  361. S.
  362. Finished tests in 0.00
  363. 2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
  364. EOM
  365. assert_report expected
  366. end
  367. def test_run_skip_verbose
  368. Class.new MiniTest::Unit::TestCase do
  369. def test_something
  370. assert true
  371. end
  372. def test_skip
  373. skip "not yet"
  374. end
  375. end
  376. expected = clean <<-EOM
  377. #<Class:0xXXX>#test_skip = 0.00 s = S
  378. #<Class:0xXXX>#test_something = 0.00 s = .
  379. Finished tests in 0.00
  380. 1) Skipped:
  381. #<Class:0xXXX>#test_skip [FILE:LINE]:
  382. not yet
  383. 2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
  384. EOM
  385. assert_report expected, %w[--seed 42 --verbose]
  386. end
  387. def test_run_with_other_runner
  388. MiniTest::Unit.runner = Class.new MiniTest::Unit do
  389. def _run_suite suite, type
  390. suite.before_suite # Run once before each suite
  391. super suite, type
  392. end
  393. end.new
  394. Class.new MiniTest::Unit::TestCase do
  395. def self.name; "wacky!" end
  396. def self.before_suite
  397. MiniTest::Unit.output.puts "Running #{self.name} tests"
  398. @@foo = 1
  399. end
  400. def test_something
  401. assert_equal 1, @@foo
  402. end
  403. def test_something_else
  404. assert_equal 1, @@foo
  405. end
  406. end
  407. expected = clean <<-EOM
  408. Running wacky! tests
  409. ..
  410. Finished tests in 0.00
  411. 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips
  412. EOM
  413. assert_report expected
  414. end
  415. require 'monitor'
  416. class Latch
  417. def initialize count = 1
  418. @count = count
  419. @lock = Monitor.new
  420. @cv = @lock.new_cond
  421. end
  422. def release
  423. @lock.synchronize do
  424. @count -= 1 if @count > 0
  425. @cv.broadcast if @count == 0
  426. end
  427. end
  428. def await
  429. @lock.synchronize { @cv.wait_while { @count > 0 } }
  430. end
  431. end
  432. end
  433. class TestMiniTestUnitOrder < MetaMetaMetaTestCase
  434. # do not parallelize this suite... it just can't handle it.
  435. def test_before_setup
  436. call_order = []
  437. Class.new MiniTest::Unit::TestCase do
  438. define_method :setup do
  439. super()
  440. call_order << :setup
  441. end
  442. define_method :before_setup do
  443. call_order << :before_setup
  444. end
  445. def test_omg; assert true; end
  446. end
  447. with_output do
  448. @tu.run %w[--seed 42]
  449. end
  450. expected = [:before_setup, :setup]
  451. assert_equal expected, call_order
  452. end
  453. def test_after_teardown
  454. call_order = []
  455. Class.new MiniTest::Unit::TestCase do
  456. define_method :teardown do
  457. super()
  458. call_order << :teardown
  459. end
  460. define_method :after_teardown do
  461. call_order << :after_teardown
  462. end
  463. def test_omg; assert true; end
  464. end
  465. with_output do
  466. @tu.run %w[--seed 42]
  467. end
  468. expected = [:teardown, :after_teardown]
  469. assert_equal expected, call_order
  470. end
  471. def test_all_teardowns_are_guaranteed_to_run
  472. call_order = []
  473. Class.new MiniTest::Unit::TestCase do
  474. define_method :after_teardown do
  475. super()
  476. call_order << :after_teardown
  477. raise
  478. end
  479. define_method :teardown do
  480. super()
  481. call_order << :teardown
  482. raise
  483. end
  484. define_method :before_teardown do
  485. super()
  486. call_order << :before_teardown
  487. raise
  488. end
  489. def test_omg; assert true; end
  490. end
  491. with_output do
  492. @tu.run %w[--seed 42]
  493. end
  494. expected = [:before_teardown, :teardown, :after_teardown]
  495. assert_equal expected, call_order
  496. end
  497. def test_setup_and_teardown_survive_inheritance
  498. call_order = []
  499. parent = Class.new MiniTest::Unit::TestCase do
  500. define_method :setup do
  501. call_order << :setup_method
  502. end
  503. define_method :teardown do
  504. call_order << :teardown_method
  505. end
  506. define_method :test_something do
  507. call_order << :test
  508. end
  509. end
  510. _ = Class.new parent
  511. with_output do
  512. @tu.run %w[--seed 42]
  513. end
  514. # Once for the parent class, once for the child
  515. expected = [:setup_method, :test, :teardown_method] * 2
  516. assert_equal expected, call_order
  517. end
  518. end
  519. class TestMiniTestUnitTestCase < MiniTest::Unit::TestCase
  520. # do not call parallelize_me! - teardown accesses @tc._assertions
  521. # which is not threadsafe. Nearly every method in here is an
  522. # assertion test so it isn't worth splitting it out further.
  523. RUBY18 = ! defined? Encoding
  524. def setup
  525. super
  526. MiniTest::Unit::TestCase.reset
  527. @tc = MiniTest::Unit::TestCase.new 'fake tc'
  528. @zomg = "zomg ponies!"
  529. @assertion_count = 1
  530. end
  531. def teardown
  532. assert_equal(@assertion_count, @tc._assertions,
  533. "expected #{@assertion_count} assertions to be fired during the test, not #{@tc._assertions}") if @tc.passed?
  534. end
  535. def non_verbose
  536. orig_verbose = $VERBOSE
  537. $VERBOSE = false
  538. yield
  539. ensure
  540. $VERBOSE = orig_verbose
  541. end
  542. def test_assert
  543. @assertion_count = 2
  544. @tc.assert_equal true, @tc.assert(true), "returns true on success"
  545. end
  546. def test_assert__triggered
  547. util_assert_triggered "Failed assertion, no message given." do
  548. @tc.assert false
  549. end
  550. end
  551. def test_assert__triggered_message
  552. util_assert_triggered @zomg do
  553. @tc.assert false, @zomg
  554. end
  555. end
  556. def test_assert_empty
  557. @assertion_count = 2
  558. @tc.assert_empty []
  559. end
  560. def test_assert_empty_triggered
  561. @assertion_count = 2
  562. util_assert_triggered "Expected [1] to be empty." do
  563. @tc.assert_empty [1]
  564. end
  565. end
  566. def test_assert_equal
  567. @tc.assert_equal 1, 1
  568. end
  569. def test_assert_equal_different_collection_array_hex_invisible
  570. object1 = Object.new
  571. object2 = Object.new
  572. msg = "No visible difference in the Array#inspect output.
  573. You should look at the implementation of #== on Array or its members.
  574. [#<Object:0xXXXXXX>]".gsub(/^ +/, "")
  575. util_assert_triggered msg do
  576. @tc.assert_equal [object1], [object2]
  577. end
  578. end
  579. def test_assert_equal_different_collection_hash_hex_invisible
  580. h1, h2 = {}, {}
  581. h1[1] = Object.new
  582. h2[1] = Object.new
  583. msg = "No visible difference in the Hash#inspect output.
  584. You should look at the implementation of #== on Hash or its members.
  585. {1=>#<Object:0xXXXXXX>}".gsub(/^ +/, "")
  586. util_assert_triggered msg do
  587. @tc.assert_equal h1, h2
  588. end
  589. end
  590. def test_assert_equal_different_diff_deactivated
  591. skip "https://github.com/MagLev/maglev/issues/209" if maglev?
  592. without_diff do
  593. util_assert_triggered util_msg("haha" * 10, "blah" * 10) do
  594. o1 = "haha" * 10
  595. o2 = "blah" * 10
  596. @tc.assert_equal o1, o2
  597. end
  598. end
  599. end
  600. def test_assert_equal_different_hex
  601. c = Class.new do
  602. def initialize s; @name = s; end
  603. end
  604. o1 = c.new "a"
  605. o2 = c.new "b"
  606. msg = "--- expected
  607. +++ actual
  608. @@ -1 +1 @@
  609. -#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"a\">
  610. +#<#<Class:0xXXXXXX>:0xXXXXXX @name=\"b\">
  611. ".gsub(/^ +/, "")
  612. util_assert_triggered msg do
  613. @tc.assert_equal o1, o2
  614. end
  615. end
  616. def test_assert_equal_different_hex_invisible
  617. o1 = Object.new
  618. o2 = Object.new
  619. msg = "No visible difference in the Object#inspect output.
  620. You should look at the implementation of #== on Object or its members.
  621. #<Object:0xXXXXXX>".gsub(/^ +/, "")
  622. util_assert_triggered msg do
  623. @tc.assert_equal o1, o2
  624. end
  625. end
  626. def test_assert_equal_different_long
  627. msg = "--- expected
  628. +++ actual
  629. @@ -1 +1 @@
  630. -\"hahahahahahahahahahahahahahahahahahahaha\"
  631. +\"blahblahblahblahblahblahblahblahblahblah\"
  632. ".gsub(/^ +/, "")
  633. util_assert_triggered msg do
  634. o1 = "haha" * 10
  635. o2 = "blah" * 10
  636. @tc.assert_equal o1, o2
  637. end
  638. end
  639. def test_assert_equal_different_long_invisible
  640. msg = "No visible difference in the String#inspect output.
  641. You should look at the implementation of #== on String or its members.
  642. \"blahblahblahblahblahblahblahblahblahblah\"".gsub(/^ +/, "")
  643. util_assert_triggered msg do
  644. o1 = "blah" * 10
  645. o2 = "blah" * 10
  646. def o1.== o
  647. false
  648. end
  649. @tc.assert_equal o1, o2
  650. end
  651. end
  652. def test_assert_equal_different_long_msg
  653. msg = "message.
  654. --- expected
  655. +++ actual
  656. @@ -1 +1 @@
  657. -\"hahahahahahahahahahahahahahahahahahahaha\"
  658. +\"blahblahblahblahblahblahblahblahblahblah\"
  659. ".gsub(/^ +/, "")
  660. util_assert_triggered msg do
  661. o1 = "haha" * 10
  662. o2 = "blah" * 10
  663. @tc.assert_equal o1, o2, "message"
  664. end
  665. end
  666. def test_assert_equal_different_short
  667. util_assert_triggered util_msg(1, 2) do
  668. @tc.assert_equal 1, 2
  669. end
  670. end
  671. def test_assert_equal_different_short_msg
  672. util_assert_triggered util_msg(1, 2, "message") do
  673. @tc.assert_equal 1, 2, "message"
  674. end
  675. end
  676. def test_assert_equal_different_short_multiline
  677. msg = "--- expected\n+++ actual\n@@ -1,2 +1,2 @@\n \"a\n-b\"\n+c\"\n"
  678. util_assert_triggered msg do
  679. @tc.assert_equal "a\nb", "a\nc"
  680. end
  681. end
  682. def test_assert_in_delta
  683. @tc.assert_in_delta 0.0, 1.0 / 1000, 0.1
  684. end
  685. def test_delta_consistency
  686. @tc.assert_in_delta 0, 1, 1
  687. util_assert_triggered "Expected |0 - 1| (1) to not be <= 1." do
  688. @tc.refute_in_delta 0, 1, 1
  689. end
  690. end
  691. def test_assert_in_delta_triggered
  692. x = maglev? ? "9.999999xxxe-07" : "1.0e-06"
  693. util_assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= #{x}." do
  694. @tc.assert_in_delta 0.0, 1.0 / 1000, 0.000001
  695. end
  696. end
  697. def test_assert_in_epsilon
  698. @assertion_count = 10
  699. @tc.assert_in_epsilon 10000, 9991
  700. @tc.assert_in_epsilon 9991, 10000
  701. @tc.assert_in_epsilon 1.0, 1.001
  702. @tc.assert_in_epsilon 1.001, 1.0
  703. @tc.assert_in_epsilon 10000, 9999.1, 0.0001
  704. @tc.assert_in_epsilon 9999.1, 10000, 0.0001
  705. @tc.assert_in_epsilon 1.0, 1.0001, 0.0001
  706. @tc.assert_in_epsilon 1.0001, 1.0, 0.0001
  707. @tc.assert_in_epsilon(-1, -1)
  708. @tc.assert_in_epsilon(-10000, -9991)
  709. end
  710. def test_epsilon_consistency
  711. @tc.assert_in_epsilon 1.0, 1.001
  712. msg = "Expected |1.0 - 1.001| (0.000999xxx) to not be <= 0.001."
  713. util_assert_triggered msg do
  714. @tc.refute_in_epsilon 1.0, 1.001
  715. end
  716. end
  717. def test_assert_in_epsilon_triggered
  718. util_assert_triggered 'Expected |10000 - 9990| (10) to be <= 9.99.' do
  719. @tc.assert_in_epsilon 10000, 9990
  720. end
  721. end
  722. def test_assert_in_epsilon_triggered_negative_case
  723. x = (RUBY18 and not maglev?) ? "0.1" : "0.100000xxx"
  724. y = maglev? ? "0.100000xxx" : "0.1"
  725. util_assert_triggered "Expected |-1.1 - -1| (#{x}) to be <= #{y}." do
  726. @tc.assert_in_epsilon(-1.1, -1, 0.1)
  727. end
  728. end
  729. def test_assert_includes
  730. @assertion_count = 2
  731. @tc.assert_includes [true], true
  732. end
  733. def test_assert_includes_triggered
  734. @assertion_count = 3
  735. e = @tc.assert_raises MiniTest::Assertion do
  736. @tc.assert_includes [true], false
  737. end
  738. expected = "Expected [true] to include false."
  739. assert_equal expected, e.message
  740. end
  741. def test_assert_instance_of
  742. @tc.assert_instance_of String, "blah"
  743. end
  744. def test_assert_instance_of_triggered
  745. util_assert_triggered 'Expected "blah" to be an instance of Array, not String.' do
  746. @tc.assert_instance_of Array, "blah"
  747. end
  748. end
  749. def test_assert_kind_of
  750. @tc.assert_kind_of String, "blah"
  751. end
  752. def test_assert_kind_of_triggered
  753. util_assert_triggered 'Expected "blah" to be a kind of Array, not String.' do
  754. @tc.assert_kind_of Array, "blah"
  755. end
  756. end
  757. def test_assert_match
  758. @assertion_count = 2
  759. @tc.assert_match(/\w+/, "blah blah blah")
  760. end
  761. def test_assert_match_matcher_object
  762. @assertion_count = 2
  763. pattern = Object.new
  764. def pattern.=~(other) true end
  765. @tc.assert_match pattern, 5
  766. end
  767. def test_assert_match_matchee_to_str
  768. @assertion_count = 2
  769. obj = Object.new
  770. def obj.to_str; "blah" end
  771. @tc.assert_match "blah", obj
  772. end
  773. def test_assert_match_object_triggered
  774. @assertion_count = 2
  775. pattern = Object.new
  776. def pattern.=~(other) false end
  777. def pattern.inspect; "[Object]" end
  778. util_assert_triggered 'Expected [Object] to match 5.' do
  779. @tc.assert_match pattern, 5
  780. end
  781. end
  782. def test_assert_match_triggered
  783. @assertion_count = 2
  784. util_assert_triggered 'Expected /\d+/ to match "blah blah blah".' do
  785. @tc.assert_match(/\d+/, "blah blah blah")
  786. end
  787. end
  788. def test_assert_nil
  789. @tc.assert_nil nil
  790. end
  791. def test_assert_nil_triggered
  792. util_assert_triggered 'Expected 42 to be nil.' do
  793. @tc.assert_nil 42
  794. end
  795. end
  796. def test_assert_operator
  797. @tc.assert_operator 2, :>, 1
  798. end
  799. def test_assert_operator_bad_object
  800. bad = Object.new
  801. def bad.==(other) true end
  802. @tc.assert_operator bad, :equal?, bad
  803. end
  804. def test_assert_operator_triggered
  805. util_assert_triggered "Expected 2 to be < 1." do
  806. @tc.assert_operator 2, :<, 1
  807. end
  808. end
  809. def test_assert_output_both
  810. @assertion_count = 2
  811. @tc.assert_output "yay", "blah" do
  812. print "yay"
  813. $stderr.print "blah"
  814. end
  815. end
  816. def test_assert_output_both_regexps
  817. @assertion_count = 4
  818. @tc.assert_output(/y.y/, /bl.h/) do
  819. print "yay"
  820. $stderr.print "blah"
  821. end
  822. end
  823. def test_assert_output_err
  824. @tc.assert_output nil, "blah" do
  825. $stderr.print "blah"
  826. end
  827. end
  828. def test_assert_output_neither
  829. @assertion_count = 0
  830. @tc.assert_output do
  831. # do nothing
  832. end
  833. end
  834. def test_assert_output_out
  835. @tc.assert_output "blah" do
  836. print "blah"
  837. end
  838. end
  839. def test_assert_output_triggered_both
  840. util_assert_triggered util_msg("blah", "blah blah", "In stderr") do
  841. @tc.assert_output "yay", "blah" do
  842. print "boo"
  843. $stderr.print "blah blah"
  844. end
  845. end
  846. end
  847. def test_assert_output_triggered_err
  848. util_assert_triggered util_msg("blah", "blah blah", "In stderr") do
  849. @tc.assert_output nil, "blah" do
  850. $stderr.print "blah blah"
  851. end
  852. end
  853. end
  854. def test_assert_output_triggered_out
  855. util_assert_triggered util_msg("blah", "blah blah", "In stdout") do
  856. @tc.assert_output "blah" do
  857. print "blah blah"
  858. end
  859. end
  860. end
  861. def test_assert_predicate
  862. @tc.assert_predicate "", :empty?
  863. end
  864. def test_assert_predicate_triggered
  865. util_assert_triggered 'Expected "blah" to be empty?.' do
  866. @tc.assert_predicate "blah", :empty?
  867. end
  868. end
  869. def test_assert_raises
  870. @tc.assert_raises RuntimeError do
  871. raise "blah"
  872. end
  873. end
  874. def test_assert_raises_module
  875. @tc.assert_raises MyModule do
  876. raise AnError
  877. end
  878. end
  879. ##
  880. # *sigh* This is quite an odd scenario, but it is from real (albeit
  881. # ugly) test code in ruby-core:
  882. #
  883. # http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29259
  884. def test_assert_raises_skip
  885. @assertion_count = 0
  886. util_assert_triggered "skipped", MiniTest::Skip do
  887. @tc.assert_raises ArgumentError do
  888. begin
  889. raise "blah"
  890. rescue
  891. skip "skipped"
  892. end
  893. end
  894. end
  895. end
  896. def test_assert_raises_triggered_different
  897. e = assert_raises MiniTest::Assertion do
  898. @tc.assert_raises RuntimeError do
  899. raise SyntaxError, "icky"
  900. end
  901. end
  902. expected = clean <<-EOM.chomp
  903. [RuntimeError] exception expected, not
  904. Class: <SyntaxError>
  905. Message: <\"icky\">
  906. ---Backtrace---
  907. FILE:LINE:in \`test_assert_raises_triggered_different\'
  908. ---------------
  909. EOM
  910. actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')
  911. actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION >= '1.9.0'
  912. assert_equal expected, actual
  913. end
  914. def test_assert_raises_triggered_different_msg
  915. e = assert_raises MiniTest::Assertion do
  916. @tc.assert_raises RuntimeError, "XXX" do
  917. raise SyntaxError, "icky"
  918. end
  919. end
  920. expected = clean <<-EOM
  921. XXX.
  922. [RuntimeError] exception expected, not
  923. Class: <SyntaxError>
  924. Message: <\"icky\">
  925. ---Backtrace---
  926. FILE:LINE:in \`test_assert_raises_triggered_different_msg\'
  927. ---------------
  928. EOM
  929. actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')
  930. actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION >= '1.9.0'
  931. assert_equal expected.chomp, actual
  932. end
  933. def test_assert_raises_triggered_none
  934. e = assert_raises MiniTest::Assertion do
  935. @tc.assert_raises MiniTest::Assertion do
  936. # do nothing
  937. end
  938. end
  939. expected = "MiniTest::Assertion expected but nothing was raised."
  940. assert_equal expected, e.message
  941. end
  942. def test_assert_raises_triggered_none_msg
  943. e = assert_raises MiniTest::Assertion do
  944. @tc.assert_raises MiniTest::Assertion, "XXX" do
  945. # do nothing
  946. end
  947. end
  948. expected = "XXX.\nMiniTest::Assertion expected but nothing was raised."
  949. assert_equal expected, e.message
  950. end
  951. def test_assert_raises_triggered_subclass
  952. e = assert_raises MiniTest::Assertion do
  953. @tc.assert_raises StandardError do
  954. raise AnError
  955. end
  956. end
  957. expected = clean <<-EOM.chomp
  958. [StandardError] exception expected, not
  959. Class: <AnError>
  960. Message: <\"AnError\">
  961. ---Backtrace---
  962. FILE:LINE:in \`test_assert_raises_triggered_subclass\'
  963. ---------------
  964. EOM
  965. actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')
  966. actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION >= '1.9.0'
  967. assert_equal expected, actual
  968. end
  969. def test_assert_respond_to
  970. @tc.assert_respond_to "blah", :empty?
  971. end
  972. def test_assert_respond_to_triggered
  973. util_assert_triggered 'Expected "blah" (String) to respond to #rawr!.' do
  974. @tc.assert_respond_to "blah", :rawr!
  975. end
  976. end
  977. def test_assert_same
  978. @assertion_count = 3
  979. o = "blah"
  980. @tc.assert_same 1, 1
  981. @tc.assert_same :blah, :blah
  982. @tc.assert_same o, o
  983. end
  984. def test_assert_same_triggered
  985. @assertion_count = 2
  986. util_assert_triggered 'Expected 2 (oid=N) to be the same as 1 (oid=N).' do
  987. @tc.assert_same 1, 2
  988. end
  989. s1 = "blah"
  990. s2 = "blah"
  991. util_assert_triggered 'Expected "blah" (oid=N) to be the same as "blah" (oid=N).' do
  992. @tc.assert_same s1, s2
  993. end
  994. end
  995. def test_assert_send
  996. @tc.assert_send [1, :<, 2]
  997. end
  998. def test_assert_send_bad
  999. util_assert_triggered "Expected 1.>(*[2]) to return true." do
  1000. @tc.assert_send [1, :>, 2]
  1001. end
  1002. end
  1003. def test_assert_silent
  1004. @assertion_count = 2
  1005. @tc.assert_silent do
  1006. # do nothing
  1007. end
  1008. end
  1009. def test_assert_silent_triggered_err
  1010. util_assert_triggered util_msg("", "blah blah", "In stderr") do
  1011. @tc.assert_silent do
  1012. $stderr.print "blah blah"
  1013. end
  1014. end
  1015. end
  1016. def test_assert_silent_triggered_out
  1017. @assertion_count = 2
  1018. util_assert_triggered util_msg("", "blah blah", "In stdout") do
  1019. @tc.assert_silent do
  1020. print "blah blah"
  1021. end
  1022. end
  1023. end
  1024. def test_assert_throws
  1025. @tc.assert_throws :blah do
  1026. throw :blah
  1027. end
  1028. end
  1029. def test_assert_throws_different
  1030. util_assert_triggered 'Expected :blah to have been thrown, not :not_blah.' do
  1031. @tc.assert_throws :blah do
  1032. throw :not_blah
  1033. end
  1034. end
  1035. end
  1036. def test_assert_throws_unthrown
  1037. util_assert_triggered 'Expected :blah to have been thrown.' do
  1038. @tc.assert_throws :blah do
  1039. # do nothing
  1040. end
  1041. end
  1042. end
  1043. def test_capture_io
  1044. @assertion_count = 0
  1045. non_verbose do
  1046. out, err = capture_io do
  1047. puts 'hi'
  1048. $stderr.puts 'bye!'
  1049. end
  1050. assert_equal "hi\n", out
  1051. assert_equal "bye!\n", err
  1052. end
  1053. end
  1054. def test_capture_subprocess_io
  1055. @assertion_count = 0
  1056. non_verbose do
  1057. out, err = capture_subprocess_io do
  1058. system("echo", "hi")
  1059. system("echo", "bye!", out: :err)
  1060. end
  1061. assert_equal "hi\n", out
  1062. assert_equal "bye!\n", err
  1063. end
  1064. end
  1065. def test_class_asserts_match_refutes
  1066. @assertion_count = 0
  1067. methods = MiniTest::Assertions.public_instance_methods
  1068. methods.map! { |m| m.to_s } if Symbol === methods.first
  1069. # These don't have corresponding refutes _on purpose_. They're
  1070. # useless and will never be added, so don't bother.
  1071. ignores = %w[assert_output assert_raises assert_send
  1072. assert_silent assert_throws]
  1073. # These are test/unit methods. I'm not actually sure why they're still here
  1074. ignores += %w[assert_no_match assert_not_equal assert_not_nil
  1075. assert_not_same assert_nothing_raised
  1076. assert_nothing_thrown assert_raise]
  1077. asserts = methods.grep(/^assert/).sort - ignores
  1078. refutes = methods.grep(/^refute/).sort - ignores
  1079. assert_empty refutes.map { |n| n.sub(/^refute/, 'assert') } - asserts
  1080. assert_empty asserts.map { |n| n.sub(/^assert/, 'refute') } - refutes
  1081. end
  1082. def test_flunk
  1083. util_assert_triggered 'Epic Fail!' do
  1084. @tc.flunk
  1085. end
  1086. end
  1087. def test_flunk_message
  1088. util_assert_triggered @zomg do
  1089. @tc.flunk @zomg
  1090. end
  1091. end
  1092. def test_message
  1093. @assertion_count = 0
  1094. assert_equal "blah2.", @tc.message { "blah2" }.call
  1095. assert_equal "blah2.", @tc.message("") { "blah2" }.call
  1096. assert_equal "blah1.\nblah2.", @tc.message(:blah1) { "blah2" }.call
  1097. assert_equal "blah1.\nblah2.", @tc.message("blah1") { "blah2" }.call
  1098. message = proc { "blah1" }
  1099. assert_equal "blah1.\nblah2.", @tc.message(message) { "blah2" }.call
  1100. message = @tc.message { "blah1" }
  1101. assert_equal "blah1.\nblah2.", @tc.message(message) { "blah2" }.call
  1102. end
  1103. def test_message_message
  1104. util_assert_triggered "whoops.\nExpected: 1\n Actual: 2" do
  1105. @tc.assert_equal 1, 2, message { "whoops" }
  1106. end
  1107. end
  1108. def test_message_lambda
  1109. util_assert_triggered "whoops.\nExpected: 1\n Actual: 2" do
  1110. @tc.assert_equal 1, 2, lambda { "whoops" }
  1111. end
  1112. end
  1113. def test_message_deferred
  1114. @assertion_count, var = 0, nil
  1115. msg = message { var = "blah" }
  1116. assert_nil var
  1117. msg.call
  1118. assert_equal "blah", var
  1119. end
  1120. def test_pass
  1121. @tc.pass
  1122. end
  1123. def test_prints
  1124. printer = Class.new { extend MiniTest::Assertions }
  1125. @tc.assert_equal '"test"', printer.mu_pp(ImmutableString.new 'test')
  1126. end
  1127. def test_refute
  1128. @assertion_count = 2
  1129. @tc.assert_equal false, @tc.refute(false), "returns false on success"
  1130. end
  1131. def test_refute_empty
  1132. @assertion_count = 2
  1133. @tc.refute_empty [1]
  1134. end
  1135. def test_refute_empty_triggered
  1136. @assertion_count = 2
  1137. util_assert_triggered "Expected [] to not be empty." do
  1138. @tc.refute_empty []
  1139. end
  1140. end
  1141. def test_refute_equal
  1142. @tc.refute_equal "blah", "yay"
  1143. end
  1144. def test_refute_equal_triggered
  1145. util_assert_triggered 'Expected "blah" to not be equal to "blah".' do
  1146. @tc.refute_equal "blah", "blah"
  1147. end
  1148. end
  1149. def test_refute_in_delta
  1150. @tc.refute_in_delta 0.0, 1.0 / 1000, 0.000001
  1151. end
  1152. def test_refute_in_delta_triggered
  1153. x = maglev? ? "0.100000xxx" : "0.1"
  1154. util_assert_triggered "Expected |0.0 - 0.001| (0.001) to not be <= #{x}." do
  1155. @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1
  1156. end
  1157. end
  1158. def test_refute_in_epsilon
  1159. @tc.refute_in_epsilon 10000, 9990-1
  1160. end
  1161. def test_refute_in_epsilon_triggered
  1162. util_assert_triggered 'Expected |10000 - 9990| (10) to not be <= 10.0.' do
  1163. @tc.refute_in_epsilon 10000, 9990
  1164. fail
  1165. end
  1166. end
  1167. def test_refute_includes
  1168. @assertion_count = 2
  1169. @tc.refute_includes [true], false
  1170. end
  1171. def test_refute_includes_triggered
  1172. @assertion_count = 3
  1173. e = @tc.assert_raises MiniTest::Assertion do
  1174. @tc.refute_includes [true], true
  1175. end
  1176. expected = "Expected [true] to not include true."
  1177. assert_equal expected, e.message
  1178. end
  1179. def test_refute_instance_of
  1180. @tc.refute_instance_of Array, "blah"
  1181. end
  1182. def test_refute_instance_of_triggered
  1183. util_assert_triggered 'Expected "blah" to not be an instance of String.' do
  1184. @tc.refute_instance_of String, "blah"
  1185. end
  1186. end
  1187. def test_refute_kind_of
  1188. @tc.refute_kind_of Array, "blah"
  1189. end
  1190. def test_refute_kind_of_triggered
  1191. util_assert_triggered 'Expected "blah" to not be a kind of String.' do
  1192. @tc.refute_kind_of String, "blah"
  1193. end
  1194. end
  1195. def test_refute_match
  1196. @assertion_count = 2
  1197. @tc.refute_match(/\d+/, "blah blah blah")
  1198. end
  1199. def test_refute_match_matcher_object
  1200. @assertion_count = 2
  1201. @tc.refute_match Object.new, 5 # default #=~ returns false
  1202. end
  1203. def test_refute_match_object_triggered
  1204. @assertion_count = 2
  1205. pattern = Object.new
  1206. def pattern.=~(other) true end
  1207. def pattern.inspect; "[Object]" end
  1208. util_assert_triggered 'Expected [Object] to not match 5.' do
  1209. @tc.refute_match pattern, 5
  1210. end
  1211. end
  1212. def test_refute_match_triggered
  1213. @assertion_count = 2
  1214. util_assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do
  1215. @tc.refute_match(/\w+/, "blah blah blah")
  1216. end
  1217. end
  1218. def test_refute_nil
  1219. @tc.refute_nil 42
  1220. end
  1221. def test_refute_nil_triggered
  1222. util_assert_triggered 'Expected nil to not be nil.' do
  1223. @tc.refute_nil nil
  1224. end
  1225. end
  1226. def test_refute_predicate
  1227. @tc.refute_predicate "42", :empty?
  1228. end
  1229. def test_refute_predicate_triggered
  1230. util_assert_triggered 'Expected "" to not be empty?.' do
  1231. @tc.refute_predicate "", :empty?
  1232. end
  1233. end
  1234. def test_refute_operator
  1235. @tc.refute_operator 2, :<, 1
  1236. end
  1237. def test_refute_operator_bad_object
  1238. bad = Object.new
  1239. def bad.==(other) true end
  1240. @tc.refute_operator true, :equal?, bad
  1241. end
  1242. def test_refute_operator_triggered
  1243. util_assert_triggered "Expected 2 to not be > 1." do
  1244. @tc.refute_operator 2, :>, 1
  1245. end
  1246. end
  1247. def test_refute_respond_to
  1248. @tc.refute_respond_to "blah", :rawr!
  1249. end
  1250. def test_refute_respond_to_triggered
  1251. util_assert_triggered 'Expected "blah" to not respond to empty?.' do
  1252. @tc.refute_respond_to "blah", :empty?
  1253. end
  1254. end
  1255. def test_refute_same
  1256. @tc.refute_same 1, 2
  1257. end
  1258. def test_refute_same_triggered
  1259. util_assert_triggered 'Expected 1 (oid=N) to not be the same as 1 (oid=N).' do
  1260. @tc.refute_same 1, 1
  1261. end
  1262. end
  1263. def test_skip
  1264. @assertion_count = 0
  1265. util_assert_triggered "haha!", MiniTest::Skip do
  1266. @tc.skip "haha!"
  1267. end
  1268. end
  1269. def test_test_methods_random
  1270. @assertion_count = 0
  1271. sample_test_case = Class.new MiniTest::Unit::TestCase do
  1272. def self.test_order; :random; end
  1273. def test_test1; assert "does not matter" end
  1274. def test_test2; assert "does not matter" end
  1275. def test_test3; assert "does not matter" end
  1276. @test_order = [1, 0, 2]
  1277. def self.rand(n) @test_order.shift; end
  1278. end
  1279. expected = %w(test_test2 test_test1 test_test3)
  1280. assert_equal expected, sample_test_case.test_methods
  1281. end
  1282. def test_test_methods_sorted
  1283. @assertion_count = 0
  1284. sample_test_case = Class.new MiniTest::Unit::TestCase do
  1285. def self.test_order; :sorted end
  1286. def test_test3; assert "does not matter" end
  1287. def test_test2; assert "does not matter" end
  1288. def test_test1; assert "does not matter" end
  1289. end
  1290. expected = %w(test_test1 test_test2 test_test3)
  1291. assert_equal expected, sample_test_case.test_methods
  1292. end
  1293. def util_assert_triggered expected, klass = MiniTest::Assertion
  1294. e = assert_raises klass do
  1295. yield
  1296. end
  1297. msg = e.message.sub(/(---Backtrace---).*/m, '\1')
  1298. msg.gsub!(/\(oid=[-0-9]+\)/, '(oid=N)')
  1299. msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform
  1300. assert_equal expected, msg
  1301. end
  1302. def util_msg exp, act, msg = nil
  1303. s = "Expected: #{exp.inspect}\n Actual: #{act.inspect}"
  1304. s = "#{msg}.\n#{s}" if msg
  1305. s
  1306. end
  1307. def without_diff
  1308. old_diff = MiniTest::Assertions.diff
  1309. MiniTest::Assertions.diff = nil
  1310. yield
  1311. ensure
  1312. MiniTest::Assertions.diff = old_diff
  1313. end
  1314. end
  1315. class TestMiniTestGuard < MiniTest::Unit::TestCase
  1316. def test_mri_eh
  1317. assert self.class.mri? "ruby blah"
  1318. assert self.mri? "ruby blah"
  1319. end
  1320. def test_jruby_eh
  1321. assert self.class.jruby? "java"
  1322. assert self.jruby? "java"
  1323. end
  1324. def test_rubinius_eh
  1325. assert self.class.rubinius? "rbx"
  1326. assert self.rubinius? "rbx"
  1327. end
  1328. def test_windows_eh
  1329. assert self.class.windows? "mswin"
  1330. assert self.windows? "mswin"
  1331. end
  1332. end
  1333. class TestMiniTestUnitRecording < MetaMetaMetaTestCase
  1334. # do not parallelize this suite... it just can't handle it.
  1335. def assert_run_record(*expected, &block)
  1336. def @tu.record suite, method, assertions, time, error
  1337. recording[method] << error
  1338. end
  1339. def @tu.recording
  1340. @recording ||= Hash.new { |h,k| h[k] = [] }
  1341. end
  1342. MiniTest::Unit.runner = @tu
  1343. Class.new MiniTest::Unit::TestCase, &block
  1344. with_output do
  1345. @tu.run
  1346. end
  1347. recorded = @tu.recording.fetch("test_method").map(&:class)
  1348. assert_equal expected, recorded
  1349. end
  1350. def test_record_passing
  1351. assert_run_record NilClass do
  1352. def test_method
  1353. assert true
  1354. end
  1355. end
  1356. end
  1357. def test_record_failing
  1358. assert_run_record MiniTest::Assertion do
  1359. def test_method
  1360. assert false
  1361. end
  1362. end
  1363. end
  1364. def test_record_error
  1365. assert_run_record RuntimeError do
  1366. def test_method
  1367. raise "unhandled exception"
  1368. end
  1369. end
  1370. end
  1371. def test_record_error_teardown
  1372. assert_run_record NilClass, RuntimeError do
  1373. def test_method
  1374. assert true
  1375. end
  1376. def teardown
  1377. raise "unhandled exception"
  1378. end
  1379. end
  1380. end
  1381. def test_record_error_in_test_and_teardown
  1382. assert_run_record AnError, RuntimeError do
  1383. def test_method
  1384. raise AnError
  1385. end
  1386. def teardown
  1387. raise "unhandled exception"
  1388. end
  1389. end
  1390. end
  1391. def test_record_skip
  1392. assert_run_record MiniTest::Skip do
  1393. def test_method
  1394. skip "not yet"
  1395. end
  1396. end
  1397. end
  1398. end