Module: Ruptr::Assertions

Included in:
Ruptr::Adapters::RuptrAssertions
Defined in:
lib/ruptr/assertions.rb

Defined Under Namespace

Classes: AssertionError, EquivalenceAssertionError, EquivalenceRefutationError, SkippedException, UnexpectedExceptionError, UnexpectedValueError

Constant Summary collapse

PASSTHROUGH_EXCEPTIONS =
[
  AssertionError,
  SkippedException,
  SignalException,
  SystemExit,
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.def_operator_shortcut(shortcut_name_suffix, operator_name, swapped: false) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
# File 'lib/ruptr/assertions.rb', line 306

def self.def_operator_shortcut(shortcut_name_suffix, operator_name, swapped: false)
  define_method(:"assert_#{shortcut_name_suffix}") do |arg1, arg2, msg = nil|
    arg1, arg2 = arg2, arg1 if swapped
    assert_operator arg1, operator_name, arg2, msg
  end
  define_method(:"refute_#{shortcut_name_suffix}") do |arg1, arg2, msg = nil|
    arg1, arg2 = arg2, arg1 if swapped
    refute_operator arg1, operator_name, arg2, msg
  end
  alias_method(:"assert_not_#{shortcut_name_suffix}", :"refute_#{shortcut_name_suffix}")
end

.def_predicate_shortcut(shortcut_name_suffix, predicate_name) ⇒ Object



296
297
298
299
300
301
302
303
304
# File 'lib/ruptr/assertions.rb', line 296

def self.def_predicate_shortcut(shortcut_name_suffix, predicate_name)
  define_method(:"assert_#{shortcut_name_suffix}") do |target, msg = nil|
    assert_predicate target, predicate_name, msg
  end
  define_method(:"refute_#{shortcut_name_suffix}") do |target, msg = nil|
    refute_predicate target, predicate_name, msg
  end
  alias_method(:"assert_not_#{shortcut_name_suffix}", :"refute_#{shortcut_name_suffix}")
end

Instance Method Details

#assert(val, msg = nil) ⇒ Object



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

def assert(val, msg = nil)
  bump_assertions_count
  return if val
  assertion_unexpected_value(val, msg || "expected #{assertion_inspect(val)} to be truthy")
end

#assert_alias_method(obj, name1, name2, msg = nil) ⇒ Object



268
269
270
271
272
273
# File 'lib/ruptr/assertions.rb', line 268

def assert_alias_method(obj, name1, name2, msg = nil)
  bump_assertions_count
  return if obj.method(name1) == obj.method(name2)
  assertion_unexpected_value(obj, msg || "expected #{assertion_inspect(obj)} " \
                                         "to have methods #{name1} and #{name2} be aliased")
end

#assert_all(enum, msg = nil) ⇒ Object



203
204
205
206
207
# File 'lib/ruptr/assertions.rb', line 203

def assert_all(enum, msg = nil, &)
  bump_assertions_count
  return if enum.all?(&)
  assertion_unexpected_value(enum, msg || "expected truthy block for all elements of #{assertion_inspect(enum)}")
end

#assert_block(msg = nil) ⇒ Object



104
# File 'lib/ruptr/assertions.rb', line 104

def assert_block(msg = nil) = assert(yield, msg)

#assert_boolean(val, msg = nil) ⇒ Object



167
# File 'lib/ruptr/assertions.rb', line 167

def assert_boolean(val, msg = nil) = assert_includes([true, false], val, msg)

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



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

def assert_equal(exp, act, msg = nil)
  bump_assertions_count
  return if exp == act
  assertion_raise(EquivalenceAssertionError,
                  msg || "expected #{assertion_inspect(act)} to be #{assertion_inspect(exp)}",
                  actual: assertion_capture_value(act),
                  expected: assertion_capture_value(exp))
end

#assert_fail_assertion(msg = nil) ⇒ Object



415
416
417
# File 'lib/ruptr/assertions.rb', line 415

def assert_fail_assertion(msg = nil, &)
  assert_raises(AssertionError, msg || "expected failed assertion", &)
end

#assert_false(val, msg = nil) ⇒ Object



166
# File 'lib/ruptr/assertions.rb', line 166

def assert_false(val, msg = nil) = assert_equal(false, val, msg)

#assert_golden(actual, msg = nil, key: nil) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
# File 'lib/ruptr/assertions.rb', line 284

def assert_golden(actual, msg = nil, key: nil)
  assertion_set_golden_trial_value(key, actual)
  # NOTE: does not yield if there are no golden value saved yet
  assertion_yield_golden_value(key) do |golden|
    if block_given?
      yield golden, actual, msg
    else
      assert_equal golden, actual, msg
    end
  end
end

#assert_in_delta(exp, act, delta = 0.001, msg = nil) ⇒ Object

Raises:

  • (ArgumentError)


229
230
231
232
233
234
235
# File 'lib/ruptr/assertions.rb', line 229

def assert_in_delta(exp, act, delta = 0.001, msg = nil)
  raise ArgumentError if delta.negative?
  bump_assertions_count
  return if (exp - act).abs <= delta
  assertion_unexpected_value(act, msg || "expected #{assertion_inspect(act)} to be within " \
                                         "#{assertion_inspect(delta)} of #{assertion_inspect(exp)}")
end

#assert_in_epsilon(exp, act, epsilon = 0.001, msg = nil) ⇒ Object



247
248
249
# File 'lib/ruptr/assertions.rb', line 247

def assert_in_epsilon(exp, act, epsilon = 0.001, msg = nil)
  assert_in_delta(exp, act, exp.zero? ? epsilon : exp.abs * epsilon, msg)
end

#assert_match(pat, act, msg = nil) ⇒ Object



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

def assert_match(pat, act, msg = nil)
  bump_assertions_count
  pat = Regexp.new(Regexp.quote(pat)) if pat.is_a?(String)
  return $~ if pat =~ act
  assertion_unexpected_value(act, msg || "expected #{assertion_inspect(pat)} " \
                                         "to match #{assertion_inspect(act)}")
end

#assert_nothing_raised(*expected) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/ruptr/assertions.rb', line 397

def assert_nothing_raised(*expected)
  msg = expected.pop if expected.last.is_a?(String)
  begin
    r = yield
  rescue Exception
    raise unless assert_raise_exception_matches?($!, *expected)
    bump_assertions_count
    assertion_unexpected_exception($!, msg)
  else
    bump_assertions_count
  end
  r
end

#assert_nothing_thrown(msg = nil) ⇒ Object



436
437
438
439
440
441
442
443
444
445
446
# File 'lib/ruptr/assertions.rb', line 436

def assert_nothing_thrown(msg = nil)
  begin
    r = yield
  rescue UncaughtThrowError
    bump_assertions_count
    assertion_failed(msg || "block threw #{assertion_inspect($!.tag)} unexpectedly")
  else
    bump_assertions_count
  end
  r
end

#assert_operator(arg1, oper, arg2 = (no_arg2 = true; nil), msg = nil) ⇒ Object Also known as: assert_compare



183
184
185
186
187
188
189
# File 'lib/ruptr/assertions.rb', line 183

def assert_operator(arg1, oper, arg2 = (no_arg2 = true; nil), msg = nil)
  return assert_predicate(arg1, oper, msg) if no_arg2
  bump_assertions_count
  return if arg1.public_send(oper, arg2)
  assertion_unexpected_value(arg1, msg || "expected #{assertion_inspect(arg1)} " \
                                          "to be #{oper} #{assertion_inspect(arg2)}")
end

#assert_output(expected_stdout = nil, expected_stderr = nil) ⇒ Object



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/ruptr/assertions.rb', line 452

def assert_output(expected_stdout = nil, expected_stderr = nil, &)
  begin
    actual_stdout, actual_stderr = capture_io(&)
  rescue Exception
    raise if assertion_exception?($!) || !standard_exception?($!)
    bump_assertions_count
    assertion_unexpected_exception($!)
  end
  bump_assertions_count
  unless expected_stderr.nil? || expected_stderr === actual_stderr
    assertion_unexpected_value(actual_stderr,
                               "expected stderr to be #{assertion_inspect(expected_stderr)} " \
                               "instead of #{assertion_inspect(actual_stderr)}")
  end
  unless expected_stdout.nil? || expected_stdout === actual_stdout
    assertion_unexpected_value(actual_stdout,
                               "expected stdout to be #{assertion_inspect(expected_stdout)} " \
                               "instead of #{assertion_inspect(actual_stdout)}")
  end
end

#assert_path_exists(path, msg = nil) ⇒ Object Also known as: assert_path_exist



257
258
259
# File 'lib/ruptr/assertions.rb', line 257

def assert_path_exists(path, msg = nil)
  assert File.exist?(path), msg || "expected path #{assertion_inspect(path)} to exist"
end

#assert_pattern(msg = nil) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/ruptr/assertions.rb', line 147

def assert_pattern(msg = nil)
  yield
rescue NoMatchingPatternError
  bump_assertions_count
  assertion_failed(msg || $!.message)
else
  bump_assertions_count
end

#assert_predicate(target, pred, msg = nil) ⇒ Object



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

def assert_predicate(target, pred, msg = nil)
  bump_assertions_count
  return if target.public_send(pred)
  assertion_unexpected_value(target, msg || "expected #{assertion_inspect(target)} to be #{pred}")
end

#assert_raise(*expected) ⇒ Object



392
393
394
395
# File 'lib/ruptr/assertions.rb', line 392

def assert_raise(*expected, &)
  msg = expected.pop if expected.last.is_a?(String)
  assert_raise_with_message(expected, nil, msg, &)
end

#assert_raise_exception_matches?(exception, *expected) ⇒ Boolean

test/unit’s

Returns:

  • (Boolean)


366
367
368
369
370
371
372
373
# File 'lib/ruptr/assertions.rb', line 366

def assert_raise_exception_matches?(exception, *expected)
  return true if expected.empty?
  expected_modules, expected_instances = expected.partition { |v| v.is_a?(Module) }
  expected_classes, expected_modules = expected_modules.partition { |v| v.is_a?(Class) }
  expected_modules.any? { |m| exception.is_a?(m) } ||
    expected_classes.any? { |c| exception.instance_of?(c) } ||
    expected_instances.any? { |e| e.class == exception.class && e.message == exception.message }
end

#assert_raise_message(expected_message, msg = nil) ⇒ Object



411
412
413
# File 'lib/ruptr/assertions.rb', line 411

def assert_raise_message(expected_message, msg = nil, &)
  assert_raise_with_message([], expected_message, msg, &)
end

#assert_raise_with_message(expected, expected_message, msg = nil) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/ruptr/assertions.rb', line 375

def assert_raise_with_message(expected, expected_message, msg = nil)
  yield
rescue Exception
  if assert_raise_exception_matches?($!, *expected) &&
     (expected_message.nil? || expected_message === $!.message)
    bump_assertions_count
    $!
  else
    raise if passthrough_exception?($!)
    bump_assertions_count
    assertion_unexpected_exception($!, msg)
  end
else
  bump_assertions_count
  assertion_failed(msg || "no exceptions raised")
end

#assert_raises(*expected) ⇒ Object Also known as: assert_raise_kind_of

minitest’s



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/ruptr/assertions.rb', line 344

def assert_raises(*expected)
  msg = expected.pop if expected.last.is_a?(String)
  expected = [StandardError] if expected.empty?
  begin
    yield
  rescue *expected
    bump_assertions_count
    $!
  rescue Exception
    raise if passthrough_exception?($!)
    bump_assertions_count
    assertion_unexpected_exception($!, msg)
  else
    bump_assertions_count
    assertion_failed(msg || "no exceptions raised")
  end
end

#assert_send(array, msg = nil) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/ruptr/assertions.rb', line 209

def assert_send(array, msg = nil)
  target, method_name, *args = array
  bump_assertions_count
  return if target.__send__(method_name, *args)
  assertion_unexpected_value(target,
                             msg || "expected #{assertion_inspect(target)}.#{method_name}" \
                                    "(#{args.map { |arg| assertion_inspect(arg) }.join(', ')}) to be truthy")
end

#assert_silentObject



473
474
475
# File 'lib/ruptr/assertions.rb', line 473

def assert_silent(&)
  assert_output('', '', &)
end

#assert_throws(tag, msg = nil) ⇒ Object Also known as: assert_throw



419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/ruptr/assertions.rb', line 419

def assert_throws(tag, msg = nil)
  caught = true
  r = catch(tag) do
    yield tag
    caught = false
  rescue Exception
    raise if assertion_exception?($!) || !standard_exception?($!)
    bump_assertions_count
    assertion_unexpected_exception($!)
  end
  bump_assertions_count
  return r if caught
  assertion_failed(msg || "expected block to throw #{assertion_inspect(tag)}")
end

#assert_true(val, msg = nil) ⇒ Object



165
# File 'lib/ruptr/assertions.rb', line 165

def assert_true(val, msg = nil) = assert_equal(true, val, msg)

#assertion_exception?(ex) ⇒ Boolean

Returns:

  • (Boolean)


338
# File 'lib/ruptr/assertions.rb', line 338

def assertion_exception?(ex) = ex.is_a?(AssertionError)

#assertion_failed(msg) ⇒ Object



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

def assertion_failed(msg)
  assertion_raise(AssertionError, msg)
end

#assertion_raise(klass, msg) ⇒ Object

Raises:

  • (klass)


64
65
66
# File 'lib/ruptr/assertions.rb', line 64

def assertion_raise(klass, msg, ...)
  raise klass.new(get_assertion_message(msg), ...)
end

#assertion_unexpected_exception(exc, msg = nil) ⇒ Object



72
73
74
# File 'lib/ruptr/assertions.rb', line 72

def assertion_unexpected_exception(exc, msg = nil)
  assertion_raise(UnexpectedExceptionError, msg || "unexpected exception: #{assertion_inspect(exc)}")
end

#assertion_unexpected_value(val, msg = nil) ⇒ Object



76
77
78
79
# File 'lib/ruptr/assertions.rb', line 76

def assertion_unexpected_value(val, msg = nil)
  assertion_raise(UnexpectedValueError, msg || "unexpected value: #{assertion_inspect(val)}",
                  actual: assertion_capture_value(val))
end

#capture_ioObject Also known as: capture_output



448
# File 'lib/ruptr/assertions.rb', line 448

def capture_io(&) = Ruptr::CaptureOutput.capture_output(&)

#flunk(msg = nil) ⇒ Object



85
86
87
88
# File 'lib/ruptr/assertions.rb', line 85

def flunk(msg = nil)
  bump_assertions_count
  assertion_failed(msg || "Flunked")
end

#omit(msg = nil) ⇒ Object



488
489
490
491
# File 'lib/ruptr/assertions.rb', line 488

def omit(msg = nil)
  skip(msg) unless block_given?
  bump_skipped_blocks_count
end

#pass(_msg = nil) ⇒ Object



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

def pass(_msg = nil)
  bump_assertions_count
end

#passthrough_exception?(ex) ⇒ Boolean

Returns:

  • (Boolean)


336
# File 'lib/ruptr/assertions.rb', line 336

def passthrough_exception?(ex) = case ex when *PASSTHROUGH_EXCEPTIONS then true else false end

#pend(msg = nil) ⇒ Object



482
483
484
485
486
# File 'lib/ruptr/assertions.rb', line 482

def pend(msg = nil, &)
  skip(msg) unless block_given?
  assert_raises(StandardError, msg, &)
  bump_skipped_blocks_count
end

#refute(val, msg = nil) ⇒ Object Also known as: assert_not



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

def refute(val, msg = nil)
  bump_assertions_count
  return unless val
  assertion_unexpected_value(val, msg || "expected #{assertion_inspect(val)} to be falsey")
end

#refute_alias_method(obj, name1, name2, msg = nil) ⇒ Object Also known as: assert_not_alias_method



275
276
277
278
279
280
# File 'lib/ruptr/assertions.rb', line 275

def refute_alias_method(obj, name1, name2, msg = nil)
  bump_assertions_count
  return unless obj.method(name1) == obj.method(name2)
  assertion_unexpected_value(obj, msg || "expected #{assertion_inspect(obj)} " \
                                         "to not have methods #{name1} and #{name2} be aliased")
end

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



105
# File 'lib/ruptr/assertions.rb', line 105

def refute_block(msg = nil) = refute(yield, msg)

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



117
118
119
120
121
122
123
124
# File 'lib/ruptr/assertions.rb', line 117

def refute_equal(exp, act, msg = nil)
  bump_assertions_count
  return unless exp == act
  assertion_raise(EquivalenceRefutationError,
                  msg || "expected #{assertion_inspect(act)} to be something else",
                  actual: assertion_capture_value(act),
                  unexpected: assertion_capture_value(exp))
end

#refute_in_delta(exp, act, delta = 0.001, msg = nil) ⇒ Object Also known as: assert_not_in_delta

Raises:

  • (ArgumentError)


237
238
239
240
241
242
243
# File 'lib/ruptr/assertions.rb', line 237

def refute_in_delta(exp, act, delta = 0.001, msg = nil)
  raise ArgumentError if delta.negative?
  bump_assertions_count
  return if (exp - act).abs > delta
  assertion_unexpected_value(act, msg || "expected #{assertion_inspect(act)} not to be within " \
                                         "#{assertion_inspect(delta)} of #{assertion_inspect(exp)}")
end

#refute_in_epsilon(exp, act, epsilon = 0.001, msg = nil) ⇒ Object Also known as: assert_not_in_epsilon



251
252
253
# File 'lib/ruptr/assertions.rb', line 251

def refute_in_epsilon(exp, act, epsilon = 0.001, msg = nil)
  refute_in_delta(exp, act, exp.zero? ? epsilon : exp.abs * epsilon, msg)
end

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



136
137
138
139
140
141
142
# File 'lib/ruptr/assertions.rb', line 136

def refute_match(pat, act, msg = nil)
  bump_assertions_count
  pat = Regexp.new(Regexp.quote(pat)) if pat.is_a?(String)
  return unless pat =~ act
  assertion_unexpected_value(act, msg || "expected #{assertion_inspect(pat)} " \
                                         "not to match #{assertion_inspect(act)}")
end

#refute_operator(arg1, oper, arg2 = (no_arg2 = true; nil), msg = nil) ⇒ Object Also known as: assert_not_operator



193
194
195
196
197
198
199
# File 'lib/ruptr/assertions.rb', line 193

def refute_operator(arg1, oper, arg2 = (no_arg2 = true; nil), msg = nil)
  return refute_predicate(arg1, oper, msg) if no_arg2
  bump_assertions_count
  return unless arg1.public_send(oper, arg2)
  assertion_unexpected_value(arg1, msg || "expected #{assertion_inspect(arg1)} " \
                                          "not to be #{oper} #{assertion_inspect(arg2)}")
end

#refute_path_exists(path, msg = nil) ⇒ Object Also known as: assert_path_not_exist



261
262
263
# File 'lib/ruptr/assertions.rb', line 261

def refute_path_exists(path, msg = nil)
  refute File.exist?(path), msg || "expected path #{assertion_inspect(path)} not to exist"
end

#refute_pattern(msg = nil) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/ruptr/assertions.rb', line 156

def refute_pattern(msg = nil)
  yield
rescue NoMatchingPatternError
  bump_assertions_count
else
  bump_assertions_count
  assertion_failed(msg || "expected pattern not to match")
end

#refute_predicate(target, pred, msg = nil) ⇒ Object Also known as: assert_not_predicate



175
176
177
178
179
# File 'lib/ruptr/assertions.rb', line 175

def refute_predicate(target, pred, msg = nil)
  bump_assertions_count
  return unless target.public_send(pred)
  assertion_unexpected_value(target, msg || "expected #{assertion_inspect(target)} not to be #{pred}")
end

#refute_send(array, msg = nil) ⇒ Object Also known as: assert_not_send



218
219
220
221
222
223
224
225
# File 'lib/ruptr/assertions.rb', line 218

def refute_send(array, msg = nil)
  target, method_name, *args = array
  bump_assertions_count
  return unless target.__send__(method_name, *args)
  assertion_unexpected_value(target,
                             msg || "expected #{assertion_inspect(target)}.#{method_name}" \
                                    "(#{args.map { |arg| assertion_inspect(arg) }.join(', ')}) not to be truthy")
end

#skip(msg = nil) ⇒ Object

Raises:

  • (ArgumentError)


477
478
479
480
# File 'lib/ruptr/assertions.rb', line 477

def skip(msg = nil)
  raise ArgumentError if block_given?
  assertion_raise(SkippedException, msg || "skipped")
end

#standard_exception?(ex) ⇒ Boolean

Returns:

  • (Boolean)


340
# File 'lib/ruptr/assertions.rb', line 340

def standard_exception?(ex) = ex.is_a?(StandardError)