Class: StableMatch::Test

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/stable_match/test.rb

Instance Method Summary collapse

Instance Method Details

#__assert__Object



6
# File 'lib/stable_match/test.rb', line 6

alias_method '__assert__' , 'assert'

#assert(*args, &block) ⇒ Object

See: github.com/ahoward/testing.rb/blob/08dd643239a23543409ecb5fee100181f1621794/lib/testing.rb#L82-107

Override assert to take a few different kinds of options Most notable argument type is a block that: -> asserts no exceptions were raised -> asserts the result of the block is truthy -> returns the result of the block



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
47
# File 'lib/stable_match/test.rb', line 15

def assert( *args , &block )
  if args.size == 1 and args.first.is_a?(Hash)
    options  = args.first
    expected = getopt(:expected, options){ missing }
    actual   = getopt(:actual, options){ missing }
    if expected == missing and actual == missing
      actual , expected , *_ = options.to_a.flatten
    end
    expected = expected.call() if expected.respond_to?(:call)
    actual   = actual.call() if actual.respond_to?(:call)
    assert_equal expected , actual
  end

  if block
    label   = "assert(#{ args.join(' ') })"
    result  = nil
    raised  = false
    result  = begin
                block.call
              rescue Object => e
                raised = e
                false
              end
    __assert__ !raised , ( raised.message rescue label )
    __assert__ result  , label
    result
  else
    result = args.shift
    label  = "assert(#{ args.join(' ') })"
    __assert__ result , label
    result
  end
end