Project

General

Profile

Bug #8778 ยป 0001-Bring-Test-Unit-up-to-100-documentation.patch

steveklabnik (Steve Klabnik), 08/18/2013 03:09 AM

View differences:

lib/test/unit.rb
1
# test/unit compatibility layer using minitest.
2

  
3 1
require 'minitest/unit'
4 2
require 'test/unit/assertions'
5 3
require 'test/unit/testcase'
6 4
require 'optparse'
7 5

  
8 6
module Test
7
  ##
8
  # = Test/Unit
9
  #
10
  # Test/Unit is an implementation of the xUnit testing framework for Ruby.
11
  #
12
  # If you are writing new test code, please use MiniTest instead of Test/Unit.
13
  # Test/Unit has been left in the standard library to support legacy test
14
  # suites.
9 15
  module Unit
10
    TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest'
16
    TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest' # :nodoc:
11 17

  
12 18
    module RunCount
13 19
      @@run_count = 0
14 20

  
15
      def self.have_run?
21
      def self.have_run? # :nodoc:
16 22
        @@run_count.nonzero?
17 23
      end
18 24

  
19
      def run(*)
25
      def run(*) # :nodoc:
20 26
        @@run_count += 1
21 27
        super
22 28
      end
23 29

  
24
      def run_once
30
      def run_once # :nodoc:
25 31
        return if have_run?
26 32
        return if $! # don't run if there was an exception
27 33
        yield
......
30 36
    end
31 37

  
32 38
    module Options
33
      def initialize(*, &block)
39
      def initialize(*, &block) # :nodoc:
34 40
        @init_hook = block
35 41
        @options = nil
36 42
        super(&nil)
37 43
      end
38 44

  
39
      def option_parser
45
      def option_parser # :nodoc:
40 46
        @option_parser ||= OptionParser.new
41 47
      end
42 48

  
43
      def process_args(args = [])
49
      def process_args(args = []) # :nodoc:
44 50
        return @options if @options
45 51
        orig_args = args.dup
46 52
        options = {}
......
154 160
    module GlobOption
155 161
      @@testfile_prefix = "test"
156 162

  
157
      def setup_options(parser, options)
163
      def setup_options(parser, options) # :nodoc:
158 164
        super
159 165
        parser.on '-b', '--basedir=DIR', 'Base directory of test suites.' do |dir|
160 166
          options[:base_directory] = dir
......
164 170
        end
165 171
      end
166 172

  
167
      def non_options(files, options)
173
      def non_options(files, options) # :nodoc:
168 174
        paths = [options.delete(:base_directory), nil].uniq
169 175
        if reject = options.delete(:reject)
170 176
          reject_pat = Regexp.union(reject.map {|r| /#{r}/ })
......
198 204
    end
199 205

  
200 206
    module LoadPathOption
201
      def setup_options(parser, options)
207
      def setup_options(parser, options) # :nodoc:
202 208
        super
203 209
        parser.on '-Idirectory', 'Add library load path' do |dirs|
204 210
          dirs.split(':').each { |d| $LOAD_PATH.unshift d }
......
207 213
    end
208 214

  
209 215
    module GCStressOption
210
      def setup_options(parser, options)
216
      def setup_options(parser, options) # :nodoc:
211 217
        super
212 218
        parser.on '--[no-]gc-stress', 'Set GC.stress as true' do |flag|
213 219
          options[:gc_stress] = flag
214 220
        end
215 221
      end
216 222

  
217
      def non_options(files, options)
223
      def non_options(files, options) # :nodoc:
218 224
        if options.delete(:gc_stress)
219 225
          MiniTest::Unit::TestCase.class_eval do
220 226
            oldrun = instance_method(:run)
......
233 239
    end
234 240

  
235 241
    module RequireFiles
236
      def non_options(files, options)
242
      def non_options(files, options) # :nodoc:
237 243
        return false if !super
238 244
        result = false
239 245
        files.each {|f|
......
260 266
      include Test::Unit::RunCount
261 267

  
262 268
      class Worker
263
        def self.launch(ruby,args=[])
269
        def self.launch(ruby,args=[]) # :nodoc:
264 270
          io = IO.popen([*ruby,
265 271
                        "#{File.dirname(__FILE__)}/unit/parallel.rb",
266 272
                        *args], "rb+")
267 273
          new(io, io.pid, :waiting)
268 274
        end
269 275

  
270
        attr_reader :quit_called
276
        attr_reader :quit_called # :nodoc:
271 277

  
272
        def initialize(io, pid, status)
278
        def initialize(io, pid, status) # :nodoc:
273 279
          @io = io
274 280
          @pid = pid
275 281
          @status = status
......
280 286
          @quit_called = false
281 287
        end
282 288

  
283
        def puts(*args)
289
        def puts(*args) # :nodoc:
284 290
          @io.puts(*args)
285 291
        end
286 292

  
287
        def run(task,type)
293
        def run(task,type) # :nodoc:
288 294
          @file = File.basename(task, ".rb")
289 295
          @real_file = task
290 296
          begin
......
300 306
          end
301 307
        end
302 308

  
303
        def hook(id,&block)
309
        def hook(id,&block) # :nodoc:
304 310
          @hooks[id] ||= []
305 311
          @hooks[id] << block
306 312
          self
307 313
        end
308 314

  
309
        def read
315
        def read # :nodoc:
310 316
          res = (@status == :quit) ? @io.read : @io.gets
311 317
          res && res.chomp
312 318
        end
313 319

  
314
        def close
320
        def close # :nodoc:
315 321
          @io.close unless @io.closed?
316 322
          self
317 323
        rescue IOError
318 324
        end
319 325

  
320
        def quit
326
        def quit # :nodoc:
321 327
          return if @io.closed?
322 328
          @quit_called = true
323 329
          @io.puts "quit"
324 330
          @io.close
325 331
        end
326 332

  
327
        def kill
333
        def kill # :nodoc:
328 334
          Process.kill(:KILL, @pid)
329 335
        rescue Errno::ESRCH
330 336
        end
331 337

  
332
        def died(*additional)
338
        def died(*additional) # :nodoc:
333 339
          @status = :quit
334 340
          @io.close
335 341

  
336 342
          call_hook(:dead,*additional)
337 343
        end
338 344

  
339
        def to_s
345
        def to_s # :nodoc:
340 346
          if @file
341 347
            "#{@pid}=#{@file}"
342 348
          else
......
344 350
          end
345 351
        end
346 352

  
347
        attr_reader :io, :pid
348
        attr_accessor :status, :file, :real_file, :loadpath
353
        attr_reader :io, :pid # :nodoc:
354
        attr_accessor :status, :file, :real_file, :loadpath # :nodoc:
349 355

  
350 356
        private
351 357

  
......
360 366
      class << self; undef autorun; end
361 367

  
362 368
      @@stop_auto_run = false
363
      def self.autorun
369
      def self.autorun # :nodoc:
364 370
        at_exit {
365 371
          Test::Unit::RunCount.run_once {
366 372
            exit(Test::Unit::Runner.new.run(ARGV) || true)
......
369 375
        @@installed_at_exit = true
370 376
      end
371 377

  
372
      def after_worker_down(worker, e=nil, c=false)
378
      def after_worker_down(worker, e=nil, c=false) # :nodoc:
373 379
        return unless @options[:parallel]
374 380
        return if @interrupt
375 381
        warn e if e
......
383 389
        exit c
384 390
      end
385 391

  
386
      def terminal_width
392
      def terminal_width # :nodoc:
387 393
        unless @terminal_width ||= nil
388 394
          begin
389 395
            require 'io/console'
......
397 403
        @terminal_width
398 404
      end
399 405

  
400
      def del_status_line
406
      def del_status_line # :nodoc:
401 407
        @status_line_size ||= 0
402 408
        unless @options[:job_status] == :replace
403 409
          $stdout.puts
......
408 414
        @status_line_size = 0
409 415
      end
410 416

  
411
      def put_status(line)
417
      def put_status(line) # :nodoc:
412 418
        unless @options[:job_status] == :replace
413 419
          print(line)
414 420
          return
......
422 428
        @status_line_size = line.size
423 429
      end
424 430

  
425
      def add_status(line)
431
      def add_status(line) # :nodoc:
426 432
        unless @options[:job_status] == :replace
427 433
          print(line)
428 434
          return
......
434 440
        @status_line_size += line.size
435 441
      end
436 442

  
437
      def jobs_status
443
      def jobs_status # :nodoc:
438 444
        return unless @options[:job_status]
439 445
        puts "" unless @options[:verbose] or @options[:job_status] == :replace
440 446
        status_line = @workers.map(&:to_s).join(" ")
441 447
        update_status(status_line) or (puts; nil)
442 448
      end
443 449

  
444
      def del_jobs_status
450
      def del_jobs_status # :nodoc:
445 451
        return unless @options[:job_status] == :replace && @status_line_size.nonzero?
446 452
        del_status_line
447 453
      end
448 454

  
449
      def after_worker_quit(worker)
455
      def after_worker_quit(worker) # :nodoc:
450 456
        return unless @options[:parallel]
451 457
        return if @interrupt
452 458
        @workers.delete(worker)
......
454 460
        @ios = @workers.map(&:io)
455 461
      end
456 462

  
457
      def launch_worker
463
      def launch_worker # :nodoc:
458 464
        begin
459 465
          worker = Worker.launch(@options[:ruby],@args)
460 466
        rescue => e
......
470 476
        worker
471 477
      end
472 478

  
473
      def delete_worker(worker)
479
      def delete_worker(worker) # :nodoc:
474 480
        @workers_hash.delete worker.io
475 481
        @workers.delete worker
476 482
        @ios.delete worker.io
477 483
      end
478 484

  
479
      def quit_workers
485
      def quit_workers # :nodoc:
480 486
        return if @workers.empty?
481 487
        @workers.reject! do |worker|
482 488
          begin
......
502 508
        end
503 509
      end
504 510

  
505
      def start_watchdog
511
      def start_watchdog # :nodoc:
506 512
        Thread.new do
507 513
          while stat = Process.wait2
508 514
            break if @interrupt # Break when interrupt
......
518 524
        end
519 525
      end
520 526

  
521
      def deal(io, type, result, rep, shutting_down = false)
527
      def deal(io, type, result, rep, shutting_down = false) # :nodoc:
522 528
        worker = @workers_hash[io]
523 529
        case worker.read
524 530
        when /^okay$/
......
561 567
        return false
562 568
      end
563 569

  
564
      def _run_parallel suites, type, result
570
      def _run_parallel suites, type, result # :nodoc:
565 571
        if @options[:parallel] < 1
566 572
          warn "Error: parameter of -j option should be greater than 0."
567 573
          return
......
644 650
        end
645 651
      end
646 652

  
647
      def _run_suites suites, type
653
      def _run_suites suites, type # :nodoc:
648 654
        _prepare_run(suites, type)
649 655
        @interrupt = nil
650 656
        result = []
......
669 675

  
670 676
      alias mini_run_suite _run_suite
671 677

  
672
      def output
678
      def output # :nodoc:
673 679
        (@output ||= nil) || super
674 680
      end
675 681

  
676
      def _prepare_run(suites, type)
682
      def _prepare_run(suites, type) # :nodoc:
677 683
        options[:job_status] ||= :replace if @tty && !@verbose
678 684
        case options[:color]
679 685
        when :always
......
710 716
        @total_tests = total.to_s(10)
711 717
      end
712 718

  
713
      def new_test(s)
719
      def new_test(s) # :nodoc:
714 720
        @test_count += 1
715 721
        update_status(s)
716 722
      end
717 723

  
718
      def update_status(s)
724
      def update_status(s) # :nodoc:
719 725
        count = @test_count.to_s(10).rjust(@total_tests.size)
720 726
        put_status("#{@passed_color}[#{count}/#{@total_tests}]#{@reset_color} #{s}")
721 727
      end
722 728

  
723
      def _print(s); $stdout.print(s); end
724
      def succeed; del_status_line; end
729
      def _print(s); $stdout.print(s); end # :nodoc:
730
      def succeed; del_status_line; end # :nodoc:
725 731

  
726 732
      def failed(s)
727 733
        sep = "\n"
......
778 784
    end
779 785

  
780 786
    class StatusLineOutput < Struct.new(:runner)
781
      def puts(*a) $stdout.puts(*a) unless a.empty? end
782
      def respond_to_missing?(*a) $stdout.respond_to?(*a) end
783
      def method_missing(*a, &b) $stdout.__send__(*a, &b) end
787
      def puts(*a) $stdout.puts(*a) unless a.empty? end # :nodoc:
788
      def respond_to_missing?(*a) $stdout.respond_to?(*a) end # :nodoc:
789
      def method_missing(*a, &b) $stdout.__send__(*a, &b) end # :nodoc:
784 790

  
785
      def print(s)
791
      def print(s) # :nodoc:
786 792
        case s
787 793
        when /\A(.*\#.*) = \z/
788 794
          runner.new_test($1)
......
803 809
        include Test::Unit::RequireFiles
804 810
      end
805 811

  
806
      attr_accessor :to_run, :options
812
      attr_accessor :to_run, :options # :nodoc:
807 813

  
808
      def initialize(force_standalone = false, default_dir = nil, argv = ARGV)
814
      def initialize(force_standalone = false, default_dir = nil, argv = ARGV) # :nodoc:
809 815
        @force_standalone = force_standalone
810 816
        @runner = Runner.new do |files, options|
811 817
          options[:base_directory] ||= default_dir
......
822 828
        @argv = argv
823 829
      end
824 830

  
825
      def process_args(*args)
831
      def process_args(*args) # :nodoc:
826 832
        @runner.process_args(*args)
827 833
        !@to_run.empty?
828 834
      end
829 835

  
830
      def run
836
      def run # :nodoc:
831 837
        if @force_standalone and not process_args(@argv)
832 838
          abort @options.banner
833 839
        end
834 840
        @runner.run(@argv) || true
835 841
      end
836 842

  
837
      def self.run(*args)
843
      def self.run(*args) # :nodoc:
838 844
        new(*args).run
839 845
      end
840 846
    end
841 847

  
842 848
    class ProxyError < StandardError
843
      def initialize(ex)
849
      def initialize(ex) # :nodoc:
844 850
        @message = ex.message
845 851
        @backtrace = ex.backtrace
846 852
      end
847 853

  
848
      attr_accessor :message, :backtrace
854
      attr_accessor :message, :backtrace # :nodoc:
849 855
    end
850 856
  end
851 857
end
852 858

  
859
module MiniTest # :nodoc:
860
  module Unit # :nodoc:
861
  end
862
end
863

  
853 864
class MiniTest::Unit::TestCase
854 865
  undef run_test
855
  RUN_TEST_TRACE = "#{__FILE__}:#{__LINE__+3}:in `run_test'".freeze
856
  def run_test(name)
866
  RUN_TEST_TRACE = "#{__FILE__}:#{__LINE__+3}:in `run_test'".freeze # :nodoc:
867
  def run_test(name) # :nodoc:
857 868
    progname, $0 = $0, "#{$0}: #{self.class}##{name}"
858 869
    self.__send__(name)
859 870
  ensure
lib/test/unit/assertions.rb
358 358
        template.gsub(/\G((?:[^\\]|\\.)*?)(\\)?\?/) { $1 + ($2 ? "?" : mu_pp(arguments.shift)) }
359 359
      end
360 360

  
361
      def message(msg = nil, *args, &default)
361
      def message(msg = nil, *args, &default) # :nodoc:
362 362
        if Proc === msg
363 363
          super(nil, *args) do
364 364
            [msg.call, (default.call if default)].compact.reject(&:empty?).join(".\n")
lib/test/unit/parallel.rb
2 2

  
3 3
module Test
4 4
  module Unit
5
    class Worker < Runner
5
    class Worker < Runner # :nodoc:
6 6
      class << self
7 7
        undef autorun
8 8
      end
......
12 12
      undef _run_suites
13 13
      undef run
14 14

  
15
      def increment_io(orig)
15
      def increment_io(orig) # :nodoc:
16 16
        *rest, io = 32.times.inject([orig.dup]){|ios, | ios << ios.last.dup }
17 17
        rest.each(&:close)
18 18
        io
19 19
      end
20 20

  
21
      def _run_suites(suites, type)
21
      def _run_suites(suites, type) # :nodoc:
22 22
        suites.map do |suite|
23 23
          _run_suite(suite, type)
24 24
        end
25 25
      end
26 26

  
27
      def _run_suite(suite, type)
27
      def _run_suite(suite, type) # :nodoc:
28 28
        @partial_report = []
29 29
        orig_testout = MiniTest::Unit.output
30 30
        i,o = IO.pipe
......
81 81
        i.close if i && !i.closed?
82 82
      end
83 83

  
84
      def run(args = [])
84
      def run(args = []) # :nodoc:
85 85
        process_args args
86 86
        @@stop_auto_run = true
87 87
        @opts = @options.dup
......
149 149
        end
150 150
      end
151 151

  
152
      def _report(res, *args)
152
      def _report(res, *args) # :nodoc:
153 153
        res = "#{res} #{args.pack("m0")}" unless args.empty?
154 154
        @stdout.puts(res)
155 155
      end
156 156

  
157
      def puke(klass, meth, e)
157
      def puke(klass, meth, e) # :nodoc:
158 158
        @partial_report << [klass.name, meth, e.is_a?(MiniTest::Assertion) ? e : ProxyError.new(e)]
159 159
        super
160 160
      end
......
167 167
    module Unit
168 168
      class TestCase < MiniTest::Unit::TestCase
169 169
        undef on_parallel_worker?
170
        def on_parallel_worker?
170
        def on_parallel_worker? # :nodoc:
171 171
          true
172 172
        end
173 173
      end
174 174
    end
175 175
  end
176 176
  require 'rubygems'
177
  module Gem # :nodoc
178
  end
177 179
  class Gem::TestCase < MiniTest::Unit::TestCase
178 180
    @@project_dir = File.expand_path('../../../..', __FILE__)
179 181
  end
lib/test/unit/testcase.rb
8 8
    class TestCase < MiniTest::Unit::TestCase
9 9
      include Assertions
10 10

  
11
      def on_parallel_worker?
11
      def on_parallel_worker? # :nodoc:
12 12
        false
13 13
      end
14 14

  
15
      def run runner
15
      def run runner # :nodoc:
16 16
        @options = runner.options
17 17
        super runner
18 18
      end
19 19

  
20
      def self.test_order
20
      def self.test_order # :nodoc:
21 21
        :sorted
22 22
      end
23 23

  
24
      def self.method_added(name)
24
      def self.method_added(name) # :nodoc:
25 25
        return unless name.to_s.start_with?("test_")
26 26
        @test_methods ||= {}
27 27
        if @test_methods[name]
28
-