rspec-unit 0.9.22
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/Gemfile +14 -0
- data/Gemfile.lock +34 -0
- data/LICENSE +20 -0
- data/README.md +121 -0
- data/Rakefile +49 -0
- data/VERSION.yml +4 -0
- data/lib/rspec-unit.rb +1 -0
- data/lib/rspec/unit.rb +3 -0
- data/lib/rspec/unit/assertions.rb +651 -0
- data/lib/rspec/unit/test_case.rb +188 -0
- data/spec/assertions_spec.rb +528 -0
- data/spec/spec_helper.rb +63 -0
- data/spec/test_case_spec.rb +403 -0
- metadata +96 -0
@@ -0,0 +1,188 @@
|
|
1
|
+
require 'rspec/unit/assertions'
|
2
|
+
|
3
|
+
module RSpec
|
4
|
+
|
5
|
+
module Core
|
6
|
+
class ExampleGroup
|
7
|
+
include ::RSpec::Unit::Assertions
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module Unit
|
12
|
+
class AssertionFailedError < RSpec::Matchers::MatcherError
|
13
|
+
end
|
14
|
+
|
15
|
+
class TestCase < RSpec::Core::ExampleGroup
|
16
|
+
|
17
|
+
TEST_METHOD_PATTERN = /^test_/
|
18
|
+
|
19
|
+
alias_example_to :test, :test_unit => true
|
20
|
+
|
21
|
+
def self.inherited(klass)
|
22
|
+
super
|
23
|
+
|
24
|
+
install_setup_and_teardown(klass)
|
25
|
+
|
26
|
+
klass.set_it_up(test_case_name(klass), {:caller => caller})
|
27
|
+
klass.metadata[:example_group][:test_unit] = true
|
28
|
+
children << klass
|
29
|
+
world.example_groups << klass
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.test_case_info(options)
|
33
|
+
metadata[:example_group].update(options)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.test_info(options)
|
37
|
+
@_metadata_for_next = options
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.method_added(id)
|
41
|
+
name = id.to_s
|
42
|
+
if test_method?(name)
|
43
|
+
caller_lines[name] = caller
|
44
|
+
test_method_metadata[name] = @_metadata_for_next
|
45
|
+
@_metadata_for_next = nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.examples
|
50
|
+
@tc_examples ||= ExamplesCollection.new(self, super)
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.ancestors
|
54
|
+
super[0..-2]
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.test_case_name(klass)
|
58
|
+
class_name = klass.name
|
59
|
+
(class_name.nil? || class_name.empty?) ? '<Anonymous TestCase>' : class_name
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.caller_lines
|
63
|
+
@_caller_lines ||= {}
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.test_method_metadata
|
67
|
+
@_test_method_metadata ||= {}
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.install_setup_and_teardown(klass)
|
71
|
+
# Only do this for direct descendants, because test/unit chains
|
72
|
+
# fixtures through explicit calls to super.
|
73
|
+
if self == ::RSpec::Unit::TestCase
|
74
|
+
klass.class_eval do
|
75
|
+
before {setup}
|
76
|
+
after {teardown}
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.test_method?(method_name)
|
82
|
+
method_name =~ TEST_METHOD_PATTERN &&
|
83
|
+
public_method_defined?(method_name) &&
|
84
|
+
(-1..0).include?(instance_method(method_name).arity)
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.test_methods
|
88
|
+
public_instance_methods(true).select{|m| test_method?(m)}
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.number_of_tests
|
92
|
+
test_methods.size
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.tests
|
96
|
+
@tests ||= test_methods.sort.map do |m|
|
97
|
+
meta = (test_method_metadata[m] || {}).merge({:caller => caller_lines[m],
|
98
|
+
:full_description => "#{display_name}##{m}",
|
99
|
+
:test_unit => true})
|
100
|
+
Core::Example.new(self, m, meta, proc{execute(m)})
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class <<self
|
105
|
+
private :test_case_name, :caller_lines, :test_method_metadata,
|
106
|
+
:install_setup_and_teardown, :test_method?, :test_methods,
|
107
|
+
:number_of_tests, :tests
|
108
|
+
end
|
109
|
+
|
110
|
+
def initialize
|
111
|
+
@test_passed = true
|
112
|
+
super
|
113
|
+
end
|
114
|
+
|
115
|
+
def setup
|
116
|
+
end
|
117
|
+
|
118
|
+
def teardown
|
119
|
+
end
|
120
|
+
|
121
|
+
def execute(method)
|
122
|
+
begin
|
123
|
+
send(method.to_sym)
|
124
|
+
rescue
|
125
|
+
@test_passed = false
|
126
|
+
raise
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
private
|
131
|
+
|
132
|
+
def passed?
|
133
|
+
@test_passed
|
134
|
+
end
|
135
|
+
|
136
|
+
class ExamplesCollection
|
137
|
+
include Enumerable
|
138
|
+
|
139
|
+
attr_accessor :testcase, :core_examples
|
140
|
+
|
141
|
+
def initialize(testcase, core_examples)
|
142
|
+
@testcase, @core_examples = testcase, core_examples
|
143
|
+
end
|
144
|
+
|
145
|
+
def tests
|
146
|
+
testcase.send(:tests)
|
147
|
+
end
|
148
|
+
|
149
|
+
def number_of_tests
|
150
|
+
testcase.send(:number_of_tests)
|
151
|
+
end
|
152
|
+
|
153
|
+
def size
|
154
|
+
core_examples.size + number_of_tests
|
155
|
+
end
|
156
|
+
|
157
|
+
def empty?
|
158
|
+
core_examples.empty? && number_of_tests == 0
|
159
|
+
end
|
160
|
+
|
161
|
+
def last
|
162
|
+
tests.last || core_examples.last
|
163
|
+
end
|
164
|
+
|
165
|
+
def <<(example)
|
166
|
+
core_examples << example
|
167
|
+
end
|
168
|
+
|
169
|
+
def each
|
170
|
+
core_examples.each{|ex| yield(ex)}
|
171
|
+
tests.each{|test| yield(test)}
|
172
|
+
end
|
173
|
+
|
174
|
+
def to_ary
|
175
|
+
core_examples + tests
|
176
|
+
end
|
177
|
+
|
178
|
+
def uniq
|
179
|
+
to_ary.uniq
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
RSpec.world.example_groups.delete(::RSpec::Unit::TestCase)
|
186
|
+
end
|
187
|
+
|
188
|
+
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 File.expand_path(File.dirname(__FILE__) + "/spec_helper")
|
6
|
+
|
7
|
+
# This is borrowed with minor changes from test/unit. I'm leaving it in test/unit format
|
8
|
+
# rather than converting to rspec-style. ---glv
|
9
|
+
class TC_Assertions < RSpec::Unit::TestCase
|
10
|
+
AssertionFailedError = RSpec::Unit::AssertionFailedError
|
11
|
+
|
12
|
+
def check(value, message="")
|
13
|
+
add_assertion
|
14
|
+
if (!value)
|
15
|
+
raise AssertionFailedError.new(message)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def check_assertions(expect_fail, expected_message="", return_value_expected=false)
|
20
|
+
@actual_assertion_count = 0
|
21
|
+
failed = true
|
22
|
+
actual_message = nil
|
23
|
+
@catch_assertions = true
|
24
|
+
return_value = nil
|
25
|
+
begin
|
26
|
+
return_value = yield
|
27
|
+
failed = false
|
28
|
+
rescue AssertionFailedError => error
|
29
|
+
actual_message = error.message
|
30
|
+
end
|
31
|
+
@catch_assertions = false
|
32
|
+
check(expect_fail == failed, (expect_fail ? "Should have failed, but didn't" : "Should not have failed, but did with message\n<#{actual_message}>"))
|
33
|
+
check(1 == @actual_assertion_count, "Should have made one assertion but made <#{@actual_assertion_count}>")
|
34
|
+
if (expect_fail)
|
35
|
+
case expected_message
|
36
|
+
when String
|
37
|
+
check(actual_message == expected_message, "Should have the correct message.\n<#{expected_message.inspect}> expected but was\n<#{actual_message.inspect}>")
|
38
|
+
when Regexp
|
39
|
+
check(actual_message =~ expected_message, "The message should match correctly.\n</#{expected_message.source}/> expected to match\n<#{actual_message.inspect}>")
|
40
|
+
else
|
41
|
+
check(false, "Incorrect expected message type in assert_nothing_failed")
|
42
|
+
end
|
43
|
+
else
|
44
|
+
if (!return_value_expected)
|
45
|
+
check(return_value.nil?, "Should not return a value but returned <#{return_value}>")
|
46
|
+
else
|
47
|
+
check(!return_value.nil?, "Should return a value")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
return return_value
|
51
|
+
end
|
52
|
+
|
53
|
+
def check_nothing_fails(return_value_expected=false, &proc)
|
54
|
+
check_assertions(false, "", return_value_expected, &proc)
|
55
|
+
end
|
56
|
+
|
57
|
+
def check_fails(expected_message="", &proc)
|
58
|
+
check_assertions(true, expected_message, &proc)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_assert_block
|
62
|
+
check_nothing_fails {
|
63
|
+
assert_block {true}
|
64
|
+
}
|
65
|
+
check_nothing_fails {
|
66
|
+
assert_block("successful assert_block") {true}
|
67
|
+
}
|
68
|
+
check_nothing_fails {
|
69
|
+
assert_block("successful assert_block") {true}
|
70
|
+
}
|
71
|
+
check_fails("assert_block failed.") {
|
72
|
+
assert_block {false}
|
73
|
+
}
|
74
|
+
check_fails("failed assert_block") {
|
75
|
+
assert_block("failed assert_block") {false}
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_assert
|
80
|
+
check_nothing_fails{assert("a")}
|
81
|
+
check_nothing_fails{assert(true)}
|
82
|
+
check_nothing_fails{assert(true, "successful assert")}
|
83
|
+
check_fails("<nil> is not true."){assert(nil)}
|
84
|
+
check_fails("<false> is not true."){assert(false)}
|
85
|
+
check_fails("failed assert.\n<false> is not true."){assert(false, "failed assert")}
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_assert_equal
|
89
|
+
check_nothing_fails {
|
90
|
+
assert_equal("string1", "string1")
|
91
|
+
}
|
92
|
+
check_nothing_fails {
|
93
|
+
assert_equal( "string1", "string1", "successful assert_equal")
|
94
|
+
}
|
95
|
+
check_nothing_fails {
|
96
|
+
assert_equal("string1", "string1", "successful assert_equal")
|
97
|
+
}
|
98
|
+
check_fails(%Q{<"string1"> expected but was\n<"string2">.}) {
|
99
|
+
assert_equal("string1", "string2")
|
100
|
+
}
|
101
|
+
check_fails(%Q{failed assert_equal.\n<"string1"> expected but was\n<"string2">.}) {
|
102
|
+
assert_equal("string1", "string2", "failed assert_equal")
|
103
|
+
}
|
104
|
+
check_fails(%Q{<"1"> expected but was\n<1>.}) do
|
105
|
+
assert_equal("1", 1)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_assert_raise
|
110
|
+
return_value = nil
|
111
|
+
check_nothing_fails(true) {
|
112
|
+
return_value = assert_raise(RuntimeError) {
|
113
|
+
raise "Error"
|
114
|
+
}
|
115
|
+
}
|
116
|
+
check(return_value.kind_of?(Exception), "Should have returned the exception from a successful assert_raise")
|
117
|
+
check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise")
|
118
|
+
check_nothing_fails(true) {
|
119
|
+
assert_raise(ArgumentError, "successful assert_raise") {
|
120
|
+
raise ArgumentError.new("Error")
|
121
|
+
}
|
122
|
+
}
|
123
|
+
check_nothing_fails(true) {
|
124
|
+
assert_raise(RuntimeError) {
|
125
|
+
raise "Error"
|
126
|
+
}
|
127
|
+
}
|
128
|
+
check_nothing_fails(true) {
|
129
|
+
assert_raise(RuntimeError, "successful assert_raise") {
|
130
|
+
raise "Error"
|
131
|
+
}
|
132
|
+
}
|
133
|
+
check_fails("<RuntimeError> exception expected but none was thrown.") {
|
134
|
+
assert_raise(RuntimeError) {
|
135
|
+
1 + 1
|
136
|
+
}
|
137
|
+
}
|
138
|
+
check_fails(%r{\Afailed assert_raise.\n<ArgumentError> exception expected but was\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
|
139
|
+
assert_raise(ArgumentError, "failed assert_raise") {
|
140
|
+
raise "Error"
|
141
|
+
}
|
142
|
+
}
|
143
|
+
check_fails("Should expect a class of exception, Object.\n<false> is not true.") {
|
144
|
+
assert_nothing_raised(Object) {
|
145
|
+
1 + 1
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
exceptions = [ArgumentError, TypeError]
|
150
|
+
modules = [Math, Comparable]
|
151
|
+
rescues = exceptions + modules
|
152
|
+
exceptions.each do |exc|
|
153
|
+
check_nothing_fails(true) {
|
154
|
+
return_value = assert_raise(*rescues) {
|
155
|
+
raise exc, "Error"
|
156
|
+
}
|
157
|
+
}
|
158
|
+
check(return_value.instance_of?(exc), "Should have returned #{exc} but was #{return_value.class}")
|
159
|
+
check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise")
|
160
|
+
end
|
161
|
+
modules.each do |mod|
|
162
|
+
check_nothing_fails(true) {
|
163
|
+
return_value = assert_raise(*rescues) {
|
164
|
+
raise Exception.new("Error").extend(mod)
|
165
|
+
}
|
166
|
+
}
|
167
|
+
check(mod === return_value, "Should have returned #{mod}")
|
168
|
+
check(return_value.message == "Error", "Should have returned the correct exception from a successful assert_raise")
|
169
|
+
end
|
170
|
+
check_fails("<[ArgumentError, TypeError, Math, Comparable]> exception expected but none was thrown.") {
|
171
|
+
assert_raise(*rescues) {
|
172
|
+
1 + 1
|
173
|
+
}
|
174
|
+
}
|
175
|
+
check_fails(%r{\Afailed assert_raise.
|
176
|
+
<\[ArgumentError, TypeError\]> exception expected but was
|
177
|
+
Class: <RuntimeError>
|
178
|
+
Message: <"Error">
|
179
|
+
---Backtrace---
|
180
|
+
.+
|
181
|
+
---------------\Z}m) {
|
182
|
+
assert_raise(ArgumentError, TypeError, "failed assert_raise") {
|
183
|
+
raise "Error"
|
184
|
+
}
|
185
|
+
}
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_assert_instance_of
|
189
|
+
check_nothing_fails {
|
190
|
+
assert_instance_of(String, "string")
|
191
|
+
}
|
192
|
+
check_nothing_fails {
|
193
|
+
assert_instance_of(String, "string", "successful assert_instance_of")
|
194
|
+
}
|
195
|
+
check_nothing_fails {
|
196
|
+
assert_instance_of(String, "string", "successful assert_instance_of")
|
197
|
+
}
|
198
|
+
check_fails(%Q{<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
|
199
|
+
assert_instance_of(Hash, "string")
|
200
|
+
}
|
201
|
+
check_fails(%Q{failed assert_instance_of.\n<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
|
202
|
+
assert_instance_of(Hash, "string", "failed assert_instance_of")
|
203
|
+
}
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_assert_nil
|
207
|
+
check_nothing_fails {
|
208
|
+
assert_nil(nil)
|
209
|
+
}
|
210
|
+
check_nothing_fails {
|
211
|
+
assert_nil(nil, "successful assert_nil")
|
212
|
+
}
|
213
|
+
check_nothing_fails {
|
214
|
+
assert_nil(nil, "successful assert_nil")
|
215
|
+
}
|
216
|
+
check_fails(%Q{<nil> expected but was\n<"string">.}) {
|
217
|
+
assert_nil("string")
|
218
|
+
}
|
219
|
+
check_fails(%Q{failed assert_nil.\n<nil> expected but was\n<"string">.}) {
|
220
|
+
assert_nil("string", "failed assert_nil")
|
221
|
+
}
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_assert_not_nil
|
225
|
+
check_nothing_fails{assert_not_nil(false)}
|
226
|
+
check_nothing_fails{assert_not_nil(false, "message")}
|
227
|
+
check_fails("<nil> expected to not be nil."){assert_not_nil(nil)}
|
228
|
+
check_fails("message.\n<nil> expected to not be nil.") {assert_not_nil(nil, "message")}
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_assert_kind_of
|
232
|
+
check_nothing_fails {
|
233
|
+
assert_kind_of(Module, Array)
|
234
|
+
}
|
235
|
+
check_nothing_fails {
|
236
|
+
assert_kind_of(Object, "string", "successful assert_kind_of")
|
237
|
+
}
|
238
|
+
check_nothing_fails {
|
239
|
+
assert_kind_of(Object, "string", "successful assert_kind_of")
|
240
|
+
}
|
241
|
+
check_nothing_fails {
|
242
|
+
assert_kind_of(Comparable, 1)
|
243
|
+
}
|
244
|
+
check_fails(%Q{<"string">\nexpected to be kind_of?\n<Class> but was\n<String>.}) {
|
245
|
+
assert_kind_of(Class, "string")
|
246
|
+
}
|
247
|
+
check_fails(%Q{failed assert_kind_of.\n<"string">\nexpected to be kind_of?\n<Class> but was\n<String>.}) {
|
248
|
+
assert_kind_of(Class, "string", "failed assert_kind_of")
|
249
|
+
}
|
250
|
+
end
|
251
|
+
|
252
|
+
def test_assert_match
|
253
|
+
check_nothing_fails {
|
254
|
+
assert_match(/strin./, "string")
|
255
|
+
}
|
256
|
+
check_nothing_fails {
|
257
|
+
assert_match("strin", "string")
|
258
|
+
}
|
259
|
+
check_nothing_fails {
|
260
|
+
assert_match(/strin./, "string", "successful assert_match")
|
261
|
+
}
|
262
|
+
check_nothing_fails {
|
263
|
+
assert_match(/strin./, "string", "successful assert_match")
|
264
|
+
}
|
265
|
+
check_fails(%Q{<"string"> expected to be =~\n</slin./>.}) {
|
266
|
+
assert_match(/slin./, "string")
|
267
|
+
}
|
268
|
+
check_fails(%Q{<"string"> expected to be =~\n</strin\\./>.}) {
|
269
|
+
assert_match("strin.", "string")
|
270
|
+
}
|
271
|
+
check_fails(%Q{failed assert_match.\n<"string"> expected to be =~\n</slin./>.}) {
|
272
|
+
assert_match(/slin./, "string", "failed assert_match")
|
273
|
+
}
|
274
|
+
end
|
275
|
+
|
276
|
+
def test_assert_same
|
277
|
+
thing = "thing"
|
278
|
+
check_nothing_fails {
|
279
|
+
assert_same(thing, thing)
|
280
|
+
}
|
281
|
+
check_nothing_fails {
|
282
|
+
assert_same(thing, thing, "successful assert_same")
|
283
|
+
}
|
284
|
+
check_nothing_fails {
|
285
|
+
assert_same(thing, thing, "successful assert_same")
|
286
|
+
}
|
287
|
+
thing2 = "thing"
|
288
|
+
check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
|
289
|
+
assert_same(thing, thing2)
|
290
|
+
}
|
291
|
+
check_fails(%Q{failed assert_same.\n<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
|
292
|
+
assert_same(thing, thing2, "failed assert_same")
|
293
|
+
}
|
294
|
+
end
|
295
|
+
|
296
|
+
def test_assert_nothing_raised
|
297
|
+
check_nothing_fails {
|
298
|
+
assert_nothing_raised {
|
299
|
+
1 + 1
|
300
|
+
}
|
301
|
+
}
|
302
|
+
check_nothing_fails {
|
303
|
+
assert_nothing_raised("successful assert_nothing_raised") {
|
304
|
+
1 + 1
|
305
|
+
}
|
306
|
+
}
|
307
|
+
check_nothing_fails {
|
308
|
+
assert_nothing_raised("successful assert_nothing_raised") {
|
309
|
+
1 + 1
|
310
|
+
}
|
311
|
+
}
|
312
|
+
check_nothing_fails {
|
313
|
+
begin
|
314
|
+
assert_nothing_raised(RuntimeError, StandardError, Comparable, "successful assert_nothing_raised") {
|
315
|
+
raise ZeroDivisionError.new("ArgumentError")
|
316
|
+
}
|
317
|
+
rescue ZeroDivisionError
|
318
|
+
end
|
319
|
+
}
|
320
|
+
check_fails("Should expect a class of exception, Object.\n<false> is not true.") {
|
321
|
+
assert_nothing_raised(Object) {
|
322
|
+
1 + 1
|
323
|
+
}
|
324
|
+
}
|
325
|
+
check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
|
326
|
+
assert_nothing_raised {
|
327
|
+
raise "Error"
|
328
|
+
}
|
329
|
+
}
|
330
|
+
check_fails(%r{\Afailed assert_nothing_raised\.\nException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
|
331
|
+
assert_nothing_raised("failed assert_nothing_raised") {
|
332
|
+
raise "Error"
|
333
|
+
}
|
334
|
+
}
|
335
|
+
check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
|
336
|
+
assert_nothing_raised(StandardError, RuntimeError) {
|
337
|
+
raise "Error"
|
338
|
+
}
|
339
|
+
}
|
340
|
+
check_fails("Failure.") do
|
341
|
+
assert_nothing_raised do
|
342
|
+
flunk("Failure")
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
def test_flunk
|
348
|
+
check_fails("Flunked.") {
|
349
|
+
flunk
|
350
|
+
}
|
351
|
+
check_fails("flunk message.") {
|
352
|
+
flunk("flunk message")
|
353
|
+
}
|
354
|
+
end
|
355
|
+
|
356
|
+
def test_assert_not_same
|
357
|
+
thing = "thing"
|
358
|
+
thing2 = "thing"
|
359
|
+
check_nothing_fails {
|
360
|
+
assert_not_same(thing, thing2)
|
361
|
+
}
|
362
|
+
check_nothing_fails {
|
363
|
+
assert_not_same(thing, thing2, "message")
|
364
|
+
}
|
365
|
+
check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
|
366
|
+
assert_not_same(thing, thing)
|
367
|
+
}
|
368
|
+
check_fails(%Q{message.\n<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
|
369
|
+
assert_not_same(thing, thing, "message")
|
370
|
+
}
|
371
|
+
end
|
372
|
+
|
373
|
+
def test_assert_not_equal
|
374
|
+
check_nothing_fails {
|
375
|
+
assert_not_equal("string1", "string2")
|
376
|
+
}
|
377
|
+
check_nothing_fails {
|
378
|
+
assert_not_equal("string1", "string2", "message")
|
379
|
+
}
|
380
|
+
check_fails(%Q{<"string"> expected to be != to\n<"string">.}) {
|
381
|
+
assert_not_equal("string", "string")
|
382
|
+
}
|
383
|
+
check_fails(%Q{message.\n<"string"> expected to be != to\n<"string">.}) {
|
384
|
+
assert_not_equal("string", "string", "message")
|
385
|
+
}
|
386
|
+
end
|
387
|
+
|
388
|
+
def test_assert_no_match
|
389
|
+
check_nothing_fails{assert_no_match(/sling/, "string")}
|
390
|
+
check_nothing_fails{assert_no_match(/sling/, "string", "message")}
|
391
|
+
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
|
392
|
+
assert_no_match("asdf", "asdf")
|
393
|
+
end
|
394
|
+
check_fails(%Q{</string/> expected to not match\n<"string">.}) do
|
395
|
+
assert_no_match(/string/, "string")
|
396
|
+
end
|
397
|
+
check_fails(%Q{message.\n</string/> expected to not match\n<"string">.}) do
|
398
|
+
assert_no_match(/string/, "string", "message")
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
def test_assert_throws
|
403
|
+
check_nothing_fails {
|
404
|
+
assert_throws(:thing, "message") {
|
405
|
+
throw :thing
|
406
|
+
}
|
407
|
+
}
|
408
|
+
check_fails("message.\n<:thing> expected to be thrown but\n<:thing2> was thrown.") {
|
409
|
+
assert_throws(:thing, "message") {
|
410
|
+
throw :thing2
|
411
|
+
}
|
412
|
+
}
|
413
|
+
check_fails("message.\n<:thing> should have been thrown.") {
|
414
|
+
assert_throws(:thing, "message") {
|
415
|
+
1 + 1
|
416
|
+
}
|
417
|
+
}
|
418
|
+
end
|
419
|
+
|
420
|
+
def test_assert_nothing_thrown
|
421
|
+
check_nothing_fails {
|
422
|
+
assert_nothing_thrown("message") {
|
423
|
+
1 + 1
|
424
|
+
}
|
425
|
+
}
|
426
|
+
check_fails("message.\n<:thing> was thrown when nothing was expected.") {
|
427
|
+
assert_nothing_thrown("message") {
|
428
|
+
throw :thing
|
429
|
+
}
|
430
|
+
}
|
431
|
+
end
|
432
|
+
|
433
|
+
def test_assert_operator
|
434
|
+
check_nothing_fails {
|
435
|
+
assert_operator("thing", :==, "thing", "message")
|
436
|
+
}
|
437
|
+
check_fails(%Q{<0.15>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to?(:to_str).}) do
|
438
|
+
assert_operator("thing", 0.15, "thing")
|
439
|
+
end
|
440
|
+
check_fails(%Q{message.\n<"thing1"> expected to be\n==\n<"thing2">.}) {
|
441
|
+
assert_operator("thing1", :==, "thing2", "message")
|
442
|
+
}
|
443
|
+
end
|
444
|
+
|
445
|
+
def test_assert_respond_to
|
446
|
+
check_nothing_fails {
|
447
|
+
assert_respond_to("thing", :to_s, "message")
|
448
|
+
}
|
449
|
+
check_nothing_fails {
|
450
|
+
assert_respond_to("thing", "to_s", "message")
|
451
|
+
}
|
452
|
+
check_fails("<0.15>\ngiven as the method name argument to #assert_respond_to must be a Symbol or #respond_to?(:to_str).") {
|
453
|
+
assert_respond_to("thing", 0.15)
|
454
|
+
}
|
455
|
+
check_fails("message.\n<:symbol>\nof type <Symbol>\nexpected to respond_to?<:non_existent>.") {
|
456
|
+
assert_respond_to(:symbol, :non_existent, "message")
|
457
|
+
}
|
458
|
+
end
|
459
|
+
|
460
|
+
def test_assert_in_delta
|
461
|
+
check_nothing_fails {
|
462
|
+
assert_in_delta(1.4, 1.4, 0)
|
463
|
+
}
|
464
|
+
check_nothing_fails {
|
465
|
+
assert_in_delta(0.5, 0.4, 0.1, "message")
|
466
|
+
}
|
467
|
+
check_nothing_fails {
|
468
|
+
float_thing = Object.new
|
469
|
+
def float_thing.to_f
|
470
|
+
0.2
|
471
|
+
end
|
472
|
+
assert_in_delta(0.1, float_thing, 0.1)
|
473
|
+
}
|
474
|
+
check_fails("message.\n<0.5> and\n<0.4> expected to be within\n<0.05> of each other.") {
|
475
|
+
assert_in_delta(0.5, 0.4, 0.05, "message")
|
476
|
+
}
|
477
|
+
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>.}) {
|
478
|
+
assert_in_delta(Object.new, 0.4, 0.1)
|
479
|
+
}
|
480
|
+
check_fails("The delta should not be negative.\n<-0.1> expected to be\n>=\n<0.0>.") {
|
481
|
+
assert_in_delta(0.5, 0.4, -0.1, "message")
|
482
|
+
}
|
483
|
+
end
|
484
|
+
|
485
|
+
def test_assert_send
|
486
|
+
object = Object.new
|
487
|
+
class << object
|
488
|
+
private
|
489
|
+
def return_argument(argument, bogus)
|
490
|
+
return argument
|
491
|
+
end
|
492
|
+
end
|
493
|
+
check_nothing_fails {
|
494
|
+
assert_send([object, :return_argument, true, "bogus"], "message")
|
495
|
+
}
|
496
|
+
check_fails(%r{\Amessage\.\n<.+> expected to respond to\n<return_argument\(\[false, "bogus"\]\)> with a true value.\Z}) {
|
497
|
+
assert_send([object, :return_argument, false, "bogus"], "message")
|
498
|
+
}
|
499
|
+
end
|
500
|
+
|
501
|
+
def test_condition_invariant
|
502
|
+
object = Object.new
|
503
|
+
def object.inspect
|
504
|
+
@changed = true
|
505
|
+
end
|
506
|
+
def object.==(other)
|
507
|
+
@changed ||= false
|
508
|
+
return (!@changed)
|
509
|
+
end
|
510
|
+
check_nothing_fails {
|
511
|
+
assert_equal(object, object, "message")
|
512
|
+
}
|
513
|
+
end
|
514
|
+
|
515
|
+
def add_failure(message, location=caller)
|
516
|
+
if (!@catch_assertions)
|
517
|
+
super
|
518
|
+
end
|
519
|
+
end
|
520
|
+
|
521
|
+
def add_assertion
|
522
|
+
if (!@catch_assertions)
|
523
|
+
super
|
524
|
+
else
|
525
|
+
@actual_assertion_count += 1
|
526
|
+
end
|
527
|
+
end
|
528
|
+
end
|