mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
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:
parent
4f0e2f6e46
commit
ddaed636c4
2 changed files with 82 additions and 92 deletions
|
@ -1,43 +1,11 @@
|
||||||
require "../../spec_helper"
|
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
|
Spectator.describe Spectator::Mock do
|
||||||
let(stub1) { Spectator::ValueStub.new(:method1, 777) }
|
let(stub1) { Spectator::ValueStub.new(:method1, 777) }
|
||||||
let(stub2) { Spectator::ValueStub.new(:method2, :override) }
|
let(stub2) { Spectator::ValueStub.new(:method2, :override) }
|
||||||
let(stub3) { Spectator::ValueStub.new(:method3, "stubbed") }
|
let(stub3) { Spectator::ValueStub.new(:method3, "stubbed") }
|
||||||
|
|
||||||
describe "#define_subclass" do
|
describe "#define_subtype" do
|
||||||
class Thing
|
class Thing
|
||||||
def method1
|
def method1
|
||||||
42
|
42
|
||||||
|
@ -52,7 +20,7 @@ Spectator.describe Spectator::Mock do
|
||||||
end
|
end
|
||||||
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
|
stub def method2
|
||||||
:stubbed
|
:stubbed
|
||||||
end
|
end
|
||||||
|
@ -102,11 +70,24 @@ Spectator.describe Spectator::Mock do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#inject" do
|
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
|
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
|
stub def method2
|
||||||
:stubbed
|
:stubbed
|
||||||
end
|
end
|
||||||
|
@ -163,7 +144,23 @@ Spectator.describe Spectator::Mock do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with a struct" do
|
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
|
stub def method2
|
||||||
:stubbed
|
:stubbed
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,9 +8,10 @@ require "./value_stub"
|
||||||
|
|
||||||
module Spectator
|
module Spectator
|
||||||
module Mock
|
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 %}
|
{% if name %}@[::Spectator::StubbedName({{name}})]{% end %}
|
||||||
class {{type_name.id}} < {{mocked_type.id}}
|
{{base.id}} {{type_name.id}} < {{mocked_type.id}}
|
||||||
include ::Spectator::Mocked
|
include ::Spectator::Mocked
|
||||||
|
|
||||||
{% begin %}
|
{% begin %}
|
||||||
|
@ -42,29 +43,21 @@ module Spectator
|
||||||
{% if block %}{{block.body}}{% end %}
|
{% if block %}{{block.body}}{% end %}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
{% end %}
|
||||||
end
|
end
|
||||||
|
|
||||||
macro inject(type_name, name = nil, **value_methods, &block)
|
macro inject(base, 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 %}
|
|
||||||
|
|
||||||
{% begin %}
|
{% begin %}
|
||||||
{% if name %}@[::Spectator::StubbedName({{name}})]{% end %}
|
{% if name %}@[::Spectator::StubbedName({{name}})]{% end %}
|
||||||
{{base}} ::{{type.name}}
|
{{base.id}} {{type_name.id}}
|
||||||
include ::Spectator::Mocked
|
include ::Spectator::Mocked
|
||||||
|
|
||||||
{% if type.class? %}
|
{% if base == :class %}
|
||||||
@@_spectator_mock_registry = ::Spectator::ReferenceMockRegistry.new
|
@@_spectator_mock_registry = ::Spectator::ReferenceMockRegistry.new
|
||||||
{% elsif type.struct? %}
|
{% elsif base == :struct %}
|
||||||
@@_spectator_mock_registry = ::Spectator::ValueMockRegistry(self).new
|
@@_spectator_mock_registry = ::Spectator::ValueMockRegistry(self).new
|
||||||
{% else %}
|
{% else %}
|
||||||
{% raise "Unsupported type for injecting mock" %}
|
{% raise "Unsupported base type #{base} for injecting mock" %}
|
||||||
{% end %}
|
{% end %}
|
||||||
|
|
||||||
private def _spectator_stubs
|
private def _spectator_stubs
|
||||||
|
@ -86,14 +79,14 @@ module Spectator
|
||||||
# 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}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">"
|
"#<Mock {{type_name.id}} \"" + \{{(anno[0] || :Anonymous.id).stringify}} + "\">"
|
||||||
\{% else %}
|
\{% else %}
|
||||||
"#<Mock {{type.name}}>"
|
"#<Mock {{type_name.id}}>"
|
||||||
\{% end %}
|
\{% end %}
|
||||||
end
|
end
|
||||||
|
|
||||||
macro finished
|
macro finished
|
||||||
stub_type {{type.name}}
|
stub_type {{type_name.id}}
|
||||||
|
|
||||||
{% if block %}{{block.body}}{% end %}
|
{% if block %}{{block.body}}{% end %}
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue