Module: Minitest::Assertions

Defined in:
lib/minitest-bonus-assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert_between(*args) ⇒ Object

Fails unless exp is between lo and hi, or is in range. This test is exclusive of the boundaries. That is:

assert_between 1, 10, 1

will return false, but:

assert_between 0.99, 10.1, 1

will return true.

:call-seq:

assert_between lo, hi, exp, msg = nil
assert_between range, exp, msg = nil


39
40
41
42
43
44
45
46
47
48
# File 'lib/minitest-bonus-assertions.rb', line 39

def assert_between(*args)
  lo, hi, exp, msg = if args.first.kind_of?(Range)
                       [args.first.begin, args.first.end, args[1], args[2]]
                     else
                       args[0..3]
                     end
  lo, hi = hi, lo if lo > hi
  msg = message(msg) { "Expected #{mu_pp(exp)} to be between #{mu_pp(lo)} and #{mu_pp(hi)}" }
  assert (lo < exp && exp < hi), msg
end

#assert_false(obj, msg = nil) ⇒ Object

Fails unless obj is literally false.



10
11
12
13
# File 'lib/minitest-bonus-assertions.rb', line 10

def assert_false obj, msg = nil
  msg = message(msg) { "<false> expected but was #{mu_pp(obj)}" }
  assert obj == false, msg
end

#assert_has_keys(obj, keys, msg = nil) ⇒ Object Also known as: refute_missing_keys

Fails unless obj has all of the keys listed.



53
54
55
56
57
# File 'lib/minitest-bonus-assertions.rb', line 53

def assert_has_keys obj, keys, msg = nil
  keys = [ keys ] unless keys.kind_of?(Array)
  msg = message(msg) { "Expected #{mu_pp(obj)} to include all keys #{mu_pp(keys)}" }
  keys.all? { |key| assert obj.key?(key), msg }
end

#assert_missing_keys(obj, keys, msg = nil) ⇒ Object Also known as: refute_has_keys

Fails if obj has any of the keys listed.



63
64
65
66
67
68
69
# File 'lib/minitest-bonus-assertions.rb', line 63

def assert_missing_keys obj, keys, msg = nil
  keys = [ keys ] unless keys.kind_of?(Array)
  msg = message(msg) {
    "Expected #{mu_pp(obj)} not to include any of these keys #{mu_pp(keys)}"
  }
  keys.none? { |key| refute obj.key?(key), msg }
end

#assert_raises_with_message(exp, exp_msg, msg = nil) ⇒ Object

Fails unless the block raises exp with the message exp_msg. Returns the exception matched so you can check other attributes.



76
77
78
79
80
81
82
83
84
85
# File 'lib/minitest-bonus-assertions.rb', line 76

def assert_raises_with_message exp, exp_msg, msg = nil
  msg = message(msg) { "#{mu_pp(exp)} exception expected with message #{mu_pp(exp_msg)}" }

  exception = assert_raises exp do
    yield
  end

  assert_equal exp_msg, exception.message, msg
  exception
end

#assert_set_equal(expected, actual, msg = nil) ⇒ Object

Fails unless the set from actual matches the set from exp.



90
91
92
93
94
95
96
# File 'lib/minitest-bonus-assertions.rb', line 90

def assert_set_equal expected, actual, msg = nil
  require 'set'
  msg = message(msg) {
    "Expected #{mu_pp(actual)} to be set equivalent to #{mu_pp(expected)}"
  }
  assert_equal Set.new(expected), Set.new(actual), msg
end

#assert_true(obj, msg = nil) ⇒ Object

Fails unless obj is literally true.



18
19
20
21
# File 'lib/minitest-bonus-assertions.rb', line 18

def assert_true obj, msg = nil
  msg = message(msg) { "<true> expected but was #{mu_pp(obj)}" }
  assert obj == true, msg
end

#refute_set_equal(expected, actual, msg = nil) ⇒ Object

Fails unless the set from actual differs from the set from exp.



101
102
103
104
105
106
107
# File 'lib/minitest-bonus-assertions.rb', line 101

def refute_set_equal expected, actual, msg = nil
  require 'set'
  msg = message(msg) {
    "Expected #{mu_pp(actual)} not to be set equivalent to #{mu_pp(expected)}"
  }
  refute_equal Set.new(expected), Set.new(actual), msg
end