PageRenderTime 50ms CodeModel.GetById 20ms 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/collector/test-load.rb

http://github.com/IronLanguages/main
Ruby | 442 lines | 387 code | 55 blank | 0 comment | 4 complexity | 2c5fea662c29a688ce804fdf565b7ae1 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. require 'tmpdir'
  2. require 'pathname'
  3. require 'test/unit'
  4. require 'test/unit/collector/load'
  5. class TestUnitCollectorLoad < Test::Unit::TestCase
  6. def setup
  7. @previous_descendants = Test::Unit::TestCase::DESCENDANTS.dup
  8. Test::Unit::TestCase::DESCENDANTS.clear
  9. @temporary_test_cases_module_name = "TempTestCases"
  10. ::Object.const_set(@temporary_test_cases_module_name, Module.new)
  11. @test_dir = Pathname(Dir.tmpdir) + "test-unit"
  12. @extra_test_dir = Pathname(Dir.tmpdir) + "test-unit-extra"
  13. ensure_clean_directory(@test_dir)
  14. ensure_clean_directory(@extra_test_dir)
  15. end
  16. setup
  17. def setup_top_level_test_cases
  18. @test_case1 = @test_dir + "test_case1.rb"
  19. @test_case2 = @test_dir + "test_case2.rb"
  20. @no_load_test_case3 = @test_dir + "case3.rb"
  21. @test_case1.open("w") do |test_case|
  22. test_case.puts(<<-EOT)
  23. module #{@temporary_test_cases_module_name}
  24. class TestCase1 < Test::Unit::TestCase
  25. def test1_1
  26. end
  27. def test1_2
  28. end
  29. end
  30. end
  31. EOT
  32. end
  33. @test_case2.open("w") do |test_case|
  34. test_case.puts(<<-EOT)
  35. module #{@temporary_test_cases_module_name}
  36. class TestCase2 < Test::Unit::TestCase
  37. def test2
  38. end
  39. end
  40. end
  41. EOT
  42. end
  43. @no_load_test_case3.open("w") do |test_case|
  44. test_case.puts(<<-EOT)
  45. module #{@temporary_test_cases_module_name}
  46. class NoLoadTestCase3 < Test::Unit::TestCase
  47. def test3
  48. end
  49. end
  50. end
  51. EOT
  52. end
  53. end
  54. setup
  55. def setup_sub_level_test_cases
  56. @sub_test_dir = @test_dir + "sub"
  57. @sub_test_dir.mkpath
  58. @sub_test_case4 = @sub_test_dir + "test_case4.rb"
  59. @no_load_sub_test_case5 = @sub_test_dir + "case5.rb"
  60. @sub_test_case6 = @sub_test_dir + "test_case6.rb"
  61. @sub_test_case4.open("w") do |test_case|
  62. test_case.puts(<<-EOT)
  63. module #{@temporary_test_cases_module_name}
  64. class SubTestCase4 < Test::Unit::TestCase
  65. def test4_1
  66. end
  67. def test4_2
  68. end
  69. end
  70. end
  71. EOT
  72. end
  73. @no_load_sub_test_case5.open("w") do |test_case|
  74. test_case.puts(<<-EOT)
  75. module #{@temporary_test_cases_module_name}
  76. class NoLoadSubTestCase5 < Test::Unit::TestCase
  77. def test5_1
  78. end
  79. def test5_2
  80. end
  81. end
  82. end
  83. EOT
  84. end
  85. @sub_test_case6.open("w") do |test_case|
  86. test_case.puts(<<-EOT)
  87. module #{@temporary_test_cases_module_name}
  88. class SubTestCase6 < Test::Unit::TestCase
  89. def test6
  90. end
  91. end
  92. end
  93. EOT
  94. end
  95. end
  96. setup
  97. def setup_sub_level_test_cases2
  98. @sub2_test_dir = @test_dir + "sub2"
  99. @sub2_test_dir.mkpath
  100. @no_load_sub2_test_case7 = @sub2_test_dir + "case7.rb"
  101. @sub2_test_case8 = @sub2_test_dir + "test_case8.rb"
  102. @sub2_test_case9 = @sub2_test_dir + "test_case9.rb"
  103. @no_load_sub2_test_case7.open("w") do |test_case|
  104. test_case.puts(<<-EOT)
  105. module #{@temporary_test_cases_module_name}
  106. class NoLoadSub2TestCase7 < Test::Unit::TestCase
  107. def test7_1
  108. end
  109. def test7_2
  110. end
  111. end
  112. end
  113. EOT
  114. end
  115. @sub2_test_case8.open("w") do |test_case|
  116. test_case.puts(<<-EOT)
  117. module #{@temporary_test_cases_module_name}
  118. class Sub2TestCase8 < Test::Unit::TestCase
  119. def test8_1
  120. end
  121. def test8_2
  122. end
  123. end
  124. end
  125. EOT
  126. end
  127. @sub2_test_case9.open("w") do |test_case|
  128. test_case.puts(<<-EOT)
  129. module #{@temporary_test_cases_module_name}
  130. class Sub2TestCase9 < Test::Unit::TestCase
  131. def test9
  132. end
  133. end
  134. end
  135. EOT
  136. end
  137. end
  138. setup
  139. def setup_svn_test_cases
  140. @svn_test_dir = @test_dir + ".svn"
  141. @svn_test_dir.mkpath
  142. @svn_test_case10 = @svn_test_dir + "test_case10.rb"
  143. @svn_test_case10.open("w") do |test_case|
  144. test_case.puts(<<-EOT)
  145. module #{@temporary_test_cases_module_name}
  146. class SvnTestCase10 < Test::Unit::TestCase
  147. def test7
  148. end
  149. end
  150. end
  151. EOT
  152. end
  153. end
  154. setup
  155. def setup_sub_cvs_test_cases
  156. @sub_cvs_test_dir = @sub_test_dir + "CVS"
  157. @sub_cvs_test_dir.mkpath
  158. @sub_cvs_test_case11 = @sub_cvs_test_dir + "test_case11.rb"
  159. @sub_cvs_test_case11.open("w") do |test_case|
  160. test_case.puts(<<-EOT)
  161. module #{@temporary_test_cases_module_name}
  162. class SubCVSTestCase11 < Test::Unit::TestCase
  163. def test11
  164. end
  165. end
  166. end
  167. EOT
  168. end
  169. end
  170. setup
  171. def setup_sub_git_test_cases
  172. @sub_git_test_dir = @sub_test_dir + ".git"
  173. @sub_git_test_dir.mkpath
  174. @sub_git_test_case11 = @sub_git_test_dir + "test_case11.rb"
  175. @sub_git_test_case11.open("w") do |test_case|
  176. test_case.puts(<<-EOT)
  177. module #{@temporary_test_cases_module_name}
  178. class SubGitTestCase11 < Test::Unit::TestCase
  179. def test11
  180. end
  181. end
  182. end
  183. EOT
  184. end
  185. end
  186. setup
  187. def setup_extra_top_level_test_cases
  188. @test_cases12 = @extra_test_dir + "test_cases12.rb"
  189. @test_cases12.open("w") do |test_case|
  190. test_case.puts(<<-EOT)
  191. module #{@temporary_test_cases_module_name}
  192. class TestCase121 < Test::Unit::TestCase
  193. def test121_1
  194. end
  195. def test121_2
  196. end
  197. end
  198. class TestCase122 < Test::Unit::TestCase
  199. def test122_1
  200. end
  201. def test122_2
  202. end
  203. end
  204. end
  205. EOT
  206. end
  207. end
  208. setup
  209. def setup_sub_level_extra_test_cases
  210. @sub_extra_test_dir = @extra_test_dir + "sub"
  211. @sub_extra_test_dir.mkpath
  212. @cases13_test = @sub_extra_test_dir + "13cases_test.rb"
  213. @cases13_test.open("w") do |test_case|
  214. test_case.puts(<<-EOT)
  215. module #{@temporary_test_cases_module_name}
  216. class SubTestCase13 < Test::Unit::TestCase
  217. def test13_1
  218. end
  219. def test13_2
  220. end
  221. end
  222. end
  223. EOT
  224. end
  225. end
  226. def teardown
  227. @test_dir.rmtree if @test_dir.exist?
  228. ::Object.send(:remove_const, @temporary_test_cases_module_name)
  229. Test::Unit::TestCase::DESCENDANTS.replace(@previous_descendants)
  230. end
  231. def test_simple_collect
  232. assert_collect([:suite, {:name => @sub_test_dir.basename.to_s},
  233. [:suite, {:name => _test_case_name("SubTestCase4")},
  234. [:test, {:name => "test4_1"}],
  235. [:test, {:name => "test4_2"}]],
  236. [:suite, {:name => _test_case_name("SubTestCase6")},
  237. [:test, {:name => "test6"}]]],
  238. @sub_test_dir.to_s)
  239. end
  240. def test_simple_collect_test_suffix
  241. assert_collect([:suite, {:name => @extra_test_dir.basename.to_s},
  242. [:suite, {:name => _test_case_name("TestCase121")},
  243. [:test, {:name => "test121_1"}],
  244. [:test, {:name => "test121_2"}]],
  245. [:suite, {:name => _test_case_name("TestCase122")},
  246. [:test, {:name => "test122_1"}],
  247. [:test, {:name => "test122_2"}]],
  248. [:suite, {:name => @sub_extra_test_dir.basename.to_s},
  249. [:suite, {:name => _test_case_name("SubTestCase13")},
  250. [:test, {:name => "test13_1"}],
  251. [:test, {:name => "test13_2"}]]]],
  252. @extra_test_dir.to_s)
  253. end
  254. def test_multilevel_collect
  255. assert_collect([:suite, {:name => "."},
  256. [:suite, {:name => _test_case_name("TestCase1")},
  257. [:test, {:name => "test1_1"}],
  258. [:test, {:name => "test1_2"}]],
  259. [:suite, {:name => _test_case_name("TestCase2")},
  260. [:test, {:name => "test2"}]],
  261. [:suite, {:name => @sub_test_dir.basename.to_s},
  262. [:suite, {:name => _test_case_name("SubTestCase4")},
  263. [:test, {:name => "test4_1"}],
  264. [:test, {:name => "test4_2"}]],
  265. [:suite, {:name => _test_case_name("SubTestCase6")},
  266. [:test, {:name => "test6"}]]],
  267. [:suite, {:name => @sub2_test_dir.basename.to_s},
  268. [:suite, {:name => _test_case_name("Sub2TestCase8")},
  269. [:test, {:name => "test8_1"}],
  270. [:test, {:name => "test8_2"}]],
  271. [:suite, {:name => _test_case_name("Sub2TestCase9")},
  272. [:test, {:name => "test9"}]]]])
  273. end
  274. def test_collect_file
  275. assert_collect([:suite, {:name => _test_case_name("TestCase1")},
  276. [:test, {:name => "test1_1"}],
  277. [:test, {:name => "test1_2"}]],
  278. @test_case1.to_s)
  279. end
  280. def test_collect_file_no_pattern_match_file_name
  281. assert_collect([:suite, {:name => _test_case_name("NoLoadSubTestCase5")},
  282. [:test, {:name => "test5_1"}],
  283. [:test, {:name => "test5_2"}]],
  284. @no_load_sub_test_case5.to_s)
  285. end
  286. def test_collect_file_test_cases
  287. assert_collect([:suite, {:name => "[#{@test_cases12}]"},
  288. [:suite, {:name => _test_case_name("TestCase121")},
  289. [:test, {:name => "test121_1"}],
  290. [:test, {:name => "test121_2"}]],
  291. [:suite, {:name => _test_case_name("TestCase122")},
  292. [:test, {:name => "test122_1"}],
  293. [:test, {:name => "test122_2"}]]],
  294. @test_cases12.to_s)
  295. end
  296. def test_collect_files
  297. assert_collect([:suite,
  298. {:name => "[#{@test_case1}, #{@test_case2}]"},
  299. [:suite, {:name => _test_case_name("TestCase1")},
  300. [:test, {:name => "test1_1"}],
  301. [:test, {:name => "test1_2"}]],
  302. [:suite, {:name => _test_case_name("TestCase2")},
  303. [:test, {:name => "test2"}]]],
  304. @test_case1.to_s, @test_case2.to_s)
  305. end
  306. def test_nil_pattern
  307. assert_collect([:suite, {:name => @sub_test_dir.basename.to_s},
  308. [:suite, {:name => _test_case_name("NoLoadSubTestCase5")},
  309. [:test, {:name => "test5_1"}],
  310. [:test, {:name => "test5_2"}]],
  311. [:suite, {:name => _test_case_name("SubTestCase4")},
  312. [:test, {:name => "test4_1"}],
  313. [:test, {:name => "test4_2"}]],
  314. [:suite, {:name => _test_case_name("SubTestCase6")},
  315. [:test, {:name => "test6"}]]],
  316. @sub_test_dir.to_s) do |collector|
  317. collector.patterns.clear
  318. end
  319. end
  320. def test_filtering
  321. assert_collect([:suite, {:name => "."},
  322. [:suite, {:name => _test_case_name("TestCase1")},
  323. [:test, {:name => "test1_1"}],
  324. [:test, {:name => "test1_2"}]]]) do |collector|
  325. collector.filter = Proc.new do |test|
  326. !/\Atest1/.match(test.method_name).nil?
  327. end
  328. end
  329. end
  330. def test_collect_multi
  331. test_dirs = [@sub_test_dir.to_s, @sub2_test_dir.to_s]
  332. assert_collect([:suite, {:name => "[#{test_dirs.join(', ')}]"},
  333. [:suite, {:name => @sub_test_dir.basename.to_s},
  334. [:suite, {:name => _test_case_name("SubTestCase4")},
  335. [:test, {:name => "test4_1"}],
  336. [:test, {:name => "test4_2"}]],
  337. [:suite, {:name => _test_case_name("SubTestCase6")},
  338. [:test, {:name => "test6"}]]],
  339. [:suite, {:name => @sub2_test_dir.basename.to_s},
  340. [:suite, {:name => _test_case_name("Sub2TestCase8")},
  341. [:test, {:name => "test8_1"}],
  342. [:test, {:name => "test8_2"}]],
  343. [:suite, {:name => _test_case_name("Sub2TestCase9")},
  344. [:test, {:name => "test9"}]]]],
  345. *test_dirs)
  346. end
  347. private
  348. def assert_collect(expected, *collect_args)
  349. keep_required_files do
  350. Dir.chdir(@test_dir.to_s) do
  351. collector = Test::Unit::Collector::Load.new
  352. yield(collector) if block_given?
  353. actual = inspect_test_object(collector.send(:collect, *collect_args))
  354. assert_equal(expected, actual)
  355. end
  356. end
  357. end
  358. def ensure_clean_directory(directory)
  359. directory.rmtree if directory.exist?
  360. directory.mkpath
  361. end
  362. def keep_required_files
  363. required_files = $".dup
  364. yield
  365. ensure
  366. $".replace(required_files)
  367. end
  368. def _test_case_name(test_case_class_name)
  369. "#{@temporary_test_cases_module_name}::#{test_case_class_name}"
  370. end
  371. def inspect_test_object(test_object)
  372. return nil if test_object.nil?
  373. case test_object
  374. when Test::Unit::TestSuite
  375. sub_tests = test_object.tests.collect do |test|
  376. inspect_test_object(test)
  377. end.sort_by do |type, attributes, *children|
  378. attributes[:name]
  379. end
  380. [:suite, {:name => test_object.name}, *sub_tests]
  381. when Test::Unit::TestCase
  382. [:test, {:name => test_object.method_name}]
  383. else
  384. raise "unexpected test object: #{test_object.inspect}"
  385. end
  386. end
  387. end