Class: Whitestone::Assertion::ExpectError

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

Overview

class Assertion::Identity

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) ⇒ ExpectError

Returns a new instance of ExpectError.



362
363
364
365
366
367
368
369
# File 'lib/whitestone/assertion_classes.rb', line 362

def initialize(mode, *args, &block)
  super
  @exceptions = args.empty? ? [StandardError] : args
  unless @exceptions.all? { |klass| klass.is_a? Class }
    raise AssertionSpecificationError, "Invalid arguments: must all be classes"
  end
  @block = block_required(block)
end

Instance Method Details

#messageObject



386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/whitestone/assertion_classes.rb', line 386

def message
  _or_ = Col[' or '].yb
  kinds_str = @exceptions.map { |ex| Col[ex].rb }.join(_or_)
  klass = @exception_class
  case @mode
  when :assert 
    Col["Expected block to raise ", kinds_str, "; nothing raised"].fmt 'yb,_,yb'
  when :negate
    Col[  "Expected block NOT to raise ", kinds_str, "; ", klass, " raised"].
      fmt :yb,                            :_,        :yb,  :rb,   :yb  
  end
end

#runObject



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/whitestone/assertion_classes.rb', line 370

def run
  # Return true if the block raises an exception, false otherwise.
  # Only the exceptions specified in @exceptions will be caught.
  begin
    @block.call
    return false
  rescue ::Exception => e
    if @exceptions.any? { |klass| e.is_a? klass }
      @exception_class = e.class
      Whitestone.exception = e
      return true
    else
      raise e  # It's not one of the exceptions we wanted; re-raise it.
    end
  end
end