Module: Kernel
- Defined in:
- lib/brass.rb,
lib/brass/expect.rb
Class Method Summary collapse
-
.assert(truth, *raise_arguments) ⇒ Object
Universal assertion method.
-
.fail!(*raise_arguments) ⇒ Object
Alternate for #fail that also sets assertion flag to
true
. -
.refute(truth, *raise_arguments) ⇒ Object
Universal refutation method (opposite of ‘#assert`).
Instance Method Summary collapse
-
#expect(error_class) ⇒ Object
Executate a block asserting that a type of error will be raised.
Class Method Details
.assert(truth, *raise_arguments) ⇒ Object
Universal assertion method.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/brass.rb', line 29 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
.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/brass.rb', line 65 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 = StandardError 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`).
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/brass.rb', line 47 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} was thrown") end end |