PageRenderTime 51ms CodeModel.GetById 15ms 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-fixture.rb

http://github.com/IronLanguages/main
Ruby | 287 lines | 238 code | 49 blank | 0 comment | 32 complexity | 648d303b0ff6d348aeb27e52ef616c3e MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. class TestUnitFixture < Test::Unit::TestCase
  2. module EmptyModule
  3. end
  4. def test_setup_without_option
  5. expected_setup_calls = [:setup,
  6. :custom_setup_method0,
  7. :custom_setup_method1,
  8. :custom_setup_method3]
  9. test_case = assert_setup(expected_setup_calls, [])
  10. assert_inherited_setup(expected_setup_calls, test_case)
  11. assert_inherited_setup([:setup], nil)
  12. assert_called_fixtures(expected_setup_calls, test_case)
  13. end
  14. def test_setup_with_before_option
  15. expected_setup_calls = [:custom_setup_method3,
  16. :custom_setup_method0,
  17. :custom_setup_method1,
  18. :setup]
  19. test_case = assert_setup(expected_setup_calls,
  20. [[{:before => :append}],
  21. [{:before => :append}],
  22. [{:before => :prepend}],
  23. [{:before => :prepend}]])
  24. assert_inherited_setup(expected_setup_calls, test_case)
  25. assert_inherited_setup([:setup], nil)
  26. assert_called_fixtures(expected_setup_calls, test_case)
  27. end
  28. def test_setup_with_after_option
  29. expected_setup_calls = [:setup,
  30. :custom_setup_method3,
  31. :custom_setup_method0,
  32. :custom_setup_method1]
  33. test_case = assert_setup(expected_setup_calls,
  34. [[{:after => :append}],
  35. [{:after => :append}],
  36. [{:after => :prepend}],
  37. [{:after => :prepend}]])
  38. assert_inherited_setup(expected_setup_calls, test_case)
  39. assert_inherited_setup([:setup], nil)
  40. assert_called_fixtures(expected_setup_calls, test_case)
  41. end
  42. def test_setup_with_invalid_option
  43. assert_invalid_setup_option(:unknown => true)
  44. assert_invalid_setup_option(:before => :unknown)
  45. assert_invalid_setup_option(:after => :unknown)
  46. end
  47. def test_setup_with_option_to_inherited
  48. expected_setup_calls = [:setup]
  49. test_case = assert_setup(expected_setup_calls, nil)
  50. assert_inherited_setup([:setup,
  51. :custom_setup_method0,
  52. :custom_setup_method1,
  53. :custom_setup_method3],
  54. test_case,
  55. [])
  56. assert_inherited_setup([:setup], nil)
  57. assert_called_fixtures(expected_setup_calls, test_case)
  58. end
  59. def test_teardown_without_option
  60. expected_teardown_calls = [:custom_teardown_method3,
  61. :custom_teardown_method1,
  62. :custom_teardown_method0,
  63. :teardown]
  64. test_case = assert_teardown(expected_teardown_calls, [])
  65. assert_inherited_teardown(expected_teardown_calls, test_case)
  66. assert_inherited_teardown([:teardown], nil)
  67. assert_called_fixtures(expected_teardown_calls, test_case)
  68. end
  69. def test_teardown_with_before_option
  70. expected_teardown_calls = [:custom_teardown_method3,
  71. :custom_teardown_method0,
  72. :custom_teardown_method1,
  73. :teardown]
  74. test_case = assert_teardown(expected_teardown_calls,
  75. [[{:before => :append}],
  76. [{:before => :append}],
  77. [{:before => :prepend}],
  78. [{:before => :prepend}]])
  79. assert_inherited_teardown(expected_teardown_calls, test_case)
  80. assert_inherited_teardown([:teardown], nil)
  81. assert_called_fixtures(expected_teardown_calls, test_case)
  82. end
  83. def test_teardown_with_after_option
  84. expected_teardown_calls = [:teardown,
  85. :custom_teardown_method3,
  86. :custom_teardown_method0,
  87. :custom_teardown_method1]
  88. test_case = assert_teardown(expected_teardown_calls,
  89. [[{:after => :append}],
  90. [{:after => :append}],
  91. [{:after => :prepend}],
  92. [{:after => :prepend}]])
  93. assert_inherited_teardown(expected_teardown_calls, test_case)
  94. assert_inherited_teardown([:teardown], nil)
  95. assert_called_fixtures(expected_teardown_calls, test_case)
  96. end
  97. def test_teardown_with_invalid_option
  98. assert_invalid_teardown_option(:unknown => true)
  99. assert_invalid_teardown_option(:before => :unknown)
  100. assert_invalid_teardown_option(:after => :unknown)
  101. end
  102. def test_teardown_with_option_to_inherited
  103. expected_teardown_calls = [:teardown]
  104. test_case = assert_teardown(expected_teardown_calls, nil)
  105. assert_inherited_teardown([:custom_teardown_method3,
  106. :custom_teardown_method1,
  107. :custom_teardown_method0,
  108. :teardown],
  109. test_case, [])
  110. assert_inherited_teardown([:teardown], nil)
  111. assert_called_fixtures(expected_teardown_calls, test_case)
  112. end
  113. private
  114. def assert_called_fixtures(expected, test_case)
  115. test = test_case.new("test_nothing")
  116. test.run(Test::Unit::TestResult.new) {}
  117. assert_equal(expected, test.called_ids)
  118. end
  119. def assert_setup_customizable(expected, parent, options)
  120. test_case = Class.new(parent || Test::Unit::TestCase) do
  121. yield(self, :before) if block_given?
  122. def called_ids
  123. @called_ids ||= []
  124. end
  125. def called(id)
  126. called_ids << id
  127. end
  128. def setup
  129. called(:setup)
  130. end
  131. setup(*(options[0] || [])) if options
  132. def custom_setup_method0
  133. called(:custom_setup_method0)
  134. end
  135. def custom_setup_method1
  136. called(:custom_setup_method1)
  137. end
  138. setup(*[:custom_setup_method1, *(options[1] || [])]) if options
  139. setup(*(options[2] || [])) if options
  140. def custom_setup_method2
  141. called(:custom_setup_method2)
  142. end
  143. unregister_setup(:custom_setup_method2) if options
  144. setup(*(options[3] || [])) if options
  145. def custom_setup_method3
  146. called(:custom_setup_method3)
  147. end
  148. def test_nothing
  149. end
  150. yield(self, :after) if block_given?
  151. end
  152. assert_called_fixtures(expected, test_case)
  153. test_case
  154. end
  155. def assert_setup(expected, options)
  156. _test_case = assert_setup_customizable(expected, nil, options)
  157. assert_setup_customizable(expected, nil, options) do |test_case, tag|
  158. test_case.send(:include, EmptyModule) if tag == :before
  159. end
  160. _test_case
  161. end
  162. def assert_inherited_setup(expected, parent, options=nil)
  163. _test_case = assert_setup_customizable(expected, parent, options)
  164. assert_setup_customizable(expected, parent, options) do |test_case, tag|
  165. test_case.send(:include, EmptyModule) if tag == :before
  166. end
  167. _test_case
  168. end
  169. def assert_teardown_customizable(expected, parent, options)
  170. test_case = Class.new(parent || Test::Unit::TestCase) do
  171. yield(self, :before) if block_given?
  172. def called_ids
  173. @called_ids ||= []
  174. end
  175. def called(id)
  176. called_ids << id
  177. end
  178. def teardown
  179. called(:teardown)
  180. end
  181. teardown(*(options[0] || [])) if options
  182. def custom_teardown_method0
  183. called(:custom_teardown_method0)
  184. end
  185. def custom_teardown_method1
  186. called(:custom_teardown_method1)
  187. end
  188. teardown(*[:custom_teardown_method1, *(options[1] || [])]) if options
  189. teardown(*(options[2] || [])) if options
  190. def custom_teardown_method2
  191. called(:custom_teardown_method2)
  192. end
  193. unregister_teardown(:custom_teardown_method2) if options
  194. teardown(*(options[3] || [])) if options
  195. def custom_teardown_method3
  196. called(:custom_teardown_method3)
  197. end
  198. def test_nothing
  199. end
  200. yield(self, :after) if block_given?
  201. end
  202. assert_called_fixtures(expected, test_case)
  203. test_case
  204. end
  205. def assert_teardown(expected, options)
  206. assert_teardown_customizable(expected, nil, options)
  207. assert_teardown_customizable(expected, nil, options) do |test_case, tag|
  208. test_case.send(:include, EmptyModule) if tag == :before
  209. end
  210. end
  211. def assert_inherited_teardown(expected, parent, options=nil)
  212. assert_teardown_customizable(expected, parent, options)
  213. assert_teardown_customizable(expected, parent, options) do |test_case, tag|
  214. test_case.send(:include, EmptyModule) if tag == :before
  215. end
  216. end
  217. def assert_invalid_option(fixture_type, option)
  218. exception = assert_raise(ArgumentError) do
  219. Class.new(Test::Unit::TestCase) do
  220. def test_nothing
  221. end
  222. send(fixture_type, option)
  223. def fixture
  224. end
  225. end
  226. end
  227. assert_equal("must be {:before => :prepend}, {:before => :append}, " +
  228. "{:after => :prepend} or {:after => :append}" +
  229. ": #{option.inspect}",
  230. exception.message)
  231. end
  232. def assert_invalid_setup_option(option)
  233. assert_invalid_option(:setup, option)
  234. end
  235. def assert_invalid_teardown_option(option)
  236. assert_invalid_option(:teardown, option)
  237. end
  238. end