Forgot to remove test/unit tests

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19561 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ryan 2008-09-25 21:56:30 +00:00
parent 69b0d492ee
commit 3ceca921fc
11 changed files with 0 additions and 1798 deletions

View File

@ -1,411 +0,0 @@
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 => ex
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_raise(Errno::ENOENT) do
fs.chdir('bogus')
end
assert_raise(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_raise(Errno::ENOENT) do
fs.entries('z')
end
assert_raise(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_raise(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 = [@t1.suite]
subsuites = []
@c.collect_file('test_1.rb', subsuites, @c.find_test_cases)
assert_equal(expected, subsuites)
expected = [@t4.suite]
subsuites = []
@c.collect_file('t4.rb', subsuites, @c.find_test_cases)
assert_equal(expected, subsuites)
assert_raise(LoadError) do
@c.collect_file('tloaderr.rb', [], [])
end
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

View File

@ -1,98 +0,0 @@
# 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

View File

@ -1,543 +0,0 @@
# 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_nothing_fails {
assert_throws(0, "message") {
throw 0
}
}
obj = Object.new
check_nothing_fails {
assert_throws(obj, "message") {
throw obj
}
}
check_fails("message.\n<\"string\"> expected to be thrown but\n<\"string\"> was thrown.") {
assert_throws("string", "message") {
throw "string"
}
}
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
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

View File

@ -1,26 +0,0 @@
# 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

View File

@ -1,33 +0,0 @@
# 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

View File

@ -1,275 +0,0 @@
# 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

View File

@ -1,104 +0,0 @@
# 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

View File

@ -1,129 +0,0 @@
# 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

View File

@ -1,41 +0,0 @@
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

View File

@ -1,102 +0,0 @@
# 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_raise(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_raise(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

View File

@ -1,36 +0,0 @@
# 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