PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/IronLanguages/main
Ruby | 483 lines | 209 code | 38 blank | 236 comment | 14 complexity | 0786f5e1ed97f89f1766414d27833a3a MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  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/output'
  22. require 'test/unit/util/method-owner-finder'
  23. module Test
  24. module Unit
  25. # Ties everything together. If you subclass and add your own
  26. # test methods, it takes care of making them into tests and
  27. # wrapping those tests into a suite. It also does the
  28. # nitty-gritty of actually running an individual test and
  29. # collecting its results into a Test::Unit::TestResult object.
  30. #
  31. # You can run two hooks before/after a TestCase run.
  32. #
  33. # Example:
  34. # class TestMyClass < Test::Unit::TestCase
  35. # class << self
  36. # def startup
  37. # ...
  38. # end
  39. #
  40. # def shutdown
  41. # ...
  42. # end
  43. # end
  44. #
  45. # def setup
  46. # ...
  47. # end
  48. #
  49. # def teardown
  50. # ...
  51. # end
  52. #
  53. # def test_my_method1
  54. # ...
  55. # end
  56. #
  57. # def test_my_method2
  58. # ...
  59. # end
  60. # end
  61. #
  62. # Here is a call order:
  63. # * startup
  64. # * setup
  65. # * test_my_method1
  66. # * teardown
  67. # * setup
  68. # * test_my_method2
  69. # * teardown
  70. # * shutdown
  71. class TestCase
  72. include Attribute
  73. include Fixture
  74. include ExceptionHandler
  75. include ErrorHandler
  76. include FailureHandler
  77. include TestCasePendingSupport
  78. include TestCaseOmissionSupport
  79. include TestCaseNotificationSupport
  80. include Priority
  81. include Assertions
  82. include Util::BacktraceFilter
  83. include Util::Output
  84. STARTED = name + "::STARTED" # :nodoc:
  85. FINISHED = name + "::FINISHED" # :nodoc:
  86. DESCENDANTS = [] # :nodoc:
  87. AVAILABLE_ORDERS = [:alphabetic, :random, :defined] # :nodoc:
  88. class << self
  89. def inherited(sub_class) # :nodoc:
  90. DESCENDANTS << sub_class
  91. end
  92. @@added_methods = {}
  93. def method_added(name) # :nodoc:
  94. super
  95. added_methods = (@@added_methods[self] ||= [])
  96. stringified_name = name.to_s
  97. if added_methods.include?(stringified_name)
  98. attribute(:redefined, true, {}, stringified_name)
  99. end
  100. added_methods << stringified_name
  101. end
  102. # Rolls up all of the test* methods in the fixture into
  103. # one suite, creating a new instance of the fixture for
  104. # each method.
  105. def suite
  106. suite = TestSuite.new(name, self)
  107. collect_test_names.each do |test|
  108. catch(:invalid_test) do
  109. suite << new(test)
  110. end
  111. end
  112. if suite.empty?
  113. catch(:invalid_test) do
  114. suite << new("default_test")
  115. end
  116. end
  117. suite
  118. end
  119. # Called before every test case runs. Can be used
  120. # to set up fixture information used in test case
  121. # scope.
  122. #
  123. # Here is an example test case:
  124. # class TestMyClass < Test::Unit::TestCase
  125. # class << self
  126. # def startup
  127. # ...
  128. # end
  129. # end
  130. #
  131. # def setup
  132. # ...
  133. # end
  134. #
  135. # def test_my_class1
  136. # ...
  137. # end
  138. #
  139. # def test_my_class2
  140. # ...
  141. # end
  142. # end
  143. #
  144. # Here is a call order:
  145. # * startup
  146. # * setup
  147. # * test_my_class1 (or test_my_class2)
  148. # * setup
  149. # * test_my_class2 (or test_my_class1)
  150. #
  151. # Note that you should not assume test order. Tests
  152. # should be worked in any order.
  153. def startup
  154. end
  155. # Called after every test case runs. Can be used to tear
  156. # down fixture information used in test case scope.
  157. #
  158. # Here is an example test case:
  159. # class TestMyClass < Test::Unit::TestCase
  160. # class << self
  161. # def shutdown
  162. # ...
  163. # end
  164. # end
  165. #
  166. # def teardown
  167. # ...
  168. # end
  169. #
  170. # def test_my_class1
  171. # ...
  172. # end
  173. #
  174. # def test_my_class2
  175. # ...
  176. # end
  177. # end
  178. #
  179. # Here is a call order:
  180. # * test_my_class1 (or test_my_class2)
  181. # * teardown
  182. # * test_my_class2 (or test_my_class1)
  183. # * teardown
  184. # * shutdown
  185. #
  186. # Note that you should not assume test order. Tests
  187. # should be worked in any order.
  188. def shutdown
  189. end
  190. @@test_orders = {}
  191. # Returns the current test order. This returns
  192. # +:alphabetic+ by default.
  193. def test_order
  194. @@test_orders[self] || AVAILABLE_ORDERS.first
  195. end
  196. # Sets the current test order.
  197. #
  198. # Here are the available _order_:
  199. # [:alphabetic]
  200. # Default. Tests are sorted in alphabetic order.
  201. # [:random]
  202. # Tests are sorted in random order.
  203. # [:defined]
  204. # Tests are sorted in defined order.
  205. def test_order=(order)
  206. @@test_orders[self] = order
  207. end
  208. # Defines a test in declarative syntax.
  209. #
  210. # The following two test definitions are the same:
  211. #
  212. # description "register user"
  213. # def test_register_user
  214. # ...
  215. # end
  216. #
  217. # test "register user" do
  218. # ...
  219. # end
  220. def test(test_description, &block)
  221. normalized_description = test_description.gsub(/[^a-zA-Z\d_]+/, '_')
  222. method_name = "test_#{normalized_description}".to_sym
  223. define_method(method_name, &block)
  224. description(test_description, method_name)
  225. end
  226. # Describes a test.
  227. #
  228. # The following example associates "register a
  229. # normal user" description with "test_register"
  230. # test.
  231. #
  232. # description "register a normal user"
  233. # def test_register
  234. # ...
  235. # end
  236. def description(value, target=nil)
  237. attribute(:description, value, {}, target || [])
  238. end
  239. # :stopdoc:
  240. private
  241. def collect_test_names
  242. method_names = public_instance_methods(true).collect do |name|
  243. name.to_s
  244. end
  245. test_names = method_names.find_all do |method_name|
  246. method_name =~ /^test./
  247. end
  248. send("sort_test_names_in_#{test_order}_order", test_names)
  249. end
  250. def sort_test_names_in_alphabetic_order(test_names)
  251. test_names.sort
  252. end
  253. def sort_test_names_in_random_order(test_names)
  254. test_names.sort_by {rand(test_names.size)}
  255. end
  256. def sort_test_names_in_defined_order(test_names)
  257. added_methods = @@added_methods[self]
  258. test_names.sort do |test1, test2|
  259. test1_defined_order = added_methods.index(test1)
  260. test2_defined_order = added_methods.index(test2)
  261. if test1_defined_order and test2_defined_order
  262. test1_defined_order <=> test2_defined_order
  263. elsif test1_defined_order
  264. 1
  265. elsif test2_defined_order
  266. -1
  267. else
  268. test1 <=> test2
  269. end
  270. end
  271. end
  272. # :startdoc:
  273. end
  274. attr_reader :method_name
  275. # Creates a new instance of the fixture for running the
  276. # test represented by test_method_name.
  277. def initialize(test_method_name)
  278. throw :invalid_test unless respond_to?(test_method_name)
  279. test_method = method(test_method_name)
  280. throw :invalid_test if test_method.arity > 0
  281. owner = Util::MethodOwnerFinder.find(self, test_method_name)
  282. if owner.class != Module and self.class != owner
  283. throw :invalid_test
  284. end
  285. @method_name = test_method_name
  286. @test_passed = true
  287. @interrupted = false
  288. end
  289. # Runs the individual test method represented by this
  290. # instance of the fixture, collecting statistics, failures
  291. # and errors in result.
  292. def run(result)
  293. begin
  294. @_result = result
  295. yield(STARTED, name)
  296. begin
  297. run_setup
  298. run_test
  299. rescue Exception
  300. @interrupted = true
  301. raise unless handle_exception($!)
  302. ensure
  303. begin
  304. run_teardown
  305. rescue Exception
  306. raise unless handle_exception($!)
  307. end
  308. end
  309. result.add_run
  310. yield(FINISHED, name)
  311. ensure
  312. # @_result = nil # For test-spec's after_all :<
  313. end
  314. end
  315. # Called before every test method runs. Can be used
  316. # to set up fixture information.
  317. #
  318. # You can add additional setup tasks by the following
  319. # code:
  320. # class TestMyClass < Test::Unit::TestCase
  321. # def setup
  322. # ...
  323. # end
  324. #
  325. # setup
  326. # def my_setup1
  327. # ...
  328. # end
  329. #
  330. # setup
  331. # def my_setup2
  332. # ...
  333. # end
  334. #
  335. # def test_my_class
  336. # ...
  337. # end
  338. # end
  339. #
  340. # Here is a call order:
  341. # * setup
  342. # * my_setup1
  343. # * my_setup2
  344. # * test_my_class
  345. def setup
  346. end
  347. # Called after every test method runs. Can be used to tear
  348. # down fixture information.
  349. #
  350. # You can add additional teardown tasks by the following
  351. # code:
  352. # class TestMyClass < Test::Unit::TestCase
  353. # def teardown
  354. # ...
  355. # end
  356. #
  357. # teardown
  358. # def my_teardown1
  359. # ...
  360. # end
  361. #
  362. # teardown
  363. # def my_teardown2
  364. # ...
  365. # end
  366. #
  367. # def test_my_class
  368. # ...
  369. # end
  370. # end
  371. #
  372. # Here is a call order:
  373. # * test_my_class
  374. # * my_teardown2
  375. # * my_teardown1
  376. # * teardown
  377. def teardown
  378. end
  379. def default_test
  380. flunk("No tests were specified")
  381. end
  382. def size
  383. 1
  384. end
  385. # Returns a human-readable name for the specific test that
  386. # this instance of TestCase represents.
  387. def name
  388. "#{@method_name}(#{self.class.name})"
  389. end
  390. # Returns a description for the test. A description
  391. # will be associated by Test::Unit::TestCase.test or
  392. # Test::Unit::TestCase.description.
  393. #
  394. # Returns a name for the test for no description test.
  395. def description
  396. self[:description] || name
  397. end
  398. # Overridden to return #name.
  399. def to_s
  400. name
  401. end
  402. # It's handy to be able to compare TestCase instances.
  403. def ==(other)
  404. return false unless(other.kind_of?(self.class))
  405. return false unless(@method_name == other.method_name)
  406. self.class == other.class
  407. end
  408. def interrupted?
  409. @interrupted
  410. end
  411. private
  412. def current_result
  413. @_result
  414. end
  415. def run_test
  416. if self.class.get_attribute(@method_name, :redefined)
  417. notify("#{self.class}\##{@method_name} was redefined")
  418. end
  419. __send__(@method_name)
  420. add_pass
  421. end
  422. def handle_exception(exception)
  423. self.class.exception_handlers.each do |handler|
  424. return true if send(handler, exception)
  425. end
  426. false
  427. end
  428. # Returns whether this individual test passed or
  429. # not. Primarily for use in teardown so that artifacts
  430. # can be left behind if the test fails.
  431. def passed?
  432. @test_passed
  433. end
  434. def problem_occurred
  435. @test_passed = false
  436. end
  437. def add_assertion
  438. current_result.add_assertion
  439. end
  440. def add_pass
  441. current_result.add_pass
  442. end
  443. end
  444. end
  445. end