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.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/brass.rb', line 44

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.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/brass.rb', line 80

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`).



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

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

#assert(truth, *raise_arguments) ⇒ Object (private)

Universal assertion method.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/brass.rb', line 44

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

#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

#fail!(*raise_arguments) ⇒ Object (private)

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/brass.rb', line 80

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 (private)

Universal refutation method (opposite of ‘#assert`).



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

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