Module: Assert::Assertions
- Included in:
- Context
- Defined in:
- lib/assert/assertions.rb
Constant Summary
collapse
- IGNORED_ASSERTION_HELPERS =
[ :assert_throws, :assert_nothing_thrown, :assert_send,
:assert_operator, :refute_operator, :assert_in_epsilon, :refute_in_epsilon,
:assert_in_delta, :refute_in_delta
]
Instance Method Summary
collapse
-
#assert_block(fail_desc = nil) ⇒ Object
-
#assert_empty(collection, fail_desc = nil) ⇒ Object
-
#assert_equal(expected, actual, fail_desc = nil) ⇒ Object
-
#assert_includes(object, collection, fail_desc = nil) ⇒ Object
(also: #assert_included)
-
#assert_instance_of(klass, instance, fail_desc = nil) ⇒ Object
-
#assert_kind_of(klass, instance, fail_desc = nil) ⇒ Object
-
#assert_match(expected, actual, fail_desc = nil) ⇒ Object
-
#assert_nil(object, fail_desc = nil) ⇒ Object
-
#assert_not_block(fail_desc = nil) ⇒ Object
(also: #refute_block)
-
#assert_not_empty(collection, fail_desc = nil) ⇒ Object
(also: #refute_empty)
-
#assert_not_equal(expected, actual, fail_desc = nil) ⇒ Object
(also: #refute_equal)
-
#assert_not_includes(object, collection, fail_desc = nil) ⇒ Object
(also: #assert_not_included, #refute_includes, #refute_included)
-
#assert_not_instance_of(klass, instance, fail_desc = nil) ⇒ Object
(also: #refute_instance_of)
-
#assert_not_kind_of(klass, instance, fail_desc = nil) ⇒ Object
(also: #refute_kind_of)
-
#assert_not_match(expected, actual, fail_desc = nil) ⇒ Object
(also: #refute_match, #assert_no_match)
-
#assert_not_nil(object, fail_desc = nil) ⇒ Object
(also: #refute_nil)
-
#assert_not_respond_to(method, object, fail_desc = nil) ⇒ Object
(also: #refute_respond_to)
-
#assert_not_same(expected, actual, fail_desc = nil) ⇒ Object
(also: #refute_same)
-
#assert_nothing_raised(*args, &block) ⇒ Object
(also: #assert_not_raises, #assert_not_raise)
-
#assert_raises(*args, &block) ⇒ Object
(also: #assert_raise)
-
#assert_respond_to(method, object, fail_desc = nil) ⇒ Object
-
#assert_same(expected, actual, fail_desc = nil) ⇒ Object
-
#method_missing(method, *args, &block) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/assert/assertions.rb', line 187
def method_missing(method, *args, &block)
if IGNORED_ASSERTION_HELPERS.include?(method.to_sym)
ignore([
"The assertion helper '#{method}' is not supported. Please use ",
"another helper or the basic assert."
].join)
else
super
end
end
|
Instance Method Details
#assert_block(fail_desc = nil) ⇒ Object
4
5
6
7
|
# File 'lib/assert/assertions.rb', line 4
def assert_block(fail_desc=nil)
what_failed_msg ||= "Expected block to return true value."
assert(yield, fail_desc, what_failed_msg)
end
|
#assert_empty(collection, fail_desc = nil) ⇒ Object
141
142
143
144
|
# File 'lib/assert/assertions.rb', line 141
def assert_empty(collection, fail_desc=nil)
what_failed_msg = "Expected #{collection.inspect} to be empty."
assert(collection.empty?, fail_desc, what_failed_msg)
end
|
#assert_equal(expected, actual, fail_desc = nil) ⇒ Object
108
109
110
111
|
# File 'lib/assert/assertions.rb', line 108
def assert_equal(expected, actual, fail_desc=nil)
what_failed_msg = "Expected #{expected.inspect}, not #{actual.inspect}."
assert(actual == expected, fail_desc, what_failed_msg)
end
|
#assert_includes(object, collection, fail_desc = nil) ⇒ Object
Also known as:
assert_included
154
155
156
157
|
# File 'lib/assert/assertions.rb', line 154
def assert_includes(object, collection, fail_desc=nil)
what_failed_msg = "Expected #{collection.inspect} to include #{object.inspect}."
assert(collection.include?(object), fail_desc, what_failed_msg)
end
|
#assert_instance_of(klass, instance, fail_desc = nil) ⇒ Object
51
52
53
54
55
56
57
|
# File 'lib/assert/assertions.rb', line 51
def assert_instance_of(klass, instance, fail_desc=nil)
what_failed_msg = [
"Expected #{instance.inspect} to be an instance ",
"of #{klass}, not #{instance.class}."
].join
assert(instance.instance_of?(klass), fail_desc, what_failed_msg)
end
|
#assert_kind_of(klass, instance, fail_desc = nil) ⇒ Object
32
33
34
35
36
37
38
|
# File 'lib/assert/assertions.rb', line 32
def assert_kind_of(klass, instance, fail_desc=nil)
what_failed_msg = [
"Expected #{instance.inspect} to be a kind ",
"of #{klass}, not #{instance.class}."
].join
assert(instance.kind_of?(klass), fail_desc, what_failed_msg)
end
|
#assert_match(expected, actual, fail_desc = nil) ⇒ Object
123
124
125
126
127
|
# File 'lib/assert/assertions.rb', line 123
def assert_match(expected, actual, fail_desc=nil)
what_failed_msg = "Expected #{actual.inspect} to match #{expected.inspect}."
expected = /#{Regexp.escape(expected)}/ if String === expected && String === actual
assert(actual =~ expected, fail_desc, what_failed_msg)
end
|
#assert_nil(object, fail_desc = nil) ⇒ Object
170
171
172
173
|
# File 'lib/assert/assertions.rb', line 170
def assert_nil(object, fail_desc=nil)
what_failed_msg = "Expected nil, not #{object.inspect}."
assert(object.nil?, fail_desc, what_failed_msg)
end
|
#assert_not_block(fail_desc = nil) ⇒ Object
Also known as:
refute_block
9
10
11
12
|
# File 'lib/assert/assertions.rb', line 9
def assert_not_block(fail_desc=nil)
what_failed_msg ||= "Expected block to return false value."
assert(!yield, fail_desc, what_failed_msg)
end
|
#assert_not_empty(collection, fail_desc = nil) ⇒ Object
Also known as:
refute_empty
146
147
148
149
|
# File 'lib/assert/assertions.rb', line 146
def assert_not_empty(collection, fail_desc=nil)
what_failed_msg = "Expected #{collection.inspect} to not be empty."
assert(!collection.empty?, fail_desc, what_failed_msg)
end
|
#assert_not_equal(expected, actual, fail_desc = nil) ⇒ Object
Also known as:
refute_equal
113
114
115
116
117
118
|
# File 'lib/assert/assertions.rb', line 113
def assert_not_equal(expected, actual, fail_desc=nil)
what_failed_msg = [
"#{expected.inspect} not expected to be equal ", "to #{actual.inspect}."
].join
assert(actual != expected, fail_desc, what_failed_msg)
end
|
#assert_not_includes(object, collection, fail_desc = nil) ⇒ Object
Also known as:
assert_not_included, refute_includes, refute_included
160
161
162
163
|
# File 'lib/assert/assertions.rb', line 160
def assert_not_includes(object, collection, fail_desc=nil)
what_failed_msg = "Expected #{collection.inspect} to not include #{object.inspect}."
assert(!collection.include?(object), fail_desc, what_failed_msg)
end
|
#assert_not_instance_of(klass, instance, fail_desc = nil) ⇒ Object
Also known as:
refute_instance_of
59
60
61
62
63
64
65
|
# File 'lib/assert/assertions.rb', line 59
def assert_not_instance_of(klass, instance, fail_desc=nil)
what_failed_msg = [
"#{instance.inspect} was not expected to be an ",
"instance of #{klass}."
].join
assert(!instance.instance_of?(klass), fail_desc, what_failed_msg)
end
|
#assert_not_kind_of(klass, instance, fail_desc = nil) ⇒ Object
Also known as:
refute_kind_of
40
41
42
43
44
45
46
|
# File 'lib/assert/assertions.rb', line 40
def assert_not_kind_of(klass, instance, fail_desc=nil)
what_failed_msg = [
"#{instance.inspect} was not expected to be a ",
"kind of #{klass}."
].join
assert(!instance.kind_of?(klass), fail_desc, what_failed_msg)
end
|
#assert_not_match(expected, actual, fail_desc = nil) ⇒ Object
Also known as:
refute_match, assert_no_match
129
130
131
132
133
134
135
|
# File 'lib/assert/assertions.rb', line 129
def assert_not_match(expected, actual, fail_desc=nil)
what_failed_msg = [
"#{actual.inspect} not expected to ", "match #{expected.inspect}."
].join
expected = /#{Regexp.escape(expected)}/ if String === expected && String === actual
assert(actual !~ expected, fail_desc, what_failed_msg)
end
|
#assert_not_nil(object, fail_desc = nil) ⇒ Object
Also known as:
refute_nil
175
176
177
178
|
# File 'lib/assert/assertions.rb', line 175
def assert_not_nil(object, fail_desc=nil)
what_failed_msg = "Expected #{object.inspect} to not be nil."
assert(!object.nil?, fail_desc, what_failed_msg)
end
|
#assert_not_respond_to(method, object, fail_desc = nil) ⇒ Object
Also known as:
refute_respond_to
78
79
80
81
82
83
84
|
# File 'lib/assert/assertions.rb', line 78
def assert_not_respond_to(method, object, fail_desc=nil)
what_failed_msg = [
"#{object.inspect} (#{object.class}) not expected to ",
"respond to ##{method}."
].join
assert(!object.respond_to?(method), fail_desc, what_failed_msg)
end
|
#assert_not_same(expected, actual, fail_desc = nil) ⇒ Object
Also known as:
refute_same
97
98
99
100
101
102
103
|
# File 'lib/assert/assertions.rb', line 97
def assert_not_same(expected, actual, fail_desc=nil)
what_failed_msg = [
"#{expected} (#{expected.object_id}) not expected to be the same ",
"as #{actual} (#{actual.object_id})."
].join
assert(!actual.equal?(expected), fail_desc, what_failed_msg)
end
|
#assert_nothing_raised(*args, &block) ⇒ Object
Also known as:
assert_not_raises, assert_not_raise
23
24
25
26
|
# File 'lib/assert/assertions.rb', line 23
def assert_nothing_raised(*args, &block)
assertion, fail_desc = check_exception(args, :not_raises, &block)
assert(!assertion, fail_desc, "")
end
|
#assert_raises(*args, &block) ⇒ Object
Also known as:
assert_raise
17
18
19
20
|
# File 'lib/assert/assertions.rb', line 17
def assert_raises(*args, &block)
assertion, fail_desc = check_exception(args, :raises, &block)
assert(assertion, fail_desc, "")
end
|
#assert_respond_to(method, object, fail_desc = nil) ⇒ Object
70
71
72
73
74
75
76
|
# File 'lib/assert/assertions.rb', line 70
def assert_respond_to(method, object, fail_desc=nil)
what_failed_msg = [
"Expected #{object.inspect} (#{object.class}) to ",
"respond to ##{method}."
].join
assert(object.respond_to?(method), fail_desc, what_failed_msg)
end
|
#assert_same(expected, actual, fail_desc = nil) ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/assert/assertions.rb', line 89
def assert_same(expected, actual, fail_desc=nil)
what_failed_msg = [
"Expected #{expected} (#{expected.object_id}) to be the same ",
"as #{actual} (#{actual.object_id})."
].join
assert(actual.equal?(expected), fail_desc, what_failed_msg)
end
|