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

@ -1,43 +1,11 @@
require "../../spec_helper"
class MockedClass
getter method1 = 42
def method2
:original
end
def method3
"original"
end
def instance_variables
[{{@type.instance_vars.map(&.name.symbolize).splat}}]
end
end
struct MockedStruct
getter method1 = 42
def method2
:original
end
def method3
"original"
end
def instance_variables
[{{@type.instance_vars.map(&.name.symbolize).splat}}]
end
end
Spectator.describe Spectator::Mock do
let(stub1) { Spectator::ValueStub.new(:method1, 777) }
let(stub2) { Spectator::ValueStub.new(:method2, :override) }
let(stub3) { Spectator::ValueStub.new(:method3, "stubbed") }
describe "#define_subclass" do
describe "#define_subtype" do
class Thing
def method1
42
@ -52,7 +20,7 @@ Spectator.describe Spectator::Mock do
end
end
Spectator::Mock.define_subclass(Thing, MockThing, :mock_name, method1: 123) do
Spectator::Mock.define_subtype(:class, Thing, MockThing, :mock_name, method1: 123) do
stub def method2
:stubbed
end
@ -102,11 +70,24 @@ Spectator.describe Spectator::Mock do
end
describe "#inject" do
# For some reason, `inject` can't find the types.
# Their definitions are outside of the spec as a workaround.
context "with a class" do
Spectator::Mock.inject(MockedClass, :mock_name, method1: 123) do
class MockedClass
getter method1 = 42
def method2
:original
end
def method3
"original"
end
def instance_variables
[{{@type.instance_vars.map(&.name.symbolize).splat}}]
end
end
Spectator::Mock.inject(:class, MockedClass, :mock_name, method1: 123) do
stub def method2
:stubbed
end
@ -163,7 +144,23 @@ Spectator.describe Spectator::Mock do
end
context "with a struct" do
Spectator::Mock.inject(MockedStruct, :mock_name, method1: 123) do
struct MockedStruct
getter method1 = 42
def method2
:original
end
def method3
"original"
end
def instance_variables
[{{@type.instance_vars.map(&.name.symbolize).splat}}]
end
end
Spectator::Mock.inject(:struct, MockedStruct, :mock_name, method1: 123) do
stub def method2
:stubbed
end

View file

@ -8,9 +8,10 @@ require "./value_stub"
module Spectator
module Mock
macro define_subclass(mocked_type, type_name, name = nil, **value_methods, &block)
macro define_subtype(base, mocked_type, type_name, name = nil, **value_methods, &block)
{% begin %}
{% if name %}@[::Spectator::StubbedName({{name}})]{% end %}
class {{type_name.id}} < {{mocked_type.id}}
{{base.id}} {{type_name.id}} < {{mocked_type.id}}
include ::Spectator::Mocked
{% begin %}
@ -42,29 +43,21 @@ module Spectator
{% if block %}{{block.body}}{% end %}
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 inject(base, type_name, name = nil, **value_methods, &block)
{% begin %}
{% if name %}@[::Spectator::StubbedName({{name}})]{% end %}
{{base}} ::{{type.name}}
{{base.id}} {{type_name.id}}
include ::Spectator::Mocked
{% if type.class? %}
{% 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