Remove type resolution

The `resolve` macro method operates in the scope of `@type`, not where the macro method was called.
This is why types could not be found within the spec.
Change `define_subclass` to `define_subtype` and accept base type keyword.
This commit is contained in:
Michael Miller 2022-05-18 20:56:04 -06:00
parent 4f0e2f6e46
commit ddaed636c4
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2
2 changed files with 82 additions and 92 deletions

View file

@ -8,63 +8,56 @@ require "./value_stub"
module Spectator
module Mock
macro define_subclass(mocked_type, type_name, name = nil, **value_methods, &block)
{% if name %}@[::Spectator::StubbedName({{name}})]{% end %}
class {{type_name.id}} < {{mocked_type.id}}
include ::Spectator::Mocked
{% begin %}
private getter(_spectator_stubs) do
[
{% for key, value in value_methods %}
::Spectator::ValueStub.new({{key.id.symbolize}}, {{value}}),
{% end %}
] of ::Spectator::Stub
end
{% end %}
def _spectator_clear_stubs : Nil
@_spectator_stubs = nil
end
# Returns the mock's name formatted for user output.
private def _spectator_stubbed_name : String
\{% if anno = @type.annotation(::Spectator::StubbedName) %}
"#<Mock {{mocked_type.id}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">"
\{% else %}
"#<Mock {{mocked_type.id}}>"
\{% end %}
end
macro finished
stub_type {{mocked_type.id}}
{% if block %}{{block.body}}{% end %}
end
end
end
macro inject(type_name, name = nil, **value_methods, &block)
{% type = type_name.resolve
base = if type.class?
:class
elsif type.struct?
:struct
else
raise "Unsupported mockable type - #{type}"
end.id %}
macro define_subtype(base, mocked_type, type_name, name = nil, **value_methods, &block)
{% begin %}
{% if name %}@[::Spectator::StubbedName({{name}})]{% end %}
{{base}} ::{{type.name}}
{{base.id}} {{type_name.id}} < {{mocked_type.id}}
include ::Spectator::Mocked
{% if type.class? %}
{% begin %}
private getter(_spectator_stubs) do
[
{% for key, value in value_methods %}
::Spectator::ValueStub.new({{key.id.symbolize}}, {{value}}),
{% end %}
] of ::Spectator::Stub
end
{% end %}
def _spectator_clear_stubs : Nil
@_spectator_stubs = nil
end
# Returns the mock's name formatted for user output.
private def _spectator_stubbed_name : String
\{% if anno = @type.annotation(::Spectator::StubbedName) %}
"#<Mock {{mocked_type.id}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">"
\{% else %}
"#<Mock {{mocked_type.id}}>"
\{% end %}
end
macro finished
stub_type {{mocked_type.id}}
{% if block %}{{block.body}}{% end %}
end
end
{% end %}
end
macro inject(base, type_name, name = nil, **value_methods, &block)
{% begin %}
{% if name %}@[::Spectator::StubbedName({{name}})]{% end %}
{{base.id}} {{type_name.id}}
include ::Spectator::Mocked
{% if base == :class %}
@@_spectator_mock_registry = ::Spectator::ReferenceMockRegistry.new
{% elsif type.struct? %}
{% elsif base == :struct %}
@@_spectator_mock_registry = ::Spectator::ValueMockRegistry(self).new
{% else %}
{% raise "Unsupported type for injecting mock" %}
{% raise "Unsupported base type #{base} for injecting mock" %}
{% end %}
private def _spectator_stubs
@ -86,14 +79,14 @@ module Spectator
# Returns the mock's name formatted for user output.
private def _spectator_stubbed_name : String
\{% if anno = @type.annotation(::Spectator::StubbedName) %}
"#<Mock {{type.name}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">"
"#<Mock {{type_name.id}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">"
\{% else %}
"#<Mock {{type.name}}>"
"#<Mock {{type_name.id}}>"
\{% end %}
end
macro finished
stub_type {{type.name}}
stub_type {{type_name.id}}
{% if block %}{{block.body}}{% end %}
end