diff options
author | francis <francis> | 2008-09-04 06:10:25 +0000 |
---|---|---|
committer | francis <francis> | 2008-09-04 06:10:25 +0000 |
commit | 5bde1025dc4d43ea53f63107b88711ebf8942408 (patch) | |
tree | 962c8b1fb32186fbd1ab15050ede8e560d9a63f6 /vendor/plugins/rspec/lib/spec/matchers | |
parent | ce2cf5ed73d81180e9f88d590daaa23989ee9472 (diff) |
rspec for rails 2.1
Diffstat (limited to 'vendor/plugins/rspec/lib/spec/matchers')
-rw-r--r-- | vendor/plugins/rspec/lib/spec/matchers/change.rb | 2 | ||||
-rw-r--r-- | vendor/plugins/rspec/lib/spec/matchers/has.rb | 12 | ||||
-rw-r--r-- | vendor/plugins/rspec/lib/spec/matchers/raise_error.rb | 95 |
3 files changed, 63 insertions, 46 deletions
diff --git a/vendor/plugins/rspec/lib/spec/matchers/change.rb b/vendor/plugins/rspec/lib/spec/matchers/change.rb index 784e516ed..8f4ecc187 100644 --- a/vendor/plugins/rspec/lib/spec/matchers/change.rb +++ b/vendor/plugins/rspec/lib/spec/matchers/change.rb @@ -114,7 +114,7 @@ EOF # # string = "string" # lambda { - # string.reverse + # string.reverse! # }.should change { string }.from("string").to("gnirts") # # lambda { diff --git a/vendor/plugins/rspec/lib/spec/matchers/has.rb b/vendor/plugins/rspec/lib/spec/matchers/has.rb index cc5a250b8..60199f54d 100644 --- a/vendor/plugins/rspec/lib/spec/matchers/has.rb +++ b/vendor/plugins/rspec/lib/spec/matchers/has.rb @@ -8,24 +8,14 @@ module Spec end def matches?(target) - @target = target - begin - return target.send(predicate, *@args) - rescue => @error - # This clause should be empty, but rcov will not report it as covered - # unless something (anything) is executed within the clause - rcov_error_report = "http://eigenclass.org/hiki.rb?rcov-0.8.0" - end - return false + target.send(predicate, *@args) end def failure_message - raise @error if @error "expected ##{predicate}(#{@args[0].inspect}) to return true, got false" end def negative_failure_message - raise @error if @error "expected ##{predicate}(#{@args[0].inspect}) to return false, got true" end diff --git a/vendor/plugins/rspec/lib/spec/matchers/raise_error.rb b/vendor/plugins/rspec/lib/spec/matchers/raise_error.rb index b45dcf65c..c003849b6 100644 --- a/vendor/plugins/rspec/lib/spec/matchers/raise_error.rb +++ b/vendor/plugins/rspec/lib/spec/matchers/raise_error.rb @@ -1,50 +1,66 @@ module Spec module Matchers - class RaiseError #:nodoc: - def initialize(error_or_message=Exception, message=nil) - if String === error_or_message - @expected_error = Exception - @expected_message = error_or_message + def initialize(error_or_message=Exception, message=nil, &block) + @block = block + case error_or_message + when String, Regexp + @expected_error, @expected_message = Exception, error_or_message else - @expected_error = error_or_message - @expected_message = message + @expected_error, @expected_message = error_or_message, message end end - + def matches?(proc) @raised_expected_error = false - @raised_other = false + @with_expected_message = false + @eval_block = false + @eval_block_passed = false begin proc.call rescue @expected_error => @actual_error - if @expected_message.nil? - @raised_expected_error = true - else - case @expected_message - when Regexp - if @expected_message =~ @actual_error.message - @raised_expected_error = true - else - @raised_other = true - end - else - if @expected_message == @actual_error.message - @raised_expected_error = true - else - @raised_other = true - end - end - end - rescue => @actual_error - @raised_other = true - ensure - return @raised_expected_error + @raised_expected_error = true + @with_expected_message = verify_message + rescue Exception => @actual_error + # This clause should be empty, but rcov will not report it as covered + # unless something (anything) is executed within the clause + rcov_error_report = "http://eigenclass.org/hiki.rb?rcov-0.8.0" + end + + unless negative_expectation? + eval_block if @raised_expected_error && @with_expected_message && @block + end + ensure + return (@raised_expected_error && @with_expected_message) ? (@eval_block ? @eval_block_passed : true) : false + end + + def eval_block + @eval_block = true + begin + @block[@actual_error] + @eval_block_passed = true + rescue Exception => err + @actual_error = err + end + end + + def verify_message + case @expected_message + when nil + return true + when Regexp + return @expected_message =~ @actual_error.message + else + return @expected_message == @actual_error.message end end def failure_message - return "expected #{expected_error}#{actual_error}" if @raised_other || !@raised_expected_error + if @eval_block + return @actual_error.message + else + return "expected #{expected_error}#{actual_error}" + end end def negative_failure_message @@ -70,6 +86,11 @@ module Spec def actual_error @actual_error.nil? ? " but nothing was raised" : ", got #{@actual_error.inspect}" end + + def negative_expectation? + # YES - I'm a bad person... help me find a better way - ryand + caller.first(3).find { |s| s =~ /should_not/ } + end end # :call-seq: @@ -77,6 +98,10 @@ module Spec # should raise_error(NamedError) # should raise_error(NamedError, String) # should raise_error(NamedError, Regexp) + # should raise_error() { |error| ... } + # should raise_error(NamedError) { |error| ... } + # should raise_error(NamedError, String) { |error| ... } + # should raise_error(NamedError, Regexp) { |error| ... } # should_not raise_error() # should_not raise_error(NamedError) # should_not raise_error(NamedError, String) @@ -86,11 +111,13 @@ module Spec # With a named error, matches only if that specific error is raised. # With a named error and messsage specified as a String, matches only if both match. # With a named error and messsage specified as a Regexp, matches only if both match. + # Pass an optional block to perform extra verifications on the exception matched # # == Examples # # lambda { do_something_risky }.should raise_error # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError) + # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError) { |error| error.data.should == 42 } # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, "that was too risky") # lambda { do_something_risky }.should raise_error(PoorRiskDecisionError, /oo ri/) # @@ -98,8 +125,8 @@ module Spec # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError) # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, "that was too risky") # lambda { do_something_risky }.should_not raise_error(PoorRiskDecisionError, /oo ri/) - def raise_error(error=Exception, message=nil) - Matchers::RaiseError.new(error, message) + def raise_error(error=Exception, message=nil, &block) + Matchers::RaiseError.new(error, message, &block) end end end |