verified_double 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/CHANGELOG.markdown +12 -0
  2. data/README.md +25 -14
  3. data/features/CHANGELOG.markdown +12 -0
  4. data/features/readme.md +25 -14
  5. data/features/verified_mocks.feature +4 -2
  6. data/lib/verified_double.rb +10 -1
  7. data/lib/verified_double/matchers.rb +2 -2
  8. data/lib/verified_double/method_signature.rb +5 -5
  9. data/lib/verified_double/method_signature/boolean_value.rb +9 -0
  10. data/lib/verified_double/method_signature/class_value.rb +17 -0
  11. data/lib/verified_double/method_signature/instance_double_value.rb +9 -0
  12. data/lib/verified_double/method_signature/instance_value.rb +17 -0
  13. data/lib/verified_double/method_signature/recording_double_class_value.rb +9 -0
  14. data/lib/verified_double/method_signature/rspec_double_value.rb +9 -0
  15. data/lib/verified_double/method_signature/value.rb +37 -0
  16. data/lib/verified_double/method_signatures_report.rb +10 -3
  17. data/lib/verified_double/parse_method_signature.rb +2 -2
  18. data/lib/verified_double/recorded_method_signature.rb +9 -0
  19. data/lib/verified_double/recording_double.rb +13 -4
  20. data/lib/verified_double/stack_frame.rb +11 -0
  21. data/lib/verified_double/version.rb +1 -1
  22. data/spec/spec_helper.rb +1 -0
  23. data/spec/verified_double/matchers_spec.rb +1 -1
  24. data/spec/verified_double/method_signature/boolean_value_spec.rb +11 -0
  25. data/spec/verified_double/method_signature/class_value_spec.rb +35 -0
  26. data/spec/verified_double/method_signature/instance_double_value_spec.rb +17 -0
  27. data/spec/verified_double/method_signature/instance_value_spec.rb +39 -0
  28. data/spec/verified_double/method_signature/recording_double_class_value_spec.rb +23 -0
  29. data/spec/verified_double/method_signature/rspec_double_spec.rb +14 -0
  30. data/spec/verified_double/method_signature/value_spec.rb +89 -0
  31. data/spec/verified_double/method_signature_spec.rb +16 -16
  32. data/spec/verified_double/method_signatures_report_spec.rb +14 -12
  33. data/spec/verified_double/parse_method_signature_spec.rb +3 -3
  34. data/spec/verified_double/recorded_method_signature_spec.rb +23 -0
  35. data/spec/verified_double/recording_double_spec.rb +103 -77
  36. data/spec/verified_double/stack_frame_spec.rb +15 -0
  37. data/spec/verified_double_spec.rb +1 -1
  38. metadata +29 -7
  39. data/lib/verified_double/method_signature_value.rb +0 -45
  40. data/spec/unit_helper.rb +0 -16
  41. data/spec/verified_double/method_signature_value_spec.rb +0 -126
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe VerifiedDouble::RecordedMethodSignature do
4
+ class Dummy
5
+ end
6
+
7
+ let(:stack_frame_string){ "./lib/verified_double/method_signature.rb:7:in `block in initialize'" }
8
+
9
+ let(:attributes){
10
+ { class_name: 'Dummy',
11
+ method_operator: '#',
12
+ stack_frame: VerifiedDouble::StackFrame.new(stack_frame_string) } }
13
+
14
+ subject {
15
+ described_class.new(attributes).tap {|method_signature|
16
+ method_signature.method = 'do_something' } }
17
+
18
+ describe "#to_s" do
19
+ it "includes the stack frame" do
20
+ expect(subject.to_s).to include(stack_frame_string)
21
+ end
22
+ end
23
+ end
@@ -1,194 +1,194 @@
1
- require 'unit_helper'
1
+ require 'spec_helper'
2
2
  require "verified_double/recording_double"
3
3
 
4
4
  describe VerifiedDouble::RecordingDouble do
5
- let(:class_name) { 'Object' }
6
- let(:internal_double) { double(class_name) }
5
+ class Dummy
6
+ end
7
+
8
+ let(:class_name) { 'Dummy' }
9
+ let(:some_instance_double) { double(class_name) }
10
+ let(:some_class_double) { stub_const(class_name, Class.new) }
7
11
 
8
- subject { VerifiedDouble::RecordingDouble.new(internal_double, class_name) }
12
+ let(:recording_instance_double) { VerifiedDouble::RecordingDouble.new(some_instance_double, class_name) }
13
+ let(:recording_class_double) { VerifiedDouble::RecordingDouble.new(some_class_double, class_name) }
9
14
 
10
15
  describe "#initialize" do
11
16
  it "requires a double" do
12
- expect(subject.double).to eq(internal_double)
17
+ expect(recording_instance_double.double).to eq(some_instance_double)
13
18
  end
14
19
 
15
20
  context "with method_stubs hash" do
16
21
  let(:stubbed_method){ :some_method }
17
22
  let(:assumed_output){ :some_output }
18
23
 
19
- subject { VerifiedDouble::RecordingDouble.new(internal_double, class_name, some_method: assumed_output) }
24
+ let(:recording_instance_double) {
25
+ VerifiedDouble::RecordingDouble.new(
26
+ some_instance_double,
27
+ class_name,
28
+ some_method: assumed_output) }
20
29
 
21
30
  it "stubs the methods of the instance" do
22
- expect(subject.send(stubbed_method)).to eq(assumed_output)
31
+ expect(recording_instance_double.send(stubbed_method)).to eq(assumed_output)
23
32
  end
24
33
  end
25
34
  end
26
35
 
27
36
  it "delegates all unknown calls to its internal double" do
28
- internal_double.should_receive(:do_something)
29
- subject.do_something
37
+ some_instance_double.should_receive(:do_something)
38
+ recording_instance_double.do_something
30
39
  end
31
40
 
32
41
  describe "#to_s" do
33
- class Dummy
34
- end
35
-
36
42
  context "where the internal double is a class" do
37
- let(:class_name) { 'Dummy' }
38
- let(:internal_double) { stub_const(class_name, Class.new) }
39
-
40
- it { expect(subject.to_s).to eq("VerifiedDouble.of_class('#{class_name}')") }
43
+ it { expect(recording_class_double.to_s).to eq("VerifiedDouble.of_class('#{class_name}')") }
41
44
  end
42
45
 
43
46
  context "where the internal double is an instance" do
44
- let(:class_name) { 'Dummy' }
45
- let(:internal_double) { double(class_name) }
47
+ let(:some_instance_double) { double(class_name) }
46
48
 
47
- it { expect(subject.to_s).to eq("VerifiedDouble.of_instance('#{class_name}')") }
49
+ it { expect(recording_instance_double.to_s).to eq("VerifiedDouble.of_instance('#{class_name}')") }
48
50
  end
49
51
  end
50
52
 
51
53
  describe "#inspect" do
52
54
  it "is human-readable representation of the recording double, NOT the internal double" do
53
- expect(subject.inspect).to match("VerifiedDouble")
55
+ expect(recording_instance_double.inspect).to match("VerifiedDouble")
54
56
  end
55
57
  end
56
58
 
57
59
  describe "#class_name" do
58
60
  context "where the internal double is a double" do
59
61
  it "is the name of the class represented by the double" do
60
- internal_double = double('Object')
61
- subject = described_class.new(internal_double, 'Object')
62
- expect(subject.class_name).to eq('Object')
62
+ expect(recording_instance_double.class_name).to eq(class_name)
63
63
  end
64
64
  end
65
65
 
66
66
  context "where the internal double is a stub const" do
67
67
  it "is the name of the class represented by the class double" do
68
- internal_double = stub_const('Object', Class.new)
69
- subject = described_class.new(internal_double, 'Object')
70
- expect(subject.class_name).to eq('Object')
68
+ expect(recording_class_double.class_name).to eq(class_name)
71
69
  end
72
70
  end
73
71
  end
74
72
 
75
73
  describe "#class_double?" do
76
74
  it "should be true if the internal double is a class double" do
77
- internal_double = double('Object')
78
- subject = described_class.new(internal_double, 'Object')
79
- expect(subject).to_not be_class_double
75
+ expect(recording_instance_double).to_not be_class_double
80
76
  end
81
77
 
82
78
  it "should be false if the internal double is not an class double" do
83
- internal_double = stub_const('Object', Class.new)
84
- subject = described_class.new(internal_double, 'Object')
85
- expect(subject).to be_class_double
79
+ expect(recording_class_double).to be_class_double
86
80
  end
87
81
  end
88
82
 
89
83
  describe "#method_operator" do
90
- context "when the subject wraps an instance double" do
91
- let(:internal_double) { double(class_name) }
92
-
93
- subject { VerifiedDouble::RecordingDouble.new(internal_double, class_name) }
94
-
84
+ context "when the recording_instance_double wraps an instance double" do
95
85
  it "is #" do
96
- expect(subject.method_operator).to eq("#")
86
+ expect(recording_instance_double.method_operator).to eq("#")
97
87
  end
98
88
  end
99
89
 
100
- context "when the subject wraps a class double" do
101
- let(:internal_double) { stub_const(class_name, Class.new) }
102
-
103
- subject { VerifiedDouble::RecordingDouble.new(internal_double, class_name) }
104
-
90
+ context "when the recording_instance_double wraps a class double" do
105
91
  it "is '.'" do
106
- expect(subject.method_operator).to eq(".")
92
+ expect(recording_class_double.method_operator).to eq(".")
107
93
  end
108
94
  end
109
95
  end
110
96
 
111
97
  describe "#should_receive(method)" do
112
98
  it "appends a new method signature with the method to the recording double's method signatures" do
113
- expect(subject.method_signatures).to be_empty
99
+ expect(recording_instance_double.method_signatures).to be_empty
100
+
101
+ recording_instance_double.should_receive(:fake_to_s)
102
+ recording_instance_double.should_receive(:fake_inspect)
114
103
 
115
- subject.should_receive(:fake_to_s)
116
- subject.should_receive(:fake_inspect)
104
+ expect(recording_instance_double.method_signatures.map(&:method)).to eq(['fake_to_s', 'fake_inspect'])
117
105
 
118
- expect(subject.method_signatures.map(&:method)).to eq(['fake_to_s', 'fake_inspect'])
106
+ recording_instance_double.fake_to_s
107
+ recording_instance_double.fake_inspect
108
+ end
119
109
 
120
- subject.fake_to_s
121
- subject.fake_inspect
110
+ it "records the stack frames of the recording double's caller" do
111
+ recording_instance_double.should_receive(:fake_to_s)
112
+ recording_instance_double.fake_to_s
113
+ expect(recording_instance_double.method_signatures.first.stack_frame.to_s)
114
+ .to include("spec/verified_double/recording_double_spec.rb")
122
115
  end
123
116
  end
124
117
 
125
118
  describe "#stub(method)" do
126
119
  it "appends a new method signature with the method to the recording double's method signatures" do
127
- expect(subject.method_signatures).to be_empty
120
+ expect(recording_instance_double.method_signatures).to be_empty
121
+
122
+ recording_instance_double.stub(:fake_to_s)
123
+ recording_instance_double.stub(:fake_inspect)
128
124
 
129
- subject.stub(:fake_to_s)
130
- subject.stub(:fake_inspect)
125
+ expect(recording_instance_double.method_signatures.map(&:method)).to eq(['fake_to_s', 'fake_inspect'])
126
+ end
127
+
128
+ it "records the stack frames of the recording double's caller" do
129
+ recording_instance_double.stub(:fake_to_s)
131
130
 
132
- expect(subject.method_signatures.map(&:method)).to eq(['fake_to_s', 'fake_inspect'])
131
+ expect(recording_instance_double.method_signatures.first.stack_frame.to_s)
132
+ .to include("spec/verified_double/recording_double_spec.rb")
133
133
  end
134
134
  end
135
135
 
136
136
  describe "#with(*args)" do
137
137
  it "sets the args of the last method signature" do
138
- subject.should_receive(:fake_to_s).with(:arg_1, :arg_2)
138
+ recording_instance_double.should_receive(:fake_to_s).with(:arg_1, :arg_2)
139
139
 
140
- expect(subject.method_signatures[0].args).to be_all{|arg| arg.is_a?(VerifiedDouble::MethodSignatureValue) }
141
- expect(subject.method_signatures[0].args.map(&:value)).to eq([:arg_1, :arg_2])
140
+ expect(recording_instance_double.method_signatures[0].args).to be_all{|arg| arg.is_a?(VerifiedDouble::MethodSignature::Value) }
141
+ expect(recording_instance_double.method_signatures[0].args.map(&:content)).to eq([:arg_1, :arg_2])
142
142
 
143
- subject.fake_to_s(:arg_1, :arg_2)
143
+ recording_instance_double.fake_to_s(:arg_1, :arg_2)
144
144
  end
145
145
  end
146
146
 
147
147
  describe "#and_return(return_value)" do
148
148
  it "sets the return value of the last method signature" do
149
- subject.should_receive(:fake_to_s).with(:arg_1, :arg_2).and_return(:return_value)
149
+ recording_instance_double.should_receive(:fake_to_s).with(:arg_1, :arg_2).and_return(:return_value)
150
150
 
151
- return_values = subject.method_signatures[0].return_values
151
+ return_values = recording_instance_double.method_signatures[0].return_values
152
152
  expect(return_values).to have(1).return_value
153
- expect(return_values.first).to be_a(VerifiedDouble::MethodSignatureValue)
154
- expect(return_values.first.value).to eq(:return_value)
153
+ expect(return_values.first).to be_a(VerifiedDouble::MethodSignature::Value)
154
+ expect(return_values.first.content).to eq(:return_value)
155
155
 
156
- subject.fake_to_s(:arg_1, :arg_2)
156
+ recording_instance_double.fake_to_s(:arg_1, :arg_2)
157
157
  end
158
158
  end
159
159
 
160
160
  describe "#once" do
161
161
  it "is relayed to the internal double and returns the recording double" do
162
- expect(subject.should_receive(:fake_to_s).once).to be_a(described_class)
163
- subject.fake_to_s
162
+ expect(recording_instance_double.should_receive(:fake_to_s).once).to be_a(described_class)
163
+ recording_instance_double.fake_to_s
164
164
  end
165
165
  end
166
166
 
167
167
  describe "#twice" do
168
168
  it "is relayed to the internal double and returns the recording double" do
169
- expect(subject.should_receive(:fake_to_s).twice).to be_a(described_class)
170
- subject.fake_to_s
171
- subject.fake_to_s
169
+ expect(recording_instance_double.should_receive(:fake_to_s).twice).to be_a(described_class)
170
+ recording_instance_double.fake_to_s
171
+ recording_instance_double.fake_to_s
172
172
  end
173
173
  end
174
174
 
175
175
  describe "#at_least" do
176
176
  it "is relayed to the internal double and returns the recording double" do
177
- expect(subject.should_receive(:fake_to_s).at_least(:once)).to be_a(described_class)
178
- subject.fake_to_s
177
+ expect(recording_instance_double.should_receive(:fake_to_s).at_least(:once)).to be_a(described_class)
178
+ recording_instance_double.fake_to_s
179
179
  end
180
180
  end
181
181
 
182
182
  describe "#at_most" do
183
183
  it "is relayed to the internal double and returns the recording double" do
184
- expect(subject.should_receive(:fake_to_s).at_most(:once)).to be_a(described_class)
185
- subject.fake_to_s
184
+ expect(recording_instance_double.should_receive(:fake_to_s).at_most(:once)).to be_a(described_class)
185
+ recording_instance_double.fake_to_s
186
186
  end
187
187
  end
188
188
 
189
189
  describe "#any_number_of_times" do
190
190
  it "is relayed to the internal double and returns the recording double" do
191
- expect(subject.should_receive(:fake_to_s).any_number_of_times).to be_a(described_class)
191
+ expect(recording_instance_double.should_receive(:fake_to_s).any_number_of_times).to be_a(described_class)
192
192
  end
193
193
  end
194
194
 
@@ -196,15 +196,41 @@ describe VerifiedDouble::RecordingDouble do
196
196
  let(:some_error){ Exception.new }
197
197
 
198
198
  it "is relayed to the internal double and returns the recording double" do
199
- expect(subject.should_receive(:fake_to_s).and_raise(some_error)).to be_a(described_class)
200
- expect { subject.fake_to_s }.to raise_error(some_error)
199
+ expect(recording_instance_double.should_receive(:fake_to_s).and_raise(some_error)).to be_a(described_class)
200
+ expect { recording_instance_double.fake_to_s }.to raise_error(some_error)
201
201
  end
202
202
  end
203
203
 
204
204
  describe "#and_raise" do
205
205
  it "is relayed to the internal double and returns the recording double" do
206
- expect(subject.should_receive(:fake_to_s).and_throw(:some_error)).to be_a(described_class)
207
- expect { subject.fake_to_s }.to throw_symbol(:some_error)
206
+ expect(recording_instance_double.should_receive(:fake_to_s).and_throw(:some_error)).to be_a(described_class)
207
+ expect { recording_instance_double.fake_to_s }.to throw_symbol(:some_error)
208
+ end
209
+ end
210
+
211
+ describe "#class" do
212
+ context "where the internal double is an instance" do
213
+ it "is the constant matching the class name" do
214
+ expect(recording_instance_double.class).to eq(Dummy)
215
+ end
216
+ end
217
+
218
+ context "where the internal double is a class" do
219
+ it { expect(recording_class_double.class).to eq(Class) }
220
+ end
221
+ end
222
+
223
+ describe "#doubled_class" do
224
+ context "where the internal double is an instance" do
225
+ it "is the constant matching the class name" do
226
+ expect(recording_instance_double.doubled_class).to eq(Dummy)
227
+ end
228
+ end
229
+
230
+ context "where the internal double is a class" do
231
+ it "is the constant matching the class name" do
232
+ expect(recording_class_double.doubled_class).to eq(Dummy)
233
+ end
208
234
  end
209
235
  end
210
236
  end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe VerifiedDouble::RecordedMethodSignature do
4
+ let(:relative_stack_frame_string){ "spec/verified_double/method_signatures_report_spec.rb:31:in `block (4 levels) in <top (required)>'" }
5
+
6
+ let(:stack_frame_string){ "/verified_double/#{relative_stack_frame_string}" }
7
+
8
+ subject { VerifiedDouble::StackFrame.new(stack_frame_string) }
9
+
10
+ describe "#to_s" do
11
+ it "strips the parent dir of spec from the string" do
12
+ expect(subject.to_s).to eq("./#{relative_stack_frame_string}")
13
+ end
14
+ end
15
+ end
@@ -1,4 +1,4 @@
1
- require 'unit_helper'
1
+ require 'spec_helper'
2
2
  require 'verified_double'
3
3
 
4
4
  describe VerifiedDouble do
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: verified_double
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - George Mendoza
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-26 00:00:00.000000000 Z
12
+ date: 2013-06-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  type: :runtime
@@ -118,20 +118,35 @@ files:
118
118
  - lib/verified_double/boolean.rb
119
119
  - lib/verified_double/matchers.rb
120
120
  - lib/verified_double/method_signature.rb
121
- - lib/verified_double/method_signature_value.rb
121
+ - lib/verified_double/method_signature/boolean_value.rb
122
+ - lib/verified_double/method_signature/class_value.rb
123
+ - lib/verified_double/method_signature/instance_double_value.rb
124
+ - lib/verified_double/method_signature/instance_value.rb
125
+ - lib/verified_double/method_signature/recording_double_class_value.rb
126
+ - lib/verified_double/method_signature/rspec_double_value.rb
127
+ - lib/verified_double/method_signature/value.rb
122
128
  - lib/verified_double/method_signatures_report.rb
123
129
  - lib/verified_double/parse_method_signature.rb
130
+ - lib/verified_double/recorded_method_signature.rb
124
131
  - lib/verified_double/recording_double.rb
125
132
  - lib/verified_double/relays_to_internal_double_returning_self.rb
133
+ - lib/verified_double/stack_frame.rb
126
134
  - lib/verified_double/version.rb
127
135
  - spec/spec_helper.rb
128
- - spec/unit_helper.rb
129
136
  - spec/verified_double/matchers_spec.rb
137
+ - spec/verified_double/method_signature/boolean_value_spec.rb
138
+ - spec/verified_double/method_signature/class_value_spec.rb
139
+ - spec/verified_double/method_signature/instance_double_value_spec.rb
140
+ - spec/verified_double/method_signature/instance_value_spec.rb
141
+ - spec/verified_double/method_signature/recording_double_class_value_spec.rb
142
+ - spec/verified_double/method_signature/rspec_double_spec.rb
143
+ - spec/verified_double/method_signature/value_spec.rb
130
144
  - spec/verified_double/method_signature_spec.rb
131
- - spec/verified_double/method_signature_value_spec.rb
132
145
  - spec/verified_double/method_signatures_report_spec.rb
133
146
  - spec/verified_double/parse_method_signature_spec.rb
147
+ - spec/verified_double/recorded_method_signature_spec.rb
134
148
  - spec/verified_double/recording_double_spec.rb
149
+ - spec/verified_double/stack_frame_spec.rb
135
150
  - spec/verified_double_spec.rb
136
151
  - verified_double.gemspec
137
152
  homepage: https://www.relishapp.com/gsmendoza/verified-double
@@ -170,12 +185,19 @@ test_files:
170
185
  - features/verified_mocks.feature
171
186
  - features/verified_stubs.feature
172
187
  - spec/spec_helper.rb
173
- - spec/unit_helper.rb
174
188
  - spec/verified_double/matchers_spec.rb
189
+ - spec/verified_double/method_signature/boolean_value_spec.rb
190
+ - spec/verified_double/method_signature/class_value_spec.rb
191
+ - spec/verified_double/method_signature/instance_double_value_spec.rb
192
+ - spec/verified_double/method_signature/instance_value_spec.rb
193
+ - spec/verified_double/method_signature/recording_double_class_value_spec.rb
194
+ - spec/verified_double/method_signature/rspec_double_spec.rb
195
+ - spec/verified_double/method_signature/value_spec.rb
175
196
  - spec/verified_double/method_signature_spec.rb
176
- - spec/verified_double/method_signature_value_spec.rb
177
197
  - spec/verified_double/method_signatures_report_spec.rb
178
198
  - spec/verified_double/parse_method_signature_spec.rb
199
+ - spec/verified_double/recorded_method_signature_spec.rb
179
200
  - spec/verified_double/recording_double_spec.rb
201
+ - spec/verified_double/stack_frame_spec.rb
180
202
  - spec/verified_double_spec.rb
181
203
  has_rdoc:
@@ -1,45 +0,0 @@
1
- module VerifiedDouble
2
- class MethodSignatureValue
3
- attr_reader :value
4
-
5
- def initialize(value)
6
- @value = value
7
- end
8
-
9
- def accepts?(other)
10
- if self.value.is_a?(Class) || ! other.value.is_a?(Class)
11
- self.value == other.value
12
- else
13
- self.modified_class.ancestors.include?(other.value)
14
- end
15
- end
16
-
17
- def as_instance
18
- if self.value.is_a?(Class)
19
- begin
20
- value.new
21
- rescue NoMethodError
22
- Object.new
23
- end
24
- else
25
- self.value
26
- end
27
- end
28
-
29
- def modified_class
30
- if value.is_a?(RSpec::Mocks::Mock)
31
- value.instance_variable_get('@name').constantize
32
- elsif value.is_a?(VerifiedDouble::RecordingDouble)
33
- value.class_name.constantize
34
- elsif value == true or value == false
35
- VerifiedDouble::Boolean
36
- else
37
- value.class
38
- end
39
- end
40
-
41
- def recommended_value
42
- self.class.new(self.modified_class)
43
- end
44
- end
45
- end