changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/lib/spec/mocks/argument_expectation.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
1module Spec
2 module Mocks
3
4 class MatcherConstraint
5 def initialize(matcher)
6 @matcher = matcher
7 end
8
9 def matches?(value)
10 @matcher.matches?(value)
11 end
12 end
13
14 class LiteralArgConstraint
15 def initialize(literal)
16 @literal_value = literal
17 end
18
19 def matches?(value)
20 @literal_value == value
21 end
22 end
23
24 class RegexpArgConstraint
25 def initialize(regexp)
26 @regexp = regexp
27 end
28
29 def matches?(value)
30 return value =~ @regexp unless value.is_a?(Regexp)
31 value == @regexp
32 end
33 end
34
35 class AnyArgConstraint
36 def initialize(ignore)
37 end
38
39 def ==(other)
40 true
41 end
42
43 # TODO - need this?
44 def matches?(value)
45 true
46 end
47 end
48
49 class AnyArgsConstraint
50 def description
51 "any args"
52 end
53 end
54
55 class NoArgsConstraint
56 def description
57 "no args"
58 end
59
60 def ==(args)
61 args == []
62 end
63 end
64
65 class NumericArgConstraint
66 def initialize(ignore)
67 end
68
69 def matches?(value)
70 value.is_a?(Numeric)
71 end
72 end
73
74 class BooleanArgConstraint
75 def initialize(ignore)
76 end
77
78 def ==(value)
79 matches?(value)
80 end
81
82 def matches?(value)
83 return true if value.is_a?(TrueClass)
84 return true if value.is_a?(FalseClass)
85 false
86 end
87 end
88
89 class StringArgConstraint
90 def initialize(ignore)
91 end
92
93 def matches?(value)
94 value.is_a?(String)
95 end
96 end
97
98 class DuckTypeArgConstraint
99 def initialize(*methods_to_respond_to)
100 @methods_to_respond_to = methods_to_respond_to
101 end
102
103 def matches?(value)
104 @methods_to_respond_to.all? { |sym| value.respond_to?(sym) }
105 end
106
107 def description
108 "duck_type"
109 end
110 end
111
112 class ArgumentExpectation
113 attr_reader :args
114 @@constraint_classes = Hash.new { |hash, key| LiteralArgConstraint}
115 @@constraint_classes[:anything] = AnyArgConstraint
116 @@constraint_classes[:numeric] = NumericArgConstraint
117 @@constraint_classes[:boolean] = BooleanArgConstraint
118 @@constraint_classes[:string] = StringArgConstraint
119
120 def initialize(args)
121 @args = args
122 if [:any_args] == args
123 @expected_params = nil
124 warn_deprecated(:any_args.inspect, "any_args()")
125 elsif args.length == 1 && args[0].is_a?(AnyArgsConstraint) then @expected_params = nil
126 elsif [:no_args] == args
127 @expected_params = []
128 warn_deprecated(:no_args.inspect, "no_args()")
129 elsif args.length == 1 && args[0].is_a?(NoArgsConstraint) then @expected_params = []
130 else @expected_params = process_arg_constraints(args)
131 end
132 end
133
134 def process_arg_constraints(constraints)
135 constraints.collect do |constraint|
136 convert_constraint(constraint)
137 end
138 end
139
140 def warn_deprecated(deprecated_method, instead)
141 Kernel.warn "The #{deprecated_method} constraint is deprecated. Use #{instead} instead."
142 end
143
144 def convert_constraint(constraint)
145 if [:anything, :numeric, :boolean, :string].include?(constraint)
146 case constraint
147 when :anything
148 instead = "anything()"
149 when :boolean
150 instead = "boolean()"
151 when :numeric
152 instead = "an_instance_of(Numeric)"
153 when :string
154 instead = "an_instance_of(String)"
155 end
156 warn_deprecated(constraint.inspect, instead)
157 return @@constraint_classes[constraint].new(constraint)
158 end
159 return MatcherConstraint.new(constraint) if is_matcher?(constraint)
160 return RegexpArgConstraint.new(constraint) if constraint.is_a?(Regexp)
161 return LiteralArgConstraint.new(constraint)
162 end
163
164 def is_matcher?(obj)
165 return obj.respond_to?(:matches?) && obj.respond_to?(:description)
166 end
167
168 def check_args(args)
169 return true if @expected_params.nil?
170 return true if @expected_params == args
171 return constraints_match?(args)
172 end
173
174 def constraints_match?(args)
175 return false if args.length != @expected_params.length
176 @expected_params.each_index { |i| return false unless @expected_params[i].matches?(args[i]) }
177 return true
178 end
179
180 end
181
182 end
183end