Sha256: 1be3a6dbed4cb0a5318d855b95d23adc370ebde26c540f5a8a2e064406f1a173

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require_relative 'assertion'

# Assert that a kind of exception class is rescued
# from the execution of a block.
#
class RescueAssay < Assertion

  register :rescued

  #
  # Check assertion.
  #
  def self.pass?(*exceptions)
    exceptions = [RuntimeError] if exceptions.empty?
    begin
      yield
      false
    rescue Exception => e
      exceptions.any? do |x|
        x === e
      end
    end
  end

  #
  # Check negated assertion.
  #
  def self.fail?(*exceptions)
    exceptions = [RuntimeError] if exceptions.empty?
    begin
      yield
      true
    rescue Exception => e
      !exceptions.any? do |x|
        x === e
      end
    end
  end

  # TODO: How to add `but got class` message to error?
  #       May have to override #assert! and #refute! method.

  #
  def self.assert_message(*exceptions)
    exp = exceptions.map{ |e| e.inspect }.join(' or ')
    "raise #{exp}" #, but was #{err} instead."
  end

  #
  def self.refute_message(*exceptions)
    exp = exceptions.map{ |e| e.inspect }.join(' or ')
    "! raise #{exp}" #, but was #{err} instead."
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
assay-0.4.1 lib/assay/rescue_assay.rb