Module: Minitest::Assertions

Defined in:
lib/minitest/strict.rb

Overview

Strict assertion methods that require literal true or false return values instead of truthy or falsey.

Instance Method Summary collapse

Instance Method Details

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

Fails unless act is eql? to exp. Uses Object#eql? for equality without type coercion. Eg:

assert_eql 1, 1     # pass
assert_eql 1, 1.0   # fail


51
52
53
54
# File 'lib/minitest/strict.rb', line 51

def assert_eql exp, act, msg = nil
  msg = message(msg) { "Expected #{mu_pp act} to be eql? to #{mu_pp exp}" }
  assert exp.eql?(act), msg
end

#assert_false(obj, msg = nil) ⇒ Object

Fails unless obj is literally false (not just falsey).



23
24
25
26
# File 'lib/minitest/strict.rb', line 23

def assert_false obj, msg = nil
  msg = message(msg) { "Expected #{mu_pp obj} to be false" }
  assert obj.equal?(false), msg
end

#assert_nil(obj, msg = nil) ⇒ Object

Fails unless obj is nil. Uses equal? for identity check.



141
142
143
144
# File 'lib/minitest/strict.rb', line 141

def assert_nil obj, msg = nil
  msg = message(msg) { "Expected #{mu_pp obj} to be nil" }
  assert obj.equal?(nil), msg
end

#assert_operator(o1, op, o2 = UNDEFINED, msg = nil) ⇒ Object

For testing with binary operators. Requires the operator to return literal true, not just a truthy value. Falls through to assert_predicate if o2 is not given. Eg:

assert_operator 5, :<=, 4


100
101
102
103
104
105
106
# File 'lib/minitest/strict.rb', line 100

def assert_operator o1, op, o2 = UNDEFINED, msg = nil
  return assert_predicate o1, op, msg if o2 == UNDEFINED

  assert_respond_to o1, op, include_all: true
  msg = message(msg) { "Expected #{mu_pp o1} to be #{op} #{mu_pp o2}" }
  assert_equal true, o1.__send__(op, o2), msg
end

#assert_predicate(o1, op, msg = nil) ⇒ Object

Fails unless o1 is op. Requires op to return literal true, not just a truthy value.

assert_predicate str, :empty?


75
76
77
78
79
# File 'lib/minitest/strict.rb', line 75

def assert_predicate o1, op, msg = nil
  assert_respond_to o1, op, include_all: true
  msg = message(msg) { "Expected #{mu_pp o1} to be #{op}" }
  assert_equal true, o1.__send__(op), msg
end

#assert_true(obj, msg = nil) ⇒ Object

Fails unless obj is literally true (not just truthy).



15
16
17
18
# File 'lib/minitest/strict.rb', line 15

def assert_true obj, msg = nil
  msg = message(msg) { "Expected #{mu_pp obj} to be true" }
  assert obj.equal?(true), msg
end

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

Fails if act is eql? to exp.



59
60
61
62
# File 'lib/minitest/strict.rb', line 59

def refute_eql exp, act, msg = nil
  msg = message(msg) { "Expected #{mu_pp act} to not be eql? to #{mu_pp exp}" }
  refute exp.eql?(act), msg
end

#refute_false(obj, msg = nil) ⇒ Object

Fails if obj is literally false.



39
40
41
42
# File 'lib/minitest/strict.rb', line 39

def refute_false obj, msg = nil
  msg = message(msg) { "Expected false to not be false" }
  refute obj.equal?(false), msg
end

#refute_match(matcher, obj, msg = nil) ⇒ Object

Fails if matcher matches obj. Redefined because Minitest 6 implements this via refute_operator, but =~ returns nil or an Integer -- never false -- so the strict refute_operator above would fail even when matcher does not match. There is no boolean contract to enforce here, so this restores the standard Minitest 5 behavior.



131
132
133
134
135
136
# File 'lib/minitest/strict.rb', line 131

def refute_match matcher, obj, msg = nil
  assert_respond_to matcher, :=~
  msg = message(msg) { "Expected #{mu_pp matcher} to not match #{mu_pp obj}" }
  matcher = Regexp.new Regexp.escape matcher if matcher.is_a?(String)
  refute matcher =~ obj, msg
end

#refute_nil(obj, msg = nil) ⇒ Object

Fails if obj is nil. Uses equal? for identity check.



149
150
151
152
# File 'lib/minitest/strict.rb', line 149

def refute_nil obj, msg = nil
  msg = message(msg) { "Expected nil to not be nil" }
  refute obj.equal?(nil), msg
end

#refute_operator(o1, op, o2 = UNDEFINED, msg = nil) ⇒ Object

For testing with binary operators. Requires the operator to return literal false, not just a falsey value. Falls through to refute_predicate if o2 is not given. Eg:

refute_operator 1, :>, 2


115
116
117
118
119
120
121
# File 'lib/minitest/strict.rb', line 115

def refute_operator o1, op, o2 = UNDEFINED, msg = nil
  return refute_predicate o1, op, msg if o2 == UNDEFINED

  assert_respond_to o1, op, include_all: true
  msg = message(msg) { "Expected #{mu_pp o1} to not be #{op} #{mu_pp o2}" }
  assert_equal false, o1.__send__(op, o2), msg
end

#refute_predicate(o1, op, msg = nil) ⇒ Object

Fails if o1 is op. Requires op to return literal false, not just a falsey value.

refute_predicate str, :empty?


87
88
89
90
91
# File 'lib/minitest/strict.rb', line 87

def refute_predicate o1, op, msg = nil
  assert_respond_to o1, op, include_all: true
  msg = message(msg) { "Expected #{mu_pp o1} to not be #{op}" }
  assert_equal false, o1.__send__(op), msg
end

#refute_true(obj, msg = nil) ⇒ Object

Fails if obj is literally true.



31
32
33
34
# File 'lib/minitest/strict.rb', line 31

def refute_true obj, msg = nil
  msg = message(msg) { "Expected true to not be true" }
  refute obj.equal?(true), msg
end