mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Add mock inject macro
Debugging some type resolution issues.
This commit is contained in:
parent
95282e7510
commit
9817b2948e
2 changed files with 124 additions and 37 deletions
|
@ -1,8 +1,6 @@
|
||||||
require "../../spec_helper"
|
require "../../spec_helper"
|
||||||
|
|
||||||
Spectator.describe Spectator::Mock do
|
class Thing2
|
||||||
describe "#define_subclass" do
|
|
||||||
class Thing
|
|
||||||
def method1
|
def method1
|
||||||
42
|
42
|
||||||
end
|
end
|
||||||
|
@ -14,19 +12,66 @@ Spectator.describe Spectator::Mock do
|
||||||
def method3
|
def method3
|
||||||
"original"
|
"original"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Spectator::Mock.define_subclass(Thing, MockThing, :mock_name, method1: 123) do
|
Spectator.describe Spectator::Mock do
|
||||||
|
# describe "#define_subclass" do
|
||||||
|
# class Thing
|
||||||
|
# def method1
|
||||||
|
# 42
|
||||||
|
# end
|
||||||
|
|
||||||
|
# def method2
|
||||||
|
# :original
|
||||||
|
# end
|
||||||
|
|
||||||
|
# def method3
|
||||||
|
# "original"
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
|
||||||
|
# Spectator::Mock.define_subclass(Thing, MockThing, :mock_name, method1: 123) do
|
||||||
|
# stub def method2
|
||||||
|
# :stubbed
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
|
||||||
|
# let(thing) { MockThing.new }
|
||||||
|
|
||||||
|
# it "defines a subclass of the mocked type" do
|
||||||
|
# expect(MockThing).to be_lt(Thing)
|
||||||
|
# end
|
||||||
|
|
||||||
|
# it "overrides responses from methods with keyword arguments" do
|
||||||
|
# expect(thing.method1).to eq(123)
|
||||||
|
# end
|
||||||
|
|
||||||
|
# it "overrides responses from methods defined in the block" do
|
||||||
|
# expect(thing.method2).to eq(:stubbed)
|
||||||
|
# end
|
||||||
|
|
||||||
|
# it "allows methods to be stubbed" do
|
||||||
|
# stub1 = Spectator::ValueStub.new(:method1, 777)
|
||||||
|
# stub2 = Spectator::ValueStub.new(:method2, :override)
|
||||||
|
# stub3 = Spectator::ValueStub.new(:method3, "stubbed")
|
||||||
|
|
||||||
|
# aggregate_failures do
|
||||||
|
# expect { thing._spectator_define_stub(stub1) }.to change { thing.method1 }.to(777)
|
||||||
|
# expect { thing._spectator_define_stub(stub2) }.to change { thing.method2 }.to(:override)
|
||||||
|
# expect { thing._spectator_define_stub(stub3) }.to change { thing.method3 }.from("original").to("stubbed")
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
|
||||||
|
describe "#inject" do
|
||||||
|
context "with a class" do
|
||||||
|
Spectator::Mock.inject(Thing2, :mock_name, method1: 123) do
|
||||||
stub def method2
|
stub def method2
|
||||||
:stubbed
|
:stubbed
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
let(thing) { MockThing.new }
|
let(thing) { Thing2.new }
|
||||||
|
|
||||||
it "defines a subclass of the mocked type" do
|
|
||||||
expect(MockThing).to be_lt(Thing)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "overrides responses from methods with keyword arguments" do
|
it "overrides responses from methods with keyword arguments" do
|
||||||
expect(thing.method1).to eq(123)
|
expect(thing.method1).to eq(123)
|
||||||
|
@ -48,4 +93,5 @@ Spectator.describe Spectator::Mock do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -35,5 +35,46 @@ module Spectator
|
||||||
end
|
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 %}
|
||||||
|
|
||||||
|
{% begin %}
|
||||||
|
{% if name %}@[::Spectator::StubbedName({{name}})]{% end %}
|
||||||
|
{{base}} ::{{type.name}}
|
||||||
|
include ::Spectator::Mocked
|
||||||
|
|
||||||
|
@@_spectator_stubs = Hash(self, Array(::Spectator::Stub)).new do |hash, key|
|
||||||
|
hash[key] = [] of ::Spectator::Stub
|
||||||
|
end
|
||||||
|
|
||||||
|
private def _spectator_stubs
|
||||||
|
@@_spectator_stubs[self]
|
||||||
|
end
|
||||||
|
|
||||||
|
# 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}} + "\">"
|
||||||
|
\{% else %}
|
||||||
|
"#<Mock {{type.name}}>"
|
||||||
|
\{% end %}
|
||||||
|
end
|
||||||
|
|
||||||
|
macro finished
|
||||||
|
stub_type {{type.name}}
|
||||||
|
|
||||||
|
{% if block %}{{block.body}}{% end %}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
{% end %}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue