From 8d14be6c67140e18df6080836350a5e27eead0d3 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 28 Apr 2022 23:04:22 -0600 Subject: [PATCH] Move spectator stub retrieval to abstract getter --- src/spectator/mocks/mock.cr | 14 +------------- src/spectator/mocks/mocked.cr | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/spectator/mocks/mock.cr b/src/spectator/mocks/mock.cr index 14d9c4f..2311eae 100644 --- a/src/spectator/mocks/mock.cr +++ b/src/spectator/mocks/mock.cr @@ -12,25 +12,13 @@ module Spectator include ::Spectator::Mocked {% begin %} - @_spectator_stubs = [ + private getter _spectator_stubs = [ {% for key, value in value_methods %} ::Spectator::ValueStub.new({{key.id.symbolize}}, {{value}}), {% end %} ] of ::Spectator::Stub {% end %} - def _spectator_define_stub(stub : ::Spectator::Stub) : Nil - @_spectator_stubs.unshift(stub) - end - - private def _spectator_find_stub(call : ::Spectator::MethodCall) : ::Spectator::Stub? - @_spectator_stubs.find &.===(call) - end - - private def _spectator_stub_for_method?(method : Symbol) : Bool - @_spectator_stubs.any? { |stub| stub.method == method } - end - # Returns the mock's name formatted for user output. private def _spectator_stubbed_name : String \{% if anno = @type.annotation(::Spectator::StubbedName) %} diff --git a/src/spectator/mocks/mocked.cr b/src/spectator/mocks/mocked.cr index 3826397..c3ea26c 100644 --- a/src/spectator/mocks/mocked.cr +++ b/src/spectator/mocks/mocked.cr @@ -5,6 +5,21 @@ module Spectator module Mocked include Stubbable + # Retrieves an enumerable collection of stubs. + abstract def _spectator_stubs + + def _spectator_define_stub(stub : ::Spectator::Stub) : Nil + _spectator_stubs.unshift(stub) + end + + private def _spectator_find_stub(call : ::Spectator::MethodCall) : ::Spectator::Stub? + _spectator_stubs.find &.===(call) + end + + private def _spectator_stub_for_method?(method : Symbol) : Bool + _spectator_stubs.any? { |stub| stub.method == method } + end + # Method called when a stub isn't found. # # The received message is captured in *call*.