Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / spec / spec / matchers / handler_spec.rb
blob9f04c6bed9841c3c273dfa91c75a0d3bdb20e1a7
1 require File.dirname(__FILE__) + '/../../spec_helper.rb'
3 module ExampleExpectations
4   
5   class ArbitraryMatcher
6     def initialize(*args, &block)
7       if args.last.is_a? Hash
8         @expected = args.last[:expected]
9       end
10       if block_given?
11         @expected = block.call
12       end
13       @block = block
14     end
15     
16     def matches?(target)
17       @target = target
18       return @expected == target
19     end
20     
21     def with(new_value)
22       @expected = new_value
23       self
24     end
25     
26     def failure_message
27       "expected #{@expected}, got #{@target}"
28     end
29     
30     def negative_failure_message
31       "expected not #{@expected}, got #{@target}"
32     end
33   end
34   
35   class PositiveOnlyMatcher < ArbitraryMatcher
36     undef negative_failure_message
37   end
38   
39   def arbitrary_matcher(*args, &block)
40     ArbitraryMatcher.new(*args, &block)
41   end
42   
43   def positive_only_matcher(*args, &block)
44     PositiveOnlyMatcher.new(*args, &block)
45   end
46   
47 end
49 module Spec
50   module Expectations
51     describe ExpectationMatcherHandler, ".handle_matcher" do
52       it "should ask the matcher if it matches" do
53         matcher = mock("matcher")
54         actual = Object.new
55         matcher.should_receive(:matches?).with(actual).and_return(true)
56         ExpectationMatcherHandler.handle_matcher(actual, matcher)
57       end
58     end
59     
60     describe ExpectationMatcherHandler do
61       include ExampleExpectations
62       
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")
72       end
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 }
78       end
79   
80       it "should explain when matcher does not support should_not" do
81         lambda {
82           5.should_not positive_only_matcher(:expected => 5)
83         }.should fail_with(/Matcher does not support should_not.\n/)
84       end
86     end
87   end
88 end