Module: Passert

Defined in:
lib/passert.rb,
lib/passert/version.rb

Defined Under Namespace

Classes: AssertionFailed

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.assert(first, second = (second_omitted = true; nil)) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/passert.rb', line 7

def assert(first, second = (second_omitted = true; nil))
  # Want to support:
  #   assert(x)                       # Truthiness.
  #   assert(thing, other)            # Trip-equals.
  #   assert([thing1, thing2], other) # Multiple Trip-equals.

  if second_omitted
    comparison = nil
    truth      = first
  else
    comparison = first
    truth      = second
  end

  pass =
    if second_omitted
      truth
    elsif comparison.is_a?(Array)
      comparison.any? { |k| k === truth }
    else
      comparison === truth
    end

  return truth if pass

  message =
    if block_given?
      yield
    elsif comparison
      "Expected #{comparison.inspect}, got #{truth.inspect}!"
    else
      "Assertion failed!"
    end

  if message.is_a?(String)
    raise AssertionFailed, message
  else
    raise message
  end
end