mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Merge master into release/0.10
This commit is contained in:
parent
8d32984eba
commit
6a01ab3531
8 changed files with 111 additions and 19 deletions
|
@ -11,22 +11,26 @@ module Spectator::DSL
|
|||
type_name = "Double#{safe_name}".id
|
||||
%}
|
||||
|
||||
{% if block.is_a?(Nop) %}
|
||||
create_double({{type_name}}, {{name}}, {{stubs.double_splat}})
|
||||
{% else %}
|
||||
{% if block %}
|
||||
define_double({{type_name}}, {{name}}, {{stubs.double_splat}}) {{block}}
|
||||
{% else %}
|
||||
create_double({{type_name}}, {{name}}, {{stubs.double_splat}})
|
||||
{% end %}
|
||||
{% end %}
|
||||
end
|
||||
|
||||
macro create_double(type_name, name, **stubs)
|
||||
{% type_name.resolve? || raise("Could not find a double labeled #{name}") %}
|
||||
|
||||
{{type_name}}.new.tap do |%double|
|
||||
{% for name, value in stubs %}
|
||||
allow(%double).to receive({{name.id}}).and_return({{value}})
|
||||
{% end %}
|
||||
end
|
||||
{% if type_name.resolve? %}
|
||||
{{type_name}}.new.tap do |%double|
|
||||
{% for name, value in stubs %}
|
||||
allow(%double).to receive({{name.id}}).and_return({{value}})
|
||||
{% end %}
|
||||
end
|
||||
{% elsif @def %}
|
||||
anonymous_double({{name ? name.stringify : "Anonymous"}}, {{stubs.double_splat}})
|
||||
{% else %}
|
||||
{% raise "Block required for double definition" %}
|
||||
{% end %}
|
||||
end
|
||||
|
||||
macro define_double(type_name, name, **stubs, &block)
|
||||
|
@ -156,10 +160,10 @@ module Spectator::DSL
|
|||
|
||||
macro receive(method_name, _source_file = __FILE__, _source_line = __LINE__, &block)
|
||||
%location = ::Spectator::Location.new({{_source_file}}, {{_source_line}})
|
||||
{% if block.is_a?(Nop) %}
|
||||
::Spectator::Mocks::NilMethodStub.new({{method_name.id.symbolize}}, %location)
|
||||
{% else %}
|
||||
{% if block %}
|
||||
::Spectator::Mocks::ProcMethodStub.create({{method_name.id.symbolize}}, %location) { {{block.body}} }
|
||||
{% else %}
|
||||
::Spectator::Mocks::NilMethodStub.new({{method_name.id.symbolize}}, %location)
|
||||
{% end %}
|
||||
end
|
||||
|
||||
|
@ -171,5 +175,9 @@ module Spectator::DSL
|
|||
{% end %}
|
||||
%stubs
|
||||
end
|
||||
|
||||
def no_args
|
||||
::Spectator::Mocks::NoArguments.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
21
src/spectator/mocks/no_arguments.cr
Normal file
21
src/spectator/mocks/no_arguments.cr
Normal file
|
@ -0,0 +1,21 @@
|
|||
require "./arguments"
|
||||
|
||||
module Spectator::Mocks
|
||||
class NoArguments < Arguments
|
||||
def args
|
||||
Tuple.new
|
||||
end
|
||||
|
||||
def opts
|
||||
NamedTuple.new
|
||||
end
|
||||
|
||||
def ===(other : Arguments) : Bool
|
||||
other.args.empty? && other.opts.empty?
|
||||
end
|
||||
|
||||
def ===(other) : Bool
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
|
@ -54,11 +54,15 @@ module Spectator::Mocks
|
|||
end
|
||||
|
||||
def expect(object, stub : MethodStub) : Nil
|
||||
fetch_instance(object).expected.add(stub)
|
||||
entry = fetch_instance(object)
|
||||
entry.expected.add(stub)
|
||||
entry.stubs.unshift(stub)
|
||||
end
|
||||
|
||||
def expect(type : T.class, stub : MethodStub) : Nil forall T
|
||||
fetch_type(type).expected.add(stub)
|
||||
entry = fetch_type(type)
|
||||
entry.expected.add(stub)
|
||||
entry.stubs.unshift(stub)
|
||||
end
|
||||
|
||||
private def fetch_instance(object)
|
||||
|
|
|
@ -59,8 +59,10 @@ module Spectator::Mocks
|
|||
original = if (name == :new.id && receiver == "self.".id) ||
|
||||
(t.superclass && t.superclass.has_method?(name) && !t.overrides?(t.superclass, name))
|
||||
:super
|
||||
else
|
||||
elsif t.has_method?(name)
|
||||
:previous_def
|
||||
else
|
||||
"::#{name}"
|
||||
end.id
|
||||
%}
|
||||
|
||||
|
@ -80,7 +82,11 @@ module Spectator::Mocks
|
|||
%call = ::Spectator::Mocks::MethodCall.new({{name.symbolize}}, %args)
|
||||
%harness.mocks.record_call(self, %call)
|
||||
if (%stub = %harness.mocks.find_stub(self, %call))
|
||||
return %stub.call!(%args) { {{original}} }
|
||||
if typeof({{original}}) == NoReturn
|
||||
return %stub.call!(%args) { nil }
|
||||
else
|
||||
return %stub.call!(%args) { {{original}} }
|
||||
end
|
||||
end
|
||||
|
||||
{% if body && !body.is_a?(Nop) || return_type.is_a?(ArrayLiteral) %}
|
||||
|
@ -99,7 +105,11 @@ module Spectator::Mocks
|
|||
%call = ::Spectator::Mocks::MethodCall.new({{name.symbolize}}, %args)
|
||||
%harness.mocks.record_call(self, %call)
|
||||
if (%stub = %harness.mocks.find_stub(self, %call))
|
||||
return %stub.call!(%args) { {{original}} { |*%ya| yield *%ya } }
|
||||
if typeof({{original}}) == NoReturn
|
||||
return %stub.call!(%args) { nil }
|
||||
else
|
||||
return %stub.call!(%args) { {{original}} { |*%ya| yield *%ya } }
|
||||
end
|
||||
end
|
||||
|
||||
{% if body && !body.is_a?(Nop) || return_type.is_a?(ArrayLiteral) %}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue