summaryrefslogtreecommitdiffstats
path: root/test/lib/spec/expectations
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-03-17 02:48:41 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-03-17 02:48:41 +0000
commitba23a5ac276e59fdda8186750c6d0fd2cfecdeac (patch)
tree1e14b25ade74ea52d8da2788ede9b12b507867e8 /test/lib/spec/expectations
parent8ea6adaeb1e3d0aa6348c2a2c3a385d185372d06 (diff)
Adding spec libs, so we can use them some day
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2283 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test/lib/spec/expectations')
-rw-r--r--test/lib/spec/expectations/differs/default.rb62
-rw-r--r--test/lib/spec/expectations/errors.rb6
-rw-r--r--test/lib/spec/expectations/extensions.rb3
-rw-r--r--test/lib/spec/expectations/extensions/object.rb109
-rw-r--r--test/lib/spec/expectations/extensions/proc.rb57
-rw-r--r--test/lib/spec/expectations/extensions/string_and_symbol.rb17
-rw-r--r--test/lib/spec/expectations/handler.rb47
-rw-r--r--test/lib/spec/expectations/should.rb5
-rwxr-xr-xtest/lib/spec/expectations/should/base.rb64
-rw-r--r--test/lib/spec/expectations/should/change.rb69
-rw-r--r--test/lib/spec/expectations/should/have.rb128
-rwxr-xr-xtest/lib/spec/expectations/should/not.rb74
-rwxr-xr-xtest/lib/spec/expectations/should/should.rb81
-rw-r--r--test/lib/spec/expectations/sugar.rb47
14 files changed, 769 insertions, 0 deletions
diff --git a/test/lib/spec/expectations/differs/default.rb b/test/lib/spec/expectations/differs/default.rb
new file mode 100644
index 000000000..e08325728
--- /dev/null
+++ b/test/lib/spec/expectations/differs/default.rb
@@ -0,0 +1,62 @@
+begin
+ require 'rubygems'
+ require 'diff/lcs' #necessary due to loading bug on some machines - not sure why - DaC
+ require 'diff/lcs/hunk'
+rescue LoadError ; raise "You must gem install diff-lcs to use diffing" ; end
+
+require 'pp'
+
+module Spec
+ module Expectations
+ module Differs
+
+ # TODO add colour support
+ # TODO add some rdoc
+ class Default
+ def initialize(format=:unified,context_lines=nil,colour=nil)
+
+ context_lines ||= 3
+ colour ||= false
+
+ @format,@context_lines,@colour = format,context_lines,colour
+ end
+
+ # This is snagged from diff/lcs/ldiff.rb (which is a commandline tool)
+ def diff_as_string(data_old, data_new)
+ data_old = data_old.split(/\n/).map! { |e| e.chomp }
+ data_new = data_new.split(/\n/).map! { |e| e.chomp }
+ output = ""
+ diffs = Diff::LCS.diff(data_old, data_new)
+ return output if diffs.empty?
+ oldhunk = hunk = nil
+ file_length_difference = 0
+ diffs.each do |piece|
+ begin
+ hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, @context_lines,
+ file_length_difference)
+ file_length_difference = hunk.file_length_difference
+ next unless oldhunk
+ # Hunks may overlap, which is why we need to be careful when our
+ # diff includes lines of context. Otherwise, we might print
+ # redundant lines.
+ if (@context_lines > 0) and hunk.overlaps?(oldhunk)
+ hunk.unshift(oldhunk)
+ else
+ output << oldhunk.diff(@format)
+ end
+ ensure
+ oldhunk = hunk
+ output << "\n"
+ end
+ end
+ #Handle the last remaining hunk
+ output << oldhunk.diff(@format) << "\n"
+ end
+
+ def diff_as_object(target,expected)
+ diff_as_string(PP.pp(target,""), PP.pp(expected,""))
+ end
+ end
+ end
+ end
+end
diff --git a/test/lib/spec/expectations/errors.rb b/test/lib/spec/expectations/errors.rb
new file mode 100644
index 000000000..03e81a064
--- /dev/null
+++ b/test/lib/spec/expectations/errors.rb
@@ -0,0 +1,6 @@
+module Spec
+ module Expectations
+ class ExpectationNotMetError < StandardError
+ end
+ end
+end
diff --git a/test/lib/spec/expectations/extensions.rb b/test/lib/spec/expectations/extensions.rb
new file mode 100644
index 000000000..0381dc7f3
--- /dev/null
+++ b/test/lib/spec/expectations/extensions.rb
@@ -0,0 +1,3 @@
+require 'spec/expectations/extensions/object'
+require 'spec/expectations/extensions/proc'
+require 'spec/expectations/extensions/string_and_symbol'
diff --git a/test/lib/spec/expectations/extensions/object.rb b/test/lib/spec/expectations/extensions/object.rb
new file mode 100644
index 000000000..dd5498fdd
--- /dev/null
+++ b/test/lib/spec/expectations/extensions/object.rb
@@ -0,0 +1,109 @@
+module Spec
+ module Expectations
+ # rspec adds #should and #should_not to every Object (and,
+ # implicitly, every Class).
+ module ObjectExpectations
+
+ # :call-seq:
+ # should(matcher)
+ # should == expected
+ # should =~ expected
+ #
+ # receiver.should(matcher)
+ # => Passes if matcher.matches?(receiver)
+ #
+ # receiver.should == expected #any value
+ # => Passes if (receiver == expected)
+ #
+ # receiver.should =~ regexp
+ # => Passes if (receiver =~ regexp)
+ #
+ # See Spec::Matchers for more information about matchers
+ #
+ # == Warning
+ #
+ # NOTE that this does NOT support receiver.should != expected.
+ # Instead, use receiver.should_not == expected
+ def should(matcher=nil, &block)
+ return ExpectationMatcherHandler.handle_matcher(self, matcher, &block) if matcher
+ Should::Should.new(self)
+ end
+
+ # :call-seq:
+ # should_not(matcher)
+ # should_not == expected
+ # should_not =~ expected
+ #
+ # receiver.should_not(matcher)
+ # => Passes unless matcher.matches?(receiver)
+ #
+ # receiver.should_not == expected
+ # => Passes unless (receiver == expected)
+ #
+ # receiver.should_not =~ regexp
+ # => Passes unless (receiver =~ regexp)
+ #
+ # See Spec::Matchers for more information about matchers
+ def should_not(matcher=nil, &block)
+ return NegativeExpectationMatcherHandler.handle_matcher(self, matcher, &block) if matcher
+ should.not
+ end
+
+ deprecated do
+ # Deprecated: use should have(n).items (see Spec::Matchers)
+ # This will be removed in 0.9
+ def should_have(expected)
+ should.have(expected)
+ end
+ alias_method :should_have_exactly, :should_have
+
+ # Deprecated: use should have_at_least(n).items (see Spec::Matchers)
+ # This will be removed in 0.9
+ def should_have_at_least(expected)
+ should.have.at_least(expected)
+ end
+
+ # Deprecated: use should have_at_most(n).items (see Spec::Matchers)
+ # This will be removed in 0.9
+ def should_have_at_most(expected)
+ should.have.at_most(expected)
+ end
+
+ # Deprecated: use should include(expected) (see Spec::Matchers)
+ # This will be removed in 0.9
+ def should_include(expected)
+ should.include(expected)
+ end
+
+ # Deprecated: use should_not include(expected) (see Spec::Matchers)
+ # This will be removed in 0.9
+ def should_not_include(expected)
+ should.not.include(expected)
+ end
+
+ # Deprecated: use should be(expected) (see Spec::Matchers)
+ # This will be removed in 0.9
+ def should_be(expected = :___no_arg)
+ should.be(expected)
+ end
+
+ # Deprecated: use should_not be(expected) (see Spec::Matchers)
+ # This will be removed in 0.9
+ def should_not_be(expected = :___no_arg)
+ should_not.be(expected)
+ end
+ end
+ end
+ end
+end
+
+class Object
+ include Spec::Expectations::ObjectExpectations
+ deprecated do
+ include Spec::Expectations::UnderscoreSugar
+ end
+end
+
+deprecated do
+ Object.handle_underscores_for_rspec!
+end \ No newline at end of file
diff --git a/test/lib/spec/expectations/extensions/proc.rb b/test/lib/spec/expectations/extensions/proc.rb
new file mode 100644
index 000000000..8286708ed
--- /dev/null
+++ b/test/lib/spec/expectations/extensions/proc.rb
@@ -0,0 +1,57 @@
+module Spec
+ module Expectations
+ module ProcExpectations
+ # Given a receiver and a message (Symbol), specifies that the result
+ # of sending that message that receiver should change after
+ # executing the proc.
+ #
+ # lambda { @team.add player }.should_change(@team.players, :size)
+ # lambda { @team.add player }.should_change(@team.players, :size).by(1)
+ # lambda { @team.add player }.should_change(@team.players, :size).to(23)
+ # lambda { @team.add player }.should_change(@team.players, :size).from(22).to(23)
+ #
+ # You can use a block instead of a message and receiver.
+ #
+ # lambda { @team.add player }.should_change{@team.players.size}
+ # lambda { @team.add player }.should_change{@team.players.size}.by(1)
+ # lambda { @team.add player }.should_change{@team.players.size}.to(23)
+ # lambda { @team.add player }.should_change{@team.players.size}.from(22).to(23)
+ def should_change(receiver=nil, message=nil, &block)
+ should.change(receiver, message, &block)
+ end
+
+ # Given a receiver and a message (Symbol), specifies that the result
+ # of sending that message that receiver should NOT change after
+ # executing the proc.
+ #
+ # lambda { @team.add player }.should_not_change(@team.players, :size)
+ #
+ # You can use a block instead of a message and receiver.
+ #
+ # lambda { @team.add player }.should_not_change{@team.players.size}
+ def should_not_change(receiver, message)
+ should.not.change(receiver, message)
+ end
+
+ def should_raise(exception=Exception, message=nil)
+ should.raise(exception, message)
+ end
+
+ def should_not_raise(exception=Exception, message=nil)
+ should.not.raise(exception, message)
+ end
+
+ def should_throw(symbol)
+ should.throw(symbol)
+ end
+
+ def should_not_throw(symbol=:___this_is_a_symbol_that_will_likely_never_occur___)
+ should.not.throw(symbol)
+ end
+ end
+ end
+end
+
+class Proc
+ include Spec::Expectations::ProcExpectations
+end \ No newline at end of file
diff --git a/test/lib/spec/expectations/extensions/string_and_symbol.rb b/test/lib/spec/expectations/extensions/string_and_symbol.rb
new file mode 100644
index 000000000..30f60d4d0
--- /dev/null
+++ b/test/lib/spec/expectations/extensions/string_and_symbol.rb
@@ -0,0 +1,17 @@
+module Spec
+ module Expectations
+ module StringHelpers
+ def starts_with?(prefix)
+ to_s[0..(prefix.length - 1)] == prefix
+ end
+ end
+ end
+end
+
+class String
+ include Spec::Expectations::StringHelpers
+end
+
+class Symbol
+ include Spec::Expectations::StringHelpers
+end \ No newline at end of file
diff --git a/test/lib/spec/expectations/handler.rb b/test/lib/spec/expectations/handler.rb
new file mode 100644
index 000000000..9d3fd1f88
--- /dev/null
+++ b/test/lib/spec/expectations/handler.rb
@@ -0,0 +1,47 @@
+module Spec
+ module Expectations
+
+ module MatcherHandlerHelper
+ def describe(matcher)
+ matcher.respond_to?(:description) ? matcher.description : "[#{matcher.class.name} does not provide a description]"
+ end
+ end
+
+ class ExpectationMatcherHandler
+ class << self
+ include MatcherHandlerHelper
+ def handle_matcher(actual, matcher, &block)
+ unless matcher.nil?
+ match = matcher.matches?(actual, &block)
+ ::Spec::Matchers.generated_description = "should #{describe(matcher)}"
+ Spec::Expectations.fail_with(matcher.failure_message) unless match
+ end
+ end
+ end
+ end
+
+ class NegativeExpectationMatcherHandler
+ class << self
+ include MatcherHandlerHelper
+ def handle_matcher(actual, matcher, &block)
+ unless matcher.nil?
+ unless matcher.respond_to?(:negative_failure_message)
+ Spec::Expectations.fail_with(
+ <<-EOF
+ Matcher does not support should_not.
+ See Spec::Matchers for more information
+ about matchers.
+ EOF
+ )
+ end
+ match = matcher.matches?(actual, &block)
+ ::Spec::Matchers.generated_description = "should not #{describe(matcher)}"
+ Spec::Expectations.fail_with(matcher.negative_failure_message) if match
+ end
+ end
+ end
+ end
+
+ end
+end
+
diff --git a/test/lib/spec/expectations/should.rb b/test/lib/spec/expectations/should.rb
new file mode 100644
index 000000000..f64e6ff78
--- /dev/null
+++ b/test/lib/spec/expectations/should.rb
@@ -0,0 +1,5 @@
+require 'spec/expectations/should/base'
+require 'spec/expectations/should/have'
+require 'spec/expectations/should/not'
+require 'spec/expectations/should/should'
+require 'spec/expectations/should/change'
diff --git a/test/lib/spec/expectations/should/base.rb b/test/lib/spec/expectations/should/base.rb
new file mode 100755
index 000000000..1be4677e8
--- /dev/null
+++ b/test/lib/spec/expectations/should/base.rb
@@ -0,0 +1,64 @@
+module Spec
+ module Expectations
+ module Should
+ class Base
+
+ #== and =~ will stay after the new syntax
+ def ==(expected)
+ __delegate_method_missing_to_target "==", "==", expected
+ end
+
+ def =~(expected)
+ __delegate_method_missing_to_target "=~", "=~", expected
+ end
+
+ #<, <=, >=, > are all implemented in Spec::Matchers::Be
+ # and will be removed with 0.9
+ deprecated do
+ def <(expected)
+ __delegate_method_missing_to_target "<", "<", expected
+ end
+
+ def <=(expected)
+ __delegate_method_missing_to_target "<=", "<=", expected
+ end
+
+ def >=(expected)
+ __delegate_method_missing_to_target ">=", ">=", expected
+ end
+
+ def >(expected)
+ __delegate_method_missing_to_target ">", ">", expected
+ end
+ end
+
+ def default_message(expectation, expected=nil)
+ return "expected #{expected.inspect}, got #{@target.inspect} (using #{expectation})" if expectation == '=='
+ "expected #{expectation} #{expected.inspect}, got #{@target.inspect}" unless expectation == '=='
+ end
+
+ def fail_with_message(message, expected=nil, target=nil)
+ Spec::Expectations.fail_with(message, expected, target)
+ end
+
+ def find_supported_sym(original_sym)
+ ["#{original_sym}?", "#{original_sym}s?"].each do |alternate_sym|
+ return alternate_sym.to_s if @target.respond_to?(alternate_sym.to_s)
+ end
+ end
+
+ deprecated do
+ def method_missing(original_sym, *args, &block)
+ if original_sym.to_s =~ /^not_/
+ return Not.new(@target).__send__(sym, *args, &block)
+ end
+ if original_sym.to_s =~ /^have_/
+ return have.__send__(original_sym.to_s[5..-1].to_sym, *args, &block)
+ end
+ __delegate_method_missing_to_target original_sym, find_supported_sym(original_sym), *args
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/test/lib/spec/expectations/should/change.rb b/test/lib/spec/expectations/should/change.rb
new file mode 100644
index 000000000..98304f1b3
--- /dev/null
+++ b/test/lib/spec/expectations/should/change.rb
@@ -0,0 +1,69 @@
+module Spec
+ module Expectations
+ module Should
+ class Change < Base
+
+ def initialize(target, receiver=nil, message=nil, &block)
+ @block = block
+ @target = target
+ @receiver = receiver
+ @message = message
+ execute_change
+ evaluate_change
+ end
+
+ def execute_change
+ @before_change = @block.nil? ? @receiver.send(@message) : @block.call
+ @target.call
+ @after_change = @block.nil? ? @receiver.send(@message) : @block.call
+ end
+
+ def message
+ @message.nil? ? 'result' : @message
+ end
+
+ def evaluate_change
+ if @before_change == @after_change
+ fail_with_message "#{message} should have changed, but is still #{@after_change.inspect}"
+ end
+ end
+
+ def from(value)
+ if @before_change != value
+ fail_with_message "#{message} should have initially been #{value.inspect}, but was #{@before_change.inspect}"
+ end
+ self
+ end
+
+ def to(value)
+ if @after_change != value
+ fail_with_message "#{message} should have been changed to #{value.inspect}, but is now #{@after_change.inspect}"
+ end
+ self
+ end
+
+ def by(expected_delta)
+ if actual_delta != expected_delta
+ fail_with_message "#{message} should have been changed by #{expected_delta}, but was changed by #{actual_delta}"
+ end
+ self
+ end
+
+ private
+ def actual_delta
+ @after_change - @before_change
+ end
+ end
+
+ class NotChange < Change
+ def evaluate_change
+ if @before_change != @after_change
+ fail_with_message "#{@message} should not have changed, but did change from #{@before_change.inspect} to #{@after_change.inspect}"
+ end
+ end
+ end
+
+ end
+ end
+end
+
diff --git a/test/lib/spec/expectations/should/have.rb b/test/lib/spec/expectations/should/have.rb
new file mode 100644
index 000000000..47ebe81db
--- /dev/null
+++ b/test/lib/spec/expectations/should/have.rb
@@ -0,0 +1,128 @@
+module Spec
+ module Expectations
+ module Should
+ class Have
+ def initialize(target, relativity=:exactly, expected=nil)
+ @target = target
+ init_collection_handler(target, relativity, expected)
+ init_item_handler(target)
+ end
+
+ def init_collection_handler(target, relativity, expected)
+ @collection_handler = CollectionHandler.new(target, relativity, expected)
+ end
+
+ def init_item_handler(target)
+ @item_handler = PositiveItemHandler.new(target)
+ end
+
+ def method_missing(sym, *args)
+ if @collection_handler.wants_to_handle(sym)
+ @collection_handler.handle_message(sym, *args)
+ elsif @item_handler.wants_to_handle(sym)
+ @item_handler.handle_message(sym, *args)
+ else
+ Spec::Expectations.fail_with("target does not respond to #has_#{sym}?")
+ end
+ end
+ end
+
+ class NotHave < Have
+ def init_item_handler(target)
+ @item_handler = NegativeItemHandler.new(target)
+ end
+ end
+
+ class CollectionHandler
+ def initialize(target, relativity=:exactly, expected=nil)
+ @target = target
+ @expected = expected == :no ? 0 : expected
+ @at_least = (relativity == :at_least)
+ @at_most = (relativity == :at_most)
+ end
+
+ def at_least(expected_number=nil)
+ @at_least = true
+ @at_most = false
+ @expected = expected_number == :no ? 0 : expected_number
+ self
+ end
+
+ def at_most(expected_number=nil)
+ @at_least = false
+ @at_most = true
+ @expected = expected_number == :no ? 0 : expected_number
+ self
+ end
+
+ def method_missing(sym, *args)
+ if @target.respond_to?(sym)
+ handle_message(sym, *args)
+ end
+ end
+
+ def wants_to_handle(sym)
+ respond_to?(sym) || @target.respond_to?(sym)
+ end
+
+ def handle_message(sym, *args)
+ return at_least(args[0]) if sym == :at_least
+ return at_most(args[0]) if sym == :at_most
+ Spec::Expectations.fail_with(build_message(sym, args)) unless as_specified?(sym, args)
+ end
+
+ def build_message(sym, args)
+ message = "expected"
+ message += " at least" if @at_least
+ message += " at most" if @at_most
+ message += " #{@expected} #{sym}, got #{actual_size_of(collection(sym, args))}"
+ end
+
+ def as_specified?(sym, args)
+ return actual_size_of(collection(sym, args)) >= @expected if @at_least
+ return actual_size_of(collection(sym, args)) <= @expected if @at_most
+ return actual_size_of(collection(sym, args)) == @expected
+ end
+
+ def collection(sym, args)
+ @target.send(sym, *args)
+ end
+
+ def actual_size_of(collection)
+ return collection.length if collection.respond_to? :length
+ return collection.size if collection.respond_to? :size
+ end
+ end
+
+ class ItemHandler
+ def wants_to_handle(sym)
+ @target.respond_to?("has_#{sym}?")
+ end
+
+ def initialize(target)
+ @target = target
+ end
+
+ def fail_with(message)
+ Spec::Expectations.fail_with(message)
+ end
+ end
+
+ class PositiveItemHandler < ItemHandler
+ def handle_message(sym, *args)
+ fail_with(
+ "expected #has_#{sym}?(#{args.collect{|arg| arg.inspect}.join(', ')}) to return true, got false"
+ ) unless @target.send("has_#{sym}?", *args)
+ end
+ end
+
+ class NegativeItemHandler < ItemHandler
+ def handle_message(sym, *args)
+ fail_with(
+ "expected #has_#{sym}?(#{args.collect{|arg| arg.inspect}.join(', ')}) to return false, got true"
+ ) if @target.send("has_#{sym}?", *args)
+ end
+ end
+ end
+ end
+end
diff --git a/test/lib/spec/expectations/should/not.rb b/test/lib/spec/expectations/should/not.rb
new file mode 100755
index 000000000..5ad530be6
--- /dev/null
+++ b/test/lib/spec/expectations/should/not.rb
@@ -0,0 +1,74 @@
+module Spec
+ module Expectations
+ module Should
+
+ class Not < Base #:nodoc:
+ def initialize(target)
+ @target = target
+ @be_seen = false
+ end
+
+ deprecated do
+ #Gone for 0.9
+ def be(expected = :___no_arg)
+ @be_seen = true
+ return self if (expected == :___no_arg)
+ fail_with_message(default_message("should not be", expected)) if (@target.equal?(expected))
+ end
+
+ #Gone for 0.9
+ def have(expected_number=nil)
+ NotHave.new(@target, :exactly, expected_number)
+ end
+
+ #Gone for 0.9
+ def change(receiver, message)
+ NotChange.new(@target, receiver, message)
+ end
+
+ #Gone for 0.9
+ def raise(exception=Exception, message=nil)
+ begin
+ @target.call
+ rescue exception => e
+ return unless message.nil? || e.message == message || (message.is_a?(Regexp) && e.message =~ message)
+ if e.kind_of?(exception)
+ failure_message = "expected no "
+ failure_message << exception.to_s
+ unless message.nil?
+ failure_message << " with "
+ failure_message << "message matching " if message.is_a?(Regexp)
+ failure_message << message.inspect
+ end
+ failure_message << ", got " << e.inspect
+ fail_with_message(failure_message)
+ end
+ rescue
+ true
+ end
+ end
+
+ #Gone for 0.9
+ def throw(symbol=:___this_is_a_symbol_that_will_likely_never_occur___)
+ begin
+ catch symbol do
+ @target.call
+ return true
+ end
+ fail_with_message("expected #{symbol.inspect} not to be thrown, but it was")
+ rescue NameError
+ true
+ end
+ end
+
+ def __delegate_method_missing_to_target original_sym, actual_sym, *args
+ ::Spec::Matchers.generated_description = "should not #{original_sym} #{args[0].inspect}"
+ return unless @target.__send__(actual_sym, *args)
+ fail_with_message(default_message("not #{original_sym}", args[0]))
+ end
+ end
+ end
+
+ end
+ end
+end
diff --git a/test/lib/spec/expectations/should/should.rb b/test/lib/spec/expectations/should/should.rb
new file mode 100755
index 000000000..cb9f3c4ce
--- /dev/null
+++ b/test/lib/spec/expectations/should/should.rb
@@ -0,0 +1,81 @@
+module Spec
+ module Expectations
+ module Should # :nodoc:
+
+ class Should < Base
+
+ def initialize(target, expectation=nil)
+ @target = target
+ @be_seen = false
+ end
+
+ deprecated do
+ #Gone for 0.9
+ def not
+ Not.new(@target)
+ end
+
+ #Gone for 0.9
+ def be(expected = :___no_arg)
+ @be_seen = true
+ return self if (expected == :___no_arg)
+ if Symbol === expected
+ fail_with_message(default_message("should be", expected)) unless (@target.equal?(expected))
+ else
+ fail_with_message("expected #{expected}, got #{@target} (using .equal?)") unless (@target.equal?(expected))
+ end
+ end
+
+ #Gone for 0.9
+ def have(expected_number=nil)
+ Have.new(@target, :exactly, expected_number)
+ end
+
+ #Gone for 0.9
+ def change(receiver=nil, message=nil, &block)
+ Change.new(@target, receiver, message, &block)
+ end
+
+ #Gone for 0.9
+ def raise(exception=Exception, message=nil)
+ begin
+ @target.call
+ rescue exception => e
+ unless message.nil?
+ if message.is_a?(Regexp)
+ e.message.should =~ message
+ else
+ e.message.should == message
+ end
+ end
+ return
+ rescue => e
+ fail_with_message("expected #{exception}#{message.nil? ? "" : " with #{message.inspect}"}, got #{e.inspect}")
+ end
+ fail_with_message("expected #{exception}#{message.nil? ? "" : " with #{message.inspect}"} but nothing was raised")
+ end
+
+ #Gone for 0.9
+ def throw(symbol)
+ begin
+ catch symbol do
+ @target.call
+ fail_with_message("expected #{symbol.inspect} to be thrown, but nothing was thrown")
+ end
+ rescue NameError => e
+ fail_with_message("expected #{symbol.inspect} to be thrown, got #{e.inspect}")
+ end
+ end
+ end
+
+ private
+ def __delegate_method_missing_to_target(original_sym, actual_sym, *args)
+ ::Spec::Matchers.generated_description = "should #{original_sym} #{args[0].inspect}"
+ return if @target.send(actual_sym, *args)
+ fail_with_message(default_message(original_sym, args[0]), args[0], @target)
+ end
+ end
+
+ end
+ end
+end
diff --git a/test/lib/spec/expectations/sugar.rb b/test/lib/spec/expectations/sugar.rb
new file mode 100644
index 000000000..906111f0e
--- /dev/null
+++ b/test/lib/spec/expectations/sugar.rb
@@ -0,0 +1,47 @@
+deprecated do
+module Spec
+ module Expectations
+ # This module adds syntactic sugar that allows usage of should_* instead of should.*
+ module UnderscoreSugar
+ def handle_underscores_for_rspec! # :nodoc:
+ original_method_missing = instance_method(:method_missing)
+ class_eval do
+ def method_missing(sym, *args, &block)
+ _method_missing(sym, args, block)
+ end
+
+ define_method :_method_missing do |sym, args, block|
+ return original_method_missing.bind(self).call(sym, *args, &block) unless sym.to_s =~ /^should_/
+ if sym.to_s =~ /^should_not_/
+ if __matcher.respond_to?(__strip_should_not(sym))
+ return should_not(__matcher.__send__(__strip_should_not(sym), *args, &block))
+ else
+ return Spec::Expectations::Should::Not.new(self).__send__(__strip_should_not(sym), *args, &block) if sym.to_s =~ /^should_not_/
+ end
+ else
+ if __matcher.respond_to?(__strip_should(sym))
+ return should(__matcher.__send__(__strip_should(sym), *args, &block))
+ else
+ return Spec::Expectations::Should::Should.new(self).__send__(__strip_should(sym), *args, &block)
+ end
+ end
+ end
+
+ def __strip_should(sym) # :nodoc
+ sym.to_s[7..-1]
+ end
+
+ def __strip_should_not(sym) # :nodoc
+ sym.to_s[11..-1]
+ end
+
+ def __matcher
+ @matcher ||= Spec::Matchers::Matcher.new
+ end
+ end
+ end
+ end
+ end
+end
+
+end \ No newline at end of file