Move spectator stub retrieval to abstract getter

This commit is contained in:
Michael Miller 2022-04-28 23:04:22 -06:00
parent d74bc76687
commit 8d14be6c67
No known key found for this signature in database
GPG Key ID: AC78B32D30CE34A2
2 changed files with 16 additions and 13 deletions

View File

@ -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) %}

View File

@ -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*.