Class: Whitestone::Assertion::Catch

Inherits:
Base show all
Defined in:
lib/whitestone/assertion_classes.rb

Overview

class Assertion::Exception

Constant Summary collapse

TOKEN =
Object.new

Instance Method Summary collapse

Methods inherited from Base

#block

Methods included from Guards

#args_or_block_one_only, #block_required, #no_block_allowed, #one_argument, #two_arguments, #two_or_three_arguments, #type_check

Constructor Details

#initialize(mode, *args, &block) ⇒ Catch

Returns a new instance of Catch.



402
403
404
405
406
# File 'lib/whitestone/assertion_classes.rb', line 402

def initialize(mode, *args, &block)
  super
  @symbol = one_argument(args)
  @block = block_required(block)
end

Instance Method Details

#messageObject



430
431
432
433
434
435
436
437
438
# File 'lib/whitestone/assertion_classes.rb', line 430

def message
  symbol = @symbol.to_sym.inspect
  case @mode
  when :assert
    Col["Expected block to throw ", symbol, "; it didn't"].fmt 'yb,rb,yb'
  when :negate
    Col["Expected block NOT to throw ", symbol, "; it did"].fmt 'yb,rb,yb'
  end
end

#runObject



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/whitestone/assertion_classes.rb', line 407

def run
  return_value =
    catch(@symbol) do
      begin
        @block.call
      rescue => e
        raise e unless e.message =~ /\Auncaught throw (`.*?'|:.*)\z/
        # ^ We don't want this exception to escape and terminate our
        #   tests.  TODO: make sure I understand this and agree with
        #   what it does.  Should we report an uncaught throw?
      end
      TOKEN  # Special object to say we reached the end of the block,
             # therefore nothing was thrown.
    end
  if return_value == TOKEN
    # The symbol we were expecting was not thrown, so this test failed.
    Whitestone.caught_value = nil
    return false
  else
    Whitestone.caught_value = return_value
    return true
  end
end