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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



203
204
205
206
207
208
209
210
211
212
# File 'lib/assert/assertions.rb', line 203

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



144
145
146
147
# File 'lib/assert/assertions.rb', line 144

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



111
112
113
114
# File 'lib/assert/assertions.rb', line 111

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_file_exists(file_path, fail_desc = nil) ⇒ Object



186
187
188
189
# File 'lib/assert/assertions.rb', line 186

def assert_file_exists(file_path, fail_desc=nil)
  what_failed_msg = "Expected #{file_path.inspect} to exist."
  assert(File.exists?(File.expand_path(file_path)), fail_desc, what_failed_msg)
end

#assert_includes(object, collection, fail_desc = nil) ⇒ Object Also known as: assert_included



157
158
159
160
# File 'lib/assert/assertions.rb', line 157

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



126
127
128
129
130
# File 'lib/assert/assertions.rb', line 126

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



173
174
175
176
# File 'lib/assert/assertions.rb', line 173

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



149
150
151
152
# File 'lib/assert/assertions.rb', line 149

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



116
117
118
119
120
121
# File 'lib/assert/assertions.rb', line 116

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_file_exists(file_path, fail_desc = nil) ⇒ Object Also known as: refute_file_exists



191
192
193
194
# File 'lib/assert/assertions.rb', line 191

def assert_not_file_exists(file_path, fail_desc=nil)
  what_failed_msg = "Expected #{file_path.inspect} to not exist."
  assert(!File.exists?(File.expand_path(file_path)), 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



163
164
165
166
# File 'lib/assert/assertions.rb', line 163

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



132
133
134
135
136
137
138
# File 'lib/assert/assertions.rb', line 132

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



178
179
180
181
# File 'lib/assert/assertions.rb', line 178

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: assert_not_responds_to, refute_respond_to, refute_responds_to



79
80
81
82
83
84
85
# File 'lib/assert/assertions.rb', line 79

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



100
101
102
103
104
105
106
# File 'lib/assert/assertions.rb', line 100

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, what_failed_msg = check_exception(args, :not_raises, &block)
  assert(!assertion, nil, what_failed_msg)
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, what_failed_msg = check_exception(args, :raises, &block)
  assert(assertion, nil, what_failed_msg)
end

#assert_respond_to(method, object, fail_desc = nil) ⇒ Object Also known as: assert_responds_to



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



92
93
94
95
96
97
98
# File 'lib/assert/assertions.rb', line 92

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