mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Store arguments in method stub
Needed for matching arguments (setting constraints).
This commit is contained in:
parent
20b80cc85a
commit
af9104dfe4
8 changed files with 52 additions and 35 deletions
|
@ -1,26 +1,4 @@
|
||||||
module Spectator::Mocks
|
module Spectator::Mocks
|
||||||
class Arguments(T, NT)
|
abstract class Arguments
|
||||||
protected getter args
|
|
||||||
protected getter opts
|
|
||||||
|
|
||||||
def initialize(@args : T, @opts : NT)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.create(*args, **opts)
|
|
||||||
Arguments.new(args, opts)
|
|
||||||
end
|
|
||||||
|
|
||||||
def pass_to(dispatcher)
|
|
||||||
dispatcher.call(*@args, **@opts)
|
|
||||||
end
|
|
||||||
|
|
||||||
def ===(other : Arguments(U, NU)) : Bool forall U, NU
|
|
||||||
return false unless @args === other.args
|
|
||||||
return false unless @opts.size === other.opts.size
|
|
||||||
|
|
||||||
@opts.keys.all? do |key|
|
|
||||||
other.opts.has_key?(key) && @opts[key] === other.opts[key]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,7 +37,7 @@ module Spectator::Mocks
|
||||||
%}
|
%}
|
||||||
|
|
||||||
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
|
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
|
||||||
%args = ::Spectator::Mocks::Arguments.create({{args.splat}})
|
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
|
||||||
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
|
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
|
||||||
@spectator_stub_calls << %call
|
@spectator_stub_calls << %call
|
||||||
if (%stub = @spectator_stubs.find(&.callable?(%call)))
|
if (%stub = @spectator_stubs.find(&.callable?(%call)))
|
||||||
|
@ -48,7 +48,7 @@ module Spectator::Mocks
|
||||||
end
|
end
|
||||||
|
|
||||||
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
|
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
|
||||||
%args = ::Spectator::Mocks::Arguments.create({{args.splat}})
|
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
|
||||||
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
|
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
|
||||||
@spectator_stub_calls << %call
|
@spectator_stub_calls << %call
|
||||||
if (%stub = @spectator_stubs.find(&.callable?(%call)))
|
if (%stub = @spectator_stubs.find(&.callable?(%call)))
|
||||||
|
|
28
src/spectator/mocks/generic_arguments.cr
Normal file
28
src/spectator/mocks/generic_arguments.cr
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
require "./arguments"
|
||||||
|
|
||||||
|
module Spectator::Mocks
|
||||||
|
class GenericArguments(T, NT) < Arguments
|
||||||
|
protected getter args
|
||||||
|
protected getter opts
|
||||||
|
|
||||||
|
def initialize(@args : T, @opts : NT)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.create(*args, **opts)
|
||||||
|
GenericArguments.new(args, opts)
|
||||||
|
end
|
||||||
|
|
||||||
|
def pass_to(dispatcher)
|
||||||
|
dispatcher.call(*@args, **@opts)
|
||||||
|
end
|
||||||
|
|
||||||
|
def ===(other : Arguments(U, NU)) : Bool forall U, NU
|
||||||
|
return false unless @args === other.args
|
||||||
|
return false unless @opts.size === other.opts.size
|
||||||
|
|
||||||
|
@opts.keys.all? do |key|
|
||||||
|
other.opts.has_key?(key) && @opts[key] === other.opts[key]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,11 +1,11 @@
|
||||||
require "./arguments"
|
require "./generic_arguments"
|
||||||
require "./method_call"
|
require "./method_call"
|
||||||
|
|
||||||
module Spectator::Mocks
|
module Spectator::Mocks
|
||||||
class GenericMethodCall(T, NT) < MethodCall
|
class GenericMethodCall(T, NT) < MethodCall
|
||||||
getter args
|
getter args
|
||||||
|
|
||||||
def initialize(name : Symbol, @args : Arguments(T, NT))
|
def initialize(name : Symbol, @args : GenericArguments(T, NT))
|
||||||
super(name)
|
super(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,18 @@
|
||||||
require "./arguments"
|
require "./arguments"
|
||||||
|
require "./generic_arguments"
|
||||||
|
require "./method_call"
|
||||||
require "./method_stub"
|
require "./method_stub"
|
||||||
|
|
||||||
module Spectator::Mocks
|
module Spectator::Mocks
|
||||||
abstract class GenericMethodStub(ReturnType) < MethodStub
|
abstract class GenericMethodStub(ReturnType) < MethodStub
|
||||||
abstract def call(args : Arguments(T, NT)) : ReturnType forall T, NT
|
def initialize(name, source, @args : Arguments? = nil)
|
||||||
|
super(name, source)
|
||||||
|
end
|
||||||
|
|
||||||
|
def callable?(call : GenericMethodCall(T, NT)) : Bool forall T, NT
|
||||||
|
super && (!@args || @args === call.args)
|
||||||
|
end
|
||||||
|
|
||||||
|
abstract def call(args : GenericArguments(T, NT)) : ReturnType forall T, NT
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
require "../source"
|
require "../source"
|
||||||
|
require "./generic_method_call"
|
||||||
|
|
||||||
module Spectator::Mocks
|
module Spectator::Mocks
|
||||||
abstract class MethodStub
|
abstract class MethodStub
|
||||||
def initialize(@name : Symbol, @source : Source)
|
def initialize(@name : Symbol, @source : Source)
|
||||||
end
|
end
|
||||||
|
|
||||||
def callable?(call : MethodCall) : Bool
|
def callable?(call : GenericMethodCall(T, NT)) : Bool forall T, NT
|
||||||
@name == call.name
|
@name == call.name
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,11 +3,11 @@ require "./generic_method_stub"
|
||||||
|
|
||||||
module Spectator::Mocks
|
module Spectator::Mocks
|
||||||
class ProcMethodStub(ReturnType) < GenericMethodStub(ReturnType)
|
class ProcMethodStub(ReturnType) < GenericMethodStub(ReturnType)
|
||||||
def initialize(name, source, @proc : -> ReturnType)
|
def initialize(name, source, @proc : -> ReturnType, args = nil)
|
||||||
super(name, source)
|
super(name, source, args)
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(_args : Arguments(T, NT)) : ReturnType forall T, NT
|
def call(_args : GenericArguments(T, NT)) : ReturnType forall T, NT
|
||||||
@proc.call
|
@proc.call
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,11 +3,11 @@ require "./generic_method_stub"
|
||||||
|
|
||||||
module Spectator::Mocks
|
module Spectator::Mocks
|
||||||
class ValueMethodStub(ReturnType) < GenericMethodStub(ReturnType)
|
class ValueMethodStub(ReturnType) < GenericMethodStub(ReturnType)
|
||||||
def initialize(name, source, @value : ReturnType)
|
def initialize(name, source, @value : ReturnType, args = nil)
|
||||||
super(name, source)
|
super(name, source, args)
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(_args : Arguments(T, NT)) : ReturnType forall T, NT
|
def call(_args : GenericArguments(T, NT)) : ReturnType forall T, NT
|
||||||
@value
|
@value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue