Sha256: 25839d86bf18693636d4c16fab7659e8c32251d3e1d633520a6606b716c55e03

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

require_relative 'rescue_assay'

#
class RaiseAssay < RescueAssay

  register :raised

  #
  # Check assertion.
  #
  def self.pass?(*exceptions)
    exceptions = [RuntimeError] if exceptions.empty?
    begin
      yield
      false
    rescue Exception => e
      exceptions.any? do |x|
        x.instance_of?(Module) ? x === e : e.class == x
      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.instance_of?(Module) ? x === e : e.class == x
      end
    end
  end

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

  #
  #
  #
  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

=begin
  #
  #
  #
  def self.assert!(*exp) #:yeild:
    options = (Hash === exp.last ? exp.pop : {})

    exp = exp.first

    begin
      yield
      test      = false
      arguments = [exp]
    rescue Exception => err
      test      = (exp === err)
      arguments = [exp, err]
    end

    message   = options[:message]   || pass_message(*arguments)
    backtrace = options[:backtrace] || caller

    if !test
      fail self, message, backtrace
    end
  end

  #
  #
  #
  def self.refute!(*exp) #:yield:
    options = (Hash === exp.last ? exp.pop : {})

    #exp = exp.first

    begin
      yield
      test      = true
      arguments = exp
    rescue Exception => err
      test      = !exp.any?{ |e| e === err }
      arguments = exp #+ [err]
    end

    message   = options[:message]   || fail_message(*arguments)
    backtrace = options[:backtrace] || caller

    if !test
      fail self, message, backtrace
    end
  end
=end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
assay-0.4.1 lib/assay/raise_assay.rb
assay-0.4.0 lib/assay/raise_assay.rb