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 =
[
  :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

Instance Method Details

#assert_block(desc = nil) ⇒ Object


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

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

#assert_changes(ruby_string_to_eval, desc: nil, from: Assert::ActualValue.not_given, to: Assert::ActualValue.not_given, &block) ⇒ Object


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/assert/assertions.rb', line 216

def assert_changes(
      ruby_string_to_eval,
      desc: nil,
      from: Assert::ActualValue.not_given,
      to: Assert::ActualValue.not_given,
      &block)
  desc_msg = desc ? "#{desc}\n" : ""
  from_eval = instance_eval(ruby_string_to_eval)
  if Assert::ActualValue.given?(from)
    assert_equal(
      from,
      from_eval,
      "#{desc_msg}Expected `#{ruby_string_to_eval}` to "\
      "change from `#{from.inspect}`.",
    )
  end

  block.call

  to_eval = instance_eval(ruby_string_to_eval)
  if Assert::ActualValue.given?(to)
    assert_equal(
      to,
      to_eval,
      "#{desc_msg}Expected `#{ruby_string_to_eval}` to "\
      "change to `#{to.inspect}`.",
    )
  end

  if (
    Assert::ActualValue.not_given?(from) &&
    Assert::ActualValue.not_given?(to)
  )
    assert_not_equal(
      from_eval,
      to_eval,
      "#{desc_msg}Expected `#{ruby_string_to_eval}` to change; "\
      "it was `#{from_eval.inspect}` and didn't change.",
    )
  end
end

#assert_empty(collection, desc = nil) ⇒ Object


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

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


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

def assert_equal(exp, act, desc = nil)
  assert(act == exp, 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


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

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


77
78
79
80
81
# File 'lib/assert/assertions.rb', line 77

def assert_file_exists(file_path, desc = nil)
  assert(File.exist?(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


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

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


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

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_is_a(klass, instance, desc = nil) ⇒ Object Also known as: assert_kind_of


123
124
125
126
127
128
# File 'lib/assert/assertions.rb', line 123

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

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


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

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

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


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

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


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

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


24
25
26
# File 'lib/assert/assertions.rb', line 24

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

#assert_not_changes(ruby_string_to_eval, desc: nil, from: Assert::ActualValue.not_given, &block) ⇒ Object Also known as: refute_changes


258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/assert/assertions.rb', line 258

def assert_not_changes(
      ruby_string_to_eval,
      desc: nil,
      from: Assert::ActualValue.not_given,
      &block)
  desc_msg = desc ? "#{desc}\n" : ""
  from_eval = instance_eval(ruby_string_to_eval)
  if Assert::ActualValue.given?(from)
    assert_equal(
      from,
      from_eval,
      "#{desc_msg}Expected `#{ruby_string_to_eval}` to "\
      "not change from `#{from.inspect}`.",
    )
  end

  block.call

  to_eval = instance_eval(ruby_string_to_eval)
  assert_equal(
    from_eval,
    to_eval,
    "#{desc_msg}Expected `#{ruby_string_to_eval}` to not change; "\
    "it was `#{from_eval.inspect}` and changed to `#{to_eval.inspect}`.",
  )
end

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


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

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


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/assert/assertions.rb', line 60

def assert_not_equal(exp, act, desc = nil)
  assert(act != exp, 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


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

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


83
84
85
86
87
# File 'lib/assert/assertions.rb', line 83

def assert_not_file_exists(file_path, desc = nil)
  assert(!File.exist?(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


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

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


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

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_match(exp, act, desc = nil) ⇒ Object Also known as: refute_match, assert_no_match


151
152
153
154
155
156
157
# File 'lib/assert/assertions.rb', line 151

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


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

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


294
295
296
297
298
299
# File 'lib/assert/assertions.rb', line 294

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


322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/assert/assertions.rb', line 322

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


180
181
182
183
184
# File 'lib/assert/assertions.rb', line 180

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


208
209
210
211
212
# File 'lib/assert/assertions.rb', line 208

def assert_nothing_raised(*exceptions, &block)
  desc = exceptions.last.is_a?(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


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

def assert_raises(*exceptions, &block)
  desc = exceptions.last.is_a?(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


286
287
288
289
290
291
# File 'lib/assert/assertions.rb', line 286

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


304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/assert/assertions.rb', line 304

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


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

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