mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Move mock containment to harness
This commit is contained in:
parent
e4aae1f60a
commit
11ea7bf2ce
7 changed files with 22 additions and 23 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
require "./mocks/registry"
|
||||||
|
|
||||||
module Spectator
|
module Spectator
|
||||||
# Helper class that acts as a gateway between example code and the test framework.
|
# Helper class that acts as a gateway between example code and the test framework.
|
||||||
# Every example must be invoked by passing it to `#run`.
|
# Every example must be invoked by passing it to `#run`.
|
||||||
|
@ -25,7 +27,8 @@ module Spectator
|
||||||
# The *example* argument will be the example to run.
|
# The *example* argument will be the example to run.
|
||||||
# The result returned from `Example#run` will be returned.
|
# The result returned from `Example#run` will be returned.
|
||||||
def self.run(example : Example) : Result
|
def self.run(example : Example) : Result
|
||||||
@@current = new(example)
|
@@current = harness = new(example)
|
||||||
|
harness.mocks.prepare(example.group.context)
|
||||||
example.run
|
example.run
|
||||||
ensure
|
ensure
|
||||||
@@current = nil
|
@@current = nil
|
||||||
|
@ -34,6 +37,8 @@ module Spectator
|
||||||
# Retrieves the current running example.
|
# Retrieves the current running example.
|
||||||
getter example : Example
|
getter example : Example
|
||||||
|
|
||||||
|
getter mocks = Mocks::Registry.new
|
||||||
|
|
||||||
# Retrieves the group for the current running example.
|
# Retrieves the group for the current running example.
|
||||||
def group
|
def group
|
||||||
example.group
|
example.group
|
||||||
|
@ -51,10 +56,6 @@ module Spectator
|
||||||
@reporter.expectations
|
@reporter.expectations
|
||||||
end
|
end
|
||||||
|
|
||||||
def double(id)
|
|
||||||
example.group.double(id, example.sample_values)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Creates a new harness.
|
# Creates a new harness.
|
||||||
# The example the harness is for should be passed in.
|
# The example the harness is for should be passed in.
|
||||||
private def initialize(@example)
|
private def initialize(@example)
|
||||||
|
|
|
@ -14,7 +14,7 @@ module Spectator::Matchers
|
||||||
end
|
end
|
||||||
|
|
||||||
def match?(actual : TestExpression(T)) : Bool forall T
|
def match?(actual : TestExpression(T)) : Bool forall T
|
||||||
calls = Mocks::Registry.calls_for(actual.value, @expected.value).select { |call| @args === call.args }
|
calls = Harness.current.mocks.calls_for(actual.value, @expected.value).select { |call| @args === call.args }
|
||||||
if (range = @range)
|
if (range = @range)
|
||||||
range.includes?(calls.size)
|
range.includes?(calls.size)
|
||||||
else
|
else
|
||||||
|
@ -28,7 +28,7 @@ module Spectator::Matchers
|
||||||
end
|
end
|
||||||
|
|
||||||
def values(actual : TestExpression(T)) forall T
|
def values(actual : TestExpression(T)) forall T
|
||||||
calls = Mocks::Registry.calls_for(actual.value, @expected.value).select { |call| @args === call.args }
|
calls = Harness.current.mocks.calls_for(actual.value, @expected.value).select { |call| @args === call.args }
|
||||||
range = @range
|
range = @range
|
||||||
{
|
{
|
||||||
expected: "#{range ? "#{humanize_range(range)} time(s)" : "At least once"} with #{@args}",
|
expected: "#{range ? "#{humanize_range(range)} time(s)" : "At least once"} with #{@args}",
|
||||||
|
|
|
@ -14,7 +14,7 @@ module Spectator::Matchers
|
||||||
end
|
end
|
||||||
|
|
||||||
def match?(actual : TestExpression(T)) : Bool forall T
|
def match?(actual : TestExpression(T)) : Bool forall T
|
||||||
calls = Mocks::Registry.calls_for(actual.value, @expected.value)
|
calls = Harness.current.mocks.calls_for(actual.value, @expected.value)
|
||||||
if (range = @range)
|
if (range = @range)
|
||||||
range.includes?(calls.size)
|
range.includes?(calls.size)
|
||||||
else
|
else
|
||||||
|
@ -28,7 +28,7 @@ module Spectator::Matchers
|
||||||
end
|
end
|
||||||
|
|
||||||
def values(actual : TestExpression(T)) forall T
|
def values(actual : TestExpression(T)) forall T
|
||||||
calls = Mocks::Registry.calls_for(actual.value, @expected.value)
|
calls = Harness.current.mocks.calls_for(actual.value, @expected.value)
|
||||||
range = @range
|
range = @range
|
||||||
{
|
{
|
||||||
expected: "#{range ? "#{humanize_range(range)} time(s)" : "At least once"} with any arguments",
|
expected: "#{range ? "#{humanize_range(range)} time(s)" : "At least once"} with any arguments",
|
||||||
|
|
|
@ -6,7 +6,7 @@ module Spectator::Mocks
|
||||||
end
|
end
|
||||||
|
|
||||||
def to(stub : MethodStub) : Nil
|
def to(stub : MethodStub) : Nil
|
||||||
Registry.add_stub(@mock, stub)
|
Harness.current.mocks.add_stub(@mock, stub)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -36,8 +36,8 @@ 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::GenericArguments.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::Mocks::Registry.record_call(self, %call)
|
::Spectator::Harness.current.mocks.record_call(self, %call)
|
||||||
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
|
if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call))
|
||||||
%stub.call!(%args, typeof(%method({{args.splat}})))
|
%stub.call!(%args, typeof(%method({{args.splat}})))
|
||||||
else
|
else
|
||||||
%method({{args.splat}})
|
%method({{args.splat}})
|
||||||
|
@ -47,8 +47,8 @@ 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::GenericArguments.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::Mocks::Registry.record_call(self, %call)
|
::Spectator::Harness.current.mocks.record_call(self, %call)
|
||||||
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
|
if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call))
|
||||||
%stub.call!(%args, typeof(%method({{args.splat}}) { |*%ya| yield *%ya }))
|
%stub.call!(%args, typeof(%method({{args.splat}}) { |*%ya| yield *%ya }))
|
||||||
else
|
else
|
||||||
%method({{args.splat}}) do |*%yield_args|
|
%method({{args.splat}}) do |*%yield_args|
|
||||||
|
|
|
@ -36,8 +36,8 @@ 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::GenericArguments.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::Mocks::Registry.record_call(self, %call)
|
::Spectator::Harness.current.mocks.record_call(self, %call)
|
||||||
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
|
if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call))
|
||||||
%stub.call!(%args, typeof({{original}}({{args.splat}})))
|
%stub.call!(%args, typeof({{original}}({{args.splat}})))
|
||||||
else
|
else
|
||||||
{{original}}({{args.splat}})
|
{{original}}({{args.splat}})
|
||||||
|
@ -47,8 +47,8 @@ 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::GenericArguments.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::Mocks::Registry.record_call(self, %call)
|
::Spectator::Harness.current.mocks.record_call(self, %call)
|
||||||
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
|
if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call))
|
||||||
%stub.call!(%args, typeof({{original}}({{args.splat}}) { |*%ya| yield *%ya }))
|
%stub.call!(%args, typeof({{original}}({{args.splat}}) { |*%ya| yield *%ya }))
|
||||||
else
|
else
|
||||||
{{original}}({{args.splat}}) do |*%yield_args|
|
{{original}}({{args.splat}}) do |*%yield_args|
|
||||||
|
|
|
@ -7,11 +7,9 @@ module Spectator
|
||||||
# Runs the example, hooks, and captures the result
|
# Runs the example, hooks, and captures the result
|
||||||
# and translates to a usable result.
|
# and translates to a usable result.
|
||||||
def run_impl : Result
|
def run_impl : Result
|
||||||
Mocks.run(group.context) do
|
result = capture_result
|
||||||
result = capture_result
|
expectations = Harness.current.expectations
|
||||||
expectations = Harness.current.expectations
|
translate_result(result, expectations)
|
||||||
translate_result(result, expectations)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Runs all hooks and the example code.
|
# Runs all hooks and the example code.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue