From a967dce241daf071c6b75cd045d906ece6821ee6 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Tue, 29 Nov 2022 21:24:31 -0700 Subject: [PATCH] Adjust double string representation to_s and inspect (with variants) are no longer "original implementation." --- spec/spectator/mocks/double_spec.cr | 70 ++++++++++++++++++++++-- spec/spectator/mocks/lazy_double_spec.cr | 66 +++++++++++++++++++++- spec/spectator/mocks/null_double_spec.cr | 67 ++++++++++++++++++++++- src/spectator/mocks/double.cr | 30 +++++----- src/spectator/mocks/lazy_double.cr | 6 +- src/spectator/mocks/null_double.cr | 8 +-- 6 files changed, 215 insertions(+), 32 deletions(-) diff --git a/spec/spectator/mocks/double_spec.cr b/spec/spectator/mocks/double_spec.cr index 1b1abc9..41bfad2 100644 --- a/spec/spectator/mocks/double_spec.cr +++ b/spec/spectator/mocks/double_spec.cr @@ -212,14 +212,10 @@ Spectator.describe Spectator::Double do expect(dbl.hash).to be_a(UInt64) expect(dbl.in?([42])).to be_false expect(dbl.in?(1, 2, 3)).to be_false - expect(dbl.inspect).to contain("EmptyDouble") expect(dbl.itself).to be(dbl) expect(dbl.not_nil!).to be(dbl) - expect(dbl.pretty_inspect).to contain("EmptyDouble") expect(dbl.pretty_print(pp)).to be_nil expect(dbl.tap { nil }).to be(dbl) - expect(dbl.to_s).to contain("EmptyDouble") - expect(dbl.to_s(io)).to be_nil expect(dbl.try { nil }).to be_nil expect(dbl.object_id).to be_a(UInt64) expect(dbl.same?(dbl)).to be_true @@ -469,7 +465,7 @@ Spectator.describe Spectator::Double do it "stores calls to non-stubbed methods" do expect { dbl.baz }.to raise_error(Spectator::UnexpectedMessage, /baz/) - expect(called_method_names(dbl)).to eq(%i[baz]) + expect(called_method_names(dbl)).to contain(:baz) end it "stores arguments for a call" do @@ -479,4 +475,68 @@ Spectator.describe Spectator::Double do expect(call.arguments).to eq(args) end end + + describe "#to_s" do + subject(string) { dbl.to_s } + + context "with a name" do + let(dbl) { FooBarDouble.new } + + it "indicates it's a double" do + expect(string).to contain("Double") + end + + it "contains the double name" do + expect(string).to contain("dbl-name") + end + end + + context "without a name" do + let(dbl) { EmptyDouble.new } + + it "indicates it's a double" do + expect(string).to contain("Double") + end + + it "contains \"Anonymous\"" do + expect(string).to contain("Anonymous") + end + end + end + + describe "#inspect" do + subject(string) { dbl.inspect } + + context "with a name" do + let(dbl) { FooBarDouble.new } + + it "indicates it's a double" do + expect(string).to contain("Double") + end + + it "contains the double name" do + expect(string).to contain("dbl-name") + end + + it "contains the object ID" do + expect(string).to contain(dbl.object_id.to_s(16)) + end + end + + context "without a name" do + let(dbl) { EmptyDouble.new } + + it "indicates it's a double" do + expect(string).to contain("Double") + end + + it "contains \"Anonymous\"" do + expect(string).to contain("Anonymous") + end + + it "contains the object ID" do + expect(string).to contain(dbl.object_id.to_s(16)) + end + end + end end diff --git a/spec/spectator/mocks/lazy_double_spec.cr b/spec/spectator/mocks/lazy_double_spec.cr index c3402b5..8ea5a5d 100644 --- a/spec/spectator/mocks/lazy_double_spec.cr +++ b/spec/spectator/mocks/lazy_double_spec.cr @@ -275,7 +275,7 @@ Spectator.describe Spectator::LazyDouble do it "stores calls to non-stubbed methods" do expect { dbl.baz }.to raise_error(Spectator::UnexpectedMessage, /baz/) - expect(called_method_names(dbl)).to eq(%i[baz]) + expect(called_method_names(dbl)).to contain(:baz) end it "stores arguments for a call" do @@ -285,4 +285,68 @@ Spectator.describe Spectator::LazyDouble do expect(call.arguments).to eq(args) end end + + describe "#to_s" do + subject(string) { dbl.to_s } + + context "with a name" do + let(dbl) { Spectator::LazyDouble.new("dbl-name") } + + it "indicates it's a double" do + expect(string).to contain("LazyDouble") + end + + it "contains the double name" do + expect(string).to contain("dbl-name") + end + end + + context "without a name" do + let(dbl) { Spectator::LazyDouble.new } + + it "contains the double type" do + expect(string).to contain("LazyDouble") + end + + it "contains \"Anonymous\"" do + expect(string).to contain("Anonymous") + end + end + end + + describe "#inspect" do + subject(string) { dbl.inspect } + + context "with a name" do + let(dbl) { Spectator::LazyDouble.new("dbl-name") } + + it "contains the double type" do + expect(string).to contain("LazyDouble") + end + + it "contains the double name" do + expect(string).to contain("dbl-name") + end + + it "contains the object ID" do + expect(string).to contain(dbl.object_id.to_s(16)) + end + end + + context "without a name" do + let(dbl) { Spectator::LazyDouble.new } + + it "contains the double type" do + expect(string).to contain("LazyDouble") + end + + it "contains \"Anonymous\"" do + expect(string).to contain("Anonymous") + end + + it "contains the object ID" do + expect(string).to contain(dbl.object_id.to_s(16)) + end + end + end end diff --git a/spec/spectator/mocks/null_double_spec.cr b/spec/spectator/mocks/null_double_spec.cr index 4a8ef3a..ad87ea9 100644 --- a/spec/spectator/mocks/null_double_spec.cr +++ b/spec/spectator/mocks/null_double_spec.cr @@ -186,12 +186,9 @@ Spectator.describe Spectator::NullDouble do expect(dbl.hash).to be_a(UInt64) expect(dbl.in?([42])).to be_false expect(dbl.in?(1, 2, 3)).to be_false - expect(dbl.inspect).to contain("EmptyDouble") expect(dbl.itself).to be(dbl) expect(dbl.not_nil!).to be(dbl) - expect(dbl.pretty_inspect).to contain("EmptyDouble") expect(dbl.tap { nil }).to be(dbl) - expect(dbl.to_s).to contain("EmptyDouble") expect(dbl.try { nil }).to be_nil expect(dbl.object_id).to be_a(UInt64) expect(dbl.same?(dbl)).to be_true @@ -439,4 +436,68 @@ Spectator.describe Spectator::NullDouble do expect(call.arguments).to eq(args) end end + + describe "#to_s" do + subject(string) { dbl.to_s } + + context "with a name" do + let(dbl) { FooBarDouble.new } + + it "indicates it's a double" do + expect(string).to contain("NullDouble") + end + + it "contains the double name" do + expect(string).to contain("dbl-name") + end + end + + context "without a name" do + let(dbl) { EmptyDouble.new } + + it "contains the double type" do + expect(string).to contain("NullDouble") + end + + it "contains \"Anonymous\"" do + expect(string).to contain("Anonymous") + end + end + end + + describe "#inspect" do + subject(string) { dbl.inspect } + + context "with a name" do + let(dbl) { FooBarDouble.new } + + it "contains the double type" do + expect(string).to contain("NullDouble") + end + + it "contains the double name" do + expect(string).to contain("dbl-name") + end + + it "contains the object ID" do + expect(string).to contain(dbl.object_id.to_s(16)) + end + end + + context "without a name" do + let(dbl) { EmptyDouble.new } + + it "contains the double type" do + expect(string).to contain("NullDouble") + end + + it "contains \"Anonymous\"" do + expect(string).to contain("Anonymous") + end + + it "contains the object ID" do + expect(string).to contain(dbl.object_id.to_s(16)) + end + end + end end diff --git a/src/spectator/mocks/double.cr b/src/spectator/mocks/double.cr index ad7d327..e5e43f4 100644 --- a/src/spectator/mocks/double.cr +++ b/src/spectator/mocks/double.cr @@ -98,16 +98,14 @@ module Spectator # Simplified string representation of a double. # Avoids displaying nested content and bloating method instantiation. def to_s(io : IO) : Nil - io << _spectator_stubbed_name + io << "#<" + {{@type.name(generic_args: false).stringify}} + " " + io << _spectator_stubbed_name << '>' end # :ditto: def inspect(io : IO) : Nil - {% if anno = @type.annotation(::Spectator::StubbedName) %} - io << "#" + {{(anno[0] || :Anonymous.id).stringify}} {% else %} - "#" + "Anonymous" {% end %} end private def self._spectator_stubbed_name : String {% if anno = @type.annotation(StubbedName) %} - "#" + {{(anno[0] || :Anonymous.id).stringify}} {% else %} - "#" + "Anonymous" {% end %} end @@ -188,7 +186,7 @@ module Spectator "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." end - raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}") + raise UnexpectedMessage.new("#{inspect} received unexpected message #{call}") end private def _spectator_abstract_stub_fallback(call : MethodCall, type) @@ -207,9 +205,9 @@ module Spectator call = ::Spectator::MethodCall.new({{call.name.symbolize}}, args) _spectator_record_call(call) - Log.trace { "#{_spectator_stubbed_name} got undefined method `#{call}{% if call.block %} { ... }{% end %}`" } + Log.trace { "#{inspect} got undefined method `#{call}{% if call.block %} { ... }{% end %}`" } - raise ::Spectator::UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}") + raise ::Spectator::UnexpectedMessage.new("#{inspect} received unexpected message #{call}") nil # Necessary for compiler to infer return type as nil. Avoids runtime "can't execute ... `x` has no type errors". end end diff --git a/src/spectator/mocks/lazy_double.cr b/src/spectator/mocks/lazy_double.cr index 9a9257d..75fe30c 100644 --- a/src/spectator/mocks/lazy_double.cr +++ b/src/spectator/mocks/lazy_double.cr @@ -37,13 +37,13 @@ module Spectator # Returns the double's name formatted for user output. private def _spectator_stubbed_name : String - "#" + @name || "Anonymous" end private def _spectator_stub_fallback(call : MethodCall, &) if _spectator_stub_for_method?(call.method) Log.info { "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." } - raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}") + raise UnexpectedMessage.new("#{inspect} received unexpected message #{call}") else Log.trace { "Fallback for #{call} - call original" } yield @@ -57,7 +57,7 @@ module Spectator %call = ::Spectator::MethodCall.new({{call.name.symbolize}}, %args) _spectator_record_call(%call) - Log.trace { "#{_spectator_stubbed_name} got undefined method `#{%call}{% if call.block %} { ... }{% end %}`" } + Log.trace { "#{inspect} got undefined method `#{%call}{% if call.block %} { ... }{% end %}`" } # Attempt to find a stub that satisfies the method call and arguments. if %stub = _spectator_find_stub(%call) diff --git a/src/spectator/mocks/null_double.cr b/src/spectator/mocks/null_double.cr index 0ddd03d..587f4ab 100644 --- a/src/spectator/mocks/null_double.cr +++ b/src/spectator/mocks/null_double.cr @@ -26,7 +26,7 @@ module Spectator private def _spectator_abstract_stub_fallback(call : MethodCall) if _spectator_stub_for_method?(call.method) Log.info { "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." } - raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}") + raise UnexpectedMessage.new("#{inspect} received unexpected message #{call}") else Log.trace { "Fallback for #{call} - return self" } self @@ -42,9 +42,9 @@ module Spectator private def _spectator_abstract_stub_fallback(call : MethodCall, type) if _spectator_stub_for_method?(call.method) Log.info { "Stubs are defined for #{call.method.inspect}, but none matched (no argument constraints met)." } - raise UnexpectedMessage.new("#{_spectator_stubbed_name} received unexpected message #{call}") + raise UnexpectedMessage.new("#{inspect} received unexpected message #{call}") else - raise TypeCastError.new("#{_spectator_stubbed_name} received message #{call} and is attempting to return `self`, but returned type must be `#{type}`.") + raise TypeCastError.new("#{inspect} received message #{call} and is attempting to return `self`, but returned type must be `#{type}`.") end end @@ -56,7 +56,7 @@ module Spectator %call = ::Spectator::MethodCall.new({{call.name.symbolize}}, %args) _spectator_record_call(%call) - Log.trace { "#{_spectator_stubbed_name} got undefined method `#{%call}{% if call.block %} { ... }{% end %}`" } + Log.trace { "#{inspect} got undefined method `#{%call}{% if call.block %} { ... }{% end %}`" } self end