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,50 +1,96 @@
|
|||
require "../../spec_helper"
|
||||
|
||||
class Thing2
|
||||
def method1
|
||||
42
|
||||
end
|
||||
|
||||
def method2
|
||||
:original
|
||||
end
|
||||
|
||||
def method3
|
||||
"original"
|
||||
end
|
||||
end
|
||||
|
||||
Spectator.describe Spectator::Mock do
|
||||
describe "#define_subclass" do
|
||||
class Thing
|
||||
def method1
|
||||
42
|
||||
# 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
|
||||
:stubbed
|
||||
end
|
||||
end
|
||||
|
||||
def method2
|
||||
:original
|
||||
let(thing) { Thing2.new }
|
||||
|
||||
it "overrides responses from methods with keyword arguments" do
|
||||
expect(thing.method1).to eq(123)
|
||||
end
|
||||
|
||||
def method3
|
||||
"original"
|
||||
it "overrides responses from methods defined in the block" do
|
||||
expect(thing.method2).to eq(:stubbed)
|
||||
end
|
||||
end
|
||||
|
||||
Spectator::Mock.define_subclass(Thing, MockThing, :mock_name, method1: 123) do
|
||||
stub def method2
|
||||
:stubbed
|
||||
end
|
||||
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")
|
||||
|
||||
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")
|
||||
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
|
||||
end
|
||||
|
|
|
@ -35,5 +35,46 @@ module Spectator
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue