From 2dee50f19eea00fbe2cad6bbc98e353fa70183d0 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 20 Nov 2019 15:43:45 -0700 Subject: [PATCH] Pass original (and type constraint) to stub via block --- src/spectator/mocks/anonymous_double.cr | 4 +++- src/spectator/mocks/anonymous_null_double.cr | 2 +- src/spectator/mocks/double.cr | 4 ++-- src/spectator/mocks/method_stub.cr | 2 +- src/spectator/mocks/stubs.cr | 4 ++-- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/spectator/mocks/anonymous_double.cr b/src/spectator/mocks/anonymous_double.cr index d2e67c9..1fd9a47 100644 --- a/src/spectator/mocks/anonymous_double.cr +++ b/src/spectator/mocks/anonymous_double.cr @@ -12,7 +12,9 @@ module Spectator::Mocks call = ::Spectator::Mocks::GenericMethodCall.new({{call.name.symbolize}}, args) ::Spectator::Harness.current.mocks.record_call(self, call) if (stub = ::Spectator::Harness.current.mocks.find_stub(self, call)) - stub.call!(args, typeof(@values.fetch({{call.name.symbolize}}) { raise })) + stub.call!(args) do + @values.fetch({{call.name.symbolize}}) { raise "Consistency error - method stubbed with no implementation" } + end else @values.fetch({{call.name.symbolize}}) do return nil if ::Spectator::Harness.current.mocks.expected?(self, {{call.name.symbolize}}) diff --git a/src/spectator/mocks/anonymous_null_double.cr b/src/spectator/mocks/anonymous_null_double.cr index 3da8122..8fa338c 100644 --- a/src/spectator/mocks/anonymous_null_double.cr +++ b/src/spectator/mocks/anonymous_null_double.cr @@ -8,7 +8,7 @@ module Spectator::Mocks call = ::Spectator::Mocks::GenericMethodCall.new({{call.name.symbolize}}, args) ::Spectator::Harness.current.mocks.record_call(self, call) if (stub = ::Spectator::Harness.current.mocks.find_stub(self, call)) - stub.call!(args, typeof(@values.fetch({{call.name.symbolize}}) { self })) + stub.call!(args) { @values.fetch({{call.name.symbolize}}) { self } } else @values.fetch({{call.name.symbolize}}) { self } end diff --git a/src/spectator/mocks/double.cr b/src/spectator/mocks/double.cr index f6e3c67..ce83f53 100644 --- a/src/spectator/mocks/double.cr +++ b/src/spectator/mocks/double.cr @@ -39,7 +39,7 @@ module Spectator::Mocks %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args) ::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}}))) + %stub.call!(%args) { %method({{args.splat}}) } else %method({{args.splat}}) end @@ -50,7 +50,7 @@ module Spectator::Mocks %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args) ::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 })) + %stub.call!(%args) { %method({{args.splat}}) { |*%ya| yield *%ya } } else %method({{args.splat}}) do |*%yield_args| yield *%yield_args diff --git a/src/spectator/mocks/method_stub.cr b/src/spectator/mocks/method_stub.cr index 12b9999..4ee3a6e 100644 --- a/src/spectator/mocks/method_stub.cr +++ b/src/spectator/mocks/method_stub.cr @@ -16,7 +16,7 @@ module Spectator::Mocks abstract def call(args : GenericArguments(T, NT)) forall T, NT - def call!(args : GenericArguments(T, NT), _rt : RT.class) : RT forall T, NT, RT + def call!(args : GenericArguments(T, NT), &original : -> RT) : RT forall T, NT, RT value = call(args) if value.is_a?(RT) value.as(RT) diff --git a/src/spectator/mocks/stubs.cr b/src/spectator/mocks/stubs.cr index 2c2cf1f..089e637 100644 --- a/src/spectator/mocks/stubs.cr +++ b/src/spectator/mocks/stubs.cr @@ -56,7 +56,7 @@ module Spectator::Mocks %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args) %harness.mocks.record_call(self, %call) if (%stub = %harness.mocks.find_stub(self, %call)) - return %stub.call!(%args, typeof({{original}}({{args.splat}}))) + return %stub.call!(%args) { {{original}}({{args.splat}}) } end end {{original}}({{args.splat}}) @@ -68,7 +68,7 @@ module Spectator::Mocks %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args) %harness.mocks.record_call(self, %call) if (%stub = %harness.mocks.find_stub(self, %call)) - return %stub.call!(%args, typeof({{original}}({{args.splat}}) { |*%ya| yield *%ya })) + return %stub.call!(%args) { {{original}}({{args.splat}}) { |*%ya| yield *%ya } } end end {{original}}({{args.splat}}) do |*%yield_args|