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
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)
60 describe ExpectationMatcherHandler do
61 include ExampleExpectations
63 it "should handle submitted args" do
64 5.should arbitrary_matcher(:expected => 5)
65 5.should arbitrary_matcher(:expected => "wrong").with(5)
66 lambda { 5.should arbitrary_matcher(:expected => 4) }.should fail_with("expected 4, got 5")
67 lambda { 5.should arbitrary_matcher(:expected => 5).with(4) }.should fail_with("expected 4, got 5")
68 5.should_not arbitrary_matcher(:expected => 4)
69 5.should_not arbitrary_matcher(:expected => 5).with(4)
70 lambda { 5.should_not arbitrary_matcher(:expected => 5) }.should fail_with("expected not 5, got 5")
71 lambda { 5.should_not arbitrary_matcher(:expected => 4).with(5) }.should fail_with("expected not 5, got 5")
74 it "should handle the submitted block" do
75 5.should arbitrary_matcher { 5 }
76 5.should arbitrary_matcher(:expected => 4) { 5 }
77 5.should arbitrary_matcher(:expected => 4).with(5) { 3 }
80 it "should explain when matcher does not support should_not" do
82 5.should_not positive_only_matcher(:expected => 5)
83 }.should fail_with(/Matcher does not support should_not.\n/)