Module: AbortIf::Assert

Defined in:
lib/assert/assert.rb

Defined Under Namespace

Classes: AssertionFailureError

Instance Method Summary collapse

Instance Method Details

#assert(test, msg = "Assertion failed", *args) ⇒ nil

If test is true, return nil, else raise AssertionFailureError with the given message.

Examples:

Test is true

a, b = 1, 1
assert a == b, "%d should equal %d", a, b
#=> nil

Test is false

arr = [1,2,3]
assert arr.empty?,
       "Array should be empty, had %d items"
       arr.count
# raises AssertionFailureError with given msg

Parameters:

  • test

    Some object or test

  • msg (String) (defaults to: "Assertion failed")

    the message passed to the AssertionFailureError

  • *args

    arguments to interpolate into msg

Returns:

  • (nil)

    if test is truthy

Raises:



48
49
50
51
52
# File 'lib/assert/assert.rb', line 48

def assert test, msg="Assertion failed", *args
  unless test
    raise AssertionFailureError, msg % args
  end
end

#assert_has_key(hash, key) ⇒ nil

If the key is present in hash, return nil, else raise AssertionFailureError.

Examples:

Passing

assert_has_key {a: 2, b: 1}, :a
#=> nil

Failing

assert_has_key {a: 2, b: 1}, :c
# raises AssertionFailureError

Parameters:

  • hash (#has_key?)

    anything that responds to :has_key?

  • key

    the key to check for

Returns:

  • (nil)

    if hash has key

Raises:



154
155
156
157
158
159
# File 'lib/assert/assert.rb', line 154

def assert_has_key hash, key
  check_responds_to hash, :has_key?

  assert hash.has_key?(key),
         "Expected hash to include key"
end

#assert_includes(coll, obj) ⇒ nil

If coll includes obj, return nil, else raise AssertionFailureError.

Examples:

Passing

assert_includes [1,2,3], 1
#=> nil

Failing

assert_includes [1,2,3], 10
# raises AssertionFailureError

Parameters:

  • coll (#include?)

    some collection

  • obj

    the object to check for

Returns:

  • (nil)

    if coll includes obj

Raises:



86
87
88
89
90
91
# File 'lib/assert/assert.rb', line 86

def assert_includes coll, obj
  check_responds_to coll, :include?

  assert coll.include?(obj),
         "Expected coll to include obj"
end

#assert_keys(coll, *keys) ⇒ nil

If any key is not present in coll, raise AssertionFailureError, else return nil.

Examples:

Passing

assert_keys {a: 2, b: 1}, :a, :b
#=> nil

Failing

assert_keys {a: 2, b: 1}, :a, :b, :c
# raises AssertionFailureError

Parameters:

  • coll (#[])

    collection of things

  • *keys

    keys to check for

Returns:

  • (nil)

    if coll has every key

Raises:



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

def assert_keys coll, *keys
  check_responds_to coll, :[]

  check_not_empty keys

  assert keys.all? { |key| coll[key] },
         "Expected coll to include all keys"
end

#assert_length(coll, len) ⇒ nil

If coll is given length, return nil, else raise AssertionFailureError.

Parameters:

  • coll (#length)

    anything that responds to :length

  • len (Number)

    the length to check for

Returns:

  • (nil)

    if length of coll matches given len

Raises:



196
197
198
199
200
201
202
# File 'lib/assert/assert.rb', line 196

def assert_length coll, len
  check_responds_to coll, :length

  assert coll.length == len,
         "Expected coll to have %d items",
         len
end

#refute(test, msg = "Assertion failed", *args) ⇒ nil

Note:

The opposite of assert

If test is false or nil, return nil, else raise AssertionFailureError.

Parameters:

  • test

    Some object or test

  • msg (String) (defaults to: "Assertion failed")

    the message passed to the AssertionFailureError

  • *args

    arguments to interpolate into msg

Returns:

  • (nil)

    if thest is false or nil

Raises:



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

def refute test, msg="Assertion failed", *args
  assert !test, msg, *args
end

#refute_has_key(hash, key) ⇒ nil

If the key is present in hash, raise AssertionFailureError, else return nil.

Examples:

Passing

refute_has_key {a: 2, b: 1}, :a
# raises AssertionFailureError

Failing

refute_has_key {a: 2, b: 1}, :c
#=> nil

Parameters:

  • hash (#has_key?)

    anything that responds to :has_key?

  • key

    the key to check for

Returns:

  • (nil)

    if hash does not have key

Raises:



178
179
180
181
182
183
# File 'lib/assert/assert.rb', line 178

def refute_has_key hash, key
  check_responds_to hash, :has_key?

  refute hash.has_key?(key),
         "Expected hash not to include key"
end

#refute_includes(coll, obj) ⇒ nil

If coll includes obj, raise AssertionFailureError, else return nil.

Parameters:

  • coll (#include?)

    some collection

  • obj

    the object to check for

Returns:

  • (nil)

    if coll does not include obj

Raises:



102
103
104
105
106
107
# File 'lib/assert/assert.rb', line 102

def refute_includes coll, obj
  check_responds_to coll, :include?

  refute coll.include?(obj),
         "Expected coll not to include obj"
end