test-unit 1.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +5 -0
- data/Manifest.txt +48 -0
- data/README.txt +27 -0
- data/Rakefile +15 -0
- data/bin/testrb +5 -0
- data/lib/test/unit.rb +280 -0
- data/lib/test/unit/assertionfailederror.rb +14 -0
- data/lib/test/unit/assertions.rb +622 -0
- data/lib/test/unit/autorunner.rb +220 -0
- data/lib/test/unit/collector.rb +43 -0
- data/lib/test/unit/collector/dir.rb +108 -0
- data/lib/test/unit/collector/objectspace.rb +34 -0
- data/lib/test/unit/error.rb +56 -0
- data/lib/test/unit/failure.rb +51 -0
- data/lib/test/unit/testcase.rb +160 -0
- data/lib/test/unit/testresult.rb +80 -0
- data/lib/test/unit/testsuite.rb +76 -0
- data/lib/test/unit/ui/console/testrunner.rb +127 -0
- data/lib/test/unit/ui/fox/testrunner.rb +268 -0
- data/lib/test/unit/ui/gtk/testrunner.rb +416 -0
- data/lib/test/unit/ui/gtk2/testrunner.rb +465 -0
- data/lib/test/unit/ui/testrunnermediator.rb +68 -0
- data/lib/test/unit/ui/testrunnerutilities.rb +46 -0
- data/lib/test/unit/ui/tk/testrunner.rb +260 -0
- data/lib/test/unit/util/backtracefilter.rb +40 -0
- data/lib/test/unit/util/observable.rb +90 -0
- data/lib/test/unit/util/procwrapper.rb +48 -0
- data/lib/test/unit/version.rb +7 -0
- data/sample/adder.rb +13 -0
- data/sample/subtracter.rb +12 -0
- data/sample/tc_adder.rb +18 -0
- data/sample/tc_subtracter.rb +18 -0
- data/sample/ts_examples.rb +7 -0
- data/test/collector/test_dir.rb +406 -0
- data/test/collector/test_objectspace.rb +98 -0
- data/test/runit/test_assert.rb +402 -0
- data/test/runit/test_testcase.rb +91 -0
- data/test/runit/test_testresult.rb +144 -0
- data/test/runit/test_testsuite.rb +49 -0
- data/test/test_assertions.rb +528 -0
- data/test/test_error.rb +26 -0
- data/test/test_failure.rb +33 -0
- data/test/test_testcase.rb +275 -0
- data/test/test_testresult.rb +104 -0
- data/test/test_testsuite.rb +129 -0
- data/test/util/test_backtracefilter.rb +41 -0
- data/test/util/test_observable.rb +102 -0
- data/test/util/test_procwrapper.rb +36 -0
- metadata +128 -0
@@ -0,0 +1,91 @@
|
|
1
|
+
# Author:: Masaki Suketa.
|
2
|
+
# Adapted by:: Nathaniel Talbott.
|
3
|
+
# Copyright:: Copyright (c) Masaki Suketa. All rights reserved.
|
4
|
+
# Copyright:: Copyright (c) 2002 Nathaniel Talbott. All rights reserved.
|
5
|
+
# License:: Ruby license.
|
6
|
+
|
7
|
+
require 'rubyunit'
|
8
|
+
|
9
|
+
module RUNIT
|
10
|
+
class DummyError < StandardError
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestTestCase < RUNIT::TestCase
|
14
|
+
def setup
|
15
|
+
@dummy_testcase = Class.new(RUNIT::TestCase) do
|
16
|
+
def self.name
|
17
|
+
"DummyTestCase"
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :status, :dummy_called, :dummy2_called
|
21
|
+
|
22
|
+
def initialize(*arg)
|
23
|
+
super(*arg)
|
24
|
+
@status = 0
|
25
|
+
@dummy_called = false
|
26
|
+
@dummy2_called = false
|
27
|
+
end
|
28
|
+
|
29
|
+
def setup
|
30
|
+
@status = 1 if @status == 0
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_dummy
|
34
|
+
@status = 2 if @status == 1
|
35
|
+
@dummy_called = true
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_dummy2
|
39
|
+
@status = 2 if @status == 1
|
40
|
+
@dummy2_called = true
|
41
|
+
raise DummyError
|
42
|
+
end
|
43
|
+
|
44
|
+
def teardown
|
45
|
+
@status = 3 if @status == 2
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
@test1 = @dummy_testcase.new('test_dummy')
|
50
|
+
@test2 = @dummy_testcase.new('test_dummy2', 'TestCase')
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_name
|
54
|
+
assert_equal('DummyTestCase#test_dummy', @test1.name) # The second parameter to #initialize is ignored in emulation
|
55
|
+
assert_equal('DummyTestCase#test_dummy2', @test2.name)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_run
|
59
|
+
result = RUNIT::TestResult.new
|
60
|
+
@test1.run(result)
|
61
|
+
assert_equal(1, result.run_count)
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_s_suite
|
65
|
+
suite = @dummy_testcase.suite
|
66
|
+
assert_instance_of(RUNIT::TestSuite, suite)
|
67
|
+
assert_equal(2, suite.count_test_cases)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_teardown_err
|
71
|
+
suite = Class.new(RUNIT::TestCase) do
|
72
|
+
def test_foo
|
73
|
+
assert(false)
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_bar
|
77
|
+
assert(true)
|
78
|
+
end
|
79
|
+
|
80
|
+
def teardown
|
81
|
+
raise StandardError
|
82
|
+
end
|
83
|
+
end.suite
|
84
|
+
|
85
|
+
result = RUNIT::TestResult.new
|
86
|
+
suite.run(result)
|
87
|
+
assert_equal(2, result.error_size)
|
88
|
+
assert_equal(1, result.failure_size)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
# Author:: Masaki Suketa.
|
2
|
+
# Adapted by:: Nathaniel Talbott.
|
3
|
+
# Copyright:: Copyright (c) Masaki Suketa. All rights reserved.
|
4
|
+
# Copyright:: Copyright (c) 2002 Nathaniel Talbott. All rights reserved.
|
5
|
+
# License:: Ruby license.
|
6
|
+
|
7
|
+
require 'rubyunit'
|
8
|
+
|
9
|
+
module RUNIT
|
10
|
+
class TestTestResult < RUNIT::TestCase
|
11
|
+
def setup
|
12
|
+
@result = RUNIT::TestResult.new
|
13
|
+
|
14
|
+
@normal_suite = Class::new(RUNIT::TestCase) do
|
15
|
+
def test_1
|
16
|
+
assert(true)
|
17
|
+
assert(true)
|
18
|
+
end
|
19
|
+
end.suite
|
20
|
+
|
21
|
+
@failure_suite = Class::new(RUNIT::TestCase) do
|
22
|
+
def test_1
|
23
|
+
assert(true)
|
24
|
+
assert(false)
|
25
|
+
end
|
26
|
+
end.suite
|
27
|
+
|
28
|
+
@error_suite = Class::new(RUNIT::TestCase) do
|
29
|
+
def setup
|
30
|
+
raise ScriptError
|
31
|
+
end
|
32
|
+
def test_1
|
33
|
+
assert(true)
|
34
|
+
end
|
35
|
+
end.suite
|
36
|
+
|
37
|
+
@multi_failure_suite = Class::new(RUNIT::TestCase) do
|
38
|
+
def test1
|
39
|
+
assert(false)
|
40
|
+
end
|
41
|
+
def test2
|
42
|
+
assert(false)
|
43
|
+
end
|
44
|
+
def test3
|
45
|
+
assert(false)
|
46
|
+
end
|
47
|
+
end.suite
|
48
|
+
|
49
|
+
@with_error_suite = Class::new(RUNIT::TestCase) do
|
50
|
+
def test1
|
51
|
+
raise StandardError
|
52
|
+
end
|
53
|
+
end.suite
|
54
|
+
|
55
|
+
@multi_error_suite = Class::new(RUNIT::TestCase) do
|
56
|
+
def test1
|
57
|
+
raise StandardError
|
58
|
+
end
|
59
|
+
def test2
|
60
|
+
raise StandardError
|
61
|
+
end
|
62
|
+
def test3
|
63
|
+
raise StandardError
|
64
|
+
end
|
65
|
+
end.suite
|
66
|
+
|
67
|
+
@multi_suite = Class::new(RUNIT::TestCase) do
|
68
|
+
def test_1
|
69
|
+
assert(true)
|
70
|
+
assert(true)
|
71
|
+
end
|
72
|
+
def test_2
|
73
|
+
assert(true)
|
74
|
+
end
|
75
|
+
def test_3
|
76
|
+
assert(true)
|
77
|
+
assert(false)
|
78
|
+
assert(true)
|
79
|
+
end
|
80
|
+
end.suite
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_error_size
|
84
|
+
@normal_suite.run(@result)
|
85
|
+
assert_equal(0, @result.error_size)
|
86
|
+
@with_error_suite.run(@result)
|
87
|
+
assert_equal(1, @result.error_size)
|
88
|
+
@multi_error_suite.run(@result)
|
89
|
+
assert_equal(4, @result.error_size)
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_errors
|
93
|
+
@normal_suite.run(@result)
|
94
|
+
assert_equal(0, @result.errors.size)
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_failure_size
|
98
|
+
@normal_suite.run(@result)
|
99
|
+
assert_equal(0, @result.failure_size)
|
100
|
+
@failure_suite.run(@result)
|
101
|
+
assert_equal(1, @result.failure_size)
|
102
|
+
@multi_failure_suite.run(@result)
|
103
|
+
assert_equal(4, @result.failure_size)
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_failures
|
107
|
+
@normal_suite.run(@result)
|
108
|
+
assert_equal(0, @result.failures.size)
|
109
|
+
@failure_suite.run(@result)
|
110
|
+
assert_equal(1, @result.failures.size)
|
111
|
+
@multi_failure_suite.run(@result)
|
112
|
+
assert_equal(4, @result.failures.size)
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_run_no_exception
|
116
|
+
assert_no_exception {
|
117
|
+
@error_suite.run(@result)
|
118
|
+
}
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_run_asserts
|
122
|
+
@normal_suite.run(@result)
|
123
|
+
assert_equal(2, @result.run_asserts)
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_run_asserts2
|
127
|
+
@failure_suite.run(@result)
|
128
|
+
assert_equal(2, @result.run_asserts)
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_run_tests
|
132
|
+
assert_equal(0, @result.run_tests)
|
133
|
+
@normal_suite.run(@result)
|
134
|
+
assert_equal(1, @result.run_tests)
|
135
|
+
@multi_suite.run(@result)
|
136
|
+
assert_equal(4, @result.run_tests)
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_succeed?
|
140
|
+
@normal_suite.run(@result)
|
141
|
+
assert(@result.succeed?)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# Author:: Masaki Suketa.
|
2
|
+
# Adapted by:: Nathaniel Talbott.
|
3
|
+
# Copyright:: Copyright (c) Masaki Suketa. All rights reserved.
|
4
|
+
# Copyright:: Copyright (c) 2002 Nathaniel Talbott. All rights reserved.
|
5
|
+
# License:: Ruby license.
|
6
|
+
|
7
|
+
require 'rubyunit'
|
8
|
+
|
9
|
+
module RUNIT
|
10
|
+
class TestTestSuite < RUNIT::TestCase
|
11
|
+
def setup
|
12
|
+
@testsuite = RUNIT::TestSuite.new
|
13
|
+
@dummy_test = Class.new(RUNIT::TestCase) do
|
14
|
+
def test_foo
|
15
|
+
end
|
16
|
+
def test_bar
|
17
|
+
end
|
18
|
+
end
|
19
|
+
@dummy_empty_test = Class.new(RUNIT::TestCase){}
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_count_test_cases
|
23
|
+
assert_equal(0, @testsuite.count_test_cases)
|
24
|
+
|
25
|
+
@testsuite.add(@dummy_empty_test.suite)
|
26
|
+
assert_equal(0, @testsuite.count_test_cases)
|
27
|
+
|
28
|
+
@testsuite.add(@dummy_test.suite)
|
29
|
+
assert_equal(2, @testsuite.count_test_cases)
|
30
|
+
|
31
|
+
@testsuite.add(@dummy_test.suite)
|
32
|
+
assert_equal(4, @testsuite.count_test_cases)
|
33
|
+
|
34
|
+
dummytest_foo = @dummy_test.new('test_foo')
|
35
|
+
@testsuite.add(dummytest_foo)
|
36
|
+
assert_equal(5, @testsuite.count_test_cases)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_add
|
40
|
+
@testsuite.add(@dummy_empty_test.suite)
|
41
|
+
assert_equal(0, @testsuite.size)
|
42
|
+
assert_equal(0, @testsuite.count_test_cases)
|
43
|
+
|
44
|
+
@testsuite.add(@dummy_test.suite)
|
45
|
+
assert_equal(2, @testsuite.size)
|
46
|
+
assert_equal(2, @testsuite.count_test_cases)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,528 @@
|
|
1
|
+
# Author:: Nathaniel Talbott.
|
2
|
+
# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
|
3
|
+
# License:: Ruby license.
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
|
7
|
+
module Test
|
8
|
+
module Unit
|
9
|
+
class TC_Assertions < TestCase
|
10
|
+
def check(value, message="")
|
11
|
+
add_assertion
|
12
|
+
if (!value)
|
13
|
+
raise AssertionFailedError.new(message)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def check_assertions(expect_fail, expected_message="", return_value_expected=false)
|
18
|
+
@actual_assertion_count = 0
|
19
|
+
failed = true
|
20
|
+
actual_message = nil
|
21
|
+
@catch_assertions = true
|
22
|
+
return_value = nil
|
23
|
+
begin
|
24
|
+
return_value = yield
|
25
|
+
failed = false
|
26
|
+
rescue AssertionFailedError => error
|
27
|
+
actual_message = error.message
|
28
|
+
end
|
29
|
+
@catch_assertions = false
|
30
|
+
check(expect_fail == failed, (expect_fail ? "Should have failed, but didn't" : "Should not have failed, but did with message\n<#{actual_message}>"))
|
31
|
+
check(1 == @actual_assertion_count, "Should have made one assertion but made <#{@actual_assertion_count}>")
|
32
|
+
if (expect_fail)
|
33
|
+
case expected_message
|
34
|
+
when String
|
35
|
+
check(actual_message == expected_message, "Should have the correct message.\n<#{expected_message.inspect}> expected but was\n<#{actual_message.inspect}>")
|
36
|
+
when Regexp
|
37
|
+
check(actual_message =~ expected_message, "The message should match correctly.\n</#{expected_message.source}/> expected to match\n<#{actual_message.inspect}>")
|
38
|
+
else
|
39
|
+
check(false, "Incorrect expected message type in assert_nothing_failed")
|
40
|
+
end
|
41
|
+
else
|
42
|
+
if (!return_value_expected)
|
43
|
+
check(return_value.nil?, "Should not return a value but returned <#{return_value}>")
|
44
|
+
else
|
45
|
+
check(!return_value.nil?, "Should return a value")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
return return_value
|
49
|
+
end
|
50
|
+
|
51
|
+
def check_nothing_fails(return_value_expected=false, &proc)
|
52
|
+
check_assertions(false, "", return_value_expected, &proc)
|
53
|
+
end
|
54
|
+
|
55
|
+
def check_fails(expected_message="", &proc)
|
56
|
+
check_assertions(true, expected_message, &proc)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_assert_block
|
60
|
+
check_nothing_fails {
|
61
|
+
assert_block {true}
|
62
|
+
}
|
63
|
+
check_nothing_fails {
|
64
|
+
assert_block("successful assert_block") {true}
|
65
|
+
}
|
66
|
+
check_nothing_fails {
|
67
|
+
assert_block("successful assert_block") {true}
|
68
|
+
}
|
69
|
+
check_fails("assert_block failed.") {
|
70
|
+
assert_block {false}
|
71
|
+
}
|
72
|
+
check_fails("failed assert_block") {
|
73
|
+
assert_block("failed assert_block") {false}
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_assert
|
78
|
+
check_nothing_fails{assert("a")}
|
79
|
+
check_nothing_fails{assert(true)}
|
80
|
+
check_nothing_fails{assert(true, "successful assert")}
|
81
|
+
check_fails("<nil> is not true."){assert(nil)}
|
82
|
+
check_fails("<false> is not true."){assert(false)}
|
83
|
+
check_fails("failed assert.\n<false> is not true."){assert(false, "failed assert")}
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_assert_equal
|
87
|
+
check_nothing_fails {
|
88
|
+
assert_equal("string1", "string1")
|
89
|
+
}
|
90
|
+
check_nothing_fails {
|
91
|
+
assert_equal( "string1", "string1", "successful assert_equal")
|
92
|
+
}
|
93
|
+
check_nothing_fails {
|
94
|
+
assert_equal("string1", "string1", "successful assert_equal")
|
95
|
+
}
|
96
|
+
check_fails(%Q{<"string1"> expected but was\n<"string2">.}) {
|
97
|
+
assert_equal("string1", "string2")
|
98
|
+
}
|
99
|
+
check_fails(%Q{failed assert_equal.\n<"string1"> expected but was\n<"string2">.}) {
|
100
|
+
assert_equal("string1", "string2", "failed assert_equal")
|
101
|
+
}
|
102
|
+
check_fails(%Q{<"1"> expected but was\n<1>.}) do
|
103
|
+
assert_equal("1", 1)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_assert_raise
|
108
|
+
return_value = nil
|
109
|
+
check_nothing_fails(true) {
|
110
|
+
return_value = assert_raise(RuntimeError) {
|
111
|
+
raise "Error"
|
112
|
+
}
|
113
|
+
}
|
114
|
+
check(return_value.kind_of?(Exception), "Should have returned the exception from a successful assert_raise")
|
115
|
+
check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise")
|
116
|
+
check_nothing_fails(true) {
|
117
|
+
assert_raise(ArgumentError, "successful assert_raise") {
|
118
|
+
raise ArgumentError.new("Error")
|
119
|
+
}
|
120
|
+
}
|
121
|
+
check_nothing_fails(true) {
|
122
|
+
assert_raise(RuntimeError) {
|
123
|
+
raise "Error"
|
124
|
+
}
|
125
|
+
}
|
126
|
+
check_nothing_fails(true) {
|
127
|
+
assert_raise(RuntimeError, "successful assert_raise") {
|
128
|
+
raise "Error"
|
129
|
+
}
|
130
|
+
}
|
131
|
+
check_fails("<RuntimeError> exception expected but none was thrown.") {
|
132
|
+
assert_raise(RuntimeError) {
|
133
|
+
1 + 1
|
134
|
+
}
|
135
|
+
}
|
136
|
+
check_fails(%r{\Afailed assert_raise.\n<ArgumentError> exception expected but was\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
|
137
|
+
assert_raise(ArgumentError, "failed assert_raise") {
|
138
|
+
raise "Error"
|
139
|
+
}
|
140
|
+
}
|
141
|
+
check_fails("Should expect a class of exception, Object.\n<false> is not true.") {
|
142
|
+
assert_nothing_raised(Object) {
|
143
|
+
1 + 1
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
exceptions = [ArgumentError, TypeError]
|
148
|
+
modules = [Math, Comparable]
|
149
|
+
rescues = exceptions + modules
|
150
|
+
exceptions.each do |exc|
|
151
|
+
check_nothing_fails(true) {
|
152
|
+
return_value = assert_raise(*rescues) {
|
153
|
+
raise exc, "Error"
|
154
|
+
}
|
155
|
+
}
|
156
|
+
check(return_value.instance_of?(exc), "Should have returned #{exc} but was #{return_value.class}")
|
157
|
+
check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise")
|
158
|
+
end
|
159
|
+
modules.each do |mod|
|
160
|
+
check_nothing_fails(true) {
|
161
|
+
return_value = assert_raise(*rescues) {
|
162
|
+
raise Exception.new("Error").extend(mod)
|
163
|
+
}
|
164
|
+
}
|
165
|
+
check(mod === return_value, "Should have returned #{mod}")
|
166
|
+
check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise")
|
167
|
+
end
|
168
|
+
check_fails("<[ArgumentError, TypeError, Math, Comparable]> exception expected but none was thrown.") {
|
169
|
+
assert_raise(*rescues) {
|
170
|
+
1 + 1
|
171
|
+
}
|
172
|
+
}
|
173
|
+
check_fails(%r{\Afailed assert_raise.
|
174
|
+
<\[ArgumentError, TypeError\]> exception expected but was
|
175
|
+
Class: <RuntimeError>
|
176
|
+
Message: <"Error">
|
177
|
+
---Backtrace---
|
178
|
+
.+
|
179
|
+
---------------\Z}m) {
|
180
|
+
assert_raise(ArgumentError, TypeError, "failed assert_raise") {
|
181
|
+
raise "Error"
|
182
|
+
}
|
183
|
+
}
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_assert_instance_of
|
187
|
+
check_nothing_fails {
|
188
|
+
assert_instance_of(String, "string")
|
189
|
+
}
|
190
|
+
check_nothing_fails {
|
191
|
+
assert_instance_of(String, "string", "successful assert_instance_of")
|
192
|
+
}
|
193
|
+
check_nothing_fails {
|
194
|
+
assert_instance_of(String, "string", "successful assert_instance_of")
|
195
|
+
}
|
196
|
+
check_fails(%Q{<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
|
197
|
+
assert_instance_of(Hash, "string")
|
198
|
+
}
|
199
|
+
check_fails(%Q{failed assert_instance_of.\n<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
|
200
|
+
assert_instance_of(Hash, "string", "failed assert_instance_of")
|
201
|
+
}
|
202
|
+
end
|
203
|
+
|
204
|
+
def test_assert_nil
|
205
|
+
check_nothing_fails {
|
206
|
+
assert_nil(nil)
|
207
|
+
}
|
208
|
+
check_nothing_fails {
|
209
|
+
assert_nil(nil, "successful assert_nil")
|
210
|
+
}
|
211
|
+
check_nothing_fails {
|
212
|
+
assert_nil(nil, "successful assert_nil")
|
213
|
+
}
|
214
|
+
check_fails(%Q{<nil> expected but was\n<"string">.}) {
|
215
|
+
assert_nil("string")
|
216
|
+
}
|
217
|
+
check_fails(%Q{failed assert_nil.\n<nil> expected but was\n<"string">.}) {
|
218
|
+
assert_nil("string", "failed assert_nil")
|
219
|
+
}
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_assert_not_nil
|
223
|
+
check_nothing_fails{assert_not_nil(false)}
|
224
|
+
check_nothing_fails{assert_not_nil(false, "message")}
|
225
|
+
check_fails("<nil> expected to not be nil."){assert_not_nil(nil)}
|
226
|
+
check_fails("message.\n<nil> expected to not be nil.") {assert_not_nil(nil, "message")}
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_assert_kind_of
|
230
|
+
check_nothing_fails {
|
231
|
+
assert_kind_of(Module, Array)
|
232
|
+
}
|
233
|
+
check_nothing_fails {
|
234
|
+
assert_kind_of(Object, "string", "successful assert_kind_of")
|
235
|
+
}
|
236
|
+
check_nothing_fails {
|
237
|
+
assert_kind_of(Object, "string", "successful assert_kind_of")
|
238
|
+
}
|
239
|
+
check_nothing_fails {
|
240
|
+
assert_kind_of(Comparable, 1)
|
241
|
+
}
|
242
|
+
check_fails(%Q{<"string">\nexpected to be kind_of?\n<Class> but was\n<String>.}) {
|
243
|
+
assert_kind_of(Class, "string")
|
244
|
+
}
|
245
|
+
check_fails(%Q{failed assert_kind_of.\n<"string">\nexpected to be kind_of?\n<Class> but was\n<String>.}) {
|
246
|
+
assert_kind_of(Class, "string", "failed assert_kind_of")
|
247
|
+
}
|
248
|
+
end
|
249
|
+
|
250
|
+
def test_assert_match
|
251
|
+
check_nothing_fails {
|
252
|
+
assert_match(/strin./, "string")
|
253
|
+
}
|
254
|
+
check_nothing_fails {
|
255
|
+
assert_match("strin", "string")
|
256
|
+
}
|
257
|
+
check_nothing_fails {
|
258
|
+
assert_match(/strin./, "string", "successful assert_match")
|
259
|
+
}
|
260
|
+
check_nothing_fails {
|
261
|
+
assert_match(/strin./, "string", "successful assert_match")
|
262
|
+
}
|
263
|
+
check_fails(%Q{<"string"> expected to be =~\n</slin./>.}) {
|
264
|
+
assert_match(/slin./, "string")
|
265
|
+
}
|
266
|
+
check_fails(%Q{<"string"> expected to be =~\n</strin\\./>.}) {
|
267
|
+
assert_match("strin.", "string")
|
268
|
+
}
|
269
|
+
check_fails(%Q{failed assert_match.\n<"string"> expected to be =~\n</slin./>.}) {
|
270
|
+
assert_match(/slin./, "string", "failed assert_match")
|
271
|
+
}
|
272
|
+
end
|
273
|
+
|
274
|
+
def test_assert_same
|
275
|
+
thing = "thing"
|
276
|
+
check_nothing_fails {
|
277
|
+
assert_same(thing, thing)
|
278
|
+
}
|
279
|
+
check_nothing_fails {
|
280
|
+
assert_same(thing, thing, "successful assert_same")
|
281
|
+
}
|
282
|
+
check_nothing_fails {
|
283
|
+
assert_same(thing, thing, "successful assert_same")
|
284
|
+
}
|
285
|
+
thing2 = "thing"
|
286
|
+
check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
|
287
|
+
assert_same(thing, thing2)
|
288
|
+
}
|
289
|
+
check_fails(%Q{failed assert_same.\n<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
|
290
|
+
assert_same(thing, thing2, "failed assert_same")
|
291
|
+
}
|
292
|
+
end
|
293
|
+
|
294
|
+
def test_assert_nothing_raised
|
295
|
+
check_nothing_fails {
|
296
|
+
assert_nothing_raised {
|
297
|
+
1 + 1
|
298
|
+
}
|
299
|
+
}
|
300
|
+
check_nothing_fails {
|
301
|
+
assert_nothing_raised("successful assert_nothing_raised") {
|
302
|
+
1 + 1
|
303
|
+
}
|
304
|
+
}
|
305
|
+
check_nothing_fails {
|
306
|
+
assert_nothing_raised("successful assert_nothing_raised") {
|
307
|
+
1 + 1
|
308
|
+
}
|
309
|
+
}
|
310
|
+
check_nothing_fails {
|
311
|
+
begin
|
312
|
+
assert_nothing_raised(RuntimeError, StandardError, Comparable, "successful assert_nothing_raised") {
|
313
|
+
raise ZeroDivisionError.new("ArgumentError")
|
314
|
+
}
|
315
|
+
rescue ZeroDivisionError
|
316
|
+
end
|
317
|
+
}
|
318
|
+
check_fails("Should expect a class of exception, Object.\n<false> is not true.") {
|
319
|
+
assert_nothing_raised(Object) {
|
320
|
+
1 + 1
|
321
|
+
}
|
322
|
+
}
|
323
|
+
check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
|
324
|
+
assert_nothing_raised {
|
325
|
+
raise "Error"
|
326
|
+
}
|
327
|
+
}
|
328
|
+
check_fails(%r{\Afailed assert_nothing_raised\.\nException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
|
329
|
+
assert_nothing_raised("failed assert_nothing_raised") {
|
330
|
+
raise "Error"
|
331
|
+
}
|
332
|
+
}
|
333
|
+
check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
|
334
|
+
assert_nothing_raised(StandardError, RuntimeError) {
|
335
|
+
raise "Error"
|
336
|
+
}
|
337
|
+
}
|
338
|
+
check_fails("Failure.") do
|
339
|
+
assert_nothing_raised do
|
340
|
+
flunk("Failure")
|
341
|
+
end
|
342
|
+
end
|
343
|
+
end
|
344
|
+
|
345
|
+
def test_flunk
|
346
|
+
check_fails("Flunked.") {
|
347
|
+
flunk
|
348
|
+
}
|
349
|
+
check_fails("flunk message.") {
|
350
|
+
flunk("flunk message")
|
351
|
+
}
|
352
|
+
end
|
353
|
+
|
354
|
+
def test_assert_not_same
|
355
|
+
thing = "thing"
|
356
|
+
thing2 = "thing"
|
357
|
+
check_nothing_fails {
|
358
|
+
assert_not_same(thing, thing2)
|
359
|
+
}
|
360
|
+
check_nothing_fails {
|
361
|
+
assert_not_same(thing, thing2, "message")
|
362
|
+
}
|
363
|
+
check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
|
364
|
+
assert_not_same(thing, thing)
|
365
|
+
}
|
366
|
+
check_fails(%Q{message.\n<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
|
367
|
+
assert_not_same(thing, thing, "message")
|
368
|
+
}
|
369
|
+
end
|
370
|
+
|
371
|
+
def test_assert_not_equal
|
372
|
+
check_nothing_fails {
|
373
|
+
assert_not_equal("string1", "string2")
|
374
|
+
}
|
375
|
+
check_nothing_fails {
|
376
|
+
assert_not_equal("string1", "string2", "message")
|
377
|
+
}
|
378
|
+
check_fails(%Q{<"string"> expected to be != to\n<"string">.}) {
|
379
|
+
assert_not_equal("string", "string")
|
380
|
+
}
|
381
|
+
check_fails(%Q{message.\n<"string"> expected to be != to\n<"string">.}) {
|
382
|
+
assert_not_equal("string", "string", "message")
|
383
|
+
}
|
384
|
+
end
|
385
|
+
|
386
|
+
def test_assert_no_match
|
387
|
+
check_nothing_fails{assert_no_match(/sling/, "string")}
|
388
|
+
check_nothing_fails{assert_no_match(/sling/, "string", "message")}
|
389
|
+
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
|
390
|
+
assert_no_match("asdf", "asdf")
|
391
|
+
end
|
392
|
+
check_fails(%Q{</string/> expected to not match\n<"string">.}) do
|
393
|
+
assert_no_match(/string/, "string")
|
394
|
+
end
|
395
|
+
check_fails(%Q{message.\n</string/> expected to not match\n<"string">.}) do
|
396
|
+
assert_no_match(/string/, "string", "message")
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
def test_assert_throws
|
401
|
+
check_nothing_fails {
|
402
|
+
assert_throws(:thing, "message") {
|
403
|
+
throw :thing
|
404
|
+
}
|
405
|
+
}
|
406
|
+
check_fails("message.\n<:thing> expected to be thrown but\n<:thing2> was thrown.") {
|
407
|
+
assert_throws(:thing, "message") {
|
408
|
+
throw :thing2
|
409
|
+
}
|
410
|
+
}
|
411
|
+
check_fails("message.\n<:thing> should have been thrown.") {
|
412
|
+
assert_throws(:thing, "message") {
|
413
|
+
1 + 1
|
414
|
+
}
|
415
|
+
}
|
416
|
+
end
|
417
|
+
|
418
|
+
def test_assert_nothing_thrown
|
419
|
+
check_nothing_fails {
|
420
|
+
assert_nothing_thrown("message") {
|
421
|
+
1 + 1
|
422
|
+
}
|
423
|
+
}
|
424
|
+
check_fails("message.\n<:thing> was thrown when nothing was expected.") {
|
425
|
+
assert_nothing_thrown("message") {
|
426
|
+
throw :thing
|
427
|
+
}
|
428
|
+
}
|
429
|
+
end
|
430
|
+
|
431
|
+
def test_assert_operator
|
432
|
+
check_nothing_fails {
|
433
|
+
assert_operator("thing", :==, "thing", "message")
|
434
|
+
}
|
435
|
+
check_fails(%Q{<0.15>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to?(:to_str).}) do
|
436
|
+
assert_operator("thing", 0.15, "thing")
|
437
|
+
end
|
438
|
+
check_fails(%Q{message.\n<"thing1"> expected to be\n==\n<"thing2">.}) {
|
439
|
+
assert_operator("thing1", :==, "thing2", "message")
|
440
|
+
}
|
441
|
+
end
|
442
|
+
|
443
|
+
def test_assert_respond_to
|
444
|
+
check_nothing_fails {
|
445
|
+
assert_respond_to("thing", :to_s, "message")
|
446
|
+
}
|
447
|
+
check_nothing_fails {
|
448
|
+
assert_respond_to("thing", "to_s", "message")
|
449
|
+
}
|
450
|
+
check_fails("<0.15>\ngiven as the method name argument to #assert_respond_to must be a Symbol or #respond_to?(:to_str).") {
|
451
|
+
assert_respond_to("thing", 0.15)
|
452
|
+
}
|
453
|
+
check_fails("message.\n<:symbol>\nof type <Symbol>\nexpected to respond_to?<:non_existent>.") {
|
454
|
+
assert_respond_to(:symbol, :non_existent, "message")
|
455
|
+
}
|
456
|
+
end
|
457
|
+
|
458
|
+
def test_assert_in_delta
|
459
|
+
check_nothing_fails {
|
460
|
+
assert_in_delta(1.4, 1.4, 0)
|
461
|
+
}
|
462
|
+
check_nothing_fails {
|
463
|
+
assert_in_delta(0.5, 0.4, 0.1, "message")
|
464
|
+
}
|
465
|
+
check_nothing_fails {
|
466
|
+
float_thing = Object.new
|
467
|
+
def float_thing.to_f
|
468
|
+
0.2
|
469
|
+
end
|
470
|
+
assert_in_delta(0.1, float_thing, 0.1)
|
471
|
+
}
|
472
|
+
check_fails("message.\n<0.5> and\n<0.4> expected to be within\n<0.05> of each other.") {
|
473
|
+
assert_in_delta(0.5, 0.4, 0.05, "message")
|
474
|
+
}
|
475
|
+
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>.}) {
|
476
|
+
assert_in_delta(Object.new, 0.4, 0.1)
|
477
|
+
}
|
478
|
+
check_fails("The delta should not be negative.\n<-0.1> expected to be\n>=\n<0.0>.") {
|
479
|
+
assert_in_delta(0.5, 0.4, -0.1, "message")
|
480
|
+
}
|
481
|
+
end
|
482
|
+
|
483
|
+
def test_assert_send
|
484
|
+
object = Object.new
|
485
|
+
class << object
|
486
|
+
private
|
487
|
+
def return_argument(argument, bogus)
|
488
|
+
return argument
|
489
|
+
end
|
490
|
+
end
|
491
|
+
check_nothing_fails {
|
492
|
+
assert_send([object, :return_argument, true, "bogus"], "message")
|
493
|
+
}
|
494
|
+
check_fails(%r{\Amessage\.\n<.+> expected to respond to\n<return_argument\(\[false, "bogus"\]\)> with a true value.\Z}) {
|
495
|
+
assert_send([object, :return_argument, false, "bogus"], "message")
|
496
|
+
}
|
497
|
+
end
|
498
|
+
|
499
|
+
def test_condition_invariant
|
500
|
+
object = Object.new
|
501
|
+
def object.inspect
|
502
|
+
@changed = true
|
503
|
+
end
|
504
|
+
def object.==(other)
|
505
|
+
@changed ||= false
|
506
|
+
return (!@changed)
|
507
|
+
end
|
508
|
+
check_nothing_fails {
|
509
|
+
assert_equal(object, object, "message")
|
510
|
+
}
|
511
|
+
end
|
512
|
+
|
513
|
+
def add_failure(message, location=caller)
|
514
|
+
if (!@catch_assertions)
|
515
|
+
super
|
516
|
+
end
|
517
|
+
end
|
518
|
+
|
519
|
+
def add_assertion
|
520
|
+
if (!@catch_assertions)
|
521
|
+
super
|
522
|
+
else
|
523
|
+
@actual_assertion_count += 1
|
524
|
+
end
|
525
|
+
end
|
526
|
+
end
|
527
|
+
end
|
528
|
+
end
|