Method: Whitestone::Assertion::Custom#run
- Defined in:
- lib/whitestone/custom_assertions.rb
#run ⇒ Object
Custom#run
Returns true or false for pass or fail, just like other assertions.
The Config object provides the block to run, while @context provides the context in which to run it.
We trap FailureOccurred errors because as a custom assertion we need to take responsibility for the errors, and wrap some information around the error message.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/whitestone/custom_assertions.rb', line 112 def run test_code = @config.run_block @context.instance_eval &test_code # ^^^ This gives the test code access to the 'test' method that is so # important for running a custom assertion. # See the notes on CustomTestContext for an example. return true # the custom test passed rescue FailureOccurred => f # We are here because an assertion failed. That means _this_ (custom) # assertion has failed. We need to build an error message and raise # FailureOccurred ourselves. = String.new.tap { |str| str << Col["#{@config.description} test failed: "].yb str << Col[@context.context_label].cb str << Col[" (details below)\n", f..___indent(4)].fmt(:yb, :yb) } return false rescue AssertionSpecificationError => e # While running the test block, we got an AssertionSpecificationError. # This probably means some bad data was put in, like # T :circle, c, [4,1, "radius", nil] # (The radius needs to be a number, not a string.) # We will still raise the AssertionSpecificationError but we want it to # look like it comes from the _custom_ assertion, not the _primitive_ # one. Essentially, we are acting like it's a failure: constructing the # message that includes the context label (in this case, 'r' for # radius). = String.new.tap { |str| str << Col["#{@config.description} test -- error: "].yb str << Col[@context.context_label].cb str << Col[" details below\n", e..___indent(4)].fmt(:yb, :yb) } raise AssertionSpecificationError, end |