PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/jacob.patton/team_demo
Ruby | 425 lines | 182 code | 34 blank | 209 comment | 9 complexity | 544984d716b0ab031980be001e8226bb MD5 | raw file
  1. #--
  2. #
  3. # Author:: Nathaniel Talbott.
  4. # Copyright::
  5. # * Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
  6. # * Copyright (c) 2008-2009 Kouhei Sutou <tt><kou@clear-code.com></tt>
  7. # License:: Ruby license.
  8. require 'test/unit/attribute'
  9. require 'test/unit/fixture'
  10. require 'test/unit/exceptionhandler'
  11. require 'test/unit/assertions'
  12. require 'test/unit/failure'
  13. require 'test/unit/error'
  14. require 'test/unit/pending'
  15. require 'test/unit/omission'
  16. require 'test/unit/notification'
  17. require 'test/unit/priority'
  18. require 'test/unit/testsuite'
  19. require 'test/unit/assertionfailederror'
  20. require 'test/unit/util/backtracefilter'
  21. require 'test/unit/util/method-owner-finder'
  22. module Test
  23. module Unit
  24. # Ties everything together. If you subclass and add your own
  25. # test methods, it takes care of making them into tests and
  26. # wrapping those tests into a suite. It also does the
  27. # nitty-gritty of actually running an individual test and
  28. # collecting its results into a Test::Unit::TestResult object.
  29. #
  30. # You can run two hooks before/after a TestCase run.
  31. #
  32. # Example:
  33. # class TestMyClass < Test::Unit::TestCase
  34. # class << self
  35. # def startup
  36. # ...
  37. # end
  38. #
  39. # def shutdown
  40. # ...
  41. # end
  42. # end
  43. #
  44. # def setup
  45. # ...
  46. # end
  47. #
  48. # def teardown
  49. # ...
  50. # end
  51. #
  52. # def test_my_method1
  53. # ...
  54. # end
  55. #
  56. # def test_my_method2
  57. # ...
  58. # end
  59. # end
  60. #
  61. # Here is a call order:
  62. # * startup
  63. # * setup
  64. # * test_my_method1
  65. # * teardown
  66. # * setup
  67. # * test_my_method2
  68. # * teardown
  69. # * shutdown
  70. class TestCase
  71. include Attribute
  72. include Fixture
  73. include ExceptionHandler
  74. include ErrorHandler
  75. include FailureHandler
  76. include TestCasePendingSupport
  77. include TestCaseOmissionSupport
  78. include TestCaseNotificationSupport
  79. include Priority
  80. include Assertions
  81. include Util::BacktraceFilter
  82. STARTED = name + "::STARTED" # :nodoc:
  83. FINISHED = name + "::FINISHED" # :nodoc:
  84. DESCENDANTS = [] # :nodoc:
  85. AVAILABLE_ORDERS = [:alphabetic, :random, :defined] # :nodoc:
  86. class << self
  87. def inherited(sub_class) # :nodoc:
  88. DESCENDANTS << sub_class
  89. end
  90. @@added_methods = []
  91. def method_added(name) # :nodoc:
  92. super
  93. @@added_methods << name.to_s
  94. end
  95. # Rolls up all of the test* methods in the fixture into
  96. # one suite, creating a new instance of the fixture for
  97. # each method.
  98. def suite
  99. suite = TestSuite.new(name, self)
  100. collect_test_names.each do |test|
  101. catch(:invalid_test) do
  102. suite << new(test)
  103. end
  104. end
  105. if suite.empty?
  106. catch(:invalid_test) do
  107. suite << new("default_test")
  108. end
  109. end
  110. suite
  111. end
  112. # Called before every test case runs. Can be used
  113. # to set up fixture information used in test case
  114. # scope.
  115. #
  116. # Here is an example test case:
  117. # class TestMyClass < Test::Unit::TestCase
  118. # class << self
  119. # def startup
  120. # ...
  121. # end
  122. # end
  123. #
  124. # def setup
  125. # ...
  126. # end
  127. #
  128. # def test_my_class1
  129. # ...
  130. # end
  131. #
  132. # def test_my_class2
  133. # ...
  134. # end
  135. # end
  136. #
  137. # Here is a call order:
  138. # * startup
  139. # * setup
  140. # * test_my_class1 (or test_my_class2)
  141. # * setup
  142. # * test_my_class2 (or test_my_class1)
  143. #
  144. # Note that you should not assume test order. Tests
  145. # should be worked in any order.
  146. def startup
  147. end
  148. # Called after every test case runs. Can be used to tear
  149. # down fixture information used in test case scope.
  150. #
  151. # Here is an example test case:
  152. # class TestMyClass < Test::Unit::TestCase
  153. # class << self
  154. # def shutdown
  155. # ...
  156. # end
  157. # end
  158. #
  159. # def teardown
  160. # ...
  161. # end
  162. #
  163. # def test_my_class1
  164. # ...
  165. # end
  166. #
  167. # def test_my_class2
  168. # ...
  169. # end
  170. # end
  171. #
  172. # Here is a call order:
  173. # * test_my_class1 (or test_my_class2)
  174. # * teardown
  175. # * test_my_class2 (or test_my_class1)
  176. # * teardown
  177. # * shutdown
  178. #
  179. # Note that you should not assume test order. Tests
  180. # should be worked in any order.
  181. def shutdown
  182. end
  183. @@test_order = AVAILABLE_ORDERS.first
  184. # Returns the current test order. This returns
  185. # +:alphabetic+ by default.
  186. def test_order
  187. @@test_order
  188. end
  189. # Sets the current test order.
  190. #
  191. # Here are the available _order_:
  192. # [:alphabetic]
  193. # Default. Tests are sorted in alphabetic order.
  194. # [:random]
  195. # Tests are sorted in random order.
  196. # [:defined]
  197. # Tests are sorted in defined order.
  198. def test_order=(order)
  199. @@test_order = order
  200. end
  201. # :stopdoc:
  202. private
  203. def collect_test_names
  204. method_names = public_instance_methods(true).collect do |name|
  205. name.to_s
  206. end
  207. test_names = method_names.find_all do |method_name|
  208. method_name =~ /^test./
  209. end
  210. send("sort_test_names_in_#{test_order}_order", test_names)
  211. end
  212. def sort_test_names_in_alphabetic_order(test_names)
  213. test_names.sort
  214. end
  215. def sort_test_names_in_random_order(test_names)
  216. test_names.sort_by {rand(test_names.size)}
  217. end
  218. def sort_test_names_in_defined_order(test_names)
  219. test_names.sort do |test1, test2|
  220. test1_defined_order = @@added_methods.index(test1)
  221. test2_defined_order = @@added_methods.index(test2)
  222. if test1_defined_order and test2_defined_order
  223. test1_defined_order <=> test2_defined_order
  224. elsif test1_defined_order
  225. 1
  226. elsif test2_defined_order
  227. -1
  228. else
  229. test1 <=> test2
  230. end
  231. end
  232. end
  233. # :startdoc:
  234. end
  235. attr_reader :method_name
  236. # Creates a new instance of the fixture for running the
  237. # test represented by test_method_name.
  238. def initialize(test_method_name)
  239. throw :invalid_test unless respond_to?(test_method_name)
  240. test_method = method(test_method_name)
  241. throw :invalid_test if test_method.arity > 0
  242. owner = Util::MethodOwnerFinder.find(self, test_method_name)
  243. if owner.class != Module and self.class != owner
  244. throw :invalid_test
  245. end
  246. @method_name = test_method_name
  247. @test_passed = true
  248. @interrupted = false
  249. end
  250. # Runs the individual test method represented by this
  251. # instance of the fixture, collecting statistics, failures
  252. # and errors in result.
  253. def run(result)
  254. begin
  255. @_result = result
  256. yield(STARTED, name)
  257. begin
  258. run_setup
  259. run_test
  260. rescue Exception
  261. @interrupted = true
  262. raise unless handle_exception($!)
  263. ensure
  264. begin
  265. run_teardown
  266. rescue Exception
  267. raise unless handle_exception($!)
  268. end
  269. end
  270. result.add_run
  271. yield(FINISHED, name)
  272. ensure
  273. # @_result = nil # For test-spec's after_all :<
  274. end
  275. end
  276. # Called before every test method runs. Can be used
  277. # to set up fixture information.
  278. #
  279. # You can add additional setup tasks by the following
  280. # code:
  281. # class TestMyClass < Test::Unit::TestCase
  282. # def setup
  283. # ...
  284. # end
  285. #
  286. # setup
  287. # def my_setup1
  288. # ...
  289. # end
  290. #
  291. # setup
  292. # def my_setup2
  293. # ...
  294. # end
  295. #
  296. # def test_my_class
  297. # ...
  298. # end
  299. # end
  300. #
  301. # Here is a call order:
  302. # * setup
  303. # * my_setup1
  304. # * my_setup2
  305. # * test_my_class
  306. def setup
  307. end
  308. # Called after every test method runs. Can be used to tear
  309. # down fixture information.
  310. #
  311. # You can add additional teardown tasks by the following
  312. # code:
  313. # class TestMyClass < Test::Unit::TestCase
  314. # def teardown
  315. # ...
  316. # end
  317. #
  318. # teardown
  319. # def my_teardown1
  320. # ...
  321. # end
  322. #
  323. # teardown
  324. # def my_teardown2
  325. # ...
  326. # end
  327. #
  328. # def test_my_class
  329. # ...
  330. # end
  331. # end
  332. #
  333. # Here is a call order:
  334. # * test_my_class
  335. # * my_teardown2
  336. # * my_teardown1
  337. # * teardown
  338. def teardown
  339. end
  340. def default_test
  341. flunk("No tests were specified")
  342. end
  343. def size
  344. 1
  345. end
  346. # Returns a human-readable name for the specific test that
  347. # this instance of TestCase represents.
  348. def name
  349. "#{@method_name}(#{self.class.name})"
  350. end
  351. # Overridden to return #name.
  352. def to_s
  353. name
  354. end
  355. # It's handy to be able to compare TestCase instances.
  356. def ==(other)
  357. return false unless(other.kind_of?(self.class))
  358. return false unless(@method_name == other.method_name)
  359. self.class == other.class
  360. end
  361. def interrupted?
  362. @interrupted
  363. end
  364. private
  365. def current_result
  366. @_result
  367. end
  368. def run_test
  369. __send__(@method_name)
  370. end
  371. def handle_exception(exception)
  372. self.class.exception_handlers.each do |handler|
  373. return true if send(handler, exception)
  374. end
  375. false
  376. end
  377. # Returns whether this individual test passed or
  378. # not. Primarily for use in teardown so that artifacts
  379. # can be left behind if the test fails.
  380. def passed?
  381. @test_passed
  382. end
  383. def problem_occurred
  384. @test_passed = false
  385. end
  386. def add_assertion
  387. current_result.add_assertion
  388. end
  389. end
  390. end
  391. end