PageRenderTime 34ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 1020 lines | 808 code | 202 blank | 10 comment | 5 complexity | 62428d7a53f50db62300dfb3db0f115c MD5 | raw file
  1. ############################################################
  2. # This file is imported from a different project.
  3. # DO NOT make modifications in this repo.
  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 M; end
  11. class E < StandardError; include M; end
  12. class TestMiniTest < MiniTest::Unit::TestCase
  13. def setup
  14. srand 42
  15. MiniTest::Unit::TestCase.reset
  16. @tu = MiniTest::Unit.new
  17. @output = StringIO.new("")
  18. MiniTest::Unit.output = @output
  19. assert_equal [0, 0], @tu.run_test_suites
  20. end
  21. def teardown
  22. MiniTest::Unit.output = $stdout
  23. Object.send :remove_const, :ATestCase if defined? ATestCase
  24. end
  25. pwd = Pathname.new(File.expand_path(Dir.pwd))
  26. basedir = Pathname.new(File.expand_path(MiniTest::MINI_DIR)) + 'mini'
  27. basedir = basedir.relative_path_from(pwd).to_s
  28. MINITEST_BASE_DIR = basedir[/\A\./] ? basedir : "./#{basedir}"
  29. BT_MIDDLE = ["#{MINITEST_BASE_DIR}/test.rb:165:in `run_test_suites'",
  30. "#{MINITEST_BASE_DIR}/test.rb:161:in `each'",
  31. "#{MINITEST_BASE_DIR}/test.rb:161:in `run_test_suites'",
  32. "#{MINITEST_BASE_DIR}/test.rb:158:in `each'",
  33. "#{MINITEST_BASE_DIR}/test.rb:158:in `run_test_suites'",
  34. "#{MINITEST_BASE_DIR}/test.rb:139:in `run'",
  35. "#{MINITEST_BASE_DIR}/test.rb:106:in `run'"]
  36. def test_filter_backtrace
  37. # this is a semi-lame mix of relative paths.
  38. # I cheated by making the autotest parts not have ./
  39. bt = (["lib/autotest.rb:571:in `add_exception'",
  40. "test/test_autotest.rb:62:in `test_add_exception'",
  41. "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  42. BT_MIDDLE +
  43. ["#{MINITEST_BASE_DIR}/test.rb:29",
  44. "test/test_autotest.rb:422"])
  45. bt = util_expand_bt bt
  46. ex = ["lib/autotest.rb:571:in `add_exception'",
  47. "test/test_autotest.rb:62:in `test_add_exception'"]
  48. ex = util_expand_bt ex
  49. fu = MiniTest::filter_backtrace(bt)
  50. assert_equal ex, fu
  51. end
  52. def util_expand_bt bt
  53. if RUBY_VERSION =~ /^1\.9/ then
  54. bt.map { |f| (f =~ /^\./) ? File.expand_path(f) : f }
  55. else
  56. bt
  57. end
  58. end
  59. def test_filter_backtrace_all_unit
  60. bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  61. BT_MIDDLE +
  62. ["#{MINITEST_BASE_DIR}/test.rb:29"])
  63. ex = bt.clone
  64. fu = MiniTest::filter_backtrace(bt)
  65. assert_equal ex, fu
  66. end
  67. def test_filter_backtrace_unit_starts
  68. bt = (["#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  69. BT_MIDDLE +
  70. ["#{MINITEST_BASE_DIR}/mini/test.rb:29",
  71. "-e:1"])
  72. bt = util_expand_bt bt
  73. ex = ["-e:1"]
  74. fu = MiniTest::filter_backtrace(bt)
  75. assert_equal ex, fu
  76. end
  77. def test_class_puke_with_assertion_failed
  78. exception = MiniTest::Assertion.new "Oh no!"
  79. exception.set_backtrace ["unhappy"]
  80. assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception)
  81. assert_equal 1, @tu.failures
  82. assert_match(/^Failure.*Oh no!/m, @tu.report.first)
  83. assert_match("method_name(SomeClass) [unhappy]", @tu.report.first)
  84. end
  85. def test_class_puke_with_failure_and_flunk_in_backtrace
  86. exception = begin
  87. MiniTest::Unit::TestCase.new('fake tc').flunk
  88. rescue MiniTest::Assertion => failure
  89. failure
  90. end
  91. assert_equal 'F', @tu.puke('SomeClass', 'method_name', exception)
  92. refute @tu.report.any?{|line| line =~ /in .flunk/}
  93. end
  94. def test_class_puke_with_assertion_failed_and_long_backtrace
  95. bt = (["test/test_some_class.rb:615:in `method_name'",
  96. "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'",
  97. "test/test_some_class.rb:615:in `each'",
  98. "test/test_some_class.rb:614:in `test_method_name'",
  99. "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  100. BT_MIDDLE +
  101. ["#{MINITEST_BASE_DIR}/test.rb:29"])
  102. bt = util_expand_bt bt
  103. ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first
  104. exception = MiniTest::Assertion.new "Oh no!"
  105. exception.set_backtrace bt
  106. assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)
  107. assert_equal 1, @tu.failures
  108. assert_match(/^Failure.*Oh no!/m, @tu.report.first)
  109. assert_match("test_method_name(TestSomeClass) [#{ex_location}]", @tu.report.first)
  110. end
  111. def test_class_puke_with_assertion_failed_and_user_defined_assertions
  112. bt = (["lib/test/my/util.rb:16:in `another_method_name'",
  113. "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'",
  114. "lib/test/my/util.rb:15:in `block in assert_something'",
  115. "lib/test/my/util.rb:14:in `each'",
  116. "lib/test/my/util.rb:14:in `assert_something'",
  117. "test/test_some_class.rb:615:in `each'",
  118. "test/test_some_class.rb:614:in `test_method_name'",
  119. "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  120. BT_MIDDLE +
  121. ["#{MINITEST_BASE_DIR}/test.rb:29"])
  122. bt = util_expand_bt bt
  123. ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first
  124. exception = MiniTest::Assertion.new "Oh no!"
  125. exception.set_backtrace bt
  126. assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)
  127. assert_equal 1, @tu.failures
  128. assert_match(/^Failure.*Oh no!/m, @tu.report.first)
  129. assert_match("test_method_name(TestSomeClass) [#{ex_location}]", @tu.report.first)
  130. end
  131. def test_class_puke_with_flunk_and_user_defined_assertions
  132. bt = (["lib/test/my/util.rb:16:in `flunk'",
  133. "#{MINITEST_BASE_DIR}/unit.rb:140:in `assert_raises'",
  134. "lib/test/my/util.rb:15:in `block in assert_something'",
  135. "lib/test/my/util.rb:14:in `each'",
  136. "lib/test/my/util.rb:14:in `assert_something'",
  137. "test/test_some_class.rb:615:in `each'",
  138. "test/test_some_class.rb:614:in `test_method_name'",
  139. "#{MINITEST_BASE_DIR}/test.rb:165:in `__send__'"] +
  140. BT_MIDDLE +
  141. ["#{MINITEST_BASE_DIR}/test.rb:29"])
  142. bt = util_expand_bt bt
  143. ex_location = util_expand_bt(["test/test_some_class.rb:615"]).first
  144. exception = MiniTest::Assertion.new "Oh no!"
  145. exception.set_backtrace bt
  146. assert_equal 'F', @tu.puke('TestSomeClass', 'test_method_name', exception)
  147. assert_equal 1, @tu.failures
  148. assert_match(/^Failure.*Oh no!/m, @tu.report.first)
  149. assert_match("test_method_name(TestSomeClass) [#{ex_location}]", @tu.report.first)
  150. end
  151. def test_class_puke_with_non_failure_exception
  152. exception = Exception.new("Oh no again!")
  153. assert_equal 'E', @tu.puke('SomeClass', 'method_name', exception)
  154. assert_equal 1, @tu.errors
  155. assert_match(/^Exception.*Oh no again!/m, @tu.report.first)
  156. end
  157. def test_class_run_test_suites
  158. tc = Class.new(MiniTest::Unit::TestCase) do
  159. def test_something
  160. assert true
  161. end
  162. end
  163. Object.const_set(:ATestCase, tc)
  164. assert_equal [1, 1], @tu.run_test_suites
  165. end
  166. def test_run_failing # TODO: add error test
  167. tc = Class.new(MiniTest::Unit::TestCase) do
  168. def test_something
  169. assert true
  170. end
  171. def test_failure
  172. assert false
  173. end
  174. end
  175. Object.const_set(:ATestCase, tc)
  176. @tu.run %w[-s 42]
  177. expected = "Loaded suite blah
  178. Started
  179. F.
  180. Finished in 0.00
  181. 1) Failure:
  182. test_failure(ATestCase) [FILE:LINE]:
  183. Failed assertion, no message given.
  184. 2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
  185. Test run options: --seed 42
  186. "
  187. util_assert_report expected
  188. end
  189. def test_run_error
  190. tc = Class.new(MiniTest::Unit::TestCase) do
  191. def test_something
  192. assert true
  193. end
  194. def test_error
  195. raise "unhandled exception"
  196. end
  197. end
  198. Object.const_set(:ATestCase, tc)
  199. @tu.run %w[-s 42]
  200. expected = "Loaded suite blah
  201. Started
  202. E.
  203. Finished in 0.00
  204. 1) Error:
  205. test_error(ATestCase):
  206. RuntimeError: unhandled exception
  207. FILE:LINE:in `test_error'
  208. 2 tests, 1 assertions, 0 failures, 1 errors, 0 skips
  209. Test run options: --seed 42
  210. "
  211. util_assert_report expected
  212. end
  213. def test_run_error_teardown
  214. tc = Class.new(MiniTest::Unit::TestCase) do
  215. def test_something
  216. assert true
  217. end
  218. def teardown
  219. raise "unhandled exception"
  220. end
  221. end
  222. Object.const_set(:ATestCase, tc)
  223. @tu.run %w[-s 42]
  224. expected = "Loaded suite blah
  225. Started
  226. E
  227. Finished in 0.00
  228. 1) Error:
  229. test_something(ATestCase):
  230. RuntimeError: unhandled exception
  231. FILE:LINE:in `teardown'
  232. 1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
  233. Test run options: --seed 42
  234. "
  235. util_assert_report expected
  236. end
  237. def test_run_skip
  238. tc = Class.new(MiniTest::Unit::TestCase) do
  239. def test_something
  240. assert true
  241. end
  242. def test_skip
  243. skip "not yet"
  244. end
  245. end
  246. Object.const_set(:ATestCase, tc)
  247. @tu.run %w[-s 42]
  248. expected = "Loaded suite blah
  249. Started
  250. S.
  251. Finished in 0.00
  252. 1) Skipped:
  253. test_skip(ATestCase) [FILE:LINE]:
  254. not yet
  255. 2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
  256. Test run options: --seed 42
  257. "
  258. util_assert_report expected
  259. end
  260. def util_assert_report expected = nil
  261. expected ||= "Loaded suite blah
  262. Started
  263. .
  264. Finished in 0.00
  265. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
  266. Test run options: --seed 42
  267. "
  268. output = @output.string.sub(/Finished in .*/, "Finished in 0.00")
  269. output.sub!(/Loaded suite .*/, 'Loaded suite blah')
  270. output.sub!(/^(\s+)(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+:/o, '\1FILE:LINE:')
  271. output.sub!(/\[(?:#{Regexp.union(__FILE__, File.expand_path(__FILE__))}):\d+\]/o, '[FILE:LINE]')
  272. assert_equal(expected, output)
  273. end
  274. def test_run_failing_filtered
  275. tc = Class.new(MiniTest::Unit::TestCase) do
  276. def test_something
  277. assert true
  278. end
  279. def test_failure
  280. assert false
  281. end
  282. end
  283. Object.const_set(:ATestCase, tc)
  284. @tu.run %w[-n /something/ -s 42]
  285. expected = "Loaded suite blah
  286. Started
  287. .
  288. Finished in 0.00
  289. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
  290. Test run options: --seed 42 --name \"/something/\"
  291. "
  292. util_assert_report expected
  293. end
  294. def test_run_passing
  295. tc = Class.new(MiniTest::Unit::TestCase) do
  296. def test_something
  297. assert true
  298. end
  299. end
  300. Object.const_set(:ATestCase, tc)
  301. @tu.run %w[-s 42]
  302. util_assert_report
  303. end
  304. end
  305. class TestMiniTestTestCase < MiniTest::Unit::TestCase
  306. def setup
  307. MiniTest::Unit::TestCase.reset
  308. @tc = MiniTest::Unit::TestCase.new 'fake tc'
  309. @zomg = "zomg ponies!"
  310. @assertion_count = 1
  311. end
  312. def teardown
  313. assert_equal(@assertion_count, @tc._assertions,
  314. "expected #{@assertion_count} assertions to be fired during the test, not #{@tc._assertions}") if @tc._assertions
  315. Object.send :remove_const, :ATestCase if defined? ATestCase
  316. end
  317. def test_class_inherited
  318. @assertion_count = 0
  319. Object.const_set(:ATestCase, Class.new(MiniTest::Unit::TestCase))
  320. assert_equal [ATestCase], MiniTest::Unit::TestCase.test_suites
  321. end
  322. def test_class_test_suites
  323. @assertion_count = 0
  324. Object.const_set(:ATestCase, Class.new(MiniTest::Unit::TestCase))
  325. assert_equal 1, MiniTest::Unit::TestCase.test_suites.size
  326. assert_equal [ATestCase], MiniTest::Unit::TestCase.test_suites
  327. end
  328. def test_class_asserts_match_refutes
  329. @assertion_count = 0
  330. methods = MiniTest::Assertions.public_instance_methods
  331. methods.map! { |m| m.to_s } if Symbol === methods.first
  332. ignores = %w(assert_block assert_no_match assert_not_equal assert_not_nil
  333. assert_not_same assert_nothing_thrown assert_raise
  334. assert_nothing_raised assert_raises assert_throws assert_send)
  335. asserts = methods.grep(/^assert/).sort - ignores
  336. refutes = methods.grep(/^refute/).sort - ignores
  337. assert_empty refutes.map { |n| n.sub(/^refute/, 'assert') } - asserts
  338. assert_empty asserts.map { |n| n.sub(/^assert/, 'refute') } - refutes
  339. end
  340. def test_assert
  341. @assertion_count = 2
  342. @tc.assert_equal true, @tc.assert(true), "returns true on success"
  343. end
  344. def test_assert__triggered
  345. util_assert_triggered "Failed assertion, no message given." do
  346. @tc.assert false
  347. end
  348. end
  349. def test_assert__triggered_message
  350. util_assert_triggered @zomg do
  351. @tc.assert false, @zomg
  352. end
  353. end
  354. def test_assert_block
  355. @tc.assert_block do
  356. true
  357. end
  358. end
  359. def test_assert_block_triggered
  360. util_assert_triggered 'Expected block to return true value.' do
  361. @tc.assert_block do
  362. false
  363. end
  364. end
  365. end
  366. def test_assert_empty
  367. @assertion_count = 2
  368. @tc.assert_empty []
  369. end
  370. def test_assert_empty_triggered
  371. @assertion_count = 2
  372. util_assert_triggered "Expected [1] to be empty." do
  373. @tc.assert_empty [1]
  374. end
  375. end
  376. def test_assert_equal
  377. @tc.assert_equal 1, 1
  378. end
  379. def test_assert_equal_different
  380. util_assert_triggered "Expected 1, not 2." do
  381. @tc.assert_equal 1, 2
  382. end
  383. end
  384. def test_assert_in_delta
  385. @tc.assert_in_delta 0.0, 1.0 / 1000, 0.1
  386. end
  387. def test_assert_in_delta_triggered
  388. util_assert_triggered 'Expected 0.0 - 0.001 (0.001) to be < 1.0e-06.' do
  389. @tc.assert_in_delta 0.0, 1.0 / 1000, 0.000001
  390. end
  391. end
  392. def test_assert_in_epsilon
  393. @assertion_count = 8
  394. @tc.assert_in_epsilon 10000, 9991
  395. @tc.assert_in_epsilon 9991, 10000
  396. @tc.assert_in_epsilon 1.0, 1.001
  397. @tc.assert_in_epsilon 1.001, 1.0
  398. @tc.assert_in_epsilon 10000, 9999.1, 0.0001
  399. @tc.assert_in_epsilon 9999.1, 10000, 0.0001
  400. @tc.assert_in_epsilon 1.0, 1.0001, 0.0001
  401. @tc.assert_in_epsilon 1.0001, 1.0, 0.0001
  402. end
  403. def test_assert_in_epsilon_triggered
  404. util_assert_triggered 'Expected 10000 - 9990 (10) to be < 9.99.' do
  405. @tc.assert_in_epsilon 10000, 9990
  406. end
  407. end
  408. def test_assert_includes
  409. @assertion_count = 2
  410. @tc.assert_includes [true], true
  411. end
  412. def test_assert_includes_triggered
  413. @assertion_count = 3
  414. e = @tc.assert_raises MiniTest::Assertion do
  415. @tc.assert_includes [true], false
  416. end
  417. expected = "Expected [true] to include false."
  418. assert_equal expected, e.message
  419. end
  420. def test_assert_instance_of
  421. @tc.assert_instance_of String, "blah"
  422. end
  423. def test_assert_instance_of_triggered
  424. util_assert_triggered 'Expected "blah" to be an instance of Array, not String.' do
  425. @tc.assert_instance_of Array, "blah"
  426. end
  427. end
  428. def test_assert_kind_of
  429. @tc.assert_kind_of String, "blah"
  430. end
  431. def test_assert_kind_of_triggered
  432. util_assert_triggered 'Expected "blah" to be a kind of Array, not String.' do
  433. @tc.assert_kind_of Array, "blah"
  434. end
  435. end
  436. def test_assert_match
  437. @assertion_count = 2
  438. @tc.assert_match(/\w+/, "blah blah blah")
  439. end
  440. def test_assert_match_object
  441. @assertion_count = 2
  442. pattern = Object.new
  443. def pattern.=~(other) true end
  444. @tc.assert_match pattern, 5
  445. end
  446. def test_assert_match_object_triggered
  447. @assertion_count = 2
  448. pattern = Object.new
  449. def pattern.=~(other) false end
  450. def pattern.inspect; "<<Object>>" end
  451. util_assert_triggered 'Expected <<Object>> to match 5.' do
  452. @tc.assert_match pattern, 5
  453. end
  454. end
  455. def test_assert_match_triggered
  456. @assertion_count = 2
  457. util_assert_triggered 'Expected /\d+/ to match "blah blah blah".' do
  458. @tc.assert_match(/\d+/, "blah blah blah")
  459. end
  460. end
  461. def test_assert_nil
  462. @tc.assert_nil nil
  463. end
  464. def test_assert_nil_triggered
  465. util_assert_triggered 'Expected 42 to be nil.' do
  466. @tc.assert_nil 42
  467. end
  468. end
  469. def test_assert_operator
  470. @tc.assert_operator 2, :>, 1
  471. end
  472. def test_assert_operator_triggered
  473. util_assert_triggered "Expected 2 to be < 1." do
  474. @tc.assert_operator 2, :<, 1
  475. end
  476. end
  477. def test_assert_raises
  478. @tc.assert_raises RuntimeError do
  479. raise "blah"
  480. end
  481. end
  482. def test_assert_raises_module
  483. @tc.assert_raises M do
  484. raise E
  485. end
  486. end
  487. def test_assert_raises_triggered_different
  488. e = assert_raises MiniTest::Assertion do
  489. @tc.assert_raises RuntimeError do
  490. raise SyntaxError, "icky"
  491. end
  492. end
  493. expected = "[RuntimeError] exception expected, not
  494. Class: <SyntaxError>
  495. Message: <\"icky\">
  496. ---Backtrace---
  497. FILE:LINE:in `test_assert_raises_triggered_different'
  498. ---------------"
  499. actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')
  500. actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION =~ /^1\.9/
  501. assert_equal expected, actual
  502. end
  503. def test_assert_raises_triggered_different_msg
  504. e = assert_raises MiniTest::Assertion do
  505. @tc.assert_raises RuntimeError, "XXX" do
  506. raise SyntaxError, "icky"
  507. end
  508. end
  509. expected = "XXX
  510. [RuntimeError] exception expected, not
  511. Class: <SyntaxError>
  512. Message: <\"icky\">
  513. ---Backtrace---
  514. FILE:LINE:in `test_assert_raises_triggered_different_msg'
  515. ---------------"
  516. actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')
  517. actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION =~ /^1\.9/
  518. assert_equal expected, actual
  519. end
  520. def test_assert_raises_triggered_none
  521. e = assert_raises MiniTest::Assertion do
  522. @tc.assert_raises MiniTest::Assertion do
  523. # do nothing
  524. end
  525. end
  526. expected = "MiniTest::Assertion expected but nothing was raised."
  527. assert_equal expected, e.message
  528. end
  529. def test_assert_raises_triggered_none_msg
  530. e = assert_raises MiniTest::Assertion do
  531. @tc.assert_raises MiniTest::Assertion, "XXX" do
  532. # do nothing
  533. end
  534. end
  535. expected = "XXX\nMiniTest::Assertion expected but nothing was raised."
  536. assert_equal expected, e.message
  537. end
  538. def test_assert_raises_triggered_subclass
  539. e = assert_raises MiniTest::Assertion do
  540. @tc.assert_raises StandardError do
  541. raise E
  542. end
  543. end
  544. expected = "[StandardError] exception expected, not
  545. Class: <E>
  546. Message: <\"E\">
  547. ---Backtrace---
  548. FILE:LINE:in `test_assert_raises_triggered_subclass'
  549. ---------------"
  550. actual = e.message.gsub(/^.+:\d+/, 'FILE:LINE')
  551. actual.gsub!(/block \(\d+ levels\) in /, '') if RUBY_VERSION =~ /^1\.9/
  552. assert_equal expected, actual
  553. end
  554. def test_assert_respond_to
  555. @tc.assert_respond_to "blah", :empty?
  556. end
  557. def test_assert_respond_to_triggered
  558. util_assert_triggered 'Expected "blah" (String) to respond to #rawr!.' do
  559. @tc.assert_respond_to "blah", :rawr!
  560. end
  561. end
  562. def test_assert_same
  563. @assertion_count = 3
  564. o = "blah"
  565. @tc.assert_same 1, 1
  566. @tc.assert_same :blah, :blah
  567. @tc.assert_same o, o
  568. end
  569. def test_assert_same_triggered
  570. @assertion_count = 2
  571. util_assert_triggered 'Expected 2 (oid=N) to be the same as 1 (oid=N).' do
  572. @tc.assert_same 1, 2
  573. end
  574. s1 = "blah"
  575. s2 = "blah"
  576. util_assert_triggered 'Expected "blah" (oid=N) to be the same as "blah" (oid=N).' do
  577. @tc.assert_same s1, s2
  578. end
  579. end
  580. def test_assert_send
  581. @tc.assert_send [1, :<, 2]
  582. end
  583. def test_assert_send_bad
  584. util_assert_triggered "Expected 1.>(*[2]) to return true." do
  585. @tc.assert_send [1, :>, 2]
  586. end
  587. end
  588. def test_assert_throws
  589. @tc.assert_throws(:blah) do
  590. throw :blah
  591. end
  592. end
  593. def test_assert_throws_different
  594. util_assert_triggered 'Expected :blah to have been thrown, not :not_blah.' do
  595. @tc.assert_throws(:blah) do
  596. throw :not_blah
  597. end
  598. end
  599. end
  600. def test_assert_throws_unthrown
  601. util_assert_triggered 'Expected :blah to have been thrown.' do
  602. @tc.assert_throws(:blah) do
  603. # do nothing
  604. end
  605. end
  606. end
  607. def test_capture_io
  608. @assertion_count = 0
  609. out, err = capture_io do
  610. puts 'hi'
  611. warn 'bye!'
  612. end
  613. assert_equal "hi\n", out
  614. assert_equal "bye!\n", err
  615. end
  616. def test_flunk
  617. util_assert_triggered 'Epic Fail!' do
  618. @tc.flunk
  619. end
  620. end
  621. def test_flunk_message
  622. util_assert_triggered @zomg do
  623. @tc.flunk @zomg
  624. end
  625. end
  626. def test_message
  627. @assertion_count = 0
  628. assert_equal "blah2.", @tc.message { "blah2" }.call
  629. assert_equal "blah2.", @tc.message("") { "blah2" }.call
  630. assert_equal "blah1.\nblah2.", @tc.message("blah1") { "blah2" }.call
  631. end
  632. def test_pass
  633. @tc.pass
  634. end
  635. def test_test_methods_sorted
  636. @assertion_count = 0
  637. sample_test_case = Class.new(MiniTest::Unit::TestCase) do
  638. def self.test_order; :sorted end
  639. def test_test3; assert "does not matter" end
  640. def test_test2; assert "does not matter" end
  641. def test_test1; assert "does not matter" end
  642. end
  643. expected = %w(test_test1 test_test2 test_test3)
  644. assert_equal expected, sample_test_case.test_methods
  645. end
  646. def test_test_methods_random
  647. @assertion_count = 0
  648. sample_test_case = Class.new(MiniTest::Unit::TestCase) do
  649. def test_test1; assert "does not matter" end
  650. def test_test2; assert "does not matter" end
  651. def test_test3; assert "does not matter" end
  652. end
  653. srand 42
  654. expected = %w(test_test2 test_test1 test_test3)
  655. assert_equal expected, sample_test_case.test_methods
  656. end
  657. def test_refute
  658. @assertion_count = 2
  659. @tc.assert_equal false, @tc.refute(false), "returns false on success"
  660. end
  661. def test_refute_empty
  662. @assertion_count = 2
  663. @tc.refute_empty [1]
  664. end
  665. def test_refute_empty_triggered
  666. @assertion_count = 2
  667. util_assert_triggered "Expected [] to not be empty." do
  668. @tc.refute_empty []
  669. end
  670. end
  671. def test_refute_equal
  672. @tc.refute_equal "blah", "yay"
  673. end
  674. def test_refute_equal_triggered
  675. util_assert_triggered 'Expected "blah" to not be equal to "blah".' do
  676. @tc.refute_equal "blah", "blah"
  677. end
  678. end
  679. def test_refute_in_delta
  680. @tc.refute_in_delta 0.0, 1.0 / 1000, 0.000001
  681. end
  682. def test_refute_in_delta_triggered
  683. util_assert_triggered 'Expected 0.0 - 0.001 (0.001) to not be < 0.1.' do
  684. @tc.refute_in_delta 0.0, 1.0 / 1000, 0.1
  685. end
  686. end
  687. def test_refute_in_epsilon
  688. @tc.refute_in_epsilon 10000, 9990
  689. end
  690. def test_refute_in_epsilon_triggered
  691. util_assert_triggered 'Expected 10000 - 9991 (9) to not be < 10.0.' do
  692. @tc.refute_in_epsilon 10000, 9991
  693. fail
  694. end
  695. end
  696. def test_refute_includes
  697. @assertion_count = 2
  698. @tc.refute_includes [true], false
  699. end
  700. def test_refute_includes_triggered
  701. @assertion_count = 3
  702. e = @tc.assert_raises MiniTest::Assertion do
  703. @tc.refute_includes [true], true
  704. end
  705. expected = "Expected [true] to not include true."
  706. assert_equal expected, e.message
  707. end
  708. def test_refute_instance_of
  709. @tc.refute_instance_of Array, "blah"
  710. end
  711. def test_refute_instance_of_triggered
  712. util_assert_triggered 'Expected "blah" to not be an instance of String.' do
  713. @tc.refute_instance_of String, "blah"
  714. end
  715. end
  716. def test_refute_kind_of
  717. @tc.refute_kind_of Array, "blah"
  718. end
  719. def test_refute_kind_of_triggered
  720. util_assert_triggered 'Expected "blah" to not be a kind of String.' do
  721. @tc.refute_kind_of String, "blah"
  722. end
  723. end
  724. def test_refute_match
  725. @assertion_count = 2
  726. @tc.refute_match(/\d+/, "blah blah blah")
  727. end
  728. def test_refute_match_object
  729. @assertion_count = 2
  730. @tc.refute_match Object.new, 5 # default #=~ returns false
  731. end
  732. def test_assert_object_triggered
  733. @assertion_count = 2
  734. pattern = Object.new
  735. def pattern.=~(other) false end
  736. def pattern.inspect; "<<Object>>" end
  737. util_assert_triggered 'Expected <<Object>> to match 5.' do
  738. @tc.assert_match pattern, 5
  739. end
  740. end
  741. def test_refute_match_object_triggered
  742. @assertion_count = 2
  743. pattern = Object.new
  744. def pattern.=~(other) true end
  745. def pattern.inspect; "<<Object>>" end
  746. util_assert_triggered 'Expected <<Object>> to not match 5.' do
  747. @tc.refute_match pattern, 5
  748. end
  749. end
  750. def test_refute_match_triggered
  751. @assertion_count = 2
  752. util_assert_triggered 'Expected /\w+/ to not match "blah blah blah".' do
  753. @tc.refute_match(/\w+/, "blah blah blah")
  754. end
  755. end
  756. def test_refute_nil
  757. @tc.refute_nil 42
  758. end
  759. def test_refute_nil_triggered
  760. util_assert_triggered 'Expected nil to not be nil.' do
  761. @tc.refute_nil nil
  762. end
  763. end
  764. def test_refute_operator
  765. @tc.refute_operator 2, :<, 1
  766. end
  767. def test_refute_operator_triggered
  768. util_assert_triggered "Expected 2 to not be > 1." do
  769. @tc.refute_operator 2, :>, 1
  770. end
  771. end
  772. def test_refute_respond_to
  773. @tc.refute_respond_to "blah", :rawr!
  774. end
  775. def test_refute_respond_to_triggered
  776. util_assert_triggered 'Expected "blah" to not respond to empty?.' do
  777. @tc.refute_respond_to "blah", :empty?
  778. end
  779. end
  780. def test_refute_same
  781. @tc.refute_same 1, 2
  782. end
  783. def test_refute_same_triggered
  784. util_assert_triggered 'Expected 1 (oid=N) to not be the same as 1 (oid=N).' do
  785. @tc.refute_same 1, 1
  786. end
  787. end
  788. def test_skip
  789. @assertion_count = 0
  790. util_assert_triggered "haha!", MiniTest::Skip do
  791. @tc.skip "haha!"
  792. end
  793. end
  794. def util_assert_triggered expected, klass = MiniTest::Assertion
  795. e = assert_raises(klass) do
  796. yield
  797. end
  798. msg = e.message.sub(/(---Backtrace---).*/m, '\1')
  799. msg.gsub!(/\(oid=[-0-9]+\)/, '(oid=N)')
  800. assert_equal expected, msg
  801. end
  802. end