mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
More work on method stubs
This commit is contained in:
parent
0b6465e6bc
commit
c80a28d616
5 changed files with 36 additions and 25 deletions
|
@ -4,16 +4,7 @@ module Spectator
|
||||||
abstract class Double
|
abstract class Double
|
||||||
@stubs = Deque(MethodStub).new
|
@stubs = Deque(MethodStub).new
|
||||||
|
|
||||||
private macro delegate_internal(method, *args)
|
private macro stub(definition, &block)
|
||||||
# Modified version of Object#delegate
|
|
||||||
{% if method.id.ends_with?('=') && method.id != "[]=" %}
|
|
||||||
@internal.{{method.id}} {{args.splat}}
|
|
||||||
{% else %}
|
|
||||||
@internal.{{method.id}}({{args.splat}})
|
|
||||||
{% end %}
|
|
||||||
end
|
|
||||||
|
|
||||||
macro stub(definition, &block)
|
|
||||||
{%
|
{%
|
||||||
name = nil
|
name = nil
|
||||||
params = nil
|
params = nil
|
||||||
|
@ -34,17 +25,22 @@ module Spectator
|
||||||
end
|
end
|
||||||
%}
|
%}
|
||||||
|
|
||||||
def {{name}}({{params.splat}})
|
def {{name}}(*args, **options){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
|
||||||
%stub = @stubs.find(&.callable?({{name.symbolize}}{% unless args.empty? %}, {{args.splat}}{% end %}))
|
call = ::Spectator::MethodCall.new({{name.symbolize}}, args, options)
|
||||||
if %stub
|
stub = @stubs.find(&.callable?(call))
|
||||||
%stub.call({{args.splat}})
|
if stub
|
||||||
|
stub.as(::Spectator::GenericMethodStub(typeof(@internal.{{name}}(*args, **options)))).call(call)
|
||||||
else
|
else
|
||||||
delegate_internal({{name}}{% unless args.empty? %}, {{args.splat}}{% end %})
|
@internal.{{name}}(*args, **options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def {{name}}(*args, **options, &block){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
|
||||||
|
@internal.{{name}}(*args, **options, &block)
|
||||||
|
end
|
||||||
|
|
||||||
private class Internal
|
private class Internal
|
||||||
def {{name}}({{params.splat}})
|
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
|
||||||
{% if body && !body.is_a?(Nop) %}
|
{% if body && !body.is_a?(Nop) %}
|
||||||
{{body.body}}
|
{{body.body}}
|
||||||
{% else %}
|
{% else %}
|
||||||
|
|
|
@ -30,6 +30,6 @@ module Spectator::DSL
|
||||||
|
|
||||||
macro receive(method_name, _source_file = __FILE__, _source_line = __LINE__)
|
macro receive(method_name, _source_file = __FILE__, _source_line = __LINE__)
|
||||||
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
|
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
|
||||||
::Spectator::GenericMethodStub(Nil).new({{method_name.symbolize}}, %source)
|
::Spectator::GenericMethodStub(Nil).new({{method_name.symbolize}}, %source, ->{ nil })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,13 +1,19 @@
|
||||||
|
require "./method_call"
|
||||||
require "./method_stub"
|
require "./method_stub"
|
||||||
require "./source"
|
require "./source"
|
||||||
|
|
||||||
module Spectator
|
module Spectator
|
||||||
class GenericMethodStub(ReturnType, *ArgumentTypes) < MethodStub
|
class GenericMethodStub(ReturnType) < MethodStub
|
||||||
def callable?(name : Symbol, *args) : Bool
|
def initialize(name : Symbol, source : Source, @proc : -> ReturnType)
|
||||||
|
super(name, source)
|
||||||
|
end
|
||||||
|
|
||||||
|
def callable?(call : MethodCall) : Bool
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(*args)
|
def call(call : MethodCall) : ReturnType
|
||||||
|
@proc.call
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
12
src/spectator/method_call.cr
Normal file
12
src/spectator/method_call.cr
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
module Spectator
|
||||||
|
class MethodCall(T, NT)
|
||||||
|
getter name : Symbol
|
||||||
|
|
||||||
|
getter args : T
|
||||||
|
|
||||||
|
getter options : NT
|
||||||
|
|
||||||
|
def initialize(@name : Symbol, @args : T, @options : NT)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -5,11 +5,8 @@ module Spectator
|
||||||
def initialize(@name : Symbol, @source : Source)
|
def initialize(@name : Symbol, @source : Source)
|
||||||
end
|
end
|
||||||
|
|
||||||
def callable?(name : Symbol, *args) : Bool
|
def callable?(call : MethodCall) : Bool
|
||||||
name == @name
|
@name == call.name
|
||||||
end
|
|
||||||
|
|
||||||
def call(*args)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue