diff --git a/src/spectator/harness.cr b/src/spectator/harness.cr index ebbf5db..88873e3 100644 --- a/src/spectator/harness.cr +++ b/src/spectator/harness.cr @@ -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) diff --git a/src/spectator/matchers/receive_arguments_matcher.cr b/src/spectator/matchers/receive_arguments_matcher.cr index 0110d53..bc729db 100644 --- a/src/spectator/matchers/receive_arguments_matcher.cr +++ b/src/spectator/matchers/receive_arguments_matcher.cr @@ -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}", diff --git a/src/spectator/matchers/receive_matcher.cr b/src/spectator/matchers/receive_matcher.cr index 489f453..376047a 100644 --- a/src/spectator/matchers/receive_matcher.cr +++ b/src/spectator/matchers/receive_matcher.cr @@ -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", diff --git a/src/spectator/mocks/allow.cr b/src/spectator/mocks/allow.cr index 00ecc99..1479e69 100644 --- a/src/spectator/mocks/allow.cr +++ b/src/spectator/mocks/allow.cr @@ -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 diff --git a/src/spectator/mocks/double.cr b/src/spectator/mocks/double.cr index bceaeff..e714042 100644 --- a/src/spectator/mocks/double.cr +++ b/src/spectator/mocks/double.cr @@ -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| diff --git a/src/spectator/mocks/stubs.cr b/src/spectator/mocks/stubs.cr index f4f633f..ed7e1b1 100644 --- a/src/spectator/mocks/stubs.cr +++ b/src/spectator/mocks/stubs.cr @@ -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| diff --git a/src/spectator/runnable_example.cr b/src/spectator/runnable_example.cr index c5c0b5b..d981689 100644 --- a/src/spectator/runnable_example.cr +++ b/src/spectator/runnable_example.cr @@ -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.