Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify receivers #453

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion lib/mocha/mockery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def unnamed_mock
end

def mock_impersonating(object)
add_mock(Mock.new(self, ImpersonatingName.new(object), ObjectReceiver.new(object)))
add_mock(Mock.new(self, ImpersonatingInstanceName.new(object), InstanceReceiver.new(object)))
end

def mock_impersonating_any_instance_of(klass)
Expand Down
2 changes: 1 addition & 1 deletion lib/mocha/names.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Mocha
class ImpersonatingName
class ImpersonatingInstanceName
def initialize(object)
@object = object
end
Expand Down
23 changes: 9 additions & 14 deletions lib/mocha/receivers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Mocha
class ObjectReceiver
class StubbedReceiver
def initialize(object)
@object = object
end
Expand All @@ -8,28 +8,23 @@ def mocks
object = @object
mocks = []
while object
mocha = object.mocha(false)
mocha = stubbee(object).mocha(false)
mocks << mocha if mocha
object = object.is_a?(Class) ? object.superclass : nil
end
mocks
end
end

class AnyInstanceReceiver
def initialize(klass)
@klass = klass
class InstanceReceiver < StubbedReceiver
def stubbee(object)
object
end
end

def mocks
klass = @klass
mocks = []
while klass
mocha = klass.any_instance.mocha(false)
mocks << mocha if mocha
klass = klass.superclass
end
mocks
class AnyInstanceReceiver < StubbedReceiver
def stubbee(klass)
klass.any_instance
end
end

Expand Down
12 changes: 11 additions & 1 deletion test/acceptance/mocha_example_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
require File.expand_path('../../test_helper', __FILE__)
require File.expand_path('../acceptance_test_helper', __FILE__)

class MochaExampleTest < Mocha::TestCase
include AcceptanceTest

def setup
setup_acceptance_test
end

def teardown
teardown_acceptance_test
end

class Rover
def initialize(left_track, right_track, steps_per_metre, steps_per_degree)
@left_track = left_track
Expand Down
12 changes: 11 additions & 1 deletion test/acceptance/stubba_example_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path('../../test_helper', __FILE__)
require File.expand_path('../acceptance_test_helper', __FILE__)

class Widget
def model
Expand All @@ -23,6 +23,16 @@ def self.wotsit
end

class StubbaExampleTest < Mocha::TestCase
include AcceptanceTest

def setup
setup_acceptance_test
end

def teardown
teardown_acceptance_test
end

def test_should_stub_instance_method
widget = Widget.new
widget.expects(:model).returns('different_model')
Expand Down
2 changes: 1 addition & 1 deletion test/unit/object_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_should_build_mocha_referring_to_self
mocha = @object.mocha
assert_not_nil mocha
assert mocha.is_a?(Mocha::Mock)
expected_name = Mocha::ImpersonatingName.new(@object).mocha_inspect
expected_name = Mocha::ImpersonatingInstanceName.new(@object).mocha_inspect
assert_equal expected_name, mocha.mocha_inspect
end

Expand Down
72 changes: 33 additions & 39 deletions test/unit/receivers_test.rb
Original file line number Diff line number Diff line change
@@ -1,85 +1,79 @@
require File.expand_path('../../test_helper', __FILE__)
require 'mocha/receivers'
module ReceiversTest
class FakeClass
class AnyInstance
def initialize(mocha)
@mocha = mocha
end

class ObjectReceiverTest < Mocha::TestCase
include Mocha
def mocha(_instantiate)
@mocha
end
end

class FakeObject
def initialize(mocha)
attr_reader :superclass

def initialize(superclass, mocha)
@superclass = superclass
@mocha = mocha
end

def mocha(_instantiate)
@mocha
end

def any_instance
AnyInstance.new(@mocha)
end

def is_a?(_klass)
false
true
end
end
end

class FakeClass
attr_reader :superclass
class InstanceReceiverTest < Mocha::TestCase
include Mocha
include ReceiversTest

def initialize(superclass, mocha)
@superclass = superclass
class FakeObject
def initialize(mocha)
@mocha = mocha
end

def mocha(_instantiate)
@mocha
end

def is_a?(klass)
klass == Class
def is_a?(_klass)
false
end
end

def test_mocks_returns_mock_for_object
object = FakeObject.new(:mocha)
receiver = ObjectReceiver.new(object)
receiver = InstanceReceiver.new(object)
assert_equal [:mocha], receiver.mocks
end

def test_mocks_returns_mocks_for_class_and_its_superclasses
grandparent = FakeClass.new(nil, :grandparent_mocha)
parent = FakeClass.new(grandparent, :parent_mocha)
klass = FakeClass.new(parent, :mocha)
receiver = ObjectReceiver.new(klass)
receiver = InstanceReceiver.new(klass)
assert_equal [:mocha, :parent_mocha, :grandparent_mocha], receiver.mocks
end
end

class AnyInstanceReceiverTest < Mocha::TestCase
include Mocha

class FakeAnyInstanceClass
class AnyInstance
def initialize(mocha)
@mocha = mocha
end

def mocha(_instantiate)
@mocha
end
end

attr_reader :superclass

def initialize(superclass, mocha)
@superclass = superclass
@mocha = mocha
end

def any_instance
AnyInstance.new(@mocha)
end
end
include ReceiversTest

def test_mocks_returns_mocks_for_class_and_its_superclasses
grandparent = FakeAnyInstanceClass.new(nil, :grandparent_mocha)
parent = FakeAnyInstanceClass.new(grandparent, :parent_mocha)
klass = FakeAnyInstanceClass.new(parent, :mocha)
grandparent = FakeClass.new(nil, :grandparent_mocha)
parent = FakeClass.new(grandparent, :parent_mocha)
klass = FakeClass.new(parent, :mocha)
receiver = AnyInstanceReceiver.new(klass)
assert_equal [:mocha, :parent_mocha, :grandparent_mocha], receiver.mocks
end
Expand Down