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



256
257
258
259
260
261
262
263
# File 'lib/assert/assertions.rb', line 256

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



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

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

#assert_empty(collection, desc = nil) ⇒ Object



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

def assert_empty(collection, desc = nil)
  assert(collection.empty?, desc) do
    "Expected #{Assert::U.show(collection, __assert_config__)} to be empty."
  end
end

#assert_equal(exp, act, desc = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/assert/assertions.rb', line 27

def assert_equal(exp, act, desc = nil)
  assert(exp == act, desc) do
    c = __assert_config__
    exp_show = Assert::U.show_for_diff(exp, c)
    act_show = Assert::U.show_for_diff(act, c)

    if c.use_diff_proc.call(exp_show, act_show)
      "Expected does not equal actual, diff:\n"\
      "#{c.run_diff_proc.call(exp_show, act_show)}"
    else
      "Expected #{Assert::U.show(act, c)} to be equal to #{Assert::U.show(exp, c)}."
    end
  end
end

#assert_false(object, desc = nil) ⇒ Object



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

def assert_false(object, desc = nil)
  assert(object == false, desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} to be false."
  end
end

#assert_file_exists(file_path, desc = nil) ⇒ Object



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

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

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



71
72
73
74
75
76
# File 'lib/assert/assertions.rb', line 71

def assert_includes(object, collection, desc = nil)
  assert(collection.include?(object), desc) do
    "Expected #{Assert::U.show(collection, __assert_config__)}"\
    " to include #{Assert::U.show(object, __assert_config__)}."
  end
end

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



89
90
91
92
93
94
# File 'lib/assert/assertions.rb', line 89

def assert_instance_of(klass, instance, desc = nil)
  assert(instance.instance_of?(klass), desc) do
    "Expected #{Assert::U.show(instance, __assert_config__)} (#{instance.class})"\
    " to be an instance of #{klass}."
  end
end

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



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

def assert_kind_of(klass, instance, desc = nil)
  assert(instance.kind_of?(klass), desc) do
    "Expected #{Assert::U.show(instance, __assert_config__)} (#{instance.class})"\
    " to be a kind of #{klass}."
  end
end

#assert_match(exp, act, desc = nil) ⇒ Object



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

def assert_match(exp, act, desc = nil)
  exp_regex = String === exp && String === act ? /#{Regexp.escape(exp)}/ : exp
  assert(act =~ exp_regex, desc) do
    "Expected #{Assert::U.show(act, __assert_config__)}"\
    " to match #{Assert::U.show(exp, __assert_config__)}."
  end
end

#assert_nil(object, desc = nil) ⇒ Object



137
138
139
140
141
# File 'lib/assert/assertions.rb', line 137

def assert_nil(object, desc = nil)
  assert(object.nil?, desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} to be nil."
  end
end

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



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

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

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



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

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

#assert_not_equal(exp, act, desc = nil) ⇒ Object Also known as: refute_equal



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/assert/assertions.rb', line 42

def assert_not_equal(exp, act, desc = nil)
  assert(exp != act, desc) do
    c = __assert_config__
    exp_show = Assert::U.show_for_diff(exp, c)
    act_show = Assert::U.show_for_diff(act, c)

    if c.use_diff_proc.call(exp_show, act_show)
      "Expected equals actual, diff:\n"\
      "#{c.run_diff_proc.call(exp_show, act_show)}"
    else
      "Expected #{Assert::U.show(act, c)} to not be equal to #{Assert::U.show(exp, c)}."
    end
  end
end

#assert_not_false(object, desc = nil) ⇒ Object Also known as: refute_false



169
170
171
172
173
# File 'lib/assert/assertions.rb', line 169

def assert_not_false(object, desc = nil)
  assert(object != false, desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} to not be false."
  end
end

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



64
65
66
67
68
# File 'lib/assert/assertions.rb', line 64

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

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



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

def assert_not_includes(object, collection, desc = nil)
  assert(!collection.include?(object), desc) do
    "Expected #{Assert::U.show(collection, __assert_config__)}"\
    " to not include #{Assert::U.show(object, __assert_config__)}."
  end
end

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



96
97
98
99
100
101
# File 'lib/assert/assertions.rb', line 96

def assert_not_instance_of(klass, instance, desc = nil)
  assert(!instance.instance_of?(klass), desc) do
    "Expected #{Assert::U.show(instance, __assert_config__)} (#{instance.class})"\
    " to not be an instance of #{klass}."
  end
end

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



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

def assert_not_kind_of(klass, instance, desc = nil)
  assert(!instance.kind_of?(klass), desc) do
    "Expected #{Assert::U.show(instance, __assert_config__)} (#{instance.class})"\
    " to not be a kind of #{klass}."
  end
end

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



127
128
129
130
131
132
133
# File 'lib/assert/assertions.rb', line 127

def assert_not_match(exp, act, desc = nil)
  exp = String === exp && String === act ? /#{Regexp.escape(exp)}/ : exp
  assert(act !~ exp, desc) do
    "Expected #{Assert::U.show(act, __assert_config__)}"\
    " to not match #{Assert::U.show(exp, __assert_config__)}."
  end
end

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



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

def assert_not_nil(object, desc = nil)
  assert(!object.nil?, desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} to not be nil."
  end
end

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



200
201
202
203
204
205
# File 'lib/assert/assertions.rb', line 200

def assert_not_respond_to(method, object, desc = nil)
  assert(!object.respond_to?(method), desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} (#{object.class})"\
    " to not respond to `#{method}`."
  end
end

#assert_not_same(exp, act, desc = nil) ⇒ Object Also known as: refute_same



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/assert/assertions.rb', line 228

def assert_not_same(exp, act, desc = nil)
  assert(!act.equal?(exp), desc) do
    c = __assert_config__
    exp_show = Assert::U.show_for_diff(exp, c)
    act_show = Assert::U.show_for_diff(act, c)
    exp_id = "#<#{exp.class}:#{"0x0%x" % (exp.object_id << 1)}>"
    act_id = "#<#{act.class}:#{"0x0%x" % (act.object_id << 1)}>"

    if c.use_diff_proc.call(exp_show, act_show)
      "Expected #{act_id} to not be the same as #{exp_id}, diff:\n"\
      "#{c.run_diff_proc.call(exp_show, act_show)}"
    else
      "Expected #{Assert::U.show(act, c)} (#{act_id}) to not be the same as"\
      " #{Assert::U.show(exp, c)} (#{exp_id})."
    end
  end
end

#assert_not_true(object, desc = nil) ⇒ Object Also known as: refute_true



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

def assert_not_true(object, desc = nil)
  assert(object != true, desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} to not be true."
  end
end

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



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

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



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

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 }
  err.exception
end

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



192
193
194
195
196
197
# File 'lib/assert/assertions.rb', line 192

def assert_respond_to(method, object, desc = nil)
  assert(object.respond_to?(method), desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} (#{object.class})"\
    " to respond to `#{method}`."
  end
end

#assert_same(exp, act, desc = nil) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/assert/assertions.rb', line 210

def assert_same(exp, act, desc = nil)
  assert(act.equal?(exp), desc) do
    c = __assert_config__
    exp_show = Assert::U.show_for_diff(exp, c)
    act_show = Assert::U.show_for_diff(act, c)
    exp_id = "#<#{exp.class}:#{"0x0%x" % (exp.object_id << 1)}>"
    act_id = "#<#{act.class}:#{"0x0%x" % (act.object_id << 1)}>"

    if c.use_diff_proc.call(exp_show, act_show)
      "Expected #{act_id} to be the same as #{exp_id}, diff:\n"\
      "#{c.run_diff_proc.call(exp_show, act_show)}"
    else
      "Expected #{Assert::U.show(act, c)} (#{act_id}) to be the same as"\
      " #{Assert::U.show(exp, c)} (#{exp_id})."
    end
  end
end

#assert_true(object, desc = nil) ⇒ Object



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

def assert_true(object, desc = nil)
  assert(object == true, desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} to be true."
  end
end