mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Initial work on have_received matcher
This commit is contained in:
parent
8cbe2edf30
commit
d422376aaf
5 changed files with 60 additions and 10 deletions
|
@ -1,8 +1,10 @@
|
|||
require "./method_stub"
|
||||
require "./generic_method_call"
|
||||
require "./generic_method_stub"
|
||||
|
||||
module Spectator
|
||||
abstract class Double
|
||||
@spectator_stubs = Deque(MethodStub).new
|
||||
@spectator_stub_calls = Deque(MethodCall).new
|
||||
|
||||
private macro stub(definition, &block)
|
||||
{%
|
||||
|
@ -27,7 +29,7 @@ module Spectator
|
|||
|
||||
{% if name.ends_with?('=') && name.id != "[]=" %}
|
||||
def {{name}}(arg)
|
||||
call = ::Spectator::MethodCall.new({{name.symbolize}}, {arg}, NamedTuple.new)
|
||||
call = ::Spectator::GenericMethodCall.new({{name.symbolize}}, {arg}, NamedTuple.new)
|
||||
stub = @spectator_stubs.find(&.callable?(call))
|
||||
if stub
|
||||
stub.as(::Spectator::GenericMethodStub(typeof(%method(arg)))).call(call)
|
||||
|
@ -37,7 +39,7 @@ module Spectator
|
|||
end
|
||||
{% else %}
|
||||
def {{name}}(*args, **options){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
|
||||
call = ::Spectator::MethodCall.new({{name.symbolize}}, args, options)
|
||||
call = ::Spectator::GenericMethodCall.new({{name.symbolize}}, args, options)
|
||||
stub = @spectator_stubs.find(&.callable?(call))
|
||||
if stub
|
||||
stub.as(::Spectator::GenericMethodStub(typeof(%method(*args, **options)))).call(call)
|
||||
|
@ -48,7 +50,7 @@ module Spectator
|
|||
|
||||
{% if name != "[]=" %}
|
||||
def {{name}}(*args, **options){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
|
||||
call = ::Spectator::MethodCall.new({{name.symbolize}}, args, options)
|
||||
call = ::Spectator::GenericMethodCall.new({{name.symbolize}}, args, options)
|
||||
stub = @spectator_stubs.find(&.callable?(call))
|
||||
if stub
|
||||
stub.as(::Spectator::GenericMethodStub(typeof(%method(*args, **options) { |*yield_args| yield *yield_args }))).call(call)
|
||||
|
@ -79,5 +81,9 @@ module Spectator
|
|||
protected def spectator_define_stub(stub : MethodStub) : Nil
|
||||
@spectator_stubs << stub
|
||||
end
|
||||
|
||||
protected def spectator_stub_calls(method : Symbol) : Array(MethodCall)
|
||||
@spectator_stub_calls.select { |call| call.name == method }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -683,6 +683,11 @@ module Spectator
|
|||
expect {{block}}.to raise_error({{type}}, {{message}})
|
||||
end
|
||||
|
||||
macro have_received(method)
|
||||
%test_value = ::Spectator::TestValue.new(({{method.id.symbolize}}), {{method.id.stringify}})
|
||||
::Spectator::Matchers::ReceiveMatcher.new(%test_value)
|
||||
end
|
||||
|
||||
# Used to create predicate matchers.
|
||||
# Any missing method that starts with 'be_' or 'have_' will be handled.
|
||||
# All other method names will be ignored and raise a compile-time error.
|
||||
|
|
13
src/spectator/generic_method_call.cr
Normal file
13
src/spectator/generic_method_call.cr
Normal file
|
@ -0,0 +1,13 @@
|
|||
require "./method_call"
|
||||
|
||||
module Spectator
|
||||
class GenericMethodCall(T, NT) < MethodCall
|
||||
getter args : T
|
||||
|
||||
getter options : NT
|
||||
|
||||
def initialize(name : Symbol, @args : T, @options : NT)
|
||||
super(name)
|
||||
end
|
||||
end
|
||||
end
|
30
src/spectator/matchers/receive_matcher.cr
Normal file
30
src/spectator/matchers/receive_matcher.cr
Normal file
|
@ -0,0 +1,30 @@
|
|||
require "../double"
|
||||
require "./standard_matcher"
|
||||
|
||||
module Spectator::Matchers
|
||||
struct ReceiveMatcher < StandardMatcher
|
||||
def initialize(@expected : TestExpression(Symbol))
|
||||
end
|
||||
|
||||
def description : String
|
||||
"received message #{@expected.label}"
|
||||
end
|
||||
|
||||
def match?(actual : TestExpression(T)) : Bool forall T
|
||||
double = actual.value.as(Double)
|
||||
calls = double.spectator_stub_calls(@expected.value)
|
||||
!calls.empty?
|
||||
end
|
||||
|
||||
def failure_message(actual : TestExpression(T)) : String forall T
|
||||
"#{actual.value} did not receive #{@expected.label}"
|
||||
end
|
||||
|
||||
def values(actual : TestExpression(T)) forall T
|
||||
{
|
||||
expected: "1 time with any arguments",
|
||||
received: "0 times with any arguments"
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,12 +1,8 @@
|
|||
module Spectator
|
||||
class MethodCall(T, NT)
|
||||
abstract class MethodCall
|
||||
getter name : Symbol
|
||||
|
||||
getter args : T
|
||||
|
||||
getter options : NT
|
||||
|
||||
def initialize(@name : Symbol, @args : T, @options : NT)
|
||||
def initialize(@name : Symbol)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue