changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/spec/spec/matchers/handler_spec.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
3module 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 rescue nil
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
47end
48
49module 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
59 it "should explain when the matcher parameter is not a matcher" do
60 begin
61 nonmatcher = mock("nonmatcher")
62 actual = Object.new
63 ExpectationMatcherHandler.handle_matcher(actual, nonmatcher)
64 rescue Spec::Expectations::InvalidMatcherError => e
65 end
66
67 e.message.should =~ /^Expected a matcher, got /
68 end
69 end
70
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?)
75 actual = Object.new
76 lambda {
77 NegativeExpectationMatcherHandler.handle_matcher(actual, matcher)
78 }.should fail_with(/Matcher does not support should_not.\n/)
79 end
80
81 it "should ask the matcher if it matches" do
82 matcher = mock("matcher")
83 actual = Object.new
84 matcher.stub!(:negative_failure_message)
85 matcher.should_receive(:matches?).with(actual).and_return(false)
86 NegativeExpectationMatcherHandler.handle_matcher(actual, matcher)
87 end
88
89 it "should explain when the matcher parameter is not a matcher" do
90 begin
91 nonmatcher = mock("nonmatcher")
92 actual = Object.new
93 NegativeExpectationMatcherHandler.handle_matcher(actual, nonmatcher)
94 rescue Spec::Expectations::InvalidMatcherError => e
95 end
96
97 e.message.should =~ /^Expected a matcher, got /
98 end
99 end
100
101 describe ExpectationMatcherHandler do
102 include ExampleExpectations
103
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")
113 end
114
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 }
119 end
120
121 it "should explain when matcher does not support should_not" do
122 lambda {
123 5.should_not positive_only_matcher(:expected => 5)
124 }.should fail_with(/Matcher does not support should_not.\n/)
125 end
126
127 end
128 end
129end