Fix resolution issue when mocked types use custom types

GitLab issue 51 is affected.
https://gitlab.com/arctic-fox/spectator/-/issues/51
Private types cannot be referenced with mocks.
This commit is contained in:
Michael Miller 2022-12-17 20:56:16 -07:00
parent c3e7edc700
commit 4b68b8e3de
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
4 changed files with 133 additions and 35 deletions

View file

@ -218,24 +218,29 @@ module Spectator::DSL
# end
# ```
private macro def_mock(type, name = nil, **value_methods, &block)
{% # Construct a unique type name for the mock by using the number of defined types.
index = ::Spectator::DSL::Mocks::TYPES.size
mock_type_name = "Mock#{index}".id
{% resolved = type.resolve
# Construct a unique type name for the mock by using the number of defined types.
index = ::Spectator::DSL::Mocks::TYPES.size
# The type is nested under the original so that any type names from the original can be resolved.
mock_type_name = "Mock#{index}".id
# Store information about how the mock is defined and its context.
# This is important for constructing an instance of the mock later.
::Spectator::DSL::Mocks::TYPES << {type.id.symbolize, @type.name(generic_args: false).symbolize, mock_type_name.symbolize}
# Store information about how the mock is defined and its context.
# This is important for constructing an instance of the mock later.
::Spectator::DSL::Mocks::TYPES << {type.id.symbolize, @type.name(generic_args: false).symbolize, "::#{resolved.name}::#{mock_type_name}".id.symbolize}
resolved = type.resolve
base = if resolved.class?
:class
elsif resolved.struct?
:struct
else
:module
end %}
base = if resolved.class?
:class
elsif resolved.struct?
:struct
else
:module
end %}
::Spectator::Mock.define_subtype({{base}}, {{type.id}}, {{mock_type_name}}, {{name}}, {{**value_methods}}) {{block}}
{% begin %}
{{base.id}} ::{{resolved.name}}
::Spectator::Mock.define_subtype({{base}}, {{type.id}}, {{mock_type_name}}, {{name}}, {{**value_methods}}) {{block}}
end
{% end %}
end
# Instantiates a mock.