mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add ReferenceMockRegistry
This commit is contained in:
parent
0704fd2a48
commit
380d721fad
3 changed files with 57 additions and 4 deletions
23
spec/spectator/mocks/reference_mock_registry_spec.cr
Normal file
23
spec/spectator/mocks/reference_mock_registry_spec.cr
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
require "../../spec_helper"
|
||||||
|
|
||||||
|
Spectator.describe Spectator::ReferenceMockRegistry do
|
||||||
|
subject(registry) { described_class.new }
|
||||||
|
let(stub) { Spectator::ValueStub.new(:test, 42) }
|
||||||
|
|
||||||
|
it "initially has no stubs" do
|
||||||
|
obj = "foobar"
|
||||||
|
expect(registry[obj]).to be_empty
|
||||||
|
end
|
||||||
|
|
||||||
|
it "stores stubs for an object" do
|
||||||
|
obj = "foobar"
|
||||||
|
expect { registry[obj] << stub }.to change { registry[obj] }.from([] of Spectator::Stub).to([stub])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "isolates stubs between different objects" do
|
||||||
|
obj1 = "foo"
|
||||||
|
obj2 = "bar"
|
||||||
|
registry[obj2] << Spectator::ValueStub.new(:obj2, 42)
|
||||||
|
expect { registry[obj1] << stub }.to_not change { registry[obj2] }
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,5 +1,6 @@
|
||||||
require "./method_call"
|
require "./method_call"
|
||||||
require "./mocked"
|
require "./mocked"
|
||||||
|
require "./reference_mock_registry"
|
||||||
require "./stub"
|
require "./stub"
|
||||||
require "./stubbed_name"
|
require "./stubbed_name"
|
||||||
require "./value_stub"
|
require "./value_stub"
|
||||||
|
@ -51,12 +52,14 @@ module Spectator
|
||||||
{{base}} ::{{type.name}}
|
{{base}} ::{{type.name}}
|
||||||
include ::Spectator::Mocked
|
include ::Spectator::Mocked
|
||||||
|
|
||||||
@@_spectator_stubs = Hash(self, Array(::Spectator::Stub)).new do |hash, key|
|
{% if type.class? %}
|
||||||
hash[key] = [] of ::Spectator::Stub
|
@@_spectator_mock_registry = ::Spectator::ReferenceMockRegistry.new
|
||||||
end
|
{% else %}
|
||||||
|
{% raise "Unsupported type for injecting mock" %}
|
||||||
|
{% end %}
|
||||||
|
|
||||||
private def _spectator_stubs
|
private def _spectator_stubs
|
||||||
@@_spectator_stubs[self]
|
@@_spectator_mock_registry[self]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the mock's name formatted for user output.
|
# Returns the mock's name formatted for user output.
|
||||||
|
|
27
src/spectator/mocks/reference_mock_registry.cr
Normal file
27
src/spectator/mocks/reference_mock_registry.cr
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
require "./stub"
|
||||||
|
|
||||||
|
module Spectator
|
||||||
|
# Stores collections of stubs for mocked reference (class) types.
|
||||||
|
#
|
||||||
|
# This type is intended for all mocked reference types that have functionality "injected."
|
||||||
|
# That is, the type itself has mock functionality bolted on.
|
||||||
|
# Adding instance members should be avoided, for instance, it could mess up serialization.
|
||||||
|
# This registry works around that by mapping mocks (via their memory address) to a collection of stubs.
|
||||||
|
# Doing so prevents adding data to the mocked type.
|
||||||
|
class ReferenceMockRegistry
|
||||||
|
@object_stubs : Hash(Void*, Array(Stub))
|
||||||
|
|
||||||
|
# Creates an empty registry.
|
||||||
|
def initialize
|
||||||
|
@object_stubs = Hash(Void*, Array(Stub)).new do |hash, key|
|
||||||
|
hash[key] = [] of Stub
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Retrieves all stubs defined for a mocked object.
|
||||||
|
def [](object : Reference) : Array(Stub)
|
||||||
|
key = Box.box(object)
|
||||||
|
@object_stubs[key]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue