/capybara/ruby/1.9.1/gems/test-unit-2.3.2/test/test-testcase.rb

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