lib/ae/assertor.rb in ae-1.4.0 vs lib/ae/assertor.rb in ae-1.5.0

- old
+ new

@@ -11,13 +11,10 @@ # in most respects, but is conditioned on the operation applied, # rather then simply passing-off to an alternate reciever. # class Assertor < AE::BasicObject - $assertions = 0 - $failures = 0 - # #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- @@ -85,22 +82,20 @@ # with external #expect method. (See Expect#expect) # #-- # TODO: Should we deprecate the receiver matches in favor of #expected ? # In other words, should the <code>|| @delegate</code> be dropped? - # - # TODO: respond_to?(:exception) && match = exception if Exception === match #++ def expect(*args, &block) return self if args.empty? && !block_given? # same as #assert target = block || args.shift - if ::Proc === target || target.respond_to?(:to_proc) - block = target.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) $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" @@ -124,18 +119,25 @@ else pass = (target === @delegate) msg = @message || "#{target.inspect} === #{@delegate.inspect}" end - #flunk(msg, caller) unless pass __assert__(pass, msg) end + # Is the +object+ and Exception or an instance of one. + #-- + # TODO: Should we use a more libreral determination of exception. + # e.g. <code>respond_to?(:exception)</code>. + #++ + def exception?(object) + ::Exception === object or ::Class === object and object.ancestors.include?(::Exception) + end + # - def flunk(msg=nil) - $failures += 1 - fail ::Assertion.new(msg || @message, :backtrace=>@backtrace) + def flunk(message=nil) + __assert__(false, message || @message) end # Ruby seems to have a quark in it's implementation whereby # this must be defined explicitly, otherwise it somehow # skips #method_missing. @@ -154,16 +156,10 @@ # 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) - # flunk(msg) #fail Assertion.new(msg, :backtrace=>@backtrace) - #end end # Puts together a suitable error message. # def __msg__(m, *a, &b) @@ -175,18 +171,26 @@ end #self.class.message(m)[@delegate, *a] ) end # Pure old simple assert. - def __assert__(pass, msg=nil) - $assertions += 1 - unless @negated ^ pass - flunk(msg || @message) #raise Assertion.new(msg, :backtrace=>@backtrace) - end - @negated ? !pass : !!pass + #-- + # TODO: Can the handling of the message be simplified/improved? + #++ + def __assert__(pass, message=nil) + pass = @negated ^ pass + # msg = message || @message + ::Assertion.test(pass, :message=>message, :backtrace=>@backtrace) + return pass end + # This method can be replaced to support alternate frameworks. + # The idea is to use to record that an assertion took place. + # def framework_assert(pass, message) + # # by default nothing needed + # end + # def matcher_message(matcher) if @negated if matcher.respond_to?(:negative_failure_message) return matcher.failure_message @@ -208,10 +212,10 @@ #message(:==){ |*a| "Expected #{a[0].inspect} to be equal to #{a[1].inspect}" } end # DO WE MAKE THESE EXCEPTIONS? #class BasicObject -# def assert ; +# def assert # end #end # Copyright (c) 2008,2009 Thomas Sawyer