diff options
Diffstat (limited to 'ruby_1_8_5/test/testunit')
-rw-r--r-- | ruby_1_8_5/test/testunit/collector/test_dir.rb | 389 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/collector/test_objectspace.rb | 98 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/runit/test_assert.rb | 402 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/runit/test_testcase.rb | 91 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/runit/test_testresult.rb | 144 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/runit/test_testsuite.rb | 49 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/test_assertions.rb | 528 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/test_error.rb | 26 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/test_failure.rb | 33 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/test_testcase.rb | 275 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/test_testresult.rb | 104 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/test_testsuite.rb | 129 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/util/test_backtracefilter.rb | 41 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/util/test_observable.rb | 102 | ||||
-rw-r--r-- | ruby_1_8_5/test/testunit/util/test_procwrapper.rb | 36 |
15 files changed, 2447 insertions, 0 deletions
diff --git a/ruby_1_8_5/test/testunit/collector/test_dir.rb b/ruby_1_8_5/test/testunit/collector/test_dir.rb new file mode 100644 index 0000000000..e7ae414264 --- /dev/null +++ b/ruby_1_8_5/test/testunit/collector/test_dir.rb @@ -0,0 +1,389 @@ +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) + e = find(dirname(name)) + return false unless(e) + e.directory?(basename(name)) + end + + def find(path) + if(/\A\// =~ path) + path = path.sub(/\A\//, '') + thing = @root + else + thing = @pwd + end + split(path).each do |e| + break thing = false unless(thing.kind_of?(Directory)) + thing = thing[e] + end + thing + end + + def dirname(name) + join(*split(name)[0..-2]) + end + + def basename(name) + split(name)[-1] + end + + def split(name) + name.split('/') + 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 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 diff --git a/ruby_1_8_5/test/testunit/collector/test_objectspace.rb b/ruby_1_8_5/test/testunit/collector/test_objectspace.rb new file mode 100644 index 0000000000..a1532ff3e6 --- /dev/null +++ b/ruby_1_8_5/test/testunit/collector/test_objectspace.rb @@ -0,0 +1,98 @@ +# 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 diff --git a/ruby_1_8_5/test/testunit/runit/test_assert.rb b/ruby_1_8_5/test/testunit/runit/test_assert.rb new file mode 100644 index 0000000000..a3e62b2b2d --- /dev/null +++ b/ruby_1_8_5/test/testunit/runit/test_assert.rb @@ -0,0 +1,402 @@ +# Author:: Masaki Suketa. +# Adapted by:: Nathaniel Talbott. +# Copyright:: Copyright (c) Masaki Suketa. All rights reserved. +# Copyright:: Copyright (c) 2002 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'rubyunit' + +module RUNIT + class TargetAssert + include RUNIT::Assert + end + + class TestAssert < RUNIT::TestCase + def setup + @assert = TargetAssert.new + @e = nil + end + + def test_assert + sub_test_assert_pass(true) + sub_test_assert_pass(TRUE) + sub_test_assert_failure(false) + sub_test_assert_failure(FALSE) + sub_test_assert_failure(nil) + sub_test_assert_pass("") + sub_test_assert_pass("ok") + sub_test_assert_pass(0) + sub_test_assert_pass(1) + end + + def test_assert_with_2_argument + assert_no_exception { + assert(true, "3") + } + assert_no_exception { + assert(true) + } + end + + def test_assert_equal_float_0_1 + assert_proc = Proc.new { + @assert.assert_equal_float(1.4, 1.35, 0.1) + } + sub_assert_pass(assert_proc) + end + + def test_assert_equal_float_0_5 + assert_proc = Proc.new { + @assert.assert_equal_float(1.4, 1.34, 0.5) + } + sub_assert_pass(assert_proc) + end + + def test_assert_equal_float_0 + assert_proc = Proc.new { + @assert.assert_equal_float(1.4, 1.4, 0) + } + sub_assert_pass(assert_proc) + end + + def test_assert_equal_float_0_raise + assert_proc = Proc.new { + @assert.assert_equal_float(1.4, 1.34, 0) + } + sub_assert_raise_fail(assert_proc) + end + + def test_assert_equal_float_0_01 + assert_proc = Proc.new { + @assert.assert_equal_float(1.4, 1.35, 0.01) + } + sub_assert_raise_fail(assert_proc) + end + + def test_assert_equal_float_0_001 + assert_proc = Proc.new { + @assert.assert_equal_float(Math.sqrt(2), 1.414, 0.001) + } + sub_assert_pass(assert_proc) + end + + def test_assert_equal_float_minus_1_0 + assert_proc = Proc.new { + @assert.assert_equal_float(1.4, 1.35, -1.0) + } + sub_assert_raise_fail(assert_proc) + end + + def test_assert_fail + except = nil + begin + @assert.assert_fail("failure") + rescue Exception + except = $! + end + assert_not_nil(except) + end + + def sub_test_assert_pass(obj) + assert_proc = Proc.new { + @assert.assert(obj) + } + sub_assert_pass(assert_proc) + end + + def sub_test_assert_failure(obj) + assert_proc = Proc.new { + @assert.assert(obj) + } + sub_assert_raise_fail(assert_proc) + end + + def test_assert_equal + assert_proc = Proc.new { + @assert.assert_equal(2, 2) + } + sub_assert_pass(assert_proc) + assert_proc = Proc.new { + @assert.assert_equal(2, 3) + } + sub_assert_raise_fail(assert_proc) + end + + def test_assert_nil + obj = nil + assert_proc = Proc.new { + @assert.assert_nil(obj) + } + sub_assert_pass(assert_proc) + obj = 'string' + sub_assert_raise_fail(assert_proc) + end + + def test_assert_not_nil + obj = 'string' + assert_proc = Proc.new { + @assert.assert_not_nil(obj) + } + sub_assert_pass(assert_proc) + + obj = nil + sub_assert_raise_fail(assert_proc) + end + + def test_assert_operator + assert_proc = Proc.new { + @assert.assert_operator(2, :<, 3) + } + sub_assert_pass(assert_proc) + assert_proc = Proc.new { + @assert.assert_operator(2, :>, 3) + } + sub_assert_raise_fail(assert_proc) + end + + def test_assert_respond_to + sub_test_assert_respond_to('string', 'sub', 'foo') + sub_test_assert_respond_to('string', :sub, :foo) + end + + def sub_test_assert_respond_to(obj, msg, dummy_msg) + assert_proc = Proc.new { + @assert.assert_respond_to(msg, obj) + } + sub_assert_pass(assert_proc) + assert_proc = Proc.new { + @assert.assert_respond_to(dummy_msg, obj) + } + sub_assert_raise_fail(assert_proc) + end + + def test_assert_send + assert_proc = Proc.new { + ary = [] + @assert.assert_send ary, :empty? + } + sub_assert_pass(assert_proc) + assert_proc = Proc.new { + ary = [2,3] + @assert.assert_send ary, :empty? + } + sub_assert_raise_fail(assert_proc) + assert_proc = Proc.new { + str = "abc" + @assert.assert_send str, :sub!, "z", "y" + } + sub_assert_raise_fail(assert_proc) + end + + def test_assert_kind_of + assert_proc = Proc.new { + @assert.assert_kind_of(String, "string") + } + sub_assert_pass(assert_proc) + assert_proc = Proc.new { + @assert.assert_kind_of(Regexp, "string") + } + sub_assert_raise_fail(assert_proc) + end + + def test_assert_instance_of + assert_proc = Proc.new { + @assert.assert_instance_of(String, "string") + } + sub_assert_pass(assert_proc) + assert_proc = Proc.new { + @assert.assert_instance_of(Object, "string") + } + sub_assert_raise_fail(assert_proc) + end + + def test_assert_match + assert_proc = Proc.new{ + @assert.assert_match('foostring', /foo/) + } + sub_assert_pass(assert_proc) + assert_proc = Proc.new { + @assert.assert_match('barstring', /foo/) + } + sub_assert_raise_fail(assert_proc) + match = @assert.assert_match('foostring', /foo/) + assert_instance_of(MatchData, match) + assert_equal('foo', match[0]) + end + + def test_assert_matches + assert_proc = Proc.new{ + @assert.assert_matches('foostring', /foo/) + } + sub_assert_pass(assert_proc) + assert_proc = Proc.new { + @assert.assert_matches('barstring', /foo/) + } + sub_assert_raise_fail(assert_proc) + end + + def test_assert_not_match + assert_proc = Proc.new{ + @assert.assert_not_match('barstring', /foo/) + } + sub_assert_pass(assert_proc) + assert_proc = Proc.new { + @assert.assert_not_match('foostring', /foo/) + } + sub_assert_raise_fail(assert_proc) + assert_proc = Proc.new { + @assert.assert_not_match('foobarbaz', /ba.+/) + } + sub_assert_raise_fail(assert_proc) + end + + def test_assert_same + flag = false + e = "foo" + a = e + assert_proc = Proc.new {@assert.assert_same(e, a)} + sub_assert_pass(assert_proc) + + a = "foo" + sub_assert_raise_fail(assert_proc) + end + + def test_assert_exception + assert_proc = Proc.new{ + @assert.assert_exception(IOError) { + raise IOError + } + } + sub_assert_pass(assert_proc) + + assert_proc = Proc.new{ + @assert.assert_exception(StandardError) { + raise IOError + } + } + sub_assert_raise_fail(assert_proc) + + assert_proc = Proc.new{ + @assert.assert_exception(IOError, "Exception") { + raise StandardError + } + } + sub_assert_raise_fail(assert_proc) + + assert_proc = Proc.new { + @assert.assert_exception(StandardError) { + "No Exception raised in this block" + } + } + sub_assert_raise_fail(assert_proc) + + assert_proc = Proc.new { + @assert.assert_exception(StandardError) { + exit(33) + } + } + sub_assert_raise_fail(assert_proc) + + t = @assert.assert_exception(IOError) { + raise IOError + } + assert_instance_of(IOError, t) + t = @assert.assert_exception(NameError) { + non_existent_method + } + assert_instance_of(NameError, t) + t = @assert.assert_exception(SystemExit) { + exit(33) + } + assert_instance_of(SystemExit, t) + end + + def test_assert_no_exception + assert_proc = Proc.new{ + @assert.assert_no_exception(IOError, ArgumentError) { + "No Exception raised in this block" + } + } + sub_assert_pass(assert_proc) + + assert_proc = Proc.new{ + @assert.assert_no_exception(IOError, ArgumentError) { + raise StandardError, "Standard Error raised" + } + } + sub_assert_raise_error(assert_proc) + + assert_proc = Proc.new{ + @assert.assert_no_exception(IOError, ArgumentError) { + raise ArgumentError, "Bad Argument" + } + } + sub_assert_raise_fail(assert_proc) + + assert_proc = Proc.new{ + @assert.assert_no_exception { + raise ArgumentError, "Bad Argument" + } + } + sub_assert_raise_fail(assert_proc) + + assert_proc = Proc.new{ + @assert.assert_no_exception { + raise NameError, "Bad Name" + } + } + sub_assert_raise_fail(assert_proc) + assert_proc = Proc.new { + @assert.assert_no_exception { + raise NoMemoryError + } + } + sub_assert_raise_fail(assert_proc) + end + + def sub_assert_pass(p) + flag = false + err = nil + begin + p.call + flag = true + rescue + err = $! + flag = false + end + assert(flag, err.to_s) + end + + def sub_assert_raise_fail(p) + flag = false + err = nil + begin + p.call + flag = false + rescue RUNIT::AssertionFailedError + flag = true + err = $! + rescue Exception + flag = false + err = $! + end + assert(flag, err.to_s) + end + + def sub_assert_raise_error(p) + flag = false + err = nil + begin + p.call + flag = false + rescue RUNIT::AssertionFailedError + flag = false + err = $! + rescue Exception + flag = true + err = $! + end + assert(flag, err.to_s) + end + end +end diff --git a/ruby_1_8_5/test/testunit/runit/test_testcase.rb b/ruby_1_8_5/test/testunit/runit/test_testcase.rb new file mode 100644 index 0000000000..f57e0386e3 --- /dev/null +++ b/ruby_1_8_5/test/testunit/runit/test_testcase.rb @@ -0,0 +1,91 @@ +# Author:: Masaki Suketa. +# Adapted by:: Nathaniel Talbott. +# Copyright:: Copyright (c) Masaki Suketa. All rights reserved. +# Copyright:: Copyright (c) 2002 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'rubyunit' + +module RUNIT + class DummyError < StandardError + end + + class TestTestCase < RUNIT::TestCase + def setup + @dummy_testcase = Class.new(RUNIT::TestCase) do + def self.name + "DummyTestCase" + end + + attr_reader :status, :dummy_called, :dummy2_called + + def initialize(*arg) + super(*arg) + @status = 0 + @dummy_called = false + @dummy2_called = false + end + + def setup + @status = 1 if @status == 0 + end + + def test_dummy + @status = 2 if @status == 1 + @dummy_called = true + end + + def test_dummy2 + @status = 2 if @status == 1 + @dummy2_called = true + raise DummyError + end + + def teardown + @status = 3 if @status == 2 + end + end + + @test1 = @dummy_testcase.new('test_dummy') + @test2 = @dummy_testcase.new('test_dummy2', 'TestCase') + end + + def test_name + assert_equal('DummyTestCase#test_dummy', @test1.name) # The second parameter to #initialize is ignored in emulation + assert_equal('DummyTestCase#test_dummy2', @test2.name) + end + + def test_run + result = RUNIT::TestResult.new + @test1.run(result) + assert_equal(1, result.run_count) + end + + def test_s_suite + suite = @dummy_testcase.suite + assert_instance_of(RUNIT::TestSuite, suite) + assert_equal(2, suite.count_test_cases) + end + + def test_teardown_err + suite = Class.new(RUNIT::TestCase) do + def test_foo + assert(false) + end + + def test_bar + assert(true) + end + + def teardown + raise StandardError + end + end.suite + + result = RUNIT::TestResult.new + suite.run(result) + assert_equal(2, result.error_size) + assert_equal(1, result.failure_size) + end + end +end diff --git a/ruby_1_8_5/test/testunit/runit/test_testresult.rb b/ruby_1_8_5/test/testunit/runit/test_testresult.rb new file mode 100644 index 0000000000..702b88d0c0 --- /dev/null +++ b/ruby_1_8_5/test/testunit/runit/test_testresult.rb @@ -0,0 +1,144 @@ +# Author:: Masaki Suketa. +# Adapted by:: Nathaniel Talbott. +# Copyright:: Copyright (c) Masaki Suketa. All rights reserved. +# Copyright:: Copyright (c) 2002 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'rubyunit' + +module RUNIT + class TestTestResult < RUNIT::TestCase + def setup + @result = RUNIT::TestResult.new + + @normal_suite = Class::new(RUNIT::TestCase) do + def test_1 + assert(true) + assert(true) + end + end.suite + + @failure_suite = Class::new(RUNIT::TestCase) do + def test_1 + assert(true) + assert(false) + end + end.suite + + @error_suite = Class::new(RUNIT::TestCase) do + def setup + raise ScriptError + end + def test_1 + assert(true) + end + end.suite + + @multi_failure_suite = Class::new(RUNIT::TestCase) do + def test1 + assert(false) + end + def test2 + assert(false) + end + def test3 + assert(false) + end + end.suite + + @with_error_suite = Class::new(RUNIT::TestCase) do + def test1 + raise StandardError + end + end.suite + + @multi_error_suite = Class::new(RUNIT::TestCase) do + def test1 + raise StandardError + end + def test2 + raise StandardError + end + def test3 + raise StandardError + end + end.suite + + @multi_suite = Class::new(RUNIT::TestCase) do + def test_1 + assert(true) + assert(true) + end + def test_2 + assert(true) + end + def test_3 + assert(true) + assert(false) + assert(true) + end + end.suite + end + + def test_error_size + @normal_suite.run(@result) + assert_equal(0, @result.error_size) + @with_error_suite.run(@result) + assert_equal(1, @result.error_size) + @multi_error_suite.run(@result) + assert_equal(4, @result.error_size) + end + + def test_errors + @normal_suite.run(@result) + assert_equal(0, @result.errors.size) + end + + def test_failure_size + @normal_suite.run(@result) + assert_equal(0, @result.failure_size) + @failure_suite.run(@result) + assert_equal(1, @result.failure_size) + @multi_failure_suite.run(@result) + assert_equal(4, @result.failure_size) + end + + def test_failures + @normal_suite.run(@result) + assert_equal(0, @result.failures.size) + @failure_suite.run(@result) + assert_equal(1, @result.failures.size) + @multi_failure_suite.run(@result) + assert_equal(4, @result.failures.size) + end + + def test_run_no_exception + assert_no_exception { + @error_suite.run(@result) + } + end + + def test_run_asserts + @normal_suite.run(@result) + assert_equal(2, @result.run_asserts) + end + + def test_run_asserts2 + @failure_suite.run(@result) + assert_equal(2, @result.run_asserts) + end + + def test_run_tests + assert_equal(0, @result.run_tests) + @normal_suite.run(@result) + assert_equal(1, @result.run_tests) + @multi_suite.run(@result) + assert_equal(4, @result.run_tests) + end + + def test_succeed? + @normal_suite.run(@result) + assert(@result.succeed?) + end + end +end diff --git a/ruby_1_8_5/test/testunit/runit/test_testsuite.rb b/ruby_1_8_5/test/testunit/runit/test_testsuite.rb new file mode 100644 index 0000000000..58702d3efe --- /dev/null +++ b/ruby_1_8_5/test/testunit/runit/test_testsuite.rb @@ -0,0 +1,49 @@ +# Author:: Masaki Suketa. +# Adapted by:: Nathaniel Talbott. +# Copyright:: Copyright (c) Masaki Suketa. All rights reserved. +# Copyright:: Copyright (c) 2002 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'rubyunit' + +module RUNIT + class TestTestSuite < RUNIT::TestCase + def setup + @testsuite = RUNIT::TestSuite.new + @dummy_test = Class.new(RUNIT::TestCase) do + def test_foo + end + def test_bar + end + end + @dummy_empty_test = Class.new(RUNIT::TestCase){} + end + + def test_count_test_cases + assert_equal(0, @testsuite.count_test_cases) + + @testsuite.add(@dummy_empty_test.suite) + assert_equal(0, @testsuite.count_test_cases) + + @testsuite.add(@dummy_test.suite) + assert_equal(2, @testsuite.count_test_cases) + + @testsuite.add(@dummy_test.suite) + assert_equal(4, @testsuite.count_test_cases) + + dummytest_foo = @dummy_test.new('test_foo') + @testsuite.add(dummytest_foo) + assert_equal(5, @testsuite.count_test_cases) + end + + def test_add + @testsuite.add(@dummy_empty_test.suite) + assert_equal(0, @testsuite.size) + assert_equal(0, @testsuite.count_test_cases) + + @testsuite.add(@dummy_test.suite) + assert_equal(2, @testsuite.size) + assert_equal(2, @testsuite.count_test_cases) + end + end +end diff --git a/ruby_1_8_5/test/testunit/test_assertions.rb b/ruby_1_8_5/test/testunit/test_assertions.rb new file mode 100644 index 0000000000..8ccd2a81bd --- /dev/null +++ b/ruby_1_8_5/test/testunit/test_assertions.rb @@ -0,0 +1,528 @@ +# Author:: Nathaniel Talbott. +# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'test/unit' + +module Test + module Unit + class TC_Assertions < TestCase + def check(value, message="") + add_assertion + if (!value) + raise AssertionFailedError.new(message) + end + end + + def check_assertions(expect_fail, expected_message="", return_value_expected=false) + @actual_assertion_count = 0 + failed = true + actual_message = nil + @catch_assertions = true + return_value = nil + begin + return_value = yield + failed = false + rescue AssertionFailedError => error + actual_message = error.message + end + @catch_assertions = false + check(expect_fail == failed, (expect_fail ? "Should have failed, but didn't" : "Should not have failed, but did with message\n<#{actual_message}>")) + check(1 == @actual_assertion_count, "Should have made one assertion but made <#{@actual_assertion_count}>") + if (expect_fail) + case expected_message + when String + check(actual_message == expected_message, "Should have the correct message.\n<#{expected_message.inspect}> expected but was\n<#{actual_message.inspect}>") + when Regexp + check(actual_message =~ expected_message, "The message should match correctly.\n</#{expected_message.source}/> expected to match\n<#{actual_message.inspect}>") + else + check(false, "Incorrect expected message type in assert_nothing_failed") + end + else + if (!return_value_expected) + check(return_value.nil?, "Should not return a value but returned <#{return_value}>") + else + check(!return_value.nil?, "Should return a value") + end + end + return return_value + end + + def check_nothing_fails(return_value_expected=false, &proc) + check_assertions(false, "", return_value_expected, &proc) + end + + def check_fails(expected_message="", &proc) + check_assertions(true, expected_message, &proc) + end + + def test_assert_block + check_nothing_fails { + assert_block {true} + } + check_nothing_fails { + assert_block("successful assert_block") {true} + } + check_nothing_fails { + assert_block("successful assert_block") {true} + } + check_fails("assert_block failed.") { + assert_block {false} + } + check_fails("failed assert_block") { + assert_block("failed assert_block") {false} + } + end + + def test_assert + check_nothing_fails{assert("a")} + check_nothing_fails{assert(true)} + check_nothing_fails{assert(true, "successful assert")} + check_fails("<nil> is not true."){assert(nil)} + check_fails("<false> is not true."){assert(false)} + check_fails("failed assert.\n<false> is not true."){assert(false, "failed assert")} + end + + def test_assert_equal + check_nothing_fails { + assert_equal("string1", "string1") + } + check_nothing_fails { + assert_equal( "string1", "string1", "successful assert_equal") + } + check_nothing_fails { + assert_equal("string1", "string1", "successful assert_equal") + } + check_fails(%Q{<"string1"> expected but was\n<"string2">.}) { + assert_equal("string1", "string2") + } + check_fails(%Q{failed assert_equal.\n<"string1"> expected but was\n<"string2">.}) { + assert_equal("string1", "string2", "failed assert_equal") + } + check_fails(%Q{<"1"> expected but was\n<1>.}) do + assert_equal("1", 1) + end + end + + def test_assert_raise + return_value = nil + check_nothing_fails(true) { + return_value = assert_raise(RuntimeError) { + raise "Error" + } + } + check(return_value.kind_of?(Exception), "Should have returned the exception from a successful assert_raise") + check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise") + check_nothing_fails(true) { + assert_raise(ArgumentError, "successful assert_raise") { + raise ArgumentError.new("Error") + } + } + check_nothing_fails(true) { + assert_raise(RuntimeError) { + raise "Error" + } + } + check_nothing_fails(true) { + assert_raise(RuntimeError, "successful assert_raise") { + raise "Error" + } + } + check_fails("<RuntimeError> exception expected but none was thrown.") { + assert_raise(RuntimeError) { + 1 + 1 + } + } + check_fails(%r{\Afailed assert_raise.\n<ArgumentError> exception expected but was\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) { + assert_raise(ArgumentError, "failed assert_raise") { + raise "Error" + } + } + check_fails("Should expect a class of exception, Object.\n<false> is not true.") { + assert_nothing_raised(Object) { + 1 + 1 + } + } + + exceptions = [ArgumentError, TypeError] + modules = [Math, Comparable] + rescues = exceptions + modules + exceptions.each do |exc| + check_nothing_fails(true) { + return_value = assert_raise(*rescues) { + raise exc, "Error" + } + } + check(return_value.instance_of?(exc), "Should have returned #{exc} but was #{return_value.class}") + check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise") + end + modules.each do |mod| + check_nothing_fails(true) { + return_value = assert_raise(*rescues) { + raise Exception.new("Error").extend(mod) + } + } + check(mod === return_value, "Should have returned #{mod}") + check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise") + end + check_fails("<[ArgumentError, TypeError, Math, Comparable]> exception expected but none was thrown.") { + assert_raise(*rescues) { + 1 + 1 + } + } + check_fails(%r{\Afailed assert_raise. +<\[ArgumentError, TypeError\]> exception expected but was +Class: <RuntimeError> +Message: <"Error"> +---Backtrace--- +.+ +---------------\Z}m) { + assert_raise(ArgumentError, TypeError, "failed assert_raise") { + raise "Error" + } + } + end + + def test_assert_instance_of + check_nothing_fails { + assert_instance_of(String, "string") + } + check_nothing_fails { + assert_instance_of(String, "string", "successful assert_instance_of") + } + check_nothing_fails { + assert_instance_of(String, "string", "successful assert_instance_of") + } + check_fails(%Q{<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) { + assert_instance_of(Hash, "string") + } + check_fails(%Q{failed assert_instance_of.\n<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) { + assert_instance_of(Hash, "string", "failed assert_instance_of") + } + end + + def test_assert_nil + check_nothing_fails { + assert_nil(nil) + } + check_nothing_fails { + assert_nil(nil, "successful assert_nil") + } + check_nothing_fails { + assert_nil(nil, "successful assert_nil") + } + check_fails(%Q{<nil> expected but was\n<"string">.}) { + assert_nil("string") + } + check_fails(%Q{failed assert_nil.\n<nil> expected but was\n<"string">.}) { + assert_nil("string", "failed assert_nil") + } + end + + def test_assert_not_nil + check_nothing_fails{assert_not_nil(false)} + check_nothing_fails{assert_not_nil(false, "message")} + check_fails("<nil> expected to not be nil."){assert_not_nil(nil)} + check_fails("message.\n<nil> expected to not be nil.") {assert_not_nil(nil, "message")} + end + + def test_assert_kind_of + check_nothing_fails { + assert_kind_of(Module, Array) + } + check_nothing_fails { + assert_kind_of(Object, "string", "successful assert_kind_of") + } + check_nothing_fails { + assert_kind_of(Object, "string", "successful assert_kind_of") + } + check_nothing_fails { + assert_kind_of(Comparable, 1) + } + check_fails(%Q{<"string">\nexpected to be kind_of?\n<Class> but was\n<String>.}) { + assert_kind_of(Class, "string") + } + check_fails(%Q{failed assert_kind_of.\n<"string">\nexpected to be kind_of?\n<Class> but was\n<String>.}) { + assert_kind_of(Class, "string", "failed assert_kind_of") + } + end + + def test_assert_match + check_nothing_fails { + assert_match(/strin./, "string") + } + check_nothing_fails { + assert_match("strin", "string") + } + check_nothing_fails { + assert_match(/strin./, "string", "successful assert_match") + } + check_nothing_fails { + assert_match(/strin./, "string", "successful assert_match") + } + check_fails(%Q{<"string"> expected to be =~\n</slin./>.}) { + assert_match(/slin./, "string") + } + check_fails(%Q{<"string"> expected to be =~\n</strin\\./>.}) { + assert_match("strin.", "string") + } + check_fails(%Q{failed assert_match.\n<"string"> expected to be =~\n</slin./>.}) { + assert_match(/slin./, "string", "failed assert_match") + } + end + + def test_assert_same + thing = "thing" + check_nothing_fails { + assert_same(thing, thing) + } + check_nothing_fails { + assert_same(thing, thing, "successful assert_same") + } + check_nothing_fails { + assert_same(thing, thing, "successful assert_same") + } + thing2 = "thing" + check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) { + assert_same(thing, thing2) + } + check_fails(%Q{failed assert_same.\n<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) { + assert_same(thing, thing2, "failed assert_same") + } + end + + def test_assert_nothing_raised + check_nothing_fails { + assert_nothing_raised { + 1 + 1 + } + } + check_nothing_fails { + assert_nothing_raised("successful assert_nothing_raised") { + 1 + 1 + } + } + check_nothing_fails { + assert_nothing_raised("successful assert_nothing_raised") { + 1 + 1 + } + } + check_nothing_fails { + begin + assert_nothing_raised(RuntimeError, StandardError, Comparable, "successful assert_nothing_raised") { + raise ZeroDivisionError.new("ArgumentError") + } + rescue ZeroDivisionError + end + } + check_fails("Should expect a class of exception, Object.\n<false> is not true.") { + assert_nothing_raised(Object) { + 1 + 1 + } + } + check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) { + assert_nothing_raised { + raise "Error" + } + } + check_fails(%r{\Afailed assert_nothing_raised\.\nException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) { + assert_nothing_raised("failed assert_nothing_raised") { + raise "Error" + } + } + check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) { + assert_nothing_raised(StandardError, RuntimeError) { + raise "Error" + } + } + check_fails("Failure.") do + assert_nothing_raised do + flunk("Failure") + end + end + end + + def test_flunk + check_fails("Flunked.") { + flunk + } + check_fails("flunk message.") { + flunk("flunk message") + } + end + + def test_assert_not_same + thing = "thing" + thing2 = "thing" + check_nothing_fails { + assert_not_same(thing, thing2) + } + check_nothing_fails { + assert_not_same(thing, thing2, "message") + } + check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) { + assert_not_same(thing, thing) + } + check_fails(%Q{message.\n<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) { + assert_not_same(thing, thing, "message") + } + end + + def test_assert_not_equal + check_nothing_fails { + assert_not_equal("string1", "string2") + } + check_nothing_fails { + assert_not_equal("string1", "string2", "message") + } + check_fails(%Q{<"string"> expected to be != to\n<"string">.}) { + assert_not_equal("string", "string") + } + check_fails(%Q{message.\n<"string"> expected to be != to\n<"string">.}) { + assert_not_equal("string", "string", "message") + } + end + + def test_assert_no_match + check_nothing_fails{assert_no_match(/sling/, "string")} + check_nothing_fails{assert_no_match(/sling/, "string", "message")} + check_fails(%Q{The first argument to assert_no_match should be a Regexp.\n<"asdf"> expected to be an instance of\n<Regexp> but was\n<String>.}) do + assert_no_match("asdf", "asdf") + end + check_fails(%Q{</string/> expected to not match\n<"string">.}) do + assert_no_match(/string/, "string") + end + check_fails(%Q{message.\n</string/> expected to not match\n<"string">.}) do + assert_no_match(/string/, "string", "message") + end + end + + def test_assert_throws + check_nothing_fails { + assert_throws(:thing, "message") { + throw :thing + } + } + check_fails("message.\n<:thing> expected to be thrown but\n<:thing2> was thrown.") { + assert_throws(:thing, "message") { + throw :thing2 + } + } + check_fails("message.\n<:thing> should have been thrown.") { + assert_throws(:thing, "message") { + 1 + 1 + } + } + end + + def test_assert_nothing_thrown + check_nothing_fails { + assert_nothing_thrown("message") { + 1 + 1 + } + } + check_fails("message.\n<:thing> was thrown when nothing was expected.") { + assert_nothing_thrown("message") { + throw :thing + } + } + end + + def test_assert_operator + check_nothing_fails { + assert_operator("thing", :==, "thing", "message") + } + check_fails(%Q{<0.15>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to?(:to_str).}) do + assert_operator("thing", 0.15, "thing") + end + check_fails(%Q{message.\n<"thing1"> expected to be\n==\n<"thing2">.}) { + assert_operator("thing1", :==, "thing2", "message") + } + end + + def test_assert_respond_to + check_nothing_fails { + assert_respond_to("thing", :to_s, "message") + } + check_nothing_fails { + assert_respond_to("thing", "to_s", "message") + } + check_fails("<0.15>\ngiven as the method name argument to #assert_respond_to must be a Symbol or #respond_to?(:to_str).") { + assert_respond_to("thing", 0.15) + } + check_fails("message.\n<:symbol>\nof type <Symbol>\nexpected to respond_to?<:non_existent>.") { + assert_respond_to(:symbol, :non_existent, "message") + } + end + + def test_assert_in_delta + check_nothing_fails { + assert_in_delta(1.4, 1.4, 0) + } + check_nothing_fails { + assert_in_delta(0.5, 0.4, 0.1, "message") + } + check_nothing_fails { + float_thing = Object.new + def float_thing.to_f + 0.2 + end + assert_in_delta(0.1, float_thing, 0.1) + } + check_fails("message.\n<0.5> and\n<0.4> expected to be within\n<0.05> of each other.") { + assert_in_delta(0.5, 0.4, 0.05, "message") + } + check_fails(%r{The arguments must respond to to_f; the first float did not\.\n<.+>\nof type <Object>\nexpected to respond_to\?<:to_f>.}) { + assert_in_delta(Object.new, 0.4, 0.1) + } + check_fails("The delta should not be negative.\n<-0.1> expected to be\n>=\n<0.0>.") { + assert_in_delta(0.5, 0.4, -0.1, "message") + } + end + + def test_assert_send + object = Object.new + class << object + private + def return_argument(argument, bogus) + return argument + end + end + check_nothing_fails { + assert_send([object, :return_argument, true, "bogus"], "message") + } + check_fails(%r{\Amessage\.\n<.+> expected to respond to\n<return_argument\(\[false, "bogus"\]\)> with a true value.\Z}) { + assert_send([object, :return_argument, false, "bogus"], "message") + } + end + + def test_condition_invariant + object = Object.new + def object.inspect + @changed = true + end + def object.==(other) + @changed ||= false + return (!@changed) + end + check_nothing_fails { + assert_equal(object, object, "message") + } + end + + def add_failure(message, location=caller) + if (!@catch_assertions) + super + end + end + + def add_assertion + if (!@catch_assertions) + super + else + @actual_assertion_count += 1 + end + end + end + end +end diff --git a/ruby_1_8_5/test/testunit/test_error.rb b/ruby_1_8_5/test/testunit/test_error.rb new file mode 100644 index 0000000000..56b275b362 --- /dev/null +++ b/ruby_1_8_5/test/testunit/test_error.rb @@ -0,0 +1,26 @@ +# Author:: Nathaniel Talbott. +# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'test/unit' + +module Test + module Unit + class TC_Error < TestCase + TF_Exception = Struct.new('TF_Exception', :message, :backtrace) + def test_display + ex = TF_Exception.new("message1\nmessage2", ['line1', 'line2']) + e = Error.new("name", ex) + assert_equal("name: #{TF_Exception.name}: message1", e.short_display) + assert_equal(<<EOM.strip, e.long_display) +Error: +name: +Struct::TF_Exception: message1 +message2 + line1 + line2 +EOM + end + end + end +end diff --git a/ruby_1_8_5/test/testunit/test_failure.rb b/ruby_1_8_5/test/testunit/test_failure.rb new file mode 100644 index 0000000000..164f942476 --- /dev/null +++ b/ruby_1_8_5/test/testunit/test_failure.rb @@ -0,0 +1,33 @@ +# Author:: Nathaniel Talbott. +# Copyright:: Copyright (c) 2003 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'test/unit' +require 'test/unit/failure' + +module Test::Unit + class TestFailure < TestCase + def test_display + f = Failure.new("name", [%q{location:1 in 'l'}], "message1\nmessage2") + assert_equal("name: message1", f.short_display) + assert_equal(<<EOM.strip, f.long_display) +Failure: +name [location:1]: +message1 +message2 +EOM + + f = Failure.new("name", [%q{location1:2 in 'l1'}, 'location2:1', %q{location3:3 in 'l3'}], "message1\nmessage2") + assert_equal("name: message1", f.short_display) + assert_equal(<<EOM.strip, f.long_display) +Failure: +name + [location1:2 in 'l1' + location2:1 + location3:3 in 'l3']: +message1 +message2 +EOM + end + end +end diff --git a/ruby_1_8_5/test/testunit/test_testcase.rb b/ruby_1_8_5/test/testunit/test_testcase.rb new file mode 100644 index 0000000000..2934a92fef --- /dev/null +++ b/ruby_1_8_5/test/testunit/test_testcase.rb @@ -0,0 +1,275 @@ +# Author:: Nathaniel Talbott. +# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'test/unit' + +module Test + module Unit + class TC_TestCase < TestCase + def test_creation + tc = Class.new(TestCase) do + def test_with_arguments(arg1, arg2) + end + end + + caught = true + catch(:invalid_test) do + tc.new(:test_with_arguments) + caught = false + end + check("Should have caught an invalid test when there are arguments", caught) + + caught = true + catch(:invalid_test) do + tc.new(:non_existent_test) + caught = false + end + check("Should have caught an invalid test when the method does not exist", caught) + end + + def setup + @tc_failure_error = Class.new(TestCase) do + def test_failure + assert_block("failure") { false } + end + def test_error + 1 / 0 + end + def test_nested_failure + nested + end + def nested + assert_block("nested"){false} + end + def return_passed? + return passed? + end + end + + def @tc_failure_error.name + "TC_FailureError" + end + end + + def test_add_failed_assertion + test_case = @tc_failure_error.new(:test_failure) + check("passed? should start out true", test_case.return_passed?) + result = TestResult.new + called = false + result.add_listener(TestResult::FAULT) { + | fault | + check("Should have a Failure", fault.instance_of?(Failure)) + check("The Failure should have the correct message", "failure" == fault.message) + check("The Failure should have the correct test_name (was <#{fault.test_name}>)", fault.test_name == "test_failure(TC_FailureError)") + r = /\A.*#{Regexp.escape(File.basename(__FILE__))}:\d+:in `test_failure'\Z/ + + location = fault.location + check("The location should be an array", location.kind_of?(Array)) + check("The location should have two lines (was: <#{location.inspect}>)", location.size == 2) + check("The Failure should have the correct location (was <#{location[0].inspect}>, expected <#{r.inspect}>)", r =~ location[0]) + called = true + } + progress = [] + test_case.run(result) { |*arguments| progress << arguments } + check("The failure should have triggered the listener", called) + check("The failure should have set passed?", !test_case.return_passed?) + check("The progress block should have been updated correctly", [[TestCase::STARTED, test_case.name], [TestCase::FINISHED, test_case.name]] == progress) + end + + def test_add_failure_nested + test_case = @tc_failure_error.new(:test_nested_failure) + check("passed? should start out true", test_case.return_passed?) + + result = TestResult.new + called = false + result.add_listener(TestResult::FAULT) { + | fault | + check("Should have a Failure", fault.instance_of?(Failure)) + check("The Failure should have the correct message", "nested" == fault.message) + check("The Failure should have the correct test_name (was <#{fault.test_name}>)", fault.test_name == "test_nested_failure(TC_FailureError)") + r = + + location = fault.location + check("The location should be an array", location.kind_of?(Array)) + check("The location should have the correct number of lines (was: <#{location.inspect}>)", location.size == 3) + check("The Failure should have the correct location (was <#{location[0].inspect}>)", /\A.*#{Regexp.escape(File.basename(__FILE__))}:\d+:in `nested'\Z/ =~ location[0]) + check("The Failure should have the correct location (was <#{location[1].inspect}>)", /\A.*#{Regexp.escape(File.basename(__FILE__))}:\d+:in `test_nested_failure'\Z/ =~ location[1]) + called = true + } + test_case.run(result){} + check("The failure should have triggered the listener", called) + end + + def test_add_error + test_case = @tc_failure_error.new(:test_error) + check("passed? should start out true", test_case.return_passed?) + result = TestResult.new + called = false + result.add_listener(TestResult::FAULT) { + | fault | + check("Should have a TestError", fault.instance_of?(Error)) + check("The Error should have the correct message", "ZeroDivisionError: divided by 0" == fault.message) + check("The Error should have the correct test_name", "test_error(TC_FailureError)" == fault.test_name) + check("The Error should have the correct exception", fault.exception.instance_of?(ZeroDivisionError)) + called = true + } + test_case.run(result) {} + check("The error should have triggered the listener", called) + check("The error should have set passed?", !test_case.return_passed?) + end + + def test_no_tests + suite = TestCase.suite + check("Should have a test suite", suite.instance_of?(TestSuite)) + check("Should have one test", suite.size == 1) + check("Should have the default test", suite.tests.first.name == "default_test(Test::Unit::TestCase)") + + result = TestResult.new + suite.run(result) {} + check("Should have had one test run", result.run_count == 1) + check("Should have had one test failure", result.failure_count == 1) + check("Should have had no errors", result.error_count == 0) + end + + def test_suite + tc = Class.new(TestCase) do + def test_succeed + assert_block {true} + end + def test_fail + assert_block {false} + end + def test_error + 1/0 + end + def dont_run + assert_block {true} + end + def test_dont_run(argument) + assert_block {true} + end + def test + assert_block {true} + end + end + + suite = tc.suite + check("Should have a test suite", suite.instance_of?(TestSuite)) + check("Should have three tests", suite.size == 3) + + result = TestResult.new + suite.run(result) {} + check("Should have had three test runs", result.run_count == 3) + check("Should have had one test failure", result.failure_count == 1) + check("Should have had one test error", result.error_count == 1) + end + + + def test_setup_teardown + tc = Class.new(TestCase) do + attr_reader(:setup_called, :teardown_called) + def initialize(test) + super(test) + @setup_called = false + @teardown_called = false + end + def setup + @setup_called = true + end + def teardown + @teardown_called = true + end + def test_succeed + assert_block {true} + end + def test_fail + assert_block {false} + end + def test_error + raise "Error!" + end + end + result = TestResult.new + + test = tc.new(:test_succeed) + test.run(result) {} + check("Should have called setup the correct number of times", test.setup_called) + check("Should have called teardown the correct number of times", test.teardown_called) + + test = tc.new(:test_fail) + test.run(result) {} + check("Should have called setup the correct number of times", test.setup_called) + check("Should have called teardown the correct number of times", test.teardown_called) + + test = tc.new(:test_error) + test.run(result) {} + check("Should have called setup the correct number of times", test.setup_called) + check("Should have called teardown the correct number of times", test.teardown_called) + + check("Should have had two test runs", result.run_count == 3) + check("Should have had a test failure", result.failure_count == 1) + check("Should have had a test error", result.error_count == 1) + end + + def test_assertion_failed_not_called + tc = Class.new(TestCase) do + def test_thing + raise AssertionFailedError.new + end + end + + suite = tc.suite + check("Should have one test", suite.size == 1) + result = TestResult.new + suite.run(result) {} + check("Should have had one test run", result.run_count == 1) + check("Should have had one assertion failure", result.failure_count == 1) + check("Should not have any assertion errors but had #{result.error_count}", result.error_count == 0) + end + + def test_equality + tc1 = Class.new(TestCase) do + def test_1 + end + def test_2 + end + end + + tc2 = Class.new(TestCase) do + def test_1 + end + end + + test1 = tc1.new('test_1') + test2 = tc1.new('test_1') + check("Should be equal", test1 == test2) + check("Should be equal", test2 == test1) + + test1 = tc1.new('test_2') + check("Should not be equal", test1 != test2) + check("Should not be equal", test2 != test1) + + test2 = tc1.new('test_2') + check("Should be equal", test1 == test2) + check("Should be equal", test2 == test1) + + test1 = tc1.new('test_1') + test2 = tc2.new('test_1') + check("Should not be equal", test1 != test2) + check("Should not be equal", test2 != test1) + + + check("Should not be equal", test1 != Object.new) + check("Should not be equal", Object.new != test1) + end + + def check(message, passed) + @_result.add_assertion + if ! passed + raise AssertionFailedError.new(message) + end + end + end + end +end diff --git a/ruby_1_8_5/test/testunit/test_testresult.rb b/ruby_1_8_5/test/testunit/test_testresult.rb new file mode 100644 index 0000000000..95d631a082 --- /dev/null +++ b/ruby_1_8_5/test/testunit/test_testresult.rb @@ -0,0 +1,104 @@ +# Author:: Nathaniel Talbott. +# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'test/unit/testcase' +require 'test/unit/testresult' + +module Test + module Unit + class TC_TestResult < TestCase + def setup + @my_result = TestResult.new + @my_result.add_assertion() + @my_result.add_failure("") + @my_result.add_error("") + end + def test_result_changed_notification + called1 = false + @my_result.add_listener( TestResult::CHANGED) { + |result| + assert_block("The result should be correct") { result == @my_result } + called1 = true + } + @my_result.add_assertion + assert_block("Should have been notified when the assertion happened") { called1 } + + called1, called2 = false, false + @my_result.add_listener( TestResult::CHANGED) { + |result| + assert_block("The result should be correct") { result == @my_result } + called2 = true + } + @my_result.add_assertion + assert_block("Both listeners should have been notified for a success") { called1 && called2 } + + called1, called2 = false, false + @my_result.add_failure("") + assert_block("Both listeners should have been notified for a failure") { called1 && called2 } + + called1, called2 = false, false + @my_result.add_error("") + assert_block("Both listeners should have been notified for an error") { called1 && called2 } + + called1, called2 = false, false + @my_result.add_run + assert_block("Both listeners should have been notified for a run") { called1 && called2 } + end + def test_fault_notification + called1 = false + fault = "fault" + @my_result.add_listener(TestResult::FAULT) { + | passed_fault | + assert_block("The fault should be correct") { passed_fault == fault } + called1 = true + } + + @my_result.add_assertion + assert_block("Should not have been notified when the assertion happened") { !called1 } + + @my_result.add_failure(fault) + assert_block("Should have been notified when the failure happened") { called1 } + + called1, called2 = false, false + @my_result.add_listener(TestResult::FAULT) { + | passed_fault | + assert_block("The fault should be correct") { passed_fault == fault } + called2 = true + } + + @my_result.add_assertion + assert_block("Neither listener should have been notified for a success") { !(called1 || called2) } + + called1, called2 = false, false + @my_result.add_failure(fault) + assert_block("Both listeners should have been notified for a failure") { called1 && called2 } + + called1, called2 = false, false + @my_result.add_error(fault) + assert_block("Both listeners should have been notified for an error") { called1 && called2 } + + called1, called2 = false, false + @my_result.add_run + assert_block("Neither listener should have been notified for a run") { !(called1 || called2) } + end + def test_passed? + result = TestResult.new + assert(result.passed?, "An empty result should have passed") + + result.add_assertion + assert(result.passed?, "Adding an assertion should not cause the result to not pass") + + result.add_run + assert(result.passed?, "Adding a run should not cause the result to not pass") + + result.add_failure("") + assert(!result.passed?, "Adding a failed assertion should cause the result to not pass") + + result = TestResult.new + result.add_error("") + assert(!result.passed?, "Adding an error should cause the result to not pass") + end + end + end +end diff --git a/ruby_1_8_5/test/testunit/test_testsuite.rb b/ruby_1_8_5/test/testunit/test_testsuite.rb new file mode 100644 index 0000000000..5f6631d3eb --- /dev/null +++ b/ruby_1_8_5/test/testunit/test_testsuite.rb @@ -0,0 +1,129 @@ +# Author:: Nathaniel Talbott. +# Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'test/unit' + +module Test + module Unit + class TC_TestSuite < TestCase + def setup + @testcase1 = Class.new(TestCase) do + def test_succeed1 + assert_block { true } + end + def test_fail + assert_block { false } + end + end + + @testcase2 = Class.new(TestCase) do + def test_succeed2 + assert_block { true } + end + def test_error + raise + end + end + end + + def test_add + s = TestSuite.new + assert_equal(s, s << self.class.new("test_add")) + end + + def test_delete + s = TestSuite.new + t1 = self.class.new("test_delete") + s << t1 + t2 = self.class.new("test_add") + s << t2 + assert_equal(t1, s.delete(t1)) + assert_nil(s.delete(t1)) + assert_equal(TestSuite.new << t2, s) + end + + def test_size + suite = TestSuite.new + suite2 = TestSuite.new + suite2 << self.class.new("test_size") + suite << suite2 + suite << self.class.new("test_size") + assert_equal(2, suite.size, "The count should be correct") + end + + def test_run + progress = [] + suite = @testcase1.suite + result = TestResult.new + suite.run(result) { |*values| progress << values } + + assert_equal(2, result.run_count, "Should have had four test runs") + assert_equal(1, result.failure_count, "Should have had one test failure") + assert_equal(0, result.error_count, "Should have had one test error") + assert_equal([[TestSuite::STARTED, suite.name], + [TestCase::STARTED, "test_fail(#{suite.name})"], + [TestCase::FINISHED, "test_fail(#{suite.name})"], + [TestCase::STARTED, "test_succeed1(#{suite.name})"], + [TestCase::FINISHED, "test_succeed1(#{suite.name})"], + [TestSuite::FINISHED, suite.name]], + progress, "Should have had the correct progress") + + suite = TestSuite.new + suite << @testcase1.suite + suite << @testcase2.suite + result = TestResult.new + progress = [] + suite.run(result) { |*values| progress << values } + + assert_equal(4, result.run_count, "Should have had four test runs") + assert_equal(1, result.failure_count, "Should have had one test failure") + assert_equal(1, result.error_count, "Should have had one test error") + assert_equal(14, progress.size, "Should have had the correct number of progress calls") + end + + def test_empty? + assert(TestSuite.new.empty?, "A new test suite should be empty?") + assert(!@testcase2.suite.empty?, "A test suite with tests should not be empty") + end + + def test_equality + suite1 = TestSuite.new + suite2 = TestSuite.new + assert_equal(suite1, suite2) + assert_equal(suite2, suite1) + + suite1 = TestSuite.new('name') + assert_not_equal(suite1, suite2) + assert_not_equal(suite2, suite1) + + suite2 = TestSuite.new('name') + assert_equal(suite1, suite2) + assert_equal(suite2, suite1) + + suite1 << 'test' + assert_not_equal(suite1, suite2) + assert_not_equal(suite2, suite1) + + suite2 << 'test' + assert_equal(suite1, suite2) + assert_equal(suite2, suite1) + + suite2 = Object.new + class << suite2 + def name + 'name' + end + def tests + ['test'] + end + end + assert_not_equal(suite1, suite2) + assert_not_equal(suite2, suite1) + + assert_not_equal(suite1, Object.new) + assert_not_equal(Object.new, suite1) + end + end + end +end diff --git a/ruby_1_8_5/test/testunit/util/test_backtracefilter.rb b/ruby_1_8_5/test/testunit/util/test_backtracefilter.rb new file mode 100644 index 0000000000..d4e40ea6ab --- /dev/null +++ b/ruby_1_8_5/test/testunit/util/test_backtracefilter.rb @@ -0,0 +1,41 @@ +require 'test/unit' + +require 'test/unit/util/backtracefilter' + +module Test::Unit::Util + class TestBacktraceFilter < Test::Unit::TestCase + include BacktraceFilter + + def test_filter_backtrace + backtrace = [%q{C:\some\old\path/test/unit/assertions.rb:44:in 'assert'}, + %q{tc_thing.rb:4:in 'a'}, + %q{tc_thing.rb:4:in 'test_stuff'}, + %q{C:\some\old\path/test/unit/testcase.rb:44:in 'send'}, + %q{C:\some\old\path\test\unit\testcase.rb:44:in 'run'}, + %q{C:\some\old\path\test\unit.rb:44:in 'run'}, + %q{tc_thing.rb:3}] + assert_equal(backtrace[1..2], filter_backtrace(backtrace, %q{C:\some\old\path\test\unit}), "Should filter out all TestUnit-specific lines") + +backtrace = [%q{tc_thing.rb:4:in 'a'}, + %q{tc_thing.rb:4:in 'test_stuff'}, + %q{tc_thing.rb:3}] + assert_equal(backtrace, filter_backtrace(backtrace, %q{C:\some\old\path\test\unit}), "Shouldn't filter too much") + + backtrace = [%q{C:\some\old\path/test/unit/assertions.rb:44:in 'assert'}, + %q{tc_thing.rb:4:in 'a'}, + %q{tc_thing.rb:4:in 'test_stuff'}, + %q{tc_thing.rb:3}] + assert_equal(backtrace[1..3], filter_backtrace(backtrace, %q{C:\some\old\path\test\unit}), "Should filter out all TestUnit-specific lines") + + backtrace = [%q{C:\some\old\path/test/unit/assertions.rb:44:in 'assert'}, + %q{C:\some\old\path/test/unit/testcase.rb:44:in 'send'}, + %q{C:\some\old\path\test\unit\testcase.rb:44:in 'run'}, + %q{C:\some\old\path\test\unit.rb:44:in 'run'}] + assert_equal(backtrace, filter_backtrace(backtrace, %q{C:\some\old\path\test\unit}), "Should filter out all TestUnit-specific lines") + end + + def test_nil_backtrace + assert_equal(["No backtrace"], filter_backtrace(nil)) + end + end +end diff --git a/ruby_1_8_5/test/testunit/util/test_observable.rb b/ruby_1_8_5/test/testunit/util/test_observable.rb new file mode 100644 index 0000000000..6cd10184f1 --- /dev/null +++ b/ruby_1_8_5/test/testunit/util/test_observable.rb @@ -0,0 +1,102 @@ +# Author:: Nathaniel Talbott. +# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'test/unit/util/observable' + +module Test + module Unit + module Util + class TC_Observable < TestCase + + class TF_Observable + include Observable + end + + def setup + @observable = TF_Observable.new + end + + def test_simple_observation + assert_raises(ArgumentError, "add_listener should throw an exception if no callback is supplied") do + @observable.add_listener(:property, "a") + end + + heard = false + callback = proc { heard = true } + assert_equal("a", @observable.add_listener(:property, "a", &callback), "add_listener should return the listener that was added") + + count = 0 + @observable.instance_eval do + count = notify_listeners(:property) + end + assert_equal(1, count, "notify_listeners should have returned the number of listeners that were notified") + assert(heard, "Should have heard the property changed") + + heard = false + assert_equal(callback, @observable.remove_listener(:property, "a"), "remove_listener should return the callback") + + count = 1 + @observable.instance_eval do + count = notify_listeners(:property) + end + assert_equal(0, count, "notify_listeners should have returned the number of listeners that were notified") + assert(!heard, "Should not have heard the property change") + end + + def test_value_observation + value = nil + @observable.add_listener(:property, "a") do |passed_value| + value = passed_value + end + count = 0 + @observable.instance_eval do + count = notify_listeners(:property, "stuff") + end + assert_equal(1, count, "Should have update the correct number of listeners") + assert_equal("stuff", value, "Should have received the value as an argument to the listener") + end + + def test_multiple_value_observation + values = [] + @observable.add_listener(:property, "a") do |first_value, second_value| + values = [first_value, second_value] + end + count = 0 + @observable.instance_eval do + count = notify_listeners(:property, "stuff", "more stuff") + end + assert_equal(1, count, "Should have update the correct number of listeners") + assert_equal(["stuff", "more stuff"], values, "Should have received the value as an argument to the listener") + end + + def test_add_remove_with_default_listener + assert_raises(ArgumentError, "add_listener should throw an exception if no callback is supplied") do + @observable.add_listener(:property) + end + + heard = false + callback = proc { heard = true } + assert_equal(callback, @observable.add_listener(:property, &callback), "add_listener should return the listener that was added") + + count = 0 + @observable.instance_eval do + count = notify_listeners(:property) + end + assert_equal(1, count, "notify_listeners should have returned the number of listeners that were notified") + assert(heard, "Should have heard the property changed") + + heard = false + assert_equal(callback, @observable.remove_listener(:property, callback), "remove_listener should return the callback") + + count = 1 + @observable.instance_eval do + count = notify_listeners(:property) + end + assert_equal(0, count, "notify_listeners should have returned the number of listeners that were notified") + assert(!heard, "Should not have heard the property change") + end + end + end + end +end diff --git a/ruby_1_8_5/test/testunit/util/test_procwrapper.rb b/ruby_1_8_5/test/testunit/util/test_procwrapper.rb new file mode 100644 index 0000000000..3e552c7711 --- /dev/null +++ b/ruby_1_8_5/test/testunit/util/test_procwrapper.rb @@ -0,0 +1,36 @@ +# Author:: Nathaniel Talbott. +# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved. +# License:: Ruby license. + +require 'test/unit' +require 'test/unit/util/procwrapper' + +module Test + module Unit + module Util + class TC_ProcWrapper < TestCase + def munge_proc(&a_proc) + return a_proc + end + def setup + @original = proc {} + @munged = munge_proc(&@original) + @wrapped_original = ProcWrapper.new(@original) + @wrapped_munged = ProcWrapper.new(@munged) + end + def test_wrapping + assert_same(@original, @wrapped_original.to_proc, "The wrapper should return what was wrapped") + end + def test_hashing + + assert_equal(@wrapped_original.hash, @wrapped_munged.hash, "The original and munged should have the same hash when wrapped") + assert_equal(@wrapped_original, @wrapped_munged, "The wrappers should be equivalent") + + a_hash = {@wrapped_original => @original} + assert(a_hash[@wrapped_original], "Should be able to access the wrapper in the hash") + assert_equal(a_hash[@wrapped_original], @original, "Should be able to access the wrapper in the hash") + end + end + end + end +end |