Module: Kernel

Defined in:
lib/brass.rb,
lib/brass/expect.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assert(truth, *raise_arguments) ⇒ Object

Universal assertion method.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/brass.rb', line 63

def assert(truth, *raise_arguments)
  $ASSERTION_COUNTS[:total] += 1
  if truth
    $ASSERTION_COUNTS[:pass] += 1
  else
    $ASSERTION_COUNTS[:fail] += 1
    # if fail set assertion=true then just,
    #   fail *raise_arguments
    # but alas ...
    fail! *raise_arguments
  end
end

.fail!(*raise_arguments) ⇒ Object

Alternate for #fail that also sets assertion flag to true.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/brass.rb', line 99

def fail!(*raise_arguments)
  backtrace = case raise_arguments.last
              when Array
                raise_arguments.pop
              else
                nil
              end

  exception = case raise_arguments.first
              when Exception
                raise_arguments.shift
              when Class
                raise ArgumentError unless Exception > raise_arguments.first
                error_class = raise_arguments.shift
                error_class.new(*raise_arguments)
              else
                error_class = $! || RuntimeError
                error_class.new(*raise_arguments)
              end

  exception.set_backtrace(backtrace) if backtrace
  exception.set_assertion(true)

  fail exception
end

.refute(truth, *raise_arguments) ⇒ Object

Universal refutation method (opposite of #assert).



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/brass.rb', line 81

def refute(truth, *raise_arguments)
  $ASSERTION_COUNTS[:total] += 1
  if truth
    $ASSERTION_COUNTS[:fail] += 1
    # if fail set assertion=true then just,
    #   fail *raise_arguments
    # but alas ...
    fail! *raise_arguments
  else
    $ASSERTION_COUNTS[:pass] += 1
  end
end

Instance Method Details

#expect(error_class) ⇒ Object

Executate a block asserting that a type of error will be raised.

Presently this is not part of brass by default, as whether it should be is under debate. So this file must be required separately:

require 'brass/expect'


12
13
14
15
16
17
18
19
20
21
# File 'lib/brass/expect.rb', line 12

def expect(error_class) #:yield:
  begin
    yield
    assert(false, error_class, "#{error_class} expected but none thrown")
  rescue error_class
    assert(true)
  rescue Exception => err
    assert(false, error_class, "#{error_class} expected but #{err.class} was thrown")
  end
end