Sha256: c45204f9449d4081292b67d0c6cfe90b989cbfa41726c6b82efb13c940460a7b

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 KB

Contents

require 'facet/kernel/sym_esc'
require 'yaml'

$test_mode = false
$test_failures = []


module UnitTestKernel

  def assert_on!
    $test_mode = true
  end

  def assert_off!
    $test_mode = false
  end

  def assert!(&blk)
    old = $test_mode
    $test_mode = true
    r = blk.call
    $test_mode = old
    return r
  end

  def no_assertion!(&blk)
    old = $test_mode
    $test_mode = false
    r = blk.call
    $test_mode = old
    return r
  end

end

class Object
  include UnitTestKernel
end


module UnitTesting

  class Failure

    def to_yaml_properties
      [ '@self', '@method', '@args', '@caller' ]
    end

    def initialize( slf, meth, args, callr )
      @self = slf
      @method = meth
      @args = args
      @caller = callr
    end

  end


  TRANSPARENT_ASSERTIONS = %w{
    == === eql? equal? kind_of? include?
    member? empty? nil? has_key? key? respond_to?
    between?
  }

  def self.advise_assertions

    ObjectSpace.each_object(Module) do |m|

      TRANSPARENT_ASSERTIONS.each do |meth|

        nmeth = "assert_#{sym_esc(meth)}"

        if m.method_defined?(meth)

          m.class_eval <<-HERE
            alias :#{nmeth} :#{meth}
            def #{meth}(*args)
              if $test_mode
                r = nil
                no_assertion! {
                  r = #{nmeth}(*args)
                  unless r
                    $test_failures << Failure.new( self, :"#{meth}", args, caller[2..-1] )
                  end
                }
                return r
              else
                return #{nmeth}(*args)
              end
            end
          HERE

        end #if

      end #do

    end #do

  end #def

  def self.report
    y $test_failures
  end

end

class Object
  include UnitTestKernel
end

UnitTesting.advise_assertions


=begin testing

  class X
    def a
      1
    end
  end

  class TestX < X

    def test_a
      assert! {
        a == 1
        a == 2
      }
    end

  end

  x = TestX.new
  x.test_a

  UnitTesting.report

=end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reap-4.0 lib/reap/test.rb
reap-5.10.10 lib/reap/test.rb