File: test-load.rb

package info (click to toggle)
ruby-test-unit 3.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,280 kB
  • sloc: ruby: 15,493; makefile: 9
file content (475 lines) | stat: -rw-r--r-- 14,046 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
require 'tmpdir'
require 'pathname'

require 'test/unit'
require 'test/unit/collector/load'

class TestUnitCollectorLoad < Test::Unit::TestCase
  def setup
    @previous_descendants = Test::Unit::TestCase::DESCENDANTS.dup
    Test::Unit::TestCase::DESCENDANTS.clear

    @temporary_test_cases_module_name = "TempTestCases"
    ::Object.const_set(@temporary_test_cases_module_name, Module.new)

    @test_dir = Pathname(Dir.tmpdir) + "test-unit"
    @extra_test_dir = Pathname(Dir.tmpdir) + "test-unit-extra"
    ensure_clean_directory(@test_dir)
    ensure_clean_directory(@extra_test_dir)
  end

  setup
  def setup_top_level_test_cases
    @test_case1_base_name = "test_case1.rb"

    @test_case1 = @test_dir + @test_case1_base_name
    @test_case2 = @test_dir + "test_case2.rb"
    @no_load_test_case3 = @test_dir + "case3.rb"

    @test_case1.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class TestCase1 < Test::Unit::TestCase
    def test1_1
    end

    def test1_2
    end
  end
end
EOT
    end

    @test_case2.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class TestCase2 < Test::Unit::TestCase
    def test2
    end
  end
end
EOT
    end

    @no_load_test_case3.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class NoLoadTestCase3 < Test::Unit::TestCase
    def test3
    end
  end
end
EOT
    end
  end

  setup
  def setup_sub_level_test_cases
    @sub_test_dir = @test_dir + "sub"
    @sub_test_dir.mkpath

    @sub_test_case1 = @sub_test_dir + @test_case1_base_name
    @sub_test_case4 = @sub_test_dir + "test_case4.rb"
    @no_load_sub_test_case5 = @sub_test_dir + "case5.rb"
    @sub_test_case6 = @sub_test_dir + "test_case6.rb"

    @sub_test_case1.open("w") do |test_case|
      test_case.puts(<<-TEST_CASE)
module #{@temporary_test_cases_module_name}
  class SubTestCase1 < Test::Unit::TestCase
    def test1_1
    end

    def test1_2
    end
  end
end
      TEST_CASE
    end

    @sub_test_case4.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class SubTestCase4 < Test::Unit::TestCase
    def test4_1
    end

    def test4_2
    end
  end
end
EOT
    end

    @no_load_sub_test_case5.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class NoLoadSubTestCase5 < Test::Unit::TestCase
    def test5_1
    end

    def test5_2
    end
  end
end
EOT
    end

    @sub_test_case6.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class SubTestCase6 < Test::Unit::TestCase
    def test6
    end
  end
end
EOT
    end
  end

  setup
  def setup_sub_level_test_cases2
    @sub2_test_dir = @test_dir + "sub2"
    @sub2_test_dir.mkpath

    @no_load_sub2_test_case7 = @sub2_test_dir + "case7.rb"
    @sub2_test_case8 = @sub2_test_dir + "test_case8.rb"
    @sub2_test_case9 = @sub2_test_dir + "test_case9.rb"

    @no_load_sub2_test_case7.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class NoLoadSub2TestCase7 < Test::Unit::TestCase
    def test7_1
    end

    def test7_2
    end
  end
end
EOT
    end

    @sub2_test_case8.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class Sub2TestCase8 < Test::Unit::TestCase
    def test8_1
    end

    def test8_2
    end
  end
end
EOT
    end

    @sub2_test_case9.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class Sub2TestCase9 < Test::Unit::TestCase
    def test9
    end
  end
end
EOT
    end
  end

  setup
  def setup_svn_test_cases
    @svn_test_dir = @test_dir + ".svn"
    @svn_test_dir.mkpath

    @svn_test_case10 = @svn_test_dir + "test_case10.rb"

    @svn_test_case10.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class SvnTestCase10 < Test::Unit::TestCase
    def test7
    end
  end
end
EOT
    end
  end

  setup
  def setup_sub_cvs_test_cases
    @sub_cvs_test_dir = @sub_test_dir + "CVS"
    @sub_cvs_test_dir.mkpath

    @sub_cvs_test_case11 = @sub_cvs_test_dir + "test_case11.rb"

    @sub_cvs_test_case11.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class SubCVSTestCase11 < Test::Unit::TestCase
    def test11
    end
  end
end
EOT
    end
  end

  setup
  def setup_sub_git_test_cases
    @sub_git_test_dir = @sub_test_dir + ".git"
    @sub_git_test_dir.mkpath

    @sub_git_test_case11 = @sub_git_test_dir + "test_case11.rb"

    @sub_git_test_case11.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class SubGitTestCase11 < Test::Unit::TestCase
    def test11
    end
  end
end
EOT
    end
  end

  setup
  def setup_extra_top_level_test_cases
    @test_cases12 = @extra_test_dir + "test_cases12.rb"
    @test_cases12.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class TestCase121 < Test::Unit::TestCase
    def test121_1
    end

    def test121_2
    end
  end

  class TestCase122 < Test::Unit::TestCase
    def test122_1
    end

    def test122_2
    end
  end
end
EOT
    end
  end

  setup
  def setup_sub_level_extra_test_cases
    @sub_extra_test_dir = @extra_test_dir + "sub"
    @sub_extra_test_dir.mkpath

    @cases13_test = @sub_extra_test_dir + "13cases_test.rb"
    @cases13_test.open("w") do |test_case|
      test_case.puts(<<-EOT)
module #{@temporary_test_cases_module_name}
  class SubTestCase13 < Test::Unit::TestCase
    def test13_1
    end

    def test13_2
    end
  end
end
EOT
    end
  end

  def teardown
    @test_dir.rmtree if @test_dir.exist?
    ::Object.send(:remove_const, @temporary_test_cases_module_name)
    Test::Unit::TestCase::DESCENDANTS.replace(@previous_descendants)
  end

  def test_simple_collect
    assert_collect([:suite, {:name => @sub_test_dir.basename.to_s},
                    [:suite, {:name => _test_case_name("SubTestCase1")},
                     [:test, {:name => "test1_1"}],
                     [:test, {:name => "test1_2"}]],
                    [:suite, {:name => _test_case_name("SubTestCase4")},
                     [:test, {:name => "test4_1"}],
                     [:test, {:name => "test4_2"}]],
                    [:suite, {:name => _test_case_name("SubTestCase6")},
                     [:test, {:name => "test6"}]]],
                   @sub_test_dir.to_s)
  end

  def test_simple_collect_test_suffix
    assert_collect([:suite, {:name => @extra_test_dir.basename.to_s},
                    [:suite, {:name => _test_case_name("TestCase121")},
                     [:test, {:name => "test121_1"}],
                     [:test, {:name => "test121_2"}]],
                    [:suite, {:name => _test_case_name("TestCase122")},
                     [:test, {:name => "test122_1"}],
                     [:test, {:name => "test122_2"}]],
                    [:suite, {:name => @sub_extra_test_dir.basename.to_s},
                     [:suite, {:name => _test_case_name("SubTestCase13")},
                      [:test, {:name => "test13_1"}],
                      [:test, {:name => "test13_2"}]]]],
                   @extra_test_dir.to_s)
  end

  def test_multilevel_collect
    assert_collect([:suite, {:name => "."},
                    [:suite, {:name => _test_case_name("TestCase1")},
                     [:test, {:name => "test1_1"}],
                     [:test, {:name => "test1_2"}]],
                    [:suite, {:name => _test_case_name("TestCase2")},
                     [:test, {:name => "test2"}]],
                    [:suite, {:name => @sub_test_dir.basename.to_s},
                     [:suite, {:name => _test_case_name("SubTestCase1")},
                      [:test, {:name => "test1_1"}],
                      [:test, {:name => "test1_2"}]],
                     [:suite, {:name => _test_case_name("SubTestCase4")},
                      [:test, {:name => "test4_1"}],
                      [:test, {:name => "test4_2"}]],
                     [:suite, {:name => _test_case_name("SubTestCase6")},
                      [:test, {:name => "test6"}]]],
                   [:suite, {:name => @sub2_test_dir.basename.to_s},
                     [:suite, {:name => _test_case_name("Sub2TestCase8")},
                      [:test, {:name => "test8_1"}],
                      [:test, {:name => "test8_2"}]],
                     [:suite, {:name => _test_case_name("Sub2TestCase9")},
                      [:test, {:name => "test9"}]]]])
  end

  def test_collect_file
    assert_collect([:suite, {:name => _test_case_name("TestCase1")},
                    [:test, {:name => "test1_1"}],
                    [:test, {:name => "test1_2"}]],
                   @test_case1.to_s)
  end

  def test_collect_file_no_pattern_match_file_name
    assert_collect([:suite, {:name => _test_case_name("NoLoadSubTestCase5")},
                    [:test, {:name => "test5_1"}],
                    [:test, {:name => "test5_2"}]],
                   @no_load_sub_test_case5.to_s)
  end

  def test_collect_file_test_cases
    assert_collect([:suite, {:name => "[#{@test_cases12}]"},
                    [:suite, {:name => _test_case_name("TestCase121")},
                     [:test, {:name => "test121_1"}],
                     [:test, {:name => "test121_2"}]],
                    [:suite, {:name => _test_case_name("TestCase122")},
                     [:test, {:name => "test122_1"}],
                     [:test, {:name => "test122_2"}]]],
                   @test_cases12.to_s)
  end

  def test_collect_files
    assert_collect([:suite,
                    {:name => "[#{@test_case1}, #{@test_case2}]"},
                    [:suite, {:name => _test_case_name("TestCase1")},
                     [:test, {:name => "test1_1"}],
                     [:test, {:name => "test1_2"}]],
                    [:suite, {:name => _test_case_name("TestCase2")},
                     [:test, {:name => "test2"}]]],
                   @test_case1.to_s, @test_case2.to_s)
  end

  def test_nil_pattern
    assert_collect([:suite, {:name => @sub_test_dir.basename.to_s},
                    [:suite, {:name => _test_case_name("NoLoadSubTestCase5")},
                     [:test, {:name => "test5_1"}],
                     [:test, {:name => "test5_2"}]],
                    [:suite, {:name => _test_case_name("SubTestCase1")},
                     [:test, {:name => "test1_1"}],
                     [:test, {:name => "test1_2"}]],
                    [:suite, {:name => _test_case_name("SubTestCase4")},
                     [:test, {:name => "test4_1"}],
                     [:test, {:name => "test4_2"}]],
                    [:suite, {:name => _test_case_name("SubTestCase6")},
                     [:test, {:name => "test6"}]]],
                   @sub_test_dir.to_s) do |collector|
      collector.patterns.clear
    end
  end

  def test_filtering
    assert_collect([:suite, {:name => "."},
                    [:suite, {:name => _test_case_name("TestCase1")},
                     [:test, {:name => "test1_1"}],
                     [:test, {:name => "test1_2"}]],
                    [:suite, {:name => @sub_test_dir.basename.to_s},
                     [:suite, {:name => _test_case_name("SubTestCase1")},
                      [:test, {:name => "test1_1"}],
                      [:test, {:name => "test1_2"}]]]]) do |collector|
      collector.filter = Proc.new do |test|
        !/\Atest1/.match(test.method_name).nil?
      end
    end
  end

  def test_collect_multi
    test_dirs = [@sub_test_dir.to_s, @sub2_test_dir.to_s]
    assert_collect([:suite, {:name => "[#{test_dirs.join(', ')}]"},
                    [:suite, {:name => @sub_test_dir.basename.to_s},
                     [:suite, {:name => _test_case_name("SubTestCase1")},
                      [:test, {:name => "test1_1"}],
                      [:test, {:name => "test1_2"}]],
                     [:suite, {:name => _test_case_name("SubTestCase4")},
                      [:test, {:name => "test4_1"}],
                      [:test, {:name => "test4_2"}]],
                     [:suite, {:name => _test_case_name("SubTestCase6")},
                      [:test, {:name => "test6"}]]],
                    [:suite, {:name => @sub2_test_dir.basename.to_s},
                     [:suite, {:name => _test_case_name("Sub2TestCase8")},
                      [:test, {:name => "test8_1"}],
                      [:test, {:name => "test8_2"}]],
                     [:suite, {:name => _test_case_name("Sub2TestCase9")},
                      [:test, {:name => "test9"}]]]],
                   *test_dirs)
  end

  private
  def assert_collect(expected, *collect_args)
    keep_required_files do
      Dir.chdir(@test_dir.to_s) do
        collector = Test::Unit::Collector::Load.new
        yield(collector) if block_given?
        actual = inspect_test_object(collector.send(:collect, *collect_args))
        assert_equal(expected, actual)
      end
    end
  end

  def ensure_clean_directory(directory)
    directory.rmtree if directory.exist?
    directory.mkpath
  end

  def keep_required_files
    required_files = $".dup
    yield
  ensure
    $".replace(required_files)
  end

  def _test_case_name(test_case_class_name)
    "#{@temporary_test_cases_module_name}::#{test_case_class_name}"
  end

  def inspect_test_object(test_object)
    return nil if test_object.nil?
    case test_object
    when Test::Unit::TestSuite
      sub_tests = test_object.tests.collect do |test|
        inspect_test_object(test)
      end.sort_by do |type, attributes, *children|
        attributes[:name]
      end
      [:suite, {:name => test_object.name}, *sub_tests]
    when Test::Unit::TestCase
      [:test, {:name => test_object.method_name}]
    else
      raise "unexpected test object: #{test_object.inspect}"
    end
  end
end