surrogate 0.6.5 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### 0.7.0
2
+
3
+ * BREAKING CHANGE: Bodyless methods behave as if they were defined with an empty block (arity of 0, return nil)
4
+
1
5
  ### 0.6.4
2
6
 
3
7
  * Remove explicit reference to RSpec::Matchers so that surrogate/rspec doesn't depend on rspec-expectations
data/lib/surrogate.rb CHANGED
@@ -8,8 +8,6 @@ require 'surrogate/api_comparer'
8
8
  require 'surrogate/invocation'
9
9
 
10
10
  class Surrogate
11
- UnpreparedMethodError = Class.new StandardError
12
-
13
11
  def self.endow(klass, options={}, &block)
14
12
  Endower.endow klass, options, &block
15
13
  klass
@@ -78,11 +78,11 @@ class Surrogate
78
78
 
79
79
  def enable_defining_methods(klass)
80
80
  def klass.define(method_name, options={}, &block)
81
+ block ||= lambda {}
81
82
  @hatchery.define method_name.to_sym, options, &block
82
83
  end
83
84
 
84
85
  def klass.define_reader(*method_names, &block)
85
- block ||= lambda {}
86
86
  method_names.each { |method_name| define method_name, &block }
87
87
  self
88
88
  end
@@ -53,9 +53,7 @@ class Surrogate
53
53
  end
54
54
 
55
55
  def get_default(method_name, invocation)
56
- api_methods[method_name].default instance, invocation do
57
- raise UnpreparedMethodError, "#{method_name} has been invoked without being told how to behave"
58
- end
56
+ api_methods[method_name].default instance, invocation
59
57
  end
60
58
 
61
59
  def must_know(method_name)
@@ -26,13 +26,13 @@ class Surrogate
26
26
  default_proc && errorizer.match!(*args)
27
27
  end
28
28
 
29
- def default(instance, invocation, &no_default)
29
+ def default(instance, invocation)
30
30
  if options.has_key? :default
31
31
  options[:default]
32
32
  elsif default_proc
33
33
  default_proc_as_method_on(instance).call(*invocation.args, &invocation.block)
34
34
  else
35
- no_default.call
35
+ raise "You should never get here. Please open a issue at http://github.com/JoshCheek/surrogate"
36
36
  end
37
37
  end
38
38
 
@@ -1,3 +1,3 @@
1
1
  class Surrogate
2
- VERSION = "0.6.5"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -91,10 +91,6 @@ describe Surrogate do
91
91
  # defaults are used if provided
92
92
  Mock::User.new(1).name.should == 'Josh'
93
93
 
94
- # error is raised if you try to access an attribute that hasn't been set and has no default
95
- expect { Mock::User.new(1).address }.to raise_error Surrogate::UnpreparedMethodError
96
- Mock::User.new(1).will_have_address('123 Fake St.').address.should == '123 Fake St.'
97
-
98
94
  # methods with multiple args
99
95
  user.phone_numbers.should be_empty
100
96
  user.add_phone_number '123', '456-7890'
@@ -183,9 +183,9 @@ describe 'define' do
183
183
  mocked_class.new.other_meth(1).should == 2
184
184
  end
185
185
 
186
- it 'raises an UnpreparedMethodError when it has no default block' do
186
+ it 'returns nil when there is no default block' do
187
187
  mocked_class.define :meth
188
- expect { instance.meth }.to raise_error Surrogate::UnpreparedMethodError, /meth/
188
+ instance.meth.should equal nil
189
189
  end
190
190
 
191
191
  it 'considers ivars of the same name to be its default when it has no suffix' do
@@ -222,11 +222,12 @@ describe 'define' do
222
222
  expect { instance.meth "extra", "args" }.to raise_error ArgumentError, /wrong number of arguments \(2 for 0\)/
223
223
  end
224
224
 
225
- it 'does not raise arity errors, when there is no default block and the value is overridden' do
225
+ it 'treats bastard methods as having no arity' do
226
226
  mocked_class.define :meth
227
227
  mocked = mocked_class.new
228
228
  mocked.instance_variable_set :@meth, "abc"
229
- mocked.meth 1, 2, 3
229
+ expect { mocked.meth 1, 2, 3 }.to raise_error ArgumentError
230
+ expect { mocked.meth }.to_not raise_error
230
231
  end
231
232
 
232
233
  it 'can make #initialize an api method' do
@@ -188,10 +188,14 @@ describe 'substutability matchers' do
188
188
  klass.should substitute_for surrogate, names: false, types: true
189
189
  end
190
190
 
191
- it 'disregards when surrogate has no body for an api method' do
192
- klass = Class.new { def instance_meth(a) end }
191
+ it 'treats a bastard method as a no arity method' do
193
192
  surrogate = Surrogate.endow(Class.new).define :instance_meth
193
+
194
+ klass = Class.new { def instance_meth() end }
194
195
  klass.should substitute_for surrogate, types: true
196
+
197
+ klass = Class.new { def instance_meth(a) end }
198
+ klass.should_not substitute_for surrogate, types: true
195
199
  end
196
200
 
197
201
  it 'disregards when real object has natively implemented methods that cannot be reflected on' do
@@ -286,15 +290,23 @@ describe 'substutability matchers' do
286
290
  klass.should substitute_for surrogate, types: false, names: true
287
291
  end
288
292
 
289
- it 'disregards when surrogate has no body for an api method' do
293
+ it 'treats bastard methods as no arity methods' do
290
294
  # instance
291
- klass = Class.new { def instance_meth(a) end }
292
295
  surrogate = Surrogate.endow(Class.new).define(:instance_meth)
296
+
297
+ klass = Class.new { def instance_meth(a) end }
298
+ klass.should_not substitute_for surrogate, names: true
299
+
300
+ klass = Class.new { def instance_meth() end }
293
301
  klass.should substitute_for surrogate, names: true
294
302
 
295
303
  # class
296
- klass = Class.new { def self.class_meth(a) end }
297
304
  surrogate = Surrogate.endow(Class.new) { define :class_meth }
305
+
306
+ klass = Class.new { def self.class_meth(a) end }
307
+ klass.should_not substitute_for surrogate, names: true
308
+
309
+ klass = Class.new { def self.class_meth() end }
298
310
  klass.should substitute_for surrogate, names: true
299
311
  end
300
312
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: surrogate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.5
4
+ version: 0.7.0
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-12-27 00:00:00.000000000 Z
12
+ date: 2013-01-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70276370003140 !ruby/object:Gem::Requirement
16
+ requirement: &70341864482800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70276370003140
24
+ version_requirements: *70341864482800
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70276370002580 !ruby/object:Gem::Requirement
27
+ requirement: &70341864481800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.4'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70276370002580
35
+ version_requirements: *70341864481800
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: mountain_berry_fields
38
- requirement: &70276370002000 !ruby/object:Gem::Requirement
38
+ requirement: &70341864496820 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.3
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70276370002000
46
+ version_requirements: *70341864496820
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: mountain_berry_fields-rspec
49
- requirement: &70276370001440 !ruby/object:Gem::Requirement
49
+ requirement: &70341864496280 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.0.3
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70276370001440
57
+ version_requirements: *70341864496280
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: mountain_berry_fields-magic_comments
60
- requirement: &70276370000820 !ruby/object:Gem::Requirement
60
+ requirement: &70341864495600 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: 1.0.1
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70276370000820
68
+ version_requirements: *70341864495600
69
69
  description: Framework to aid in handrolling mock/spy objects.
70
70
  email:
71
71
  - josh.cheek@gmail.com