[cairo-commit] rcairo/test-unit/test/collector test_descendant.rb, NONE, 1.1 test_dir.rb, NONE, 1.1 test_load.rb, NONE, 1.1 test_objectspace.rb, NONE, 1.1

Kouhei Sutou commit at pdx.freedesktop.org
Wed Aug 13 01:21:48 PDT 2008


Committed by: kou

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

Added Files:
	test_descendant.rb test_dir.rb test_load.rb 
	test_objectspace.rb 
Log Message:
* test-unit: imported Test::Unit 2.x.


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

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

    @test_case1 = Class.new(Test::Unit::TestCase) do
      def self.name
        "test-case1"
      end

      def test_1
      end

      def test_2
      end
    end

    @test_case2 = Class.new(Test::Unit::TestCase) do
      def self.name
        "test-case2"
      end

      def test_0
      end
    end

    @no_test_case = Class.new do
      def self.name
        "no-test-case"
      end

      def test_4
      end
    end
  end

  def teardown
    Test::Unit::TestCase::DESCENDANTS.replace(@previous_descendants)
  end

  def test_basic_collection
    assert_collect(full_suite("name"), "name")

    assert_collect(full_suite("name"), "name") do |collector|
      collector.filter = []
    end
  end

  def test_filtered_collection
    assert_collect(empty_suite) do |collector|
      collector.filter = Proc.new {false}
    end

    assert_collect(full_suite) do |collector|
      collector.filter = Proc.new {true}
    end

    assert_collect(full_suite) do |collector|
      collector.filter = Proc.new {nil}
    end

    assert_collect(empty_suite) do |collector|
      collector.filter = [Proc.new {false}, Proc.new {true}]
    end

    assert_collect(full_suite) do |collector|
      collector.filter = [Proc.new {true}, Proc.new {false}]
    end

    assert_collect(empty_suite) do |collector|
      collector.filter = [Proc.new {nil}, Proc.new {false}]
    end

    assert_collect(full_suite) do |collector|
      collector.filter = [Proc.new {nil}, Proc.new {true}]
    end

    expected = empty_suite
    suite1 = Test::Unit::TestSuite.new(@test_case1.name)
    suite1 << @test_case1.new("test_1")
    suite2 = Test::Unit::TestSuite.new(@test_case2.name)
    suite2 << @test_case2.new("test_0")
    expected << suite1 << suite2
    assert_collect(expected) do |collector|
      collector.filter = Proc.new do |test|
        ['test_1', 'test_0'].include?(test.method_name)
      end
    end

    expected = empty_suite
    suite1 = Test::Unit::TestSuite.new(@test_case1.name)
    suite1 << @test_case1.new("test_1")
    suite2 = Test::Unit::TestSuite.new(@test_case2.name)
    suite2 << @test_case2.new("test_0")
    expected << suite1 << suite2
    assert_collect(expected) do |collector|
      filters = [Proc.new {|test| test.method_name == 'test_1' ? true : nil},
                 Proc.new {|test| test.method_name == 'test_0' ? true : nil},
                 Proc.new {false}]
      collector.filter = filters
    end
  end

  private
  def assert_collect(expected, *collect_args)
    collector = Test::Unit::Collector::Descendant.new
    yield(collector) if block_given?
    assert_equal(expected, collector.send(:collect, *collect_args))
  end

  def default_name
    Test::Unit::Collector::Descendant::NAME
  end

  def empty_suite(name=nil)
    Test::Unit::TestSuite.new(name || default_name)
  end

  def full_suite(name=nil)
    sub_suite1 = Test::Unit::TestSuite.new(@test_case1.name)
    sub_suite1 << @test_case1.new('test_1')
    sub_suite1 << @test_case1.new('test_2')

    sub_suite2 = Test::Unit::TestSuite.new(@test_case2.name)
    sub_suite2 << @test_case2.new('test_0')

    suite = empty_suite(name)
    suite << sub_suite1
    suite << sub_suite2
    suite
  end
end

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

module Test
  module Unit
    module Collector
      class TestDir < TestCase
        class FileSystem
          class Directory
            def initialize(name, fs, parent=self, &block)
              @name = name
              @fs = fs
              @parent = parent
              @contents = {'.' => self, '..' => parent}
              instance_eval(&block) if(block)
            end
            
            def file(name, contents)
              @contents[name] = contents
            end

            def dir(name, &block)
              @contents[name] = self.class.new(name, @fs, self, &block)
            end

            def entries
              @contents.keys
            end

            def directory?(name)
              return true if(name.nil? || name.empty?)
              return false unless(@contents.include?(name))
              @contents[name].kind_of?(self.class)
            end

            def file?(name)
              return false unless(@contents.include?(name))
              !directory?(name)
            end

            def exist?(name)
              @contents.include?(name)
            end

            def [](name)
              raise Errno::ENOENT, name unless(@contents.include?(name))
              @contents[name]
            end

            def path_to(name=nil)
              if(!name)
                @parent.path_to(@name)
              elsif(@parent == self)
                @fs.join('/', name)
              else
                @fs.join(@parent.path_to(@name), name)
              end
            end
          end

          class ObjectSpace
            def initialize
              @objects = []
            end

            def each_object(klass, &block)
              @objects.find_all{|o| o.kind_of?(klass)}.each(&block)
            end

            def <<(object)
              @objects << object
            end
          end

          attr_reader :object_space
          
          def initialize(&block)
            @root = Directory.new('/', self, &block)
            @pwd = @root
            @object_space = ObjectSpace.new
            @required = []
          end

          def entries(dir)
            e = find(dir)
            require_directory(dir)
            e.entries
          end

          def directory?(name)
            return true if (base = basename(name)) == '/'
            e = find(dirname(name))
            return false unless(e)
            e.directory?(base)
          end

          def find(path)
            if(/\A\// =~ path)
              thing = @root
            else
              thing = @pwd
            end
            path.scan(/[^\/]+/) do |e|
              break thing = false unless(thing.kind_of?(Directory))
              thing = thing[e]
            end
            thing
          end

          def dirname(name)
            if (name = name.tr_s('/', '/')) == '/'
              name
            else
              name[%r"\A.+(?=/[^/]+/?\z)|\A/"] || "."
            end
          end

          def basename(name)
            name[%r"(\A/|[^/]+)/*\z", 1]
          end

          def split(name)
            [dirname(name), basename(name)]
          end

          def join(*parts)
            parts.join('/').gsub(%r{/+}, '/')
          end

          def file?(name)
            e = find(dirname(name))
            return false unless(e)
            e.file?(basename(name))
          end

          def pwd
            @pwd.path_to
          end

          def chdir(to)
            e = find(to)
            require_directory(to)
            @pwd = e
          end

          def expand_path(path, base = nil)
            until /\A\// =~ path
              base ||= pwd
              path = join(base, path)
              base = nil
            end
            path.gsub!(%r"(?:/\.)+(?=/)", '')
            nil while path.sub!(%r"/(?!\.\./)[^/]+/\.\.(?=/)", '')
            path.sub!(%r"\A(?:/\.\.)+(?=/)", '')
            path.sub!(%r"(?:\A(/)|/)\.\.?\z", '\1')
            path
          end

          def require_directory(path)
            raise Errno::ENOTDIR, path unless(directory?(path))
          end

          def require(file)
            return false if(@required.include?(file))
            begin
              e = find(file)
            rescue Errno::ENOENT => e
              if(/\.rb\Z/ =~ file)
                raise LoadError, file
              end
              e = find(file + '.rb')
            end
            @required << file
            @object_space << e
            true
          rescue Errno::ENOENT
            raise LoadError, file
          end
        end

        def test_dir
          inner_dir = nil
          dirs = FileSystem::Directory.new('/', nil) do
            file 'a', nil
            inner_dir = dir 'b'
          end
          assert_equal(inner_dir, dirs['b'])
        end

        def test_fs
          fs = FileSystem.new do
            file 'a', nil
            dir 'b'
          end
          assert_equal(['.', '..', 'a', 'b'].sort, fs.entries('/').sort)
          assert(fs.directory?('/'))
          assert(!fs.directory?('/a'))
          assert(!fs.directory?('/bogus'))
          assert(fs.file?('/a'))
          assert(!fs.file?('/'))
          assert(!fs.file?('/bogus'))
          assert(fs.directory?('/b'))
          assert(fs.file?('a'))
          assert(fs.directory?('b'))
        end

        def test_fs_sub
          fs = FileSystem.new do
            dir 'a' do
              file 'b', nil
              dir 'c' do
                file 'd', nil
              end
            end
          end
          assert(fs.file?('/a/b'))
          assert(!fs.file?('/a/b/c/d'))
          assert(fs.file?('/a/c/d'))
        end

        def test_fs_pwd
          fs = FileSystem.new do
            file 'a', nil
            dir 'b' do
              file 'c', nil
              dir 'd' do
                file 'e', nil
              end
            end
          end
          assert_equal('/', fs.pwd)
          assert_raises(Errno::ENOENT) do
            fs.chdir('bogus')
          end
          assert_raises(Errno::ENOTDIR) do
            fs.chdir('a')
          end
          fs.chdir('b')
          assert_equal('/b', fs.pwd)
          fs.chdir('d')
          assert_equal('/b/d', fs.pwd)
          fs.chdir('..')
          assert_equal('/b', fs.pwd)
          fs.chdir('..')
          assert_equal('/', fs.pwd)
        end

        def test_fs_entries
          fs = FileSystem.new do
            file 'a', nil
            dir 'b' do
              file 'c', nil
              file 'd', nil
            end
            file 'e', nil
            dir 'f' do
              file 'g', nil
              dir 'h' do
                file 'i', nil
              end
            end
          end
          assert_equal(['.', '..', 'a', 'b', 'e', 'f'], fs.entries('/').sort)
          assert_equal(['.', '..', 'a', 'b', 'e', 'f'], fs.entries('.').sort)
          assert_equal(['.', '..', 'a', 'b', 'e', 'f'], fs.entries('b/..').sort)
          assert_equal(['.', '..', 'c', 'd'], fs.entries('b').sort)
          assert_raises(Errno::ENOENT) do
            fs.entries('z')
          end
          assert_raises(Errno::ENOTDIR) do
            fs.entries('a')
          end
          fs.chdir('f')
          assert_equal(['.', '..', 'i'], fs.entries('h').sort)
        end

        class TestClass1
        end
        class TestClass2
        end
        def test_fs_require
          fs = FileSystem.new do
            file 'test_class1.rb', TestClass1
            dir 'dir' do
              file 'test_class2.rb', TestClass2
            end
          end
          c = []
          fs.object_space.each_object(Class) do |o|
            c << o
          end
          assert_equal([], c)

          assert_raises(LoadError) do
            fs.require('bogus')
          end
          
          assert(fs.require('test_class1.rb'))
          assert(!fs.require('test_class1.rb'))
          c = []
          fs.object_space.each_object(Class) do |o|
            c << o
          end
          assert_equal([TestClass1], c)

          fs.require('dir/test_class2')
          c = []
          fs.object_space.each_object(Class) do |o|
            c << o
          end
          assert_equal([TestClass1, TestClass2], c)

          c = []
          fs.object_space.each_object(Time) do |o|
            c << o
          end
          assert_equal([], c)
        end

        def setup
          @t1 = t1 = create_test(1)
          @t2 = t2 = create_test(2)
          @t3 = t3 = create_test(3)
          @t4 = t4 = create_test(4)
          @t5 = t5 = create_test(5)
          @t6 = t6 = create_test(6)
          fs = FileSystem.new do
            file 'test_1.rb', t1
            file 'test_2.rb', t2
            dir 'd1' do
              file 'test_3.rb', t3
            end
            file 't4.rb', t4
            dir 'd2' do
              file 'test_5', t5
              file 'test_6.rb', Time
            end
            file 't6.rb', t6
          end
          fs.require('t6')
          @c = Dir.new(fs, fs, fs.object_space, fs)
        end

        def create_test(name)
          t = Class.new(TestCase)
          t.class_eval <<-EOC
            def self.name
              "T\#{#{name}}"
            end
            def test_#{name}a
            end
            def test_#{name}b
            end
          EOC
          t
        end

        def test_simple_collect
          expected = TestSuite.new('d1')
          expected << (@t3.suite)
          assert_equal(expected, @c.collect('d1'))
        end

        def test_multilevel_collect
          expected = TestSuite.new('.')
          expected << @t1.suite << @t2.suite
          expected << (TestSuite.new('d1') << @t3.suite)
          assert_equal(expected, @c.collect)
        end

        def test_collect_file
          expected = TestSuite.new('test_1.rb')
          expected << @t1.suite
          assert_equal(expected, @c.collect('test_1.rb'))
          
          expected = TestSuite.new('t4.rb')
          expected << @t4.suite
          assert_equal(expected, @c.collect('t4.rb'))
        end

        def test_nil_pattern
          expected = TestSuite.new('d2')
          expected << @t5.suite
          @c.pattern.clear
          assert_equal(expected, @c.collect('d2'))
        end

        def test_filtering
          expected = TestSuite.new('.')
          expected << @t1.suite
          @c.filter = proc{|t| t.method_name == 'test_1a' || t.method_name == 'test_1b'}
          assert_equal(expected, @c.collect)
        end

        def test_collect_multi
          expected = TestSuite.new('[d1, d2]')
          expected << (TestSuite.new('d1') << @t3.suite)
          expected << (TestSuite.new('d2') << @t5.suite)
          @c.pattern.replace([/\btest_/])
          assert_equal(expected, @c.collect('d1', 'd2'))
        end
      end
    end
  end
end

--- NEW FILE: test_load.rb ---
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"
    @test_dir.rmtree if @test_dir.exist?
    @test_dir.mkpath
  end

  setup
  def setup_top_level_test_cases
    @test_case1 = @test_dir + "test_case1.rb"
    @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_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_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

  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("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_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("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_case1.basename.to_s},
                    [:suite, {:name => _test_case_name("TestCase1")},
                     [:test, {:name => "test1_1"}],
                     [:test, {:name => "test1_2"}]]],
                   @test_case1.to_s)

    assert_collect([:suite, {:name => @no_load_sub_test_case5.basename.to_s},
                    [:suite, {:name => _test_case_name("NoLoadSubTestCase5")},
                     [:test, {:name => "test5_1"}],
                     [:test, {:name => "test5_2"}]]],
                   @no_load_sub_test_case5.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("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"}]]]) do |collector|
      collector.filter = Proc.new do |test|
        not /\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("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 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

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

require 'test/unit'
require 'test/unit/collector/objectspace'

module Test
  module Unit
    module Collector
      class TC_ObjectSpace < TestCase
        def setup
          @tc1 = Class.new(TestCase) do
            def self.name
              "tc_1"
            end
            def test_1
            end
            def test_2
            end
          end

          @tc2 = Class.new(TestCase) do
            def self.name
              "tc_2"
            end
            def test_0
            end
          end

          @no_tc = Class.new do
            def test_4
            end
          end
 
          @object_space = {Class => [@tc1, @tc2, @no_tc], String => ['']}
          def @object_space.each_object(type)
            self[type].each{|item| yield(item) }
          end

          @c = ObjectSpace.new(@object_space)
        end

        def full_suite(name=ObjectSpace::NAME)
          expected = TestSuite.new(name)
          expected << (TestSuite.new(@tc1.name) << @tc1.new('test_1') << @tc1.new('test_2'))
          expected << (TestSuite.new(@tc2.name) << @tc2.new('test_0'))
        end

        def empty_suite
          TestSuite.new(ObjectSpace::NAME)
        end
        
        def test_basic_collection
          assert_equal(full_suite("name"), @c.collect("name"))

          @c.filter = []
          assert_equal(full_suite("name"), @c.collect("name"))
        end
        
        def test_filtered_collection
          @c.filter = proc{false}
          assert_equal(empty_suite, @c.collect)

          @c.filter = proc{true}
          assert_equal(full_suite, @c.collect)

          @c.filter = proc{nil}
          assert_equal(full_suite, @c.collect)

          @c.filter = [proc{false}, proc{true}]
          assert_equal(empty_suite, @c.collect)

          @c.filter = [proc{true}, proc{false}]
          assert_equal(full_suite, @c.collect)

          @c.filter = [proc{nil}, proc{false}]
          assert_equal(empty_suite, @c.collect)
          
          @c.filter = [proc{nil}, proc{true}]
          assert_equal(full_suite, @c.collect)
          
          expected = TestSuite.new(ObjectSpace::NAME)
          expected << (TestSuite.new(@tc1.name) << @tc1.new('test_1'))
          expected << (TestSuite.new(@tc2.name) << @tc2.new('test_0'))
          @c.filter = proc{|test| ['test_1', 'test_0'].include?(test.method_name)}
          assert_equal(expected, @c.collect)

          expected = TestSuite.new(ObjectSpace::NAME)
          expected << (TestSuite.new(@tc1.name) << @tc1.new('test_1'))
          expected << (TestSuite.new(@tc2.name) << @tc2.new('test_0'))
          @c.filter = [proc{|t| t.method_name == 'test_1' ? true : nil}, proc{|t| t.method_name == 'test_0' ? true : nil}, proc{false}]
          assert_equal(expected, @c.collect)
        end
      end
    end
  end
end



More information about the cairo-commit mailing list