Module: Assert::Assertions

Included in:
Context
Defined in:
lib/assert/assertions.rb

Defined Under Namespace

Classes: CheckException, NoRaisedException, RaisedException

Constant Summary collapse

IGNORED_ASSERTION_HELPERS =

ignored assertion helpers

[
  :assert_throws,     :assert_nothing_thrown,
  :assert_operator,   :refute_operator,
  :assert_in_epsilon, :refute_in_epsilon,
  :assert_in_delta,   :refute_in_delta,
  :assert_send
]

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



164
165
166
167
168
169
170
171
# File 'lib/assert/assertions.rb', line 164

def method_missing(method, *args, &block)
  if IGNORED_ASSERTION_HELPERS.include?(method.to_sym)
    ignore "The assertion `#{method}` is not supported."\
           " Please use another assertion or the basic `assert`."
  else
    super
  end
end

Instance Method Details

#assert_block(desc = nil) ⇒ Object



4
5
6
7
# File 'lib/assert/assertions.rb', line 4

def assert_block(desc=nil)
  msg ||= "Expected block to return a true value."
  assert(yield, desc, msg)
end

#assert_empty(collection, desc = nil) ⇒ Object



15
16
17
18
# File 'lib/assert/assertions.rb', line 15

def assert_empty(collection, desc=nil)
  msg = "Expected #{collection.inspect} to be empty."
  assert(collection.empty?, desc, msg)
end

#assert_equal(expected, actual, desc = nil) ⇒ Object



26
27
28
29
# File 'lib/assert/assertions.rb', line 26

def assert_equal(expected, actual, desc=nil)
  msg = "Expected #{expected.inspect}, not #{actual.inspect}."
  assert(actual == expected, desc, msg)
end

#assert_file_exists(file_path, desc = nil) ⇒ Object



37
38
39
40
# File 'lib/assert/assertions.rb', line 37

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

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



48
49
50
51
# File 'lib/assert/assertions.rb', line 48

def assert_includes(object, collection, desc=nil)
  msg = "Expected #{collection.inspect} to include #{object.inspect}."
  assert(collection.include?(object), desc, msg)
end

#assert_instance_of(klass, instance, desc = nil) ⇒ Object



62
63
64
65
66
# File 'lib/assert/assertions.rb', line 62

def assert_instance_of(klass, instance, desc=nil)
  msg = "Expected #{instance.inspect} (#{instance.class}) to"\
        " be an instance of #{klass}."
  assert(instance.instance_of?(klass), desc, msg)
end

#assert_kind_of(klass, instance, desc = nil) ⇒ Object



74
75
76
77
78
# File 'lib/assert/assertions.rb', line 74

def assert_kind_of(klass, instance, desc=nil)
  msg = "Expected #{instance.inspect} (#{instance.class}) to"\
        " be a kind of #{klass}."
  assert(instance.kind_of?(klass), desc, msg)
end

#assert_match(expected, actual, desc = nil) ⇒ Object



86
87
88
89
90
# File 'lib/assert/assertions.rb', line 86

def assert_match(expected, actual, desc=nil)
  msg = "Expected #{actual.inspect} to match #{expected.inspect}."
  expected = /#{Regexp.escape(expected)}/ if String === expected && String === actual
  assert(actual =~ expected, desc, msg)
end

#assert_nil(object, desc = nil) ⇒ Object



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

def assert_nil(object, desc=nil)
  msg = "Expected nil, not #{object.inspect}."
  assert(object.nil?, desc, msg)
end

#assert_not_block(desc = nil) ⇒ Object Also known as: refute_block



9
10
11
12
# File 'lib/assert/assertions.rb', line 9

def assert_not_block(desc=nil)
  msg ||= "Expected block to return a false value."
  assert(!yield, desc, msg)
end

#assert_not_empty(collection, desc = nil) ⇒ Object Also known as: refute_empty



20
21
22
23
# File 'lib/assert/assertions.rb', line 20

def assert_not_empty(collection, desc=nil)
  msg = "Expected #{collection.inspect} to not be empty."
  assert(!collection.empty?, desc, msg)
end

#assert_not_equal(expected, actual, desc = nil) ⇒ Object Also known as: refute_equal



31
32
33
34
# File 'lib/assert/assertions.rb', line 31

def assert_not_equal(expected, actual, desc=nil)
  msg = "#{actual.inspect} not expected to equal #{expected.inspect}."
  assert(actual != expected, desc, msg)
end

#assert_not_file_exists(file_path, desc = nil) ⇒ Object Also known as: refute_file_exists



42
43
44
45
# File 'lib/assert/assertions.rb', line 42

def assert_not_file_exists(file_path, desc=nil)
  msg = "Expected #{file_path.inspect} to not exist."
  assert(!File.exists?(File.expand_path(file_path)), desc, msg)
end

#assert_not_includes(object, collection, desc = nil) ⇒ Object Also known as: assert_not_included, refute_includes, refute_included



54
55
56
57
# File 'lib/assert/assertions.rb', line 54

def assert_not_includes(object, collection, desc=nil)
  msg = "Expected #{collection.inspect} to not include #{object.inspect}."
  assert(!collection.include?(object), desc, msg)
end

#assert_not_instance_of(klass, instance, desc = nil) ⇒ Object Also known as: refute_instance_of



68
69
70
71
# File 'lib/assert/assertions.rb', line 68

def assert_not_instance_of(klass, instance, desc=nil)
  msg = "#{instance.inspect} not expected to be an instance of #{klass}."
  assert(!instance.instance_of?(klass), desc, msg)
end

#assert_not_kind_of(klass, instance, desc = nil) ⇒ Object Also known as: refute_kind_of



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

def assert_not_kind_of(klass, instance, desc=nil)
  msg = "#{instance.inspect} not expected to be a kind of #{klass}."
  assert(!instance.kind_of?(klass), desc, msg)
end

#assert_not_match(expected, actual, desc = nil) ⇒ Object Also known as: refute_match, assert_no_match



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

def assert_not_match(expected, actual, desc=nil)
  msg = "#{actual.inspect} not expected to match #{expected.inspect}."
  expected = /#{Regexp.escape(expected)}/ if String === expected && String === actual
  assert(actual !~ expected, desc, msg)
end

#assert_not_nil(object, desc = nil) ⇒ Object Also known as: refute_nil



105
106
107
108
# File 'lib/assert/assertions.rb', line 105

def assert_not_nil(object, desc=nil)
  msg = "Expected #{object.inspect} to not be nil."
  assert(!object.nil?, desc, msg)
end

#assert_not_respond_to(method, object, desc = nil) ⇒ Object Also known as: assert_not_responds_to, refute_respond_to, refute_responds_to



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

def assert_not_respond_to(method, object, desc=nil)
  msg = "#{object.inspect} (#{object.class}) not expected to"\
        " respond to `#{method}`."
  assert(!object.respond_to?(method), desc, msg)
end

#assert_not_same(expected, actual, desc = nil) ⇒ Object Also known as: refute_same



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

def assert_not_same(expected, actual, desc=nil)
  msg = "#{actual} (#{actual.object_id}) not expected to"\
        " be the same as #{expected} (#{expected.object_id})."
  assert(!actual.equal?(expected), desc, msg)
end

#assert_nothing_raised(*exceptions, &block) ⇒ Object Also known as: assert_not_raises, assert_not_raise



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

def assert_nothing_raised(*exceptions, &block)
  desc = exceptions.last.kind_of?(String) ? exceptions.pop : nil
  err = NoRaisedException.new(exceptions, &block)
  assert(!err.raised?, desc, err.msg)
end

#assert_raises(*exceptions, &block) ⇒ Object Also known as: assert_raise



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

def assert_raises(*exceptions, &block)
  desc = exceptions.last.kind_of?(String) ? exceptions.pop : nil
  err = RaisedException.new(exceptions, &block)
  assert(err.raised?, desc, err.msg)
end

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



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

def assert_respond_to(method, object, desc=nil)
  msg = "Expected #{object.inspect} (#{object.class}) to"\
        " respond to `#{method}`."
  assert(object.respond_to?(method), desc, msg)
end

#assert_same(expected, actual, desc = nil) ⇒ Object



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

def assert_same(expected, actual, desc=nil)
  msg = "Expected #{actual} (#{actual.object_id}) to"\
        " be the same as #{expected} (#{expected.object_id})."
  assert(actual.equal?(expected), desc, msg)
end