PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/Ruby/ruby19/lib/ruby/gems/1.9.1/gems/test-unit-2.1.1/test/test-testcase.rb

http://github.com/IronLanguages/main
Ruby | 544 lines | 458 code | 83 blank | 3 comment | 34 complexity | df9be5febf19e9f8f4083c340d62694d MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. # Author:: Nathaniel Talbott.
  2. # Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
  3. # License:: Ruby license.
  4. require 'test/unit'
  5. module Test
  6. module Unit
  7. class TestTestCase < TestCase
  8. self.test_order = :random
  9. def test_creation
  10. tc = Class.new(TestCase) do
  11. def test_with_arguments(arg1, arg2)
  12. end
  13. end
  14. caught = true
  15. catch(:invalid_test) do
  16. tc.new(:test_with_arguments)
  17. caught = false
  18. end
  19. check("Should have caught an invalid test when there are arguments", caught)
  20. caught = true
  21. catch(:invalid_test) do
  22. tc.new(:non_existent_test)
  23. caught = false
  24. end
  25. check("Should have caught an invalid test when the method does not exist", caught)
  26. end
  27. def setup
  28. @tc_failure_error = Class.new(TestCase) do
  29. def test_failure
  30. assert_block("failure") { false }
  31. end
  32. def test_error
  33. 1 / 0
  34. end
  35. def test_nested_failure
  36. nested
  37. end
  38. def nested
  39. assert_block("nested"){false}
  40. end
  41. def return_passed?
  42. return passed?
  43. end
  44. end
  45. def @tc_failure_error.name
  46. "TC_FailureError"
  47. end
  48. end
  49. def test_add_failed_assertion
  50. test_case = @tc_failure_error.new(:test_failure)
  51. check("passed? should start out true", test_case.return_passed?)
  52. result = TestResult.new
  53. called = false
  54. result.add_listener(TestResult::FAULT) {
  55. | fault |
  56. check("Should have a Failure", fault.instance_of?(Failure))
  57. check("The Failure should have the correct message", "failure" == fault.message)
  58. check("The Failure should have the correct test_name (was <#{fault.test_name}>)", fault.test_name == "test_failure(TC_FailureError)")
  59. r = /\A.*#{Regexp.escape(File.basename(__FILE__))}:\d+:in `test_failure'\Z/
  60. location = fault.location
  61. check("The location should be an array", location.kind_of?(Array))
  62. check("The location should have two lines (was: <#{location.inspect}>)", location.size == 2)
  63. check("The Failure should have the correct location (was <#{location[0].inspect}>, expected <#{r.inspect}>)", r =~ location[0])
  64. called = true
  65. }
  66. progress = []
  67. test_case.run(result) { |*arguments| progress << arguments }
  68. check("The failure should have triggered the listener", called)
  69. check("The failure should have set passed?", !test_case.return_passed?)
  70. check("The progress block should have been updated correctly", [[TestCase::STARTED, test_case.name], [TestCase::FINISHED, test_case.name]] == progress)
  71. end
  72. def test_add_failure_nested
  73. test_case = @tc_failure_error.new(:test_nested_failure)
  74. check("passed? should start out true", test_case.return_passed?)
  75. result = TestResult.new
  76. called = false
  77. result.add_listener(TestResult::FAULT) {
  78. | fault |
  79. check("Should have a Failure", fault.instance_of?(Failure))
  80. check("The Failure should have the correct message", "nested" == fault.message)
  81. check("The Failure should have the correct test_name (was <#{fault.test_name}>)", fault.test_name == "test_nested_failure(TC_FailureError)")
  82. r =
  83. location = fault.location
  84. check("The location should be an array", location.kind_of?(Array))
  85. check("The location should have the correct number of lines (was: <#{location.inspect}>)", location.size == 3)
  86. check("The Failure should have the correct location (was <#{location[0].inspect}>)", /\A.*#{Regexp.escape(File.basename(__FILE__))}:\d+:in `nested'\Z/ =~ location[0])
  87. check("The Failure should have the correct location (was <#{location[1].inspect}>)", /\A.*#{Regexp.escape(File.basename(__FILE__))}:\d+:in `test_nested_failure'\Z/ =~ location[1])
  88. called = true
  89. }
  90. test_case.run(result){}
  91. check("The failure should have triggered the listener", called)
  92. end
  93. def test_add_error
  94. test_case = @tc_failure_error.new(:test_error)
  95. check("passed? should start out true", test_case.return_passed?)
  96. result = TestResult.new
  97. called = false
  98. result.add_listener(TestResult::FAULT) {
  99. | fault |
  100. check("Should have a TestError", fault.instance_of?(Error))
  101. check("The Error should have the correct message", "ZeroDivisionError: divided by 0" == fault.message)
  102. check("The Error should have the correct test_name", "test_error(TC_FailureError)" == fault.test_name)
  103. check("The Error should have the correct exception", fault.exception.instance_of?(ZeroDivisionError))
  104. called = true
  105. }
  106. test_case.run(result) {}
  107. check("The error should have triggered the listener", called)
  108. check("The error should have set passed?", !test_case.return_passed?)
  109. end
  110. def test_no_tests
  111. suite = TestCase.suite
  112. check("Should have a test suite", suite.instance_of?(TestSuite))
  113. check("Should have one test", suite.size == 1)
  114. check("Should have the default test", suite.tests.first.name == "default_test(Test::Unit::TestCase)")
  115. result = TestResult.new
  116. suite.run(result) {}
  117. check("Should have had one test run", result.run_count == 1)
  118. check("Should have had one test failure", result.failure_count == 1)
  119. check("Should have had no errors", result.error_count == 0)
  120. end
  121. def test_suite
  122. tc = Class.new(TestCase) do
  123. def test_succeed
  124. assert_block {true}
  125. end
  126. def test_fail
  127. assert_block {false}
  128. end
  129. def test_error
  130. 1/0
  131. end
  132. def dont_run
  133. assert_block {true}
  134. end
  135. def test_dont_run(argument)
  136. assert_block {true}
  137. end
  138. def test
  139. assert_block {true}
  140. end
  141. end
  142. suite = tc.suite
  143. check("Should have a test suite", suite.instance_of?(TestSuite))
  144. check("Should have three tests", suite.size == 3)
  145. result = TestResult.new
  146. suite.run(result) {}
  147. check("Should have had three test runs", result.run_count == 3)
  148. check("Should have had one test failure", result.failure_count == 1)
  149. check("Should have had one test error", result.error_count == 1)
  150. end
  151. def test_setup_teardown
  152. tc = Class.new(TestCase) do
  153. attr_reader(:setup_called, :teardown_called)
  154. def initialize(test)
  155. super(test)
  156. @setup_called = false
  157. @teardown_called = false
  158. end
  159. def setup
  160. @setup_called = true
  161. end
  162. def teardown
  163. @teardown_called = true
  164. end
  165. def test_succeed
  166. assert_block {true}
  167. end
  168. def test_fail
  169. assert_block {false}
  170. end
  171. def test_error
  172. raise "Error!"
  173. end
  174. end
  175. result = TestResult.new
  176. test = tc.new(:test_succeed)
  177. test.run(result) {}
  178. check("Should have called setup the correct number of times", test.setup_called)
  179. check("Should have called teardown the correct number of times", test.teardown_called)
  180. test = tc.new(:test_fail)
  181. test.run(result) {}
  182. check("Should have called setup the correct number of times", test.setup_called)
  183. check("Should have called teardown the correct number of times", test.teardown_called)
  184. test = tc.new(:test_error)
  185. test.run(result) {}
  186. check("Should have called setup the correct number of times", test.setup_called)
  187. check("Should have called teardown the correct number of times", test.teardown_called)
  188. check("Should have had two test runs", result.run_count == 3)
  189. check("Should have had a test failure", result.failure_count == 1)
  190. check("Should have had a test error", result.error_count == 1)
  191. end
  192. def test_assertion_failed_not_called
  193. tc = Class.new(TestCase) do
  194. def test_thing
  195. raise AssertionFailedError.new
  196. end
  197. end
  198. suite = tc.suite
  199. check("Should have one test", suite.size == 1)
  200. result = TestResult.new
  201. suite.run(result) {}
  202. check("Should have had one test run", result.run_count == 1)
  203. check("Should have had one assertion failure", result.failure_count == 1)
  204. check("Should not have any assertion errors but had #{result.error_count}", result.error_count == 0)
  205. end
  206. def test_equality
  207. tc1 = Class.new(TestCase) do
  208. def test_1
  209. end
  210. def test_2
  211. end
  212. end
  213. tc2 = Class.new(TestCase) do
  214. def test_1
  215. end
  216. end
  217. test1 = tc1.new('test_1')
  218. test2 = tc1.new('test_1')
  219. check("Should be equal", test1 == test2)
  220. check("Should be equal", test2 == test1)
  221. test1 = tc1.new('test_2')
  222. check("Should not be equal", test1 != test2)
  223. check("Should not be equal", test2 != test1)
  224. test2 = tc1.new('test_2')
  225. check("Should be equal", test1 == test2)
  226. check("Should be equal", test2 == test1)
  227. test1 = tc1.new('test_1')
  228. test2 = tc2.new('test_1')
  229. check("Should not be equal", test1 != test2)
  230. check("Should not be equal", test2 != test1)
  231. check("Should not be equal", test1 != Object.new)
  232. check("Should not be equal", Object.new != test1)
  233. end
  234. def test_re_raise_exception
  235. test_case = Class.new(TestCase) do
  236. def test_raise_interrupt
  237. raise Interrupt, "from test"
  238. end
  239. end
  240. test = test_case.new("test_raise_interrupt")
  241. begin
  242. test.run(TestResult.new) {}
  243. check("Should not be reached", false)
  244. rescue Exception
  245. check("Interrupt exception should be re-raised", $!.class == Interrupt)
  246. end
  247. end
  248. def test_startup_shutdown
  249. called = []
  250. test_case = Class.new(TestCase) do
  251. @@called = called
  252. class << self
  253. def startup
  254. @@called << :startup
  255. end
  256. def shutdown
  257. @@called << :shutdown
  258. end
  259. end
  260. def setup
  261. @@called << :setup
  262. end
  263. def teardown
  264. @@called << :teardown
  265. end
  266. def test1
  267. end
  268. def test2
  269. end
  270. end
  271. test_suite = test_case.suite
  272. test_suite.run(TestResult.new) {}
  273. check("startup/shutdown should be called once per test case" +
  274. ": #{called.inspect}",
  275. called == [:startup,
  276. :setup, :teardown,
  277. :setup, :teardown,
  278. :shutdown])
  279. end
  280. def test_error_on_startup
  281. test_case = Class.new(TestCase) do
  282. class << self
  283. def startup
  284. raise "from startup"
  285. end
  286. end
  287. def test_nothing
  288. end
  289. end
  290. test_suite = test_case.suite
  291. result = TestResult.new
  292. test_suite.run(result) {}
  293. check("Should record an error on startup: #{result}",
  294. result.error_count == 1)
  295. end
  296. def test_pass_through_error_on_startup
  297. test_case = Class.new(TestCase) do
  298. class << self
  299. def startup
  300. raise Interrupt, "from startup"
  301. end
  302. end
  303. def test_nothing
  304. end
  305. end
  306. test_suite = test_case.suite
  307. begin
  308. test_suite.run(TestResult.new) {}
  309. check("Should not be reached", false)
  310. rescue Exception
  311. check("Interrupt should be passed through: #{$!}",
  312. Interrupt === $!)
  313. end
  314. end
  315. def test_error_on_shutdown
  316. test_case = Class.new(TestCase) do
  317. class << self
  318. def shutdown
  319. raise "from shutdown"
  320. end
  321. end
  322. def test_nothing
  323. end
  324. end
  325. test_suite = test_case.suite
  326. result = TestResult.new
  327. test_suite.run(result) {}
  328. check("Should record an error on shutdown: #{result}",
  329. result.error_count == 1)
  330. end
  331. def test_pass_through_error_on_shutdown
  332. test_case = Class.new(TestCase) do
  333. class << self
  334. def shutdown
  335. raise Interrupt, "from shutdown"
  336. end
  337. end
  338. def test_nothing
  339. end
  340. end
  341. test_suite = test_case.suite
  342. begin
  343. test_suite.run(TestResult.new) {}
  344. check("Should not be reached", false)
  345. rescue Exception
  346. check("Interrupt should be passed through: #{$!}",
  347. Interrupt === $!)
  348. end
  349. end
  350. def test_interrupted
  351. test_case = Class.new(TestCase) do
  352. def test_fail
  353. flunk
  354. end
  355. def test_nothing
  356. end
  357. end
  358. failed_test = test_case.new(:test_fail)
  359. failed_test.run(TestResult.new) {}
  360. check("Should be interrupted", failed_test.interrupted?)
  361. success_test = test_case.new(:test_nothing)
  362. success_test.run(TestResult.new) {}
  363. check("Should not be interrupted", !success_test.interrupted?)
  364. end
  365. def test_inherited_test_should_be_ignored
  366. test_case = Class.new(TestCase) do
  367. def test_nothing
  368. end
  369. end
  370. sub_test_case = Class.new(test_case) do
  371. def test_fail
  372. flunk
  373. end
  374. end
  375. assert_nothing_thrown do
  376. test_case.new("test_nothing")
  377. end
  378. assert_nothing_thrown do
  379. sub_test_case.new("test_fail")
  380. end
  381. assert_throw(:invalid_test) do
  382. sub_test_case.new("test_nothing")
  383. end
  384. end
  385. def test_mixin_test_should_not_be_ignored
  386. test_module = Module.new do
  387. def test_nothing
  388. end
  389. end
  390. test_case = Class.new(Test::Unit::TestCase) do
  391. include test_module
  392. def test_fail
  393. flunk
  394. end
  395. end
  396. assert_nothing_thrown do
  397. test_case.new("test_nothing")
  398. end
  399. assert_nothing_thrown do
  400. test_case.new("test_fail")
  401. end
  402. end
  403. def test_defined_order
  404. test_case = Class.new(Test::Unit::TestCase) do
  405. def test_z
  406. end
  407. def test_1
  408. end
  409. def test_a
  410. end
  411. end
  412. assert_equal(["test_1", "test_a", "test_z"],
  413. test_case.suite.tests.collect {|test| test.method_name})
  414. test_case.test_order = :defined
  415. assert_equal(["test_z", "test_1", "test_a"],
  416. test_case.suite.tests.collect {|test| test.method_name})
  417. end
  418. def test_declarative_style
  419. test_case = Class.new(Test::Unit::TestCase) do
  420. test "declarative style test definition" do
  421. end
  422. test "include parenthesis" do
  423. end
  424. test "1 + 2 = 3" do
  425. end
  426. end
  427. test_case.test_order = :defined
  428. assert_equal(["test_declarative_style_test_definition",
  429. "test_include_parenthesis",
  430. "test_1_2_3"],
  431. test_case.suite.tests.collect {|test| test.method_name})
  432. assert_equal(["declarative style test definition",
  433. "include parenthesis",
  434. "1 + 2 = 3"],
  435. test_case.suite.tests.collect {|test| test.description})
  436. end
  437. def test_redefine_method
  438. test_case = Class.new(Test::Unit::TestCase) do
  439. def test_name
  440. end
  441. alias_method :test_name2, :test_name
  442. def test_name
  443. end
  444. end
  445. suite = test_case.suite
  446. assert_equal(["test_name", "test_name2"],
  447. suite.tests.collect {|test| test.method_name})
  448. result = TestResult.new
  449. suite.run(result) {}
  450. assert_equal("2 tests, 0 assertions, 0 failures, " +
  451. "0 errors, 0 pendings, 0 omissions, 1 notifications",
  452. result.summary)
  453. end
  454. private
  455. def check(message, passed)
  456. add_assertion
  457. raise AssertionFailedError.new(message) unless passed
  458. end
  459. end
  460. end
  461. end