rr 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- :build:
3
2
  :minor: 0
4
- :patch: 0
3
+ :patch: 1
5
4
  :major: 1
5
+ :build:
data/lib/rr.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  dir = File.dirname(__FILE__)
2
2
  require 'forwardable'
3
3
 
4
+ require "#{dir}/rr/class_instance_method_defined"
4
5
  require "#{dir}/rr/blank_slate"
5
6
 
6
7
  require "#{dir}/rr/errors/rr_error"
@@ -30,11 +31,9 @@ require "#{dir}/rr/double_definitions/strategies/implementation/proxy"
30
31
  require "#{dir}/rr/double_definitions/strategies/double_injection/double_injection_strategy"
31
32
  require "#{dir}/rr/double_definitions/strategies/double_injection/instance"
32
33
  require "#{dir}/rr/double_definitions/strategies/double_injection/any_instance_of"
33
- require "#{dir}/rr/double_definitions/strategies/double_injection/new_instance_of"
34
34
  require "#{dir}/rr/adapters/rr_methods"
35
35
  require "#{dir}/rr/double_definitions/double_injections/instance"
36
36
  require "#{dir}/rr/double_definitions/double_injections/any_instance_of"
37
- require "#{dir}/rr/double_definitions/double_injections/new_instance_of"
38
37
  require "#{dir}/rr/double_definitions/double_definition"
39
38
 
40
39
  require "#{dir}/rr/injections/injection"
@@ -47,7 +46,6 @@ require "#{dir}/rr/method_dispatches/method_missing_dispatch"
47
46
  require "#{dir}/rr/hash_with_object_id_key"
48
47
  require "#{dir}/rr/recorded_calls"
49
48
  require "#{dir}/rr/proc_from_block"
50
- require "#{dir}/rr/class_instance_method_defined"
51
49
 
52
50
  require "#{dir}/rr/double_definitions/double_definition_create_blank_slate"
53
51
  require "#{dir}/rr/double_definitions/double_definition_create"
@@ -27,15 +27,12 @@ module RR
27
27
  double_definition_create.strong(subject, method_name, &definition_eval_block)
28
28
  end
29
29
 
30
- def any_instance_of(subject=DoubleDefinitions::DoubleDefinitionCreate::NO_SUBJECT, method_name=nil, &definition_eval_block)
31
- double_definition_create = DoubleDefinitions::DoubleDefinitionCreate.new
32
- double_definition_create.any_instance_of(subject, method_name, &definition_eval_block)
33
- end
34
-
35
30
  def instance_of(subject=DoubleDefinitions::DoubleDefinitionCreate::NO_SUBJECT, method_name=nil, &definition_eval_block)
36
31
  double_definition_create = DoubleDefinitions::DoubleDefinitionCreate.new
37
32
  double_definition_create.instance_of(subject, method_name, &definition_eval_block)
38
33
  end
34
+ alias_method :any_instance_of, :instance_of
35
+ alias_method :all_instances_of, :instance_of
39
36
 
40
37
  # Verifies all the DoubleInjection objects have met their
41
38
  # TimesCalledExpectations.
@@ -1,6 +1,6 @@
1
1
  module RR
2
2
  module ClassInstanceMethodDefined
3
- def self.call(klass, instance_method, include_super=true)
3
+ def class_instance_method_defined(klass, instance_method, include_super=true)
4
4
  klass.instance_methods(include_super).detect {|method_name| method_name.to_sym == instance_method.to_sym} ||
5
5
  klass.protected_instance_methods(include_super).detect {|method_name| method_name.to_sym == instance_method.to_sym} ||
6
6
  klass.private_instance_methods(include_super).detect {|method_name| method_name.to_sym == instance_method.to_sym}
data/lib/rr/double.rb CHANGED
@@ -116,12 +116,14 @@ module RR
116
116
  end
117
117
 
118
118
  def verify_method_signature
119
- raise RR::Errors::SubjectDoesNotImplementMethodError unless definition.subject.respond_to?(double_injection.send(:original_method_alias_name))
119
+ unless double_injection.subject_has_original_method?
120
+ raise RR::Errors::SubjectDoesNotImplementMethodError
121
+ end
120
122
  raise RR::Errors::SubjectHasDifferentArityError unless arity_matches?
121
123
  end
122
124
 
123
125
  def subject_arity
124
- definition.subject.method(double_injection.send(:original_method_alias_name)).arity
126
+ double_injection.original_method.arity
125
127
  end
126
128
 
127
129
  def subject_accepts_only_varargs?
@@ -129,13 +129,11 @@ module RR
129
129
  end
130
130
 
131
131
  # DoubleInjection Strategies
132
- def any_instance_of(subject=NO_SUBJECT, method_name=nil, &definition_eval_block)
133
- self.add_double_injection_strategy(::RR::DoubleDefinitions::Strategies::DoubleInjection::AnyInstanceOf, subject, method_name, &definition_eval_block)
134
- end
135
-
136
132
  def instance_of(subject=NO_SUBJECT, method_name=nil, &definition_eval_block)
137
- self.add_double_injection_strategy(::RR::DoubleDefinitions::Strategies::DoubleInjection::NewInstanceOf, subject, method_name, &definition_eval_block)
133
+ self.add_double_injection_strategy(::RR::DoubleDefinitions::Strategies::DoubleInjection::AnyInstanceOf, subject, method_name, &definition_eval_block)
138
134
  end
135
+ alias_method :any_instance_of, :instance_of
136
+ alias_method :all_instances_of, :instance_of
139
137
  end
140
138
  end
141
139
  end
@@ -20,7 +20,6 @@ module RR
20
20
  if !double_definition_create.no_subject? && !double_definition_create.subject.is_a?(Class)
21
21
  raise ArgumentError, "instance_of only accepts class objects"
22
22
  end
23
- # subject, method_name
24
23
  double_injection = Injections::DoubleInjection.find_or_create(subject, method_name)
25
24
  Double.new(double_injection, definition)
26
25
  end
@@ -33,7 +33,10 @@ module RR
33
33
 
34
34
  def dispatch_method(subject, subject_class, method_name, arguments, block)
35
35
  subject_eigenclass = (class << subject; self; end)
36
- if exists?(subject_class, method_name) && (subject_class == subject_eigenclass || subject_eigenclass.superclass != (class << Class; self; end))
36
+ if (
37
+ exists?(subject_class, method_name) &&
38
+ (subject_class == subject_eigenclass) || !subject.is_a?(Class)
39
+ )
37
40
  find(subject_class, method_name.to_sym).dispatch_method(subject, arguments, block)
38
41
  else
39
42
  new(subject_class, method_name.to_sym).dispatch_original_method(subject, arguments, block)
@@ -83,6 +86,7 @@ module RR
83
86
  end
84
87
  end
85
88
  end)
89
+ include ClassInstanceMethodDefined
86
90
 
87
91
  attr_reader :subject_class, :method_name, :doubles
88
92
 
@@ -117,25 +121,25 @@ module RR
117
121
 
118
122
  def bind_method_that_self_destructs_and_delegates_to_method_missing
119
123
  subject_class_object_id = subject_class.object_id
120
- subject_class.class_eval(<<-METHOD, __FILE__, __LINE__ + 1)
124
+ subject_class.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
121
125
  def #{method_name}(*args, &block)
122
126
  ObjectSpace._id2ref(#{subject_class_object_id}).class_eval do
123
127
  remove_method(:#{method_name})
124
128
  end
125
129
  method_missing(:#{method_name}, *args, &block)
126
130
  end
127
- METHOD
131
+ RUBY
128
132
  self
129
133
  end
130
134
 
131
135
  def bind_method
132
136
  subject_class_object_id = subject_class.object_id
133
- subject_class.class_eval(<<-METHOD, __FILE__, __LINE__ + 1)
137
+ subject_class.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
134
138
  def #{method_name}(*args, &block)
135
139
  arguments = MethodArguments.new(args, block)
136
140
  RR::Injections::DoubleInjection.dispatch_method(self, ObjectSpace._id2ref(#{subject_class_object_id}), :#{method_name}, arguments.arguments, arguments.block)
137
141
  end
138
- METHOD
142
+ RUBY
139
143
  self
140
144
  end
141
145
 
@@ -178,7 +182,7 @@ module RR
178
182
  end
179
183
 
180
184
  def subject_has_original_method_missing?
181
- ClassInstanceMethodDefined.call(subject_class, MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name)
185
+ class_instance_method_defined(subject_class, MethodDispatches::MethodMissingDispatch.original_method_missing_alias_name)
182
186
  end
183
187
 
184
188
  def original_method_alias_name
@@ -8,19 +8,24 @@ module RR
8
8
  end)
9
9
 
10
10
  include Space::Reader
11
+ include ClassInstanceMethodDefined
11
12
 
12
13
  def subject_has_method_defined?(method_name_in_question)
13
- ClassInstanceMethodDefined.call(subject_class, method_name_in_question)
14
+ class_instance_method_defined(subject_class, method_name_in_question)
14
15
  end
15
16
 
16
17
  def subject_has_original_method?
17
18
  subject_has_method_defined?(original_method_alias_name)
18
19
  end
19
20
 
21
+ def original_method
22
+ subject_class.instance_method(original_method_alias_name)
23
+ end
24
+
20
25
  protected
21
26
  def subject_respond_to_method?(subject, method_name)
22
27
  subject_has_method_defined?(method_name) ||
23
- ClassInstanceMethodDefined.call(subject_class, :respond_to?) &&
28
+ class_instance_method_defined(subject_class, :respond_to?) &&
24
29
  subject.respond_to?(method_name)
25
30
  end
26
31
  end
@@ -12,6 +12,7 @@ module RR
12
12
  instances.include?(subject)
13
13
  end
14
14
  end)
15
+ include ClassInstanceMethodDefined
15
16
 
16
17
  attr_reader :subject_class
17
18
  def initialize(subject_class)
@@ -20,8 +21,8 @@ module RR
20
21
  end
21
22
 
22
23
  def bind
23
- unless ClassInstanceMethodDefined.call(subject_class, original_method_alias_name)
24
- unless ClassInstanceMethodDefined.call(subject_class, :method_missing)
24
+ unless class_instance_method_defined(subject_class, original_method_alias_name)
25
+ unless class_instance_method_defined(subject_class, :method_missing)
25
26
  @placeholder_method_defined = true
26
27
  subject_class.class_eval do
27
28
  def method_missing(method_name, *args, &block)
@@ -16,6 +16,7 @@ module RR
16
16
  instances.include?(subject)
17
17
  end
18
18
  end)
19
+ include ClassInstanceMethodDefined
19
20
 
20
21
  attr_reader :subject_class
21
22
  def initialize(subject_class)
@@ -24,8 +25,8 @@ module RR
24
25
  end
25
26
 
26
27
  def bind
27
- unless ClassInstanceMethodDefined.call(subject_class, original_method_alias_name, false)
28
- unless ClassInstanceMethodDefined.call(subject_class, :singleton_method_added, false)
28
+ unless class_instance_method_defined(subject_class, original_method_alias_name, false)
29
+ unless class_instance_method_defined(subject_class, :singleton_method_added, false)
29
30
  @placeholder_method_defined = true
30
31
  subject_class.class_eval do
31
32
  def singleton_method_added(method_name)
@@ -2,13 +2,11 @@ require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
2
2
 
3
3
  describe "all_instances_of" do
4
4
  it "applies to instances instantiated before the Double expection was created" do
5
- pending("Completion of all_instances_of") do
6
- subject_class = Class.new
7
- subject = subject_class.new
8
- all_instances_of(subject_class) do |o|
9
- o.to_s {"Subject is stubbed"}
10
- end
11
- subject.to_s.should == "Subject is stubbed"
5
+ subject_class = Class.new
6
+ subject = subject_class.new
7
+ all_instances_of(subject_class) do |o|
8
+ o.to_s {"Subject is stubbed"}
12
9
  end
10
+ subject.to_s.should == "Subject is stubbed"
13
11
  end
14
12
  end
@@ -3,13 +3,10 @@ require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
3
3
  describe "instance_of" do
4
4
  it "applies to instances instantiated before the Double expection was created" do
5
5
  subject_class = Class.new
6
- existing_subject = subject_class.new
7
- stub.instance_of(subject_class) do |o|
6
+ subject = subject_class.new
7
+ instance_of(subject_class) do |o|
8
8
  o.to_s {"Subject is stubbed"}
9
9
  end
10
- new_subject = subject_class.new
11
-
12
- existing_subject.to_s.should_not == "Subject is stubbed"
13
- new_subject.to_s.should == "Subject is stubbed"
10
+ subject.to_s.should == "Subject is stubbed"
14
11
  end
15
12
  end
@@ -8,7 +8,7 @@ class CoreExampleSuite
8
8
  files.delete_if {|file| file.include?('test_unit/')}
9
9
  puts "Running Rspec Example Suite"
10
10
  files.each do |file|
11
- require file
11
+ require File.expand_path(file)
12
12
  # puts "require '#{file}'"
13
13
  end
14
14
  end
@@ -183,11 +183,11 @@ module RR
183
183
  @klass = Class.new
184
184
  end
185
185
 
186
- context "when passed no arguments" do
187
- it "returns a DoubleDefinitiondouble_definition_create" do
188
- instance_of.instance_of.should be_instance_of(DoubleDefinitionCreate)
189
- end
190
- end
186
+ # context "when passed no arguments" do
187
+ # it "returns a DoubleDefinitiondouble_definition_create" do
188
+ # instance_of.instance_of.should be_instance_of(DoubleDefinitionCreate)
189
+ # end
190
+ # end
191
191
 
192
192
  context "when passed a method_name argument" do
193
193
  it "creates a instance_of Double for method" do
@@ -237,7 +237,7 @@ module RR
237
237
  :baz
238
238
  end
239
239
  mock(subject).foobar(1, 2)
240
-
240
+
241
241
  subject.foobar(1, 2)
242
242
  lambda {subject.foobar(1, 2)}.should raise_error(RR::Errors::TimesCalledError)
243
243
  lambda {RR.verify}.should raise_error(RR::Errors::TimesCalledError)
@@ -7,7 +7,7 @@ class RspecExampleSuite
7
7
  dir = File.dirname(__FILE__)
8
8
  Dir["#{dir}/rr/rspec/**/*_spec.rb"].each do |file|
9
9
  # puts "require '#{file}'"
10
- require file
10
+ require File.expand_path(file)
11
11
  end
12
12
  end
13
13
  end
@@ -11,7 +11,7 @@ class TestUnitTestSuite
11
11
  def require_tests
12
12
  dir = File.dirname(__FILE__)
13
13
  Dir["#{dir}/rr/test_unit/**/*_test.rb"].each do |file|
14
- require file
14
+ require File.expand_path(file)
15
15
  end
16
16
  end
17
17
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rr
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 0
10
- version: 1.0.0
9
+ - 1
10
+ version: 1.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brian Takita
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-23 00:00:00 -07:00
18
+ date: 2010-10-30 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -48,11 +48,9 @@ files:
48
48
  - lib/rr/double_definitions/double_definition_create_blank_slate.rb
49
49
  - lib/rr/double_definitions/double_injections/any_instance_of.rb
50
50
  - lib/rr/double_definitions/double_injections/instance.rb
51
- - lib/rr/double_definitions/double_injections/new_instance_of.rb
52
51
  - lib/rr/double_definitions/strategies/double_injection/any_instance_of.rb
53
52
  - lib/rr/double_definitions/strategies/double_injection/double_injection_strategy.rb
54
53
  - lib/rr/double_definitions/strategies/double_injection/instance.rb
55
- - lib/rr/double_definitions/strategies/double_injection/new_instance_of.rb
56
54
  - lib/rr/double_definitions/strategies/implementation/implementation_strategy.rb
57
55
  - lib/rr/double_definitions/strategies/implementation/proxy.rb
58
56
  - lib/rr/double_definitions/strategies/implementation/reimplementation.rb
@@ -114,10 +112,9 @@ files:
114
112
  - scratch.rb
115
113
  - spec/api/any_instance_of/all_instances_of_spec.rb
116
114
  - spec/api/any_instance_of/any_instance_of_spec.rb
115
+ - spec/api/any_instance_of/instance_of_spec.rb
117
116
  - spec/api/dont_allow/dont_allow_after_stub_spec.rb
118
117
  - spec/api/mock/mock_spec.rb
119
- - spec/api/new_instance_of/instance_of_spec.rb
120
- - spec/api/new_instance_of/new_instance_of_spec.rb
121
118
  - spec/api/proxy/proxy_spec.rb
122
119
  - spec/api/spy/spy_spec.rb
123
120
  - spec/api/strong/strong_spec.rb
@@ -1,53 +0,0 @@
1
- module RR
2
- module DoubleDefinitions
3
- module DoubleInjections
4
- class NewInstanceOf
5
- extend(Module.new do
6
- include RR::Adapters::RRMethods
7
- def call(subject, stubbed_methods={})
8
- double_definition_create = DoubleDefinitionCreate.new.stub
9
- stub.proxy(subject).allocate do |instance|
10
- add_stubbed_methods(instance, stubbed_methods)
11
- add_method_chain_definition(instance, double_definition_create)
12
- yield(instance) if block_given?
13
- instance
14
- end
15
- stub(subject).new do |*args|
16
- instance = subject.allocate
17
- initialize_subject_instance(instance, args)
18
- end
19
- DoubleDefinitionCreateBlankSlate.new(double_definition_create)
20
- end
21
-
22
- protected
23
- def add_stubbed_methods(subject_instance, stubbed_methods)
24
- stubbed_methods.each do |name, value|
25
- value_proc = value.is_a?(Proc) ? value : lambda {value}
26
- stub(subject_instance, name).returns(&value_proc)
27
- end
28
- end
29
-
30
- def add_method_chain_definition(subject_instance, double_definition_create)
31
- implementation_strategy = double_definition_create.implementation_strategy
32
- if implementation_strategy.method_name
33
- stub(subject_instance).method_missing(
34
- implementation_strategy.method_name,
35
- *implementation_strategy.args,
36
- &implementation_strategy.handler
37
- )
38
- end
39
- end
40
-
41
- def initialize_subject_instance(subject_instance, args)
42
- if args.last.is_a?(ProcFromBlock)
43
- subject_instance.__send__(:initialize, *args[0..(args.length-2)], &args.last)
44
- else
45
- subject_instance.__send__(:initialize, *args)
46
- end
47
- subject_instance
48
- end
49
- end)
50
- end
51
- end
52
- end
53
- end
@@ -1,37 +0,0 @@
1
- module RR
2
- module DoubleDefinitions
3
- module Strategies
4
- module DoubleInjection
5
- # This class is Deprecated.
6
- # Calling instance_of will cause all instances of the passed in Class
7
- # to have the Double defined.
8
- #
9
- # The following example mocks all User's valid? method and return false.
10
- # mock.instance_of(User).valid? {false}
11
- #
12
- # The following example mocks and proxies User#projects and returns the
13
- # first 3 projects.
14
- # mock.instance_of(User).projects do |projects|
15
- # projects[0..2]
16
- # end
17
- class NewInstanceOf < DoubleInjectionStrategy
18
- protected
19
- def do_call
20
- if !double_definition_create.no_subject? && !double_definition_create.subject.is_a?(Class)
21
- raise ArgumentError, "instance_of only accepts class objects"
22
- end
23
- DoubleDefinitions::DoubleInjections::NewInstanceOf.call(subject) do |subject|
24
- add_double_to_instance(subject)
25
- end
26
- end
27
-
28
- def add_double_to_instance(instance)
29
- double_injection = Injections::DoubleInjection.find_or_create((class << instance; self; end), method_name)
30
- Double.new(double_injection, definition)
31
- instance
32
- end
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,61 +0,0 @@
1
- require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
2
-
3
- describe "new_instance_of" do
4
- context "when passed a method chain" do
5
- it "stubs the called method name with the given value" do
6
- subject_class = Class.new
7
- existing_subject = subject_class.new
8
- new_instance_of(subject_class).foobar {:baz}
9
-
10
- subject_new = subject_class.new
11
- existing_subject.should_not respond_to(:foobar)
12
- subject_new.foobar.should == :baz
13
-
14
- subject_allocate = subject_class.allocate
15
- existing_subject.should_not respond_to(:foobar)
16
- subject_allocate.foobar.should == :baz
17
- end
18
- end
19
-
20
- context "when passed a block" do
21
- it "applies to instances instantiated before the Double expection was created" do
22
- subject_class = Class.new
23
- existing_subject = subject_class.new
24
- class_called = false
25
- new_instance_of(subject_class) do |o|
26
- stub(o).to_s {"Subject is stubbed"}
27
- stub.proxy(o).class {|klass| class_called = true; klass}
28
- end
29
-
30
- existing_subject.to_s.should_not == "Subject is stubbed"
31
-
32
- subject_new = subject_class.new
33
- subject_new.to_s.should == "Subject is stubbed"
34
- subject_new.class.should == subject_class
35
- class_called.should be_true
36
-
37
- subject_allocate = subject_class.allocate
38
- subject_allocate.to_s.should == "Subject is stubbed"
39
- subject_allocate.class.should == subject_class
40
- end
41
- end
42
-
43
- context "when passed a Hash" do
44
- it "stubs methods (key) with the value on instances instantiated before the Double expection was created" do
45
- subject_class = Class.new
46
- existing_subject = subject_class.new
47
- new_instance_of(subject_class, :to_s => "Subject is stubbed", :foobar => lambda {:baz})
48
-
49
- existing_subject.to_s.should_not == "Subject is stubbed"
50
- existing_subject.should_not respond_to(:foobar)
51
-
52
- subject_new = subject_class.new
53
- subject_new.to_s.should == "Subject is stubbed"
54
- subject_new.foobar.should == :baz
55
-
56
- subject_allocate = subject_class.allocate
57
- subject_allocate.to_s.should == "Subject is stubbed"
58
- subject_allocate.foobar.should == :baz
59
- end
60
- end
61
- end