Move mock containment to harness

This commit is contained in:
Michael Miller 2019-11-09 23:22:21 -07:00
parent e4aae1f60a
commit 11ea7bf2ce
7 changed files with 22 additions and 23 deletions

View file

@ -1,3 +1,5 @@
require "./mocks/registry"
module Spectator
# Helper class that acts as a gateway between example code and the test framework.
# 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 result returned from `Example#run` will be returned.
def self.run(example : Example) : Result
@@current = new(example)
@@current = harness = new(example)
harness.mocks.prepare(example.group.context)
example.run
ensure
@@current = nil
@ -34,6 +37,8 @@ module Spectator
# Retrieves the current running example.
getter example : Example
getter mocks = Mocks::Registry.new
# Retrieves the group for the current running example.
def group
example.group
@ -51,10 +56,6 @@ module Spectator
@reporter.expectations
end
def double(id)
example.group.double(id, example.sample_values)
end
# Creates a new harness.
# The example the harness is for should be passed in.
private def initialize(@example)

View file

@ -14,7 +14,7 @@ module Spectator::Matchers
end
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)
range.includes?(calls.size)
else
@ -28,7 +28,7 @@ module Spectator::Matchers
end
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
{
expected: "#{range ? "#{humanize_range(range)} time(s)" : "At least once"} with #{@args}",

View file

@ -14,7 +14,7 @@ module Spectator::Matchers
end
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)
range.includes?(calls.size)
else
@ -28,7 +28,7 @@ module Spectator::Matchers
end
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
{
expected: "#{range ? "#{humanize_range(range)} time(s)" : "At least once"} with any arguments",

View file

@ -6,7 +6,7 @@ module Spectator::Mocks
end
def to(stub : MethodStub) : Nil
Registry.add_stub(@mock, stub)
Harness.current.mocks.add_stub(@mock, stub)
end
end
end

View file

@ -36,8 +36,8 @@ module Spectator::Mocks
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Mocks::Registry.record_call(self, %call)
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
::Spectator::Harness.current.mocks.record_call(self, %call)
if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call))
%stub.call!(%args, typeof(%method({{args.splat}})))
else
%method({{args.splat}})
@ -47,8 +47,8 @@ module Spectator::Mocks
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Mocks::Registry.record_call(self, %call)
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
::Spectator::Harness.current.mocks.record_call(self, %call)
if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call))
%stub.call!(%args, typeof(%method({{args.splat}}) { |*%ya| yield *%ya }))
else
%method({{args.splat}}) do |*%yield_args|

View file

@ -36,8 +36,8 @@ module Spectator::Mocks
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Mocks::Registry.record_call(self, %call)
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
::Spectator::Harness.current.mocks.record_call(self, %call)
if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call))
%stub.call!(%args, typeof({{original}}({{args.splat}})))
else
{{original}}({{args.splat}})
@ -47,8 +47,8 @@ module Spectator::Mocks
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Mocks::Registry.record_call(self, %call)
if (%stub = ::Spectator::Mocks::Registry.find_stub(self, %call))
::Spectator::Harness.current.mocks.record_call(self, %call)
if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call))
%stub.call!(%args, typeof({{original}}({{args.splat}}) { |*%ya| yield *%ya }))
else
{{original}}({{args.splat}}) do |*%yield_args|

View file

@ -7,11 +7,9 @@ module Spectator
# Runs the example, hooks, and captures the result
# and translates to a usable result.
def run_impl : Result
Mocks.run(group.context) do
result = capture_result
expectations = Harness.current.expectations
translate_result(result, expectations)
end
result = capture_result
expectations = Harness.current.expectations
translate_result(result, expectations)
end
# Runs all hooks and the example code.