Support double names

This commit is contained in:
Michael Miller 2022-03-03 22:31:40 -07:00
parent 26deea3d20
commit 5644d54470
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2
2 changed files with 26 additions and 6 deletions

View file

@ -1,7 +1,7 @@
require "../../spec_helper" require "../../spec_helper"
Spectator.describe Spectator::Double do Spectator.describe Spectator::Double do
subject(dbl) { Spectator::Double.new(foo: 42, bar: "baz") } subject(dbl) { Spectator::Double.new("foobar", foo: 42, bar: "baz") }
it "responds to defined messages" do it "responds to defined messages" do
aggregate_failures do aggregate_failures do
@ -14,6 +14,18 @@ Spectator.describe Spectator::Double do
expect { dbl.baz }.to raise_error(Spectator::UnexpectedMessage) expect { dbl.baz }.to raise_error(Spectator::UnexpectedMessage)
end end
it "reports the name in errors" do
expect { dbl.baz }.to raise_error(/foobar/)
end
context "without a double name" do
subject(dbl) { Spectator::Double.new(foo: 42) }
it "reports as anonymous" do
expect { dbl.baz }.to raise_error(/anonymous/i)
end
end
context "with common object methods" do context "with common object methods" do
subject(dbl) do subject(dbl) do
Spectator::Double.new( Spectator::Double.new(

View file

@ -2,7 +2,7 @@ require "./unexpected_message"
module Spectator module Spectator
class Double(Messages) class Double(Messages)
def initialize(**@messages : **Messages) def initialize(@name : String? = nil, **@messages : **Messages)
end end
# TODO: Define macro to redefine a type's method. # TODO: Define macro to redefine a type's method.
@ -27,7 +27,7 @@ module Spectator
@messages[{{meth.name.symbolize}}] @messages[{{meth.name.symbolize}}]
{% end %} {% end %}
\{% else %} \{% else %}
raise UnexpectedMessage.new("Received unexpected message {{meth.name}} on double <TODO:NAME> (masking ancestor).") raise UnexpectedMessage.new("Double<#{_name}> received unexpected message :{{meth.name}} (masking ancestor) with (<TODO: ARGS>).")
\{% end %} \{% end %}
end end
{% end %} {% end %}
@ -52,7 +52,7 @@ module Spectator
@messages[{{meth.name.symbolize}}] @messages[{{meth.name.symbolize}}]
{% end %} {% end %}
\{% else %} \{% else %}
raise UnexpectedMessage.new("Received unexpected message {{meth.name}} on double <TODO:NAME> (masking ancestor).") raise UnexpectedMessage.new("Double<#{_name}> received unexpected message :{{meth.name}} (masking ancestor) with (<TODO: ARGS>).")
\{% end %} \{% end %}
end end
{% end %} {% end %}
@ -77,18 +77,26 @@ module Spectator
@messages[{{meth.name.symbolize}}] @messages[{{meth.name.symbolize}}]
{% end %} {% end %}
\{% else %} \{% else %}
raise UnexpectedMessage.new("Received unexpected message {{meth.name}} on double <TODO:NAME> (masking ancestor).") raise UnexpectedMessage.new("Double<#{_name}> received unexpected message :{{meth.name}} (masking ancestor) with (<TODO: ARGS>).")
\{% end %} \{% end %}
end end
{% end %} {% end %}
{% end %} {% end %}
private def _name
if name = @name
"\"#{name}\""
else
"Anonymous"
end
end
end end
macro method_missing(call) macro method_missing(call)
\{% if Messages.keys.includes?({{call.name.symbolize}}.id) %} \{% if Messages.keys.includes?({{call.name.symbolize}}.id) %}
@messages[{{call.name.symbolize}}] @messages[{{call.name.symbolize}}]
\{% else %} \{% else %}
raise UnexpectedMessage.new("Received unexpected message {{call.name}} on double <TODO:NAME>.") raise UnexpectedMessage.new("Double<#{_name}> received unexpected message :{{call.name}} with (<TODO: ARGS>).")
\{% end %} \{% end %}
end end
end end