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

https://github.com/vartikasingh/BackchannelApplication-1 · Ruby · 487 lines · 401 code · 86 blank · 0 comment · 48 complexity · 0fd837e6f936cabb3a169568787cb085 MD5 · raw file

  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_cleanup_without_option
  60. expected_cleanup_calls = [:custom_cleanup_method3,
  61. :custom_cleanup_method1,
  62. :custom_cleanup_method0,
  63. :cleanup]
  64. test_case = assert_cleanup(expected_cleanup_calls, [])
  65. assert_inherited_cleanup(expected_cleanup_calls, test_case)
  66. assert_inherited_cleanup([:cleanup], nil)
  67. assert_called_fixtures(expected_cleanup_calls, test_case)
  68. end
  69. def test_cleanup_with_before_option
  70. expected_cleanup_calls = [:custom_cleanup_method3,
  71. :custom_cleanup_method0,
  72. :custom_cleanup_method1,
  73. :cleanup]
  74. test_case = assert_cleanup(expected_cleanup_calls,
  75. [[{:before => :append}],
  76. [{:before => :append}],
  77. [{:before => :prepend}],
  78. [{:before => :prepend}]])
  79. assert_inherited_cleanup(expected_cleanup_calls, test_case)
  80. assert_inherited_cleanup([:cleanup], nil)
  81. assert_called_fixtures(expected_cleanup_calls, test_case)
  82. end
  83. def test_cleanup_with_after_option
  84. expected_cleanup_calls = [:cleanup,
  85. :custom_cleanup_method3,
  86. :custom_cleanup_method0,
  87. :custom_cleanup_method1]
  88. test_case = assert_cleanup(expected_cleanup_calls,
  89. [[{:after => :append}],
  90. [{:after => :append}],
  91. [{:after => :prepend}],
  92. [{:after => :prepend}]])
  93. assert_inherited_cleanup(expected_cleanup_calls, test_case)
  94. assert_inherited_cleanup([:cleanup], nil)
  95. assert_called_fixtures(expected_cleanup_calls, test_case)
  96. end
  97. def test_cleanup_with_invalid_option
  98. assert_invalid_cleanup_option(:unknown => true)
  99. assert_invalid_cleanup_option(:before => :unknown)
  100. assert_invalid_cleanup_option(:after => :unknown)
  101. end
  102. def test_cleanup_with_option_to_inherited
  103. expected_cleanup_calls = [:cleanup]
  104. test_case = assert_cleanup(expected_cleanup_calls, nil)
  105. assert_inherited_cleanup([:custom_cleanup_method3,
  106. :custom_cleanup_method1,
  107. :custom_cleanup_method0,
  108. :cleanup],
  109. test_case, [])
  110. assert_inherited_cleanup([:cleanup], nil)
  111. assert_called_fixtures(expected_cleanup_calls, test_case)
  112. end
  113. def test_cleanup_with_exception
  114. test_case = Class.new(Test::Unit::TestCase) do
  115. def called_ids
  116. @called_ids ||= []
  117. end
  118. def called(id)
  119. called_ids << id
  120. end
  121. def cleanup
  122. called(:cleanup)
  123. raise "cleanup"
  124. end
  125. cleanup
  126. def custom_cleanup_method0
  127. called(:custom_cleanup_method0)
  128. raise "custom_cleanup_method0"
  129. end
  130. cleanup
  131. def custom_cleanup_method1
  132. called(:custom_cleanup_method1)
  133. raise "custom_cleanup_method1"
  134. end
  135. def test_nothing
  136. end
  137. end
  138. assert_called_fixtures([:custom_cleanup_method1],
  139. test_case)
  140. end
  141. def test_teardown_without_option
  142. expected_teardown_calls = [:custom_teardown_method3,
  143. :custom_teardown_method1,
  144. :custom_teardown_method0,
  145. :teardown]
  146. test_case = assert_teardown(expected_teardown_calls, [])
  147. assert_inherited_teardown(expected_teardown_calls, test_case)
  148. assert_inherited_teardown([:teardown], nil)
  149. assert_called_fixtures(expected_teardown_calls, test_case)
  150. end
  151. def test_teardown_with_before_option
  152. expected_teardown_calls = [:custom_teardown_method3,
  153. :custom_teardown_method0,
  154. :custom_teardown_method1,
  155. :teardown]
  156. test_case = assert_teardown(expected_teardown_calls,
  157. [[{:before => :append}],
  158. [{:before => :append}],
  159. [{:before => :prepend}],
  160. [{:before => :prepend}]])
  161. assert_inherited_teardown(expected_teardown_calls, test_case)
  162. assert_inherited_teardown([:teardown], nil)
  163. assert_called_fixtures(expected_teardown_calls, test_case)
  164. end
  165. def test_teardown_with_after_option
  166. expected_teardown_calls = [:teardown,
  167. :custom_teardown_method3,
  168. :custom_teardown_method0,
  169. :custom_teardown_method1]
  170. test_case = assert_teardown(expected_teardown_calls,
  171. [[{:after => :append}],
  172. [{:after => :append}],
  173. [{:after => :prepend}],
  174. [{:after => :prepend}]])
  175. assert_inherited_teardown(expected_teardown_calls, test_case)
  176. assert_inherited_teardown([:teardown], nil)
  177. assert_called_fixtures(expected_teardown_calls, test_case)
  178. end
  179. def test_teardown_with_invalid_option
  180. assert_invalid_teardown_option(:unknown => true)
  181. assert_invalid_teardown_option(:before => :unknown)
  182. assert_invalid_teardown_option(:after => :unknown)
  183. end
  184. def test_teardown_with_option_to_inherited
  185. expected_teardown_calls = [:teardown]
  186. test_case = assert_teardown(expected_teardown_calls, nil)
  187. assert_inherited_teardown([:custom_teardown_method3,
  188. :custom_teardown_method1,
  189. :custom_teardown_method0,
  190. :teardown],
  191. test_case, [])
  192. assert_inherited_teardown([:teardown], nil)
  193. assert_called_fixtures(expected_teardown_calls, test_case)
  194. end
  195. def test_teardown_with_exception
  196. test_case = Class.new(Test::Unit::TestCase) do
  197. def called_ids
  198. @called_ids ||= []
  199. end
  200. def called(id)
  201. called_ids << id
  202. end
  203. def teardown
  204. called(:teardown)
  205. raise "teardown"
  206. end
  207. teardown
  208. def custom_teardown_method0
  209. called(:custom_teardown_method0)
  210. raise "custom_teardown_method0"
  211. end
  212. teardown
  213. def custom_teardown_method1
  214. called(:custom_teardown_method1)
  215. raise "custom_teardown_method1"
  216. end
  217. def test_nothing
  218. end
  219. end
  220. assert_called_fixtures([:custom_teardown_method1,
  221. :custom_teardown_method0,
  222. :teardown],
  223. test_case)
  224. end
  225. private
  226. def assert_called_fixtures(expected, test_case)
  227. test = test_case.new("test_nothing")
  228. test.run(Test::Unit::TestResult.new) {}
  229. assert_equal(expected, test.called_ids)
  230. end
  231. def assert_setup_customizable(expected, parent, options)
  232. test_case = Class.new(parent || Test::Unit::TestCase) do
  233. yield(self, :before) if block_given?
  234. def called_ids
  235. @called_ids ||= []
  236. end
  237. def called(id)
  238. called_ids << id
  239. end
  240. def setup
  241. called(:setup)
  242. end
  243. setup(*(options[0] || [])) if options
  244. def custom_setup_method0
  245. called(:custom_setup_method0)
  246. end
  247. def custom_setup_method1
  248. called(:custom_setup_method1)
  249. end
  250. setup(*[:custom_setup_method1, *(options[1] || [])]) if options
  251. setup(*(options[2] || [])) if options
  252. def custom_setup_method2
  253. called(:custom_setup_method2)
  254. end
  255. unregister_setup(:custom_setup_method2) if options
  256. setup(*(options[3] || [])) if options
  257. def custom_setup_method3
  258. called(:custom_setup_method3)
  259. end
  260. def test_nothing
  261. end
  262. yield(self, :after) if block_given?
  263. end
  264. assert_called_fixtures(expected, test_case)
  265. test_case
  266. end
  267. def assert_setup(expected, options)
  268. _test_case = assert_setup_customizable(expected, nil, options)
  269. assert_setup_customizable(expected, nil, options) do |test_case, tag|
  270. test_case.send(:include, EmptyModule) if tag == :before
  271. end
  272. _test_case
  273. end
  274. def assert_inherited_setup(expected, parent, options=nil)
  275. _test_case = assert_setup_customizable(expected, parent, options)
  276. assert_setup_customizable(expected, parent, options) do |test_case, tag|
  277. test_case.send(:include, EmptyModule) if tag == :before
  278. end
  279. _test_case
  280. end
  281. def assert_cleanup_customizable(expected, parent, options)
  282. test_case = Class.new(parent || Test::Unit::TestCase) do
  283. yield(self, :before) if block_given?
  284. def called_ids
  285. @called_ids ||= []
  286. end
  287. def called(id)
  288. called_ids << id
  289. end
  290. def cleanup
  291. called(:cleanup)
  292. end
  293. cleanup(*(options[0] || [])) if options
  294. def custom_cleanup_method0
  295. called(:custom_cleanup_method0)
  296. end
  297. def custom_cleanup_method1
  298. called(:custom_cleanup_method1)
  299. end
  300. cleanup(*[:custom_cleanup_method1, *(options[1] || [])]) if options
  301. cleanup(*(options[2] || [])) if options
  302. def custom_cleanup_method2
  303. called(:custom_cleanup_method2)
  304. end
  305. unregister_cleanup(:custom_cleanup_method2) if options
  306. cleanup(*(options[3] || [])) if options
  307. def custom_cleanup_method3
  308. called(:custom_cleanup_method3)
  309. end
  310. def test_nothing
  311. end
  312. yield(self, :after) if block_given?
  313. end
  314. assert_called_fixtures(expected, test_case)
  315. test_case
  316. end
  317. def assert_cleanup(expected, options)
  318. assert_cleanup_customizable(expected, nil, options)
  319. assert_cleanup_customizable(expected, nil, options) do |test_case, tag|
  320. test_case.send(:include, EmptyModule) if tag == :before
  321. end
  322. end
  323. def assert_inherited_cleanup(expected, parent, options=nil)
  324. assert_cleanup_customizable(expected, parent, options)
  325. assert_cleanup_customizable(expected, parent, options) do |test_case, tag|
  326. test_case.send(:include, EmptyModule) if tag == :before
  327. end
  328. end
  329. def assert_teardown_customizable(expected, parent, options)
  330. test_case = Class.new(parent || Test::Unit::TestCase) do
  331. yield(self, :before) if block_given?
  332. def called_ids
  333. @called_ids ||= []
  334. end
  335. def called(id)
  336. called_ids << id
  337. end
  338. def teardown
  339. called(:teardown)
  340. end
  341. teardown(*(options[0] || [])) if options
  342. def custom_teardown_method0
  343. called(:custom_teardown_method0)
  344. end
  345. def custom_teardown_method1
  346. called(:custom_teardown_method1)
  347. end
  348. teardown(*[:custom_teardown_method1, *(options[1] || [])]) if options
  349. teardown(*(options[2] || [])) if options
  350. def custom_teardown_method2
  351. called(:custom_teardown_method2)
  352. end
  353. unregister_teardown(:custom_teardown_method2) if options
  354. teardown(*(options[3] || [])) if options
  355. def custom_teardown_method3
  356. called(:custom_teardown_method3)
  357. end
  358. def test_nothing
  359. end
  360. yield(self, :after) if block_given?
  361. end
  362. assert_called_fixtures(expected, test_case)
  363. test_case
  364. end
  365. def assert_teardown(expected, options)
  366. assert_teardown_customizable(expected, nil, options)
  367. assert_teardown_customizable(expected, nil, options) do |test_case, tag|
  368. test_case.send(:include, EmptyModule) if tag == :before
  369. end
  370. end
  371. def assert_inherited_teardown(expected, parent, options=nil)
  372. assert_teardown_customizable(expected, parent, options)
  373. assert_teardown_customizable(expected, parent, options) do |test_case, tag|
  374. test_case.send(:include, EmptyModule) if tag == :before
  375. end
  376. end
  377. def assert_invalid_option(fixture_type, option)
  378. exception = assert_raise(ArgumentError) do
  379. Class.new(Test::Unit::TestCase) do
  380. def test_nothing
  381. end
  382. send(fixture_type, option)
  383. def fixture
  384. end
  385. end
  386. end
  387. assert_equal("must be {:before => :prepend}, {:before => :append}, " +
  388. "{:after => :prepend} or {:after => :append}" +
  389. ": #{option.inspect}",
  390. exception.message)
  391. end
  392. def assert_invalid_setup_option(option)
  393. assert_invalid_option(:setup, option)
  394. end
  395. def assert_invalid_cleanup_option(option)
  396. assert_invalid_option(:cleanup, option)
  397. end
  398. def assert_invalid_teardown_option(option)
  399. assert_invalid_option(:teardown, option)
  400. end
  401. end