Check if underlying type responds to method

This commit is contained in:
Michael Miller 2019-12-08 15:08:13 -07:00
parent 067c789019
commit 91696d490f
2 changed files with 20 additions and 0 deletions

View file

@ -15,5 +15,11 @@ module Spectator::Mocks
end
list << NilMethodStub.new(method_name, source, args)
end
def exists?(type_name : String, call : GenericMethodCall(T, NT)) : Bool forall T, NT
key = {type_name, call.name}
list = @@entries.fetch(key) { return false }
list.any?(&.callable?(call))
end
end
end

View file

@ -34,6 +34,11 @@ module Spectator::Mocks
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Harness.current.mocks.record_call(self, %call)
unless ::Spectator::Mocks::TypeRegistry.exists?(T.to_s, %call)
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{name}} - #{T} does not respond to #{%call}")
end
if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call))
%stub.call!(%args) { %method({{args.splat}}) }
else
@ -45,6 +50,11 @@ module Spectator::Mocks
%args = ::Spectator::Mocks::GenericArguments.create({{args.splat}})
%call = ::Spectator::Mocks::GenericMethodCall.new({{name.symbolize}}, %args)
::Spectator::Harness.current.mocks.record_call(self, %call)
unless ::Spectator::Mocks::TypeRegistry.exists?(T.to_s, %call)
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{name}} - #{T} does not respond to #{%call}")
end
if (%stub = ::Spectator::Harness.current.mocks.find_stub(self, %call))
%stub.call!(%args) { %method({{args.splat}}) { |*%ya| yield *%ya } }
else
@ -79,6 +89,10 @@ module Spectator::Mocks
call = ::Spectator::Mocks::GenericMethodCall.new({{call.name.symbolize}}, args)
::Spectator::Harness.current.mocks.record_call(self, call)
unless ::Spectator::Mocks::TypeRegistry.exists?(T.to_s, call)
raise ::Spectator::Mocks::UnexpectedMessageError.new("#{self} received unexpected message {{call.name}} - #{T} does not respond to #{call}")
end
return self if @null
return self if ::Spectator::Harness.current.mocks.expected?(self, call)