[cairo-commit] rcairo/test-unit/lib/test/unit/collector descendant.rb, NONE, 1.1 dir.rb, NONE, 1.1 load.rb, NONE, 1.1 objectspace.rb, NONE, 1.1

Kouhei Sutou commit at pdx.freedesktop.org
Wed Aug 13 01:20:57 PDT 2008


Committed by: kou

Update of /cvs/cairo/rcairo/test-unit/lib/test/unit/collector
In directory kemper:/tmp/cvs-serv10309/test-unit/lib/test/unit/collector

Added Files:
	descendant.rb dir.rb load.rb objectspace.rb 
Log Message:
* test-unit: imported Test::Unit 2.x.


--- NEW FILE: descendant.rb ---
require 'test/unit/collector'

module Test
  module Unit
    module Collector
      class Descendant
        include Collector

        NAME = 'collected from the subclasses of TestCase'

        def collect(name=NAME)
          suite = TestSuite.new(name)
          sub_suites = []
          TestCase::DESCENDANTS.each do |descendant_test_case|
            add_suite(sub_suites, descendant_test_case.suite)
          end
          sort(sub_suites).each {|s| suite << s}
          suite
        end
      end
    end
  end
end

--- NEW FILE: dir.rb ---
require 'test/unit/testsuite'
require 'test/unit/collector'

module Test
  module Unit
    module Collector
      class Dir
        include Collector

        attr_reader :pattern, :exclude
        attr_accessor :base

        def initialize(dir=::Dir, file=::File, object_space=::ObjectSpace, req=nil)
          super()
          @dir = dir
          @file = file
          @object_space = object_space
          @req = req
          @pattern = [/\btest_.*\.rb\Z/m]
          @exclude = []
          @base = nil
        end

        def collect(*from)
          basedir = @base
          $:.push(basedir) if basedir
          if(from.empty?)
            recursive_collect('.', find_test_cases)
          elsif(from.size == 1)
            recursive_collect(from.first, find_test_cases)
          else
            suites = []
            from.each do |f|
              suite = recursive_collect(f, find_test_cases)
              suites << suite unless(suite.tests.empty?)
            end
            suite = TestSuite.new("[#{from.join(', ')}]")
            sort(suites).each{|s| suite << s}
            suite
          end
        ensure
          $:.delete_at($:.rindex(basedir)) if basedir
        end

        def find_test_cases(ignore=[])
          cases = []
          @object_space.each_object(Class) do |c|
            cases << c if(c < TestCase && !ignore.include?(c))
          end
          ignore.concat(cases)
          cases
        end

        def recursive_collect(name, already_gathered)
          sub_suites = []
          path = realdir(name)
          if @file.directory?(path)
	    dir_name = name unless name == '.'
            @dir.entries(path).each do |e|
              next if(e == '.' || e == '..')
              e_name = dir_name ? @file.join(dir_name, e) : e
              if @file.directory?(realdir(e_name))
                next if /\A(?:CVS|\.svn)\z/ =~ e
                sub_suite = recursive_collect(e_name, already_gathered)
                sub_suites << sub_suite unless(sub_suite.empty?)
              else
                next if /~\z/ =~ e_name or /\A\.\#/ =~ e
                if @pattern and !@pattern.empty?
                  next unless @pattern.any? {|pat| pat =~ e_name}
                end
                if @exclude and !@exclude.empty?
                  next if @exclude.any? {|pat| pat =~ e_name}
                end
                collect_file(e_name, sub_suites, already_gathered)
              end
            end
          else
            collect_file(name, sub_suites, already_gathered)
          end
          suite = TestSuite.new(@file.basename(name))
          sort(sub_suites).each{|s| suite << s}
          suite
        end

        def collect_file(name, suites, already_gathered)
          dir = @file.dirname(@file.expand_path(name, @base))
          $:.unshift(dir)
          if(@req)
            @req.require(name)
          else
            require(name)
          end
          find_test_cases(already_gathered).each{|t| add_suite(suites, t.suite)}
        ensure
          $:.delete_at($:.rindex(dir)) if(dir)
        end

	def realdir(path)
	  if @base
	    @file.join(@base, path)
	  else
	    path
	  end
	end
      end
    end
  end
end

--- NEW FILE: load.rb ---
require 'pathname'

require 'test/unit/testsuite'
require 'test/unit/collector'

module Test
  module Unit
    module Collector
      class Load
        include Collector

        attr_reader :patterns, :excludes, :base

        def initialize
          super
          @system_excludes = [/~\z/, /\A\.\#/]
          @system_directory_excludes = [/\A(?:CVS|\.svn)\z/]
          @patterns = [/\Atest_.+\.rb\z/m]
          @excludes = []
          @base = nil
        end

        def base=(base)
          base = Pathname(base) unless base.nil?
          @base = base
        end

        def collect(*froms)
          add_load_path(@base) do
            froms = ["."] if froms.empty?
            test_suites = froms.collect do |from|
              test_suite = collect_recursive(from, find_test_cases)
              test_suite = nil if test_suite.tests.empty?
              test_suite
            end.compact

            if test_suites.size > 1
              test_suite = TestSuite.new("[#{froms.join(', ')}]")
              sort(test_suites).each do |sub_test_suite|
                test_suite << sub_test_suite
              end
              test_suite
            else
              test_suites.first
            end
          end
        end

        def find_test_cases(ignore=[])
          test_cases = []
          TestCase::DESCENDANTS.each do |test_case|
            test_cases << test_case unless ignore.include?(test_case)
          end
          ignore.concat(test_cases)
          test_cases
        end

        private
        def collect_recursive(name, already_gathered)
          sub_test_suites = []

          path = resolve_path(name)
          if path.directory?
            directories, files = path.children.partition do |child|
              child.directory?
            end

            files.each do |child|
              next if excluded_file?(child.basename.to_s)
              collect_file(child, sub_test_suites, already_gathered)
            end

            directories.each do |child|
              next if excluded_directory?(child.basename.to_s)
              sub_test_suite = collect_recursive(child, already_gathered)
              sub_test_suites << sub_test_suite unless sub_test_suite.empty?
            end
          else
            collect_file(path, sub_test_suites, already_gathered)
          end

          test_suite = TestSuite.new(path.basename.to_s)
          sort(sub_test_suites).each do |sub_test_suite|
            test_suite << sub_test_suite
          end
          test_suite
        end

        def collect_file(path, test_suites, already_gathered)
          add_load_path(path.expand_path.dirname) do
            require(path.to_s)
            find_test_cases(already_gathered).each do |test_case|
              add_suite(test_suites, test_case.suite)
            end
          end
        end

        def resolve_path(path)
          if @base
            @base + path
          else
            Pathname(path)
          end
        end

        def add_load_path(path)
          $LOAD_PATH.push(path.to_s) if path
          yield
        ensure
          $LOAD_PATH.delete_at($LOAD_PATH.rindex(path.to_s)) if path
        end

        def excluded_directory?(base)
          @system_directory_excludes.any? {|pattern| pattern =~ base}
        end

        def excluded_file?(base)
          return true if @system_excludes.any? {|pattern| pattern =~ base}

          patterns = @patterns || []
          unless patterns.empty?
            return true unless patterns.any? {|pattern| pattern =~ base}
          end

          excludes = @excludes || []
          unless excludes.empty?
            return true if excludes.any? {|pattern| pattern =~ base}
          end

          false
        end
      end
    end
  end
end

--- NEW FILE: objectspace.rb ---
# Author:: Nathaniel Talbott.
# Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
# License:: Ruby license.

require 'test/unit/collector'

module Test
  module Unit
    module Collector
      class ObjectSpace
        include Collector
        
        NAME = 'collected from the ObjectSpace'
        
        def initialize(source=::ObjectSpace)
          super()
          @source = source
        end
        
        def collect(name=NAME)
          suite = TestSuite.new(name)
          sub_suites = []
          @source.each_object(Class) do |klass|
            if(Test::Unit::TestCase > klass)
              add_suite(sub_suites, klass.suite)
            end
          end
          sort(sub_suites).each{|s| suite << s}
          suite
        end
      end
    end
  end
end



More information about the cairo-commit mailing list