Match RSpec behavior

This commit is contained in:
Michael Miller 2022-03-25 22:32:27 -06:00
parent 28488d308e
commit b28437ccc4
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2
2 changed files with 54 additions and 62 deletions

View file

@ -59,56 +59,54 @@ Spectator.describe Spectator::LazyDouble do
end
context "with common object methods" do
let(dup) { double(:dup) }
subject(dbl) do
Spectator::LazyDouble.new(nil, [
Spectator::ValueStub.new(:"!=", "!="),
Spectator::ValueStub.new(:"!~", "!~"),
Spectator::ValueStub.new(:"==", "=="),
Spectator::ValueStub.new(:"===", "==="),
Spectator::ValueStub.new(:"=~", "=~"),
Spectator::ValueStub.new(:class, "class"),
Spectator::ValueStub.new(:dup, "dup"),
Spectator::ValueStub.new(:hash, "hash"),
Spectator::ValueStub.new(:"!=", false),
Spectator::ValueStub.new(:"!~", false),
Spectator::ValueStub.new(:"==", true),
Spectator::ValueStub.new(:"===", true),
Spectator::ValueStub.new(:"=~", nil),
Spectator::ValueStub.new(:dup, dup),
Spectator::ValueStub.new(:hash, 42_u64),
Spectator::ValueStub.new(:"in?", true),
Spectator::ValueStub.new(:inspect, "inspect"),
Spectator::ValueStub.new(:itself, "itself"),
Spectator::ValueStub.new(:"not_nil!", "not_nil!"),
Spectator::ValueStub.new(:itself, dup),
Spectator::ValueStub.new(:"not_nil!", dup),
Spectator::ValueStub.new(:pretty_inspect, "pretty_inspect"),
Spectator::ValueStub.new(:tap, "tap"),
Spectator::ValueStub.new(:tap, dup),
Spectator::ValueStub.new(:to_json, "to_json"),
Spectator::ValueStub.new(:to_pretty_json, "to_pretty_json"),
Spectator::ValueStub.new(:to_s, "to_s"),
Spectator::ValueStub.new(:to_yaml, "to_yaml"),
Spectator::ValueStub.new(:try, "try"),
Spectator::ValueStub.new(:try, nil),
Spectator::ValueStub.new(:object_id, 42_u64),
Spectator::ValueStub.new(:"same?", true),
] of Spectator::Stub)
end
it "responds with defined messages" do
hasher = Crystal::Hasher.new
aggregate_failures do
expect(dbl.!=(42)).to eq("!=")
expect(dbl.!~(42)).to eq("!~")
expect(dbl.==(42)).to eq("==")
expect(dbl.===(42)).to eq("===")
expect(dbl.=~(42)).to eq("=~")
expect(dbl.class).to eq("class")
expect(dbl.dup).to eq("dup")
expect(dbl.hash(hasher)).to eq("hash")
expect(dbl.hash).to eq("hash")
expect(dbl.!=(42)).to eq(false)
expect(dbl.!~(42)).to eq(false)
expect(dbl.==(42)).to eq(true)
expect(dbl.===(42)).to eq(true)
expect(dbl.=~(42)).to be_nil
expect(dbl.dup).to be(dup)
expect(dbl.hash).to eq(42_u64)
expect(dbl.in?([42])).to eq(true)
expect(dbl.in?(1, 2, 3)).to eq(true)
expect(dbl.inspect).to eq("inspect")
expect(dbl.itself).to eq("itself")
expect(dbl.not_nil!).to eq("not_nil!")
expect(dbl.itself).to be(dup)
expect(dbl.not_nil!).to be(dup)
expect(dbl.pretty_inspect).to eq("pretty_inspect")
expect(dbl.tap { nil }).to eq("tap")
expect(dbl.tap { nil }).to be(dup)
expect(dbl.to_json).to eq("to_json")
expect(dbl.to_pretty_json).to eq("to_pretty_json")
expect(dbl.to_s).to eq("to_s")
expect(dbl.to_yaml).to eq("to_yaml")
expect(dbl.try { nil }).to eq("try")
expect(dbl.try { nil }).to be_nil
expect(dbl.object_id).to eq(42_u64)
expect(dbl.same?(dbl)).to eq(true)
expect(dbl.same?(nil)).to eq(true)
@ -123,47 +121,28 @@ Spectator.describe Spectator::LazyDouble do
context "without common object methods" do
subject(dbl) { Spectator::LazyDouble.new }
it "raises with undefined messages" do
it "returns the original value" do
io = IO::Memory.new
pp = PrettyPrint.new(io)
hasher = Crystal::Hasher.new
aggregate_failures do
expect { dbl.!=(42) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.!~(42) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.==(42) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.===(42) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.=~(42) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.class }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.dup }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.hash(hasher) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.hash }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.in?([42]) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.in?(1, 2, 3) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.inspect }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.itself }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.not_nil! }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.pretty_inspect }.to raise_error(Spectator::UnexpectedMessage)
# expect { dbl.pretty_inspect(io) }.to raise_error(Spectator::UnexpectedMessage)
# expect { dbl.pretty_print(pp) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.tap { nil } }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.to_json }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.to_json(io) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.to_pretty_json }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.to_pretty_json(io) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.to_s }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.to_s(io) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.to_yaml }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.to_yaml(io) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.try { nil } }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.object_id }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.same?(dbl) }.to raise_error(Spectator::UnexpectedMessage)
expect { dbl.same?(nil) }.to raise_error(Spectator::UnexpectedMessage)
expect(dbl.!=(42)).to be_true
expect(dbl.!~(42)).to be_true
expect(dbl.==(42)).to be_false
expect(dbl.===(42)).to be_false
expect(dbl.=~(42)).to be_nil
expect(dbl.class).to be_lt(Spectator::LazyDouble)
expect(dbl.in?([42])).to be_false
expect(dbl.in?(1, 2, 3)).to be_false
expect(dbl.itself).to be(dbl)
expect(dbl.not_nil!).to be(dbl)
expect(dbl.tap { nil }).to be(dbl)
expect(dbl.to_s(io)).to be_nil
expect(dbl.try { nil }).to be_nil
expect(dbl.same?(dbl)).to be_true
expect(dbl.same?(nil)).to be_false
end
end
it "reports arguments" do
expect { dbl.same?(123) }.to raise_error(Spectator::UnexpectedMessage, /\(123\)/)
end
end
context "with arguments constraints" do
@ -201,6 +180,14 @@ Spectator.describe Spectator::LazyDouble do
it "raises an error when argument count doesn't match" do
expect { dbl.same? }.to raise_error(Spectator::UnexpectedMessage)
end
context "with a fallback defined" do
subject(dbl) { Spectator::LazyDouble.new(nil, [stub], "same?": true) }
it "returns the fallback when constraint unsatisfied" do
expect(dbl.same?("baz")).to be_true
end
end
end
end

View file

@ -3,6 +3,7 @@ require "./arguments"
require "./double"
require "./method_call"
require "./stub"
require "./value_stub"
module Spectator
# Stands in for an object for testing that a SUT calls expected methods.
@ -17,8 +18,12 @@ module Spectator
@name : String?
def initialize(_spectator_double_name = nil, _spectator_double_stubs = [] of Stub, **@messages : **Messages)
super(_spectator_double_stubs)
@name = _spectator_double_name.try &.inspect
message_stubs = messages.map do |method, value|
ValueStub.new(method, value)
end
super(_spectator_double_stubs + message_stubs)
end
# Returns the double's name formatted for user output.