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

/vendor/gems/test-unit-2.0.5/test/test-testcase.rb

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