quacky 0.1.2 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/quacky/quacky.rb +43 -9
  3. data/spec/lib/quacky_spec.rb +42 -13
  4. metadata +21 -10
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.2
data/lib/quacky/quacky.rb CHANGED
@@ -8,14 +8,14 @@ module Quacky
8
8
  @duck_type = duck_type
9
9
  end
10
10
 
11
- def verify!(object)
11
+ def verify! object
12
12
  duck_type_methods.each do |method|
13
13
  raise Quacky::DuckTypeVerificationFailure, "object does not respond to `#{method.name}'" unless object.respond_to?(method.name)
14
14
 
15
15
  target_method = object.public_method(method.name)
16
16
  if target_method.parameters.count != method.parameters.count ||
17
17
  target_method.parameters.map {|p| p.first } != method.parameters.map {|p| p.first}
18
- raise Quacky::DuckTypeVerificationFailure, "method signatures differ"
18
+ raise Quacky::DuckTypeVerificationFailure, "definitions of method `#{method.name}` differ in parameters accepted."
19
19
  end
20
20
 
21
21
  true
@@ -42,7 +42,19 @@ end
42
42
 
43
43
  module Quacky
44
44
  extend self
45
- class Double; end
45
+
46
+ class Double
47
+ def initialize(name)
48
+ @name = name
49
+ end
50
+
51
+ def inspect
52
+ "<Quacky::Double :#{name}>"
53
+ end
54
+
55
+ private
56
+ attr_reader :name
57
+ end
46
58
 
47
59
  class NoMethodError < RuntimeError; end
48
60
  class MethodSignatureMismatch < ArgumentError; end
@@ -93,7 +105,7 @@ module Quacky
93
105
  raise UnsatisfiedExpectation
94
106
  end
95
107
  else
96
- was_called? || raise(UnsatisfiedExpectation)
108
+ was_called? || raise(UnsatisfiedExpectation, "Expected `#{@method.name}` to be called.")
97
109
  end
98
110
  end
99
111
 
@@ -108,7 +120,7 @@ module Quacky
108
120
  if expected_args && called_args != expected_args
109
121
  raise(
110
122
  Quacky::UnexpectedArguments,
111
- "#{@method.name} was called with unexpected arguments: #{called_args.join ", "}"
123
+ "#{@method.name} was called with unexpected arguments: #{called_args.map(&:inspect).join ", "}. expected: #{expected_args.map(&:inspect).join ", "}"
112
124
  )
113
125
  end
114
126
  end
@@ -122,7 +134,7 @@ module Quacky
122
134
  begin
123
135
  @method.call *args
124
136
  rescue ArgumentError => e
125
- raise Quacky::MethodSignatureMismatch, e.message
137
+ raise Quacky::MethodSignatureMismatch, "#{@method.receiver}##{@method.name} was called with the #{e.message}"
126
138
  end
127
139
  end
128
140
  end
@@ -173,8 +185,8 @@ module Quacky
173
185
  end
174
186
  end
175
187
 
176
- def double(*duck_types)
177
- Double.new.tap do |object|
188
+ def double(name, *duck_types)
189
+ Double.new(name).tap do |object|
178
190
  duck_types.each do |duck_type|
179
191
  object.extend duck_type
180
192
  end
@@ -182,10 +194,32 @@ module Quacky
182
194
  end
183
195
  end
184
196
 
185
- def class_double options
197
+ module ClassInspect
198
+ attr_reader :class_double_name
199
+
200
+ def name_class_double name
201
+ @class_double_name = name
202
+ end
203
+
204
+ def inspect
205
+ "Quacky::ClassDouble(:#{@class_double_name})"
206
+ end
207
+ end
208
+
209
+ module InstanceInspect
210
+ def inspect
211
+ "<Quacky::ClassDouble :#{self.class.class_double_name}>"
212
+ end
213
+ end
214
+
215
+ def class_double name, options
186
216
  class_modules, instance_modules = parse_class_double_options options
187
217
 
188
218
  Class.new do
219
+ extend ClassInspect
220
+ include InstanceInspect
221
+ name_class_double name
222
+
189
223
  class_modules.each do |class_module|
190
224
  extend class_module
191
225
  end
@@ -39,7 +39,7 @@ describe Quacky::DuckTypeVerifier do
39
39
  end
40
40
 
41
41
  it "should raise a Quacky::DuckTypeVerificationFailure" do
42
- expect { verifier.verify! non_conforming_object }.to raise_exception Quacky::DuckTypeVerificationFailure
42
+ expect { verifier.verify! non_conforming_object }.to raise_exception Quacky::DuckTypeVerificationFailure, "definitions of method `quack` differ in parameters accepted."
43
43
  end
44
44
  end
45
45
  end
@@ -71,7 +71,7 @@ describe Quacky do
71
71
 
72
72
  describe "#double" do
73
73
  context "with a single included module" do
74
- let(:eigenclass) { class << Quacky.double(Duck); self; end }
74
+ let(:eigenclass) { class << Quacky.double(:duck, Duck); self; end }
75
75
 
76
76
  subject { eigenclass }
77
77
 
@@ -80,15 +80,31 @@ describe Quacky do
80
80
 
81
81
  context "with multiple included modules" do
82
82
  let(:another_mod) { Module.new }
83
- let(:eigenclass) { class << Quacky.double(Duck, another_mod); self; end }
83
+ let(:eigenclass) { class << Quacky.double(:duck, Duck, another_mod); self; end }
84
84
  subject { eigenclass }
85
85
  its(:included_modules) { should include Duck, another_mod }
86
86
  end
87
87
  end
88
88
 
89
89
  describe "#class_double" do
90
- let(:class_double) { Quacky.class_double class: class_ducks, instance: instance_ducks }
90
+ let(:class_double) { Quacky.class_double :class_double_duck, class: class_ducks, instance: instance_ducks }
91
91
  let(:class_eigenclass) { class << class_double; self; end }
92
+ let(:class_ducks) { [] }
93
+ let(:instance_ducks) { [] }
94
+
95
+ describe ".inspect" do
96
+ context "class" do
97
+ it "should return `Quacky::ClassDouble(:<double_name>)`" do
98
+ class_double.inspect.should == "Quacky::ClassDouble(:class_double_duck)"
99
+ end
100
+ end
101
+
102
+ context "instance" do
103
+ it "should return `<Quacky::ClassDouble :<double_name>>`" do
104
+ class_double.new.inspect.should == "<Quacky::ClassDouble :class_double_duck>"
105
+ end
106
+ end
107
+ end
92
108
 
93
109
  shared_examples_for "quacky class double" do
94
110
  context "instance methods" do
@@ -118,9 +134,15 @@ describe Quacky do
118
134
  end
119
135
 
120
136
  describe Quacky::Double do
121
- let(:q_double) { Quacky.double Duck }
137
+ let(:q_double) { Quacky.double :duck, Duck }
122
138
  let(:expectation) { double(:expectation) }
123
139
 
140
+ describe ".inspect" do
141
+ it "should be formatted like `<Quacky::Double :<double_name>>`" do
142
+ Quacky::Double.new(:test).inspect.should == "<Quacky::Double :test>"
143
+ end
144
+ end
145
+
124
146
  describe ".stub" do
125
147
  it "should raise an exception if the method does not already exist on the double" do
126
148
  expect { q_double.stub("random_method_#{rand 1000000}") }.to raise_exception Quacky::NoMethodError
@@ -141,12 +163,19 @@ describe Quacky do
141
163
  end
142
164
 
143
165
  it "should support methods ending in !, ?, and regular letters" do
144
- new_return = double :new_arg
145
- question_return = double :question_arg
146
- regular_return = double :regular_arg
147
- Quacky::Stub.stub(:bang!).and_return new_return
148
- Quacky::Stub.stub(:question!).and_return question_return
149
- Quacky::Stub.stub(:regular).and_return regular_return
166
+ q_double = Quacky.double(:quacky_double, Module.new do
167
+ def bang!; end
168
+ def question?; end
169
+ def regular; end
170
+ end)
171
+
172
+ q_double.should_receive(:bang!).and_return "bang"
173
+ q_double.should_receive(:question?).and_return "question"
174
+ q_double.should_receive(:regular).and_return "regular"
175
+
176
+ q_double.bang!.should == "bang"
177
+ q_double.question?.should == "question"
178
+ q_double.regular.should == "regular"
150
179
  end
151
180
  end
152
181
 
@@ -182,7 +211,7 @@ module Quacky
182
211
 
183
212
  describe "#with" do
184
213
  it "should raise an exception if the original method's signature mismatches" do
185
- expect { q_expectation.with 1,2,3 }.to raise_exception Quacky::MethodSignatureMismatch
214
+ expect { q_expectation.with 1,2,3 }.to raise_exception Quacky::MethodSignatureMismatch, "#{object.inspect}#duck! was called with the wrong number of arguments (3 for 1)"
186
215
  end
187
216
  end
188
217
 
@@ -237,7 +266,7 @@ module Quacky
237
266
  end
238
267
 
239
268
  context "not called at all" do
240
- specify { expect { subject }.to raise_exception Quacky::UnsatisfiedExpectation }
269
+ specify { expect { subject }.to raise_exception Quacky::UnsatisfiedExpectation, "Expected `duck!` to be called." }
241
270
  end
242
271
 
243
272
  context "with expectation" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quacky
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-19 00:00:00.000000000 Z
12
+ date: 2012-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70244437807500 !ruby/object:Gem::Requirement
16
+ requirement: &70131385524940 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.2.5
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70244437807500
24
+ version_requirements: *70131385524940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70244437807100 !ruby/object:Gem::Requirement
27
+ requirement: &70131385524500 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70244437807100
35
+ version_requirements: *70131385524500
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: cucumber
38
- requirement: &70244437806640 !ruby/object:Gem::Requirement
38
+ requirement: &70131385391320 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,21 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70244437806640
46
+ version_requirements: *70131385391320
47
+ - !ruby/object:Gem::Dependency
48
+ name: specdown
49
+ requirement: &70131385390880 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70131385390880
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: rake
49
- requirement: &70244437806220 !ruby/object:Gem::Requirement
60
+ requirement: &70131385390460 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,7 +65,7 @@ dependencies:
54
65
  version: '0'
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *70244437806220
68
+ version_requirements: *70131385390460
58
69
  description:
59
70
  email: moonmaster9000@gmail.com
60
71
  executables: []