From c3cba6962c2f9ee5665d814726f15e0244d0884d Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 11 Nov 2019 23:30:30 -0700 Subject: [PATCH] Get stubs on class methods (self.) working --- src/spectator/mocks/stubs.cr | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/spectator/mocks/stubs.cr b/src/spectator/mocks/stubs.cr index a1c1423..2c2cf1f 100644 --- a/src/spectator/mocks/stubs.cr +++ b/src/spectator/mocks/stubs.cr @@ -2,11 +2,13 @@ module Spectator::Mocks module Stubs private macro stub(definition, _file = __FILE__, _line = __LINE__, &block) {% + receiver = nil name = nil params = nil args = nil body = nil if definition.is_a?(Call) # stub foo { :bar } + receiver = definition.receiver.id named = false name = definition.name.id params = definition.args @@ -31,6 +33,12 @@ module Spectator::Mocks else :super end.id + receiver = if receiver == :self.id + original = :previous_def.id + "self." + else + "" + end.id %} {% if body && !body.is_a?(Nop) %} @@ -42,7 +50,7 @@ module Spectator::Mocks ::Spectator::SpecBuilder.add_default_stub({{@type.name}}, %ds) {% end %} - def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %} + def {{receiver}}{{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %} if (%harness = ::Spectator::Harness.current?) %args = ::Spectator::Mocks::GenericArguments.create({{args.splat}}) %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args) @@ -54,7 +62,7 @@ module Spectator::Mocks {{original}}({{args.splat}}) end - def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %} + def {{receiver}}{{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %} if (%harness = ::Spectator::Harness.current?) %args = ::Spectator::Mocks::GenericArguments.create({{args.splat}}) %call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)