From 30102c7c3859053e23228bb5f93c9410a4930e56 Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Thu, 31 May 2007 14:17:05 +0200 Subject: Fix for testsuite darcs-hash:20070531121705-4fc50-bdc3919a301da12ee024b9d2508d8139ea5c8d09.gz --- lib/bacon.rb | 56 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/bacon.rb b/lib/bacon.rb index ccf7427..200aab9 100644 --- a/lib/bacon.rb +++ b/lib/bacon.rb @@ -74,11 +74,15 @@ module Bacon else Bacon::Counter[:errors] += 1 "ERROR: #{e.class}" - end + end else "" end - end + end + end + + def raise?(*args, &block) + block.raise?(*args) end end end @@ -100,15 +104,22 @@ end class Proc def raise?(*exceptions) call - rescue *(exceptions.empty? ? RuntimeError : exceptions) - true - rescue - false + rescue *(exceptions.empty? ? RuntimeError : exceptions) => e + e + # do not rescue other exceptions. else false end end +class Float + def close?(to, delta) + (to.to_f - self).abs <= delta.to_f + rescue + false + end +end + class Object def should(*args, &block) Should.new(self).be(*args, &block) @@ -119,6 +130,7 @@ class Object end end + class Should # Kills ==, ===, =~, eql?, equal?, frozen?, instance_of?, is_a?, # kind_of?, nil?, respond_to?, tainted? @@ -131,9 +143,14 @@ class Should @negated = false end - def not + def not(*args, &block) @negated = !@negated - self + + if args.empty? + self + else + be(*args, &block) + end end def be(*args, &block) @@ -150,17 +167,32 @@ class Should alias an be def satisfy(*args, &block) - unless @negated ^ yield(@object, *args) - raise Bacon::Error.new(:failed, "") + if args.size == 1 && String === args.first + description = args.shift + else + description = "" + end + + r = yield(@object, *args) + unless @negated ^ r + raise Bacon::Error.new(:failed, description) end Bacon::Counter[:requirements] += 1 @negated ^ r ? r : false end def method_missing(name, *args, &block) - satisfy { |x| - name = "#{name}?" if name.to_s =~ /\w/ + name = "#{name}?" if name.to_s =~ /\w/ + + desc = @negated ? "not " : "" + desc << @object.inspect << "." << name.to_s + desc << "(" << args.map{|x|x.inspect}.join(", ") << ")" + + satisfy(desc) { |x| x.__send__(name, *args, &block) } end + + def equal(value); self == value; end + def match(value); self =~ value; end end -- cgit 1.4.1