1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
3 module ExampleExpectations
6 def initialize(*args, &block)
7 if args.last.is_a? Hash
8 @expected = args.last[:expected]
11 @expected = block.call
18 return @expected == target
27 "expected #{@expected}, got #{@target}"
30 def negative_failure_message
31 "expected not #{@expected}, got #{@target}"
35 class PositiveOnlyMatcher < ArbitraryMatcher
36 undef negative_failure_message rescue nil
39 def arbitrary_matcher(*args, &block)
40 ArbitraryMatcher.new(*args, &block)
43 def positive_only_matcher(*args, &block)
44 PositiveOnlyMatcher.new(*args, &block)
51 describe ExpectationMatcherHandler, ".handle_matcher" do
52 it "should ask the matcher if it matches" do
53 matcher = mock("matcher")
55 matcher.should_receive(:matches?).with(actual).and_return(true)
56 ExpectationMatcherHandler.handle_matcher(actual, matcher)
59 it "should explain when the matcher parameter is not a matcher" do
61 nonmatcher = mock("nonmatcher")
63 ExpectationMatcherHandler.handle_matcher(actual, nonmatcher)
64 rescue Spec::Expectations::InvalidMatcherError => e
67 e.message.should =~ /^Expected a matcher, got /
71 describe NegativeExpectationMatcherHandler, ".handle_matcher" do
72 it "should explain when matcher does not support should_not" do
73 matcher = mock("matcher")
74 matcher.stub!(:matches?)
77 NegativeExpectationMatcherHandler.handle_matcher(actual, matcher)
78 }.should fail_with(/Matcher does not support should_not.\n/)
81 it "should ask the matcher if it matches" do
82 matcher = mock("matcher")
84 matcher.stub!(:negative_failure_message)
85 matcher.should_receive(:matches?).with(actual).and_return(false)
86 NegativeExpectationMatcherHandler.handle_matcher(actual, matcher)
89 it "should explain when the matcher parameter is not a matcher" do
91 nonmatcher = mock("nonmatcher")
93 NegativeExpectationMatcherHandler.handle_matcher(actual, nonmatcher)
94 rescue Spec::Expectations::InvalidMatcherError => e
97 e.message.should =~ /^Expected a matcher, got /
101 describe ExpectationMatcherHandler do
102 include ExampleExpectations
104 it "should handle submitted args" do
105 5.should arbitrary_matcher(:expected => 5)
106 5.should arbitrary_matcher(:expected => "wrong").with(5)
107 lambda { 5.should arbitrary_matcher(:expected => 4) }.should fail_with("expected 4, got 5")
108 lambda { 5.should arbitrary_matcher(:expected => 5).with(4) }.should fail_with("expected 4, got 5")
109 5.should_not arbitrary_matcher(:expected => 4)
110 5.should_not arbitrary_matcher(:expected => 5).with(4)
111 lambda { 5.should_not arbitrary_matcher(:expected => 5) }.should fail_with("expected not 5, got 5")
112 lambda { 5.should_not arbitrary_matcher(:expected => 4).with(5) }.should fail_with("expected not 5, got 5")
115 it "should handle the submitted block" do
116 5.should arbitrary_matcher { 5 }
117 5.should arbitrary_matcher(:expected => 4) { 5 }
118 5.should arbitrary_matcher(:expected => 4).with(5) { 3 }
121 it "should explain when matcher does not support should_not" do
123 5.should_not positive_only_matcher(:expected => 5)
124 }.should fail_with(/Matcher does not support should_not.\n/)