Use absolute names of types in mocked type methods

Prevent possibly type name collisions.
This could happen if, for instance, Array or String was redefined in the scope of the mocked type.
This commit is contained in:
Michael Miller 2022-12-17 20:37:27 -07:00
parent 149c0e6e4b
commit c3e7edc700
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF

View file

@ -50,22 +50,22 @@ module Spectator
end end
{% end %} {% end %}
def _spectator_remove_stub(stub : ::Spectator::Stub) : Nil def _spectator_remove_stub(stub : ::Spectator::Stub) : ::Nil
@_spectator_stubs.try &.delete(stub) @_spectator_stubs.try &.delete(stub)
end end
def _spectator_clear_stubs : Nil def _spectator_clear_stubs : ::Nil
@_spectator_stubs = nil @_spectator_stubs = nil
end end
private class_getter _spectator_stubs : Array(::Spectator::Stub) = [] of ::Spectator::Stub private class_getter _spectator_stubs : ::Array(::Spectator::Stub) = [] of ::Spectator::Stub
class_getter _spectator_calls : Array(::Spectator::MethodCall) = [] of ::Spectator::MethodCall class_getter _spectator_calls : ::Array(::Spectator::MethodCall) = [] of ::Spectator::MethodCall
getter _spectator_calls = [] of ::Spectator::MethodCall getter _spectator_calls = [] of ::Spectator::MethodCall
# Returns the mock's name formatted for user output. # Returns the mock's name formatted for user output.
private def _spectator_stubbed_name : String private def _spectator_stubbed_name : ::String
\{% if anno = @type.annotation(::Spectator::StubbedName) %} \{% if anno = @type.annotation(::Spectator::StubbedName) %}
"#<Mock {{mocked_type.id}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">" "#<Mock {{mocked_type.id}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">"
\{% else %} \{% else %}
@ -73,7 +73,7 @@ module Spectator
\{% end %} \{% end %}
end end
private def self._spectator_stubbed_name : String private def self._spectator_stubbed_name : ::String
\{% if anno = @type.annotation(::Spectator::StubbedName) %} \{% if anno = @type.annotation(::Spectator::StubbedName) %}
"#<Class Mock {{mocked_type.id}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">" "#<Class Mock {{mocked_type.id}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">"
\{% else %} \{% else %}
@ -132,9 +132,9 @@ module Spectator
{% raise "Unsupported base type #{base} for injecting mock" %} {% raise "Unsupported base type #{base} for injecting mock" %}
{% end %} {% end %}
private class_getter _spectator_stubs : Array(::Spectator::Stub) = [] of ::Spectator::Stub private class_getter _spectator_stubs : ::Array(::Spectator::Stub) = [] of ::Spectator::Stub
class_getter _spectator_calls : Array(::Spectator::MethodCall) = [] of ::Spectator::MethodCall class_getter _spectator_calls : ::Array(::Spectator::MethodCall) = [] of ::Spectator::MethodCall
private def _spectator_stubs private def _spectator_stubs
entry = @@_spectator_mock_registry.fetch(self) do entry = @@_spectator_mock_registry.fetch(self) do
@ -143,11 +143,11 @@ module Spectator
entry.stubs entry.stubs
end end
def _spectator_remove_stub(stub : ::Spectator::Stub) : Nil def _spectator_remove_stub(stub : ::Spectator::Stub) : ::Nil
@@_spectator_mock_registry[self]?.try &.stubs.delete(stub) @@_spectator_mock_registry[self]?.try &.stubs.delete(stub)
end end
def _spectator_clear_stubs : Nil def _spectator_clear_stubs : ::Nil
@@_spectator_mock_registry.delete(self) @@_spectator_mock_registry.delete(self)
end end
@ -169,7 +169,7 @@ module Spectator
end end
# Returns the mock's name formatted for user output. # Returns the mock's name formatted for user output.
private def _spectator_stubbed_name : String private def _spectator_stubbed_name : ::String
\{% if anno = @type.annotation(::Spectator::StubbedName) %} \{% if anno = @type.annotation(::Spectator::StubbedName) %}
"#<Mock {{type_name.id}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">" "#<Mock {{type_name.id}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">"
\{% else %} \{% else %}
@ -178,7 +178,7 @@ module Spectator
end end
# Returns the mock's name formatted for user output. # Returns the mock's name formatted for user output.
private def self._spectator_stubbed_name : String private def self._spectator_stubbed_name : ::String
\{% if anno = @type.annotation(::Spectator::StubbedName) %} \{% if anno = @type.annotation(::Spectator::StubbedName) %}
"#<Class Mock {{type_name.id}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">" "#<Class Mock {{type_name.id}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">"
\{% else %} \{% else %}