lib/ae/assertor.rb in ae-1.3.0 vs lib/ae/assertor.rb in ae-1.4.0
- old
+ new
@@ -1,6 +1,7 @@
require 'ae/assertion'
+require 'ae/basic_object'
# = Assertor (Assertion Functor)
#
# == What is a Functor?
#
@@ -8,18 +9,22 @@
# Higher Order Function. In other words, it is a function
# that acts on a function. It is very similiar to a delegator
# in most respects, but is conditioned on the operation applied,
# rather then simply passing-off to an alternate reciever.
#
-class Assertor
+class Assertor < AE::BasicObject
$assertions = 0
$failures = 0
#
- instance_methods.each{ |m| protected m unless /^__/ =~ m.to_s }
+ #instance_methods.each{ |m| protected m unless /^(__|object_id$)/ =~ m.to_s }
+ if ::RUBY_VERSION >= '1.9'
+ eval "private :==, :!, :!=" # using eval here b/c it's a syntax error in 1.8-
+ end
+
# New Assertor.
#
def initialize(delegate, opts={}) #, backtrace)
@delegate = delegate
@message = opts[:message]
@@ -48,15 +53,15 @@
# instead of
#
# assert something, parameter
#
def assert(*args, &block)
- return self if args.empty? && !block_given?
+ return self if args.empty? && !block
target = block || args.shift
- if Proc === target || target.respond_to?(:to_proc)
+ if ::Proc === target || target.respond_to?(:to_proc)
block = target.to_proc
match = args.shift
result = block.arity > 0 ? block.call(@delegate) : block.call
if match
pass = (match == result)
@@ -84,29 +89,31 @@
# In other words, should the <code>|| @delegate</code> be dropped?
#
# TODO: respond_to?(:exception) && match = exception if Exception === match
#++
def expect(*args, &block)
- # same as #assert if no arguments of block given
- return self if args.empty? && !block_given?
+ return self if args.empty? && !block_given? # same as #assert
target = block || args.shift
- if Proc === target || target.respond_to?(:to_proc)
+ if ::Proc === target || target.respond_to?(:to_proc)
block = target.to_proc
match = args.shift || @delegate
- if Exception === match || (Class===match && match.ancestors.include?(Exception))
+ if ::Exception === match || (::Class===match && match.ancestors.include?(::Exception))
+ $DEBUG, debug = false, $DEBUG # b/c it always spits-out a NameError
begin
block.arity > 0 ? block.call(@delegate) : block.call
pass = false
msg = "#{match} not raised"
rescue match => error
pass = true
msg = "#{match} raised"
rescue Exception => error
pass = false
msg = "#{match} expected but #{error.class} was raised"
+ ensure
+ $DEBUG = debug
end
else
result = block.arity > 0 ? block.call(@delegte) : block.call
pass = (match === result)
msg = @message || "#{match.inspect} === #{result.inspect}"
@@ -124,26 +131,32 @@
end
#
def flunk(msg=nil)
$failures += 1
- fail Assertion.new(msg || @message, :backtrace=>@backtrace)
+ fail ::Assertion.new(msg || @message, :backtrace=>@backtrace)
end
# Ruby seems to have a quark in it's implementation whereby
# this must be defined explicitly, otherwise it somehow
# skips #method_missing.
def =~(match)
method_missing(:"=~", match)
end
+ #
+ def send(op, *a, &b)
+ method_missing(op, *a, &b)
+ end
+
private
# Converts a missing methods into an Assertion.
#
def method_missing(sym, *a, &b)
pass = @delegate.__send__(sym, *a, &b)
+ #pass = @delegate.public_send(sym, *a, &b)
__assert__(pass, @message || __msg__(sym, *a, &b))
#Assertor.count += 1
#if (@negated ? pass : !pass)
#unless @negated ^ pass
# msg = @message || __msg__(sym, *a, &b)
@@ -152,14 +165,15 @@
end
# Puts together a suitable error message.
#
def __msg__(m, *a, &b)
+ inspection = @delegate.send(:inspect)
if @negated
- "! #{@delegate.inspect} #{m} #{a.collect{|x| x.inspect}.join(',')}"
+ "! #{inspection} #{m} #{a.collect{|x| x.inspect}.join(',')}"
else
- "#{@delegate.inspect} #{m} #{a.collect{|x| x.inspect}.join(',')}"
+ "#{inspection} #{m} #{a.collect{|x| x.inspect}.join(',')}"
end
#self.class.message(m)[@delegate, *a] )
end
# Pure old simple assert.
@@ -191,7 +205,13 @@
# block ? @message[op.to_sym] = block : @message[op.to_sym]
#end
#
#message(:==){ |*a| "Expected #{a[0].inspect} to be equal to #{a[1].inspect}" }
end
+
+# DO WE MAKE THESE EXCEPTIONS?
+#class BasicObject
+# def assert ;
+# end
+#end
# Copyright (c) 2008,2009 Thomas Sawyer