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



172
173
174
175
176
177
178
179
# File 'lib/assert/assertions.rb', line 172

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
# File 'lib/assert/assertions.rb', line 4

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

#assert_empty(collection, desc = nil) ⇒ Object



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

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

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



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

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

#assert_file_exists(file_path, desc = nil) ⇒ Object



39
40
41
42
43
# File 'lib/assert/assertions.rb', line 39

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

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



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

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

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



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

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

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



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

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

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



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

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

#assert_nil(object, desc = nil) ⇒ Object



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

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

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



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

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

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



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

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

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



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

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

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



45
46
47
48
49
# File 'lib/assert/assertions.rb', line 45

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

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



59
60
61
62
63
# File 'lib/assert/assertions.rb', line 59

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

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



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

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

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



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

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

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



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

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

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



114
115
116
# File 'lib/assert/assertions.rb', line 114

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

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



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

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

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



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

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

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



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

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



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

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



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

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

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



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

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