Secondary initializer for missing label

Label is set to actual/expected stringified value if omitted.
This commit is contained in:
Michael Miller 2018-10-18 21:52:00 -06:00
parent 9a77f8b7fd
commit 7f4690b042
5 changed files with 41 additions and 11 deletions

View file

@ -10,7 +10,7 @@ describe Spectator::Expectations::ValueExpectationPartial do
end end
describe "#label" do describe "#label" do
context "with a non-empty string" do context "when provided" do
it "contains the value passed to the constructor" do it "contains the value passed to the constructor" do
actual = 777 actual = 777
label = "lucky" label = "lucky"
@ -19,10 +19,10 @@ describe Spectator::Expectations::ValueExpectationPartial do
end end
end end
context "with an empty string" do context "when omitted" do
it "contains a stringified version of #actual" do it "contains a stringified version of #actual" do
actual = 777 actual = 777
partial = Spectator::Expectations::ValueExpectationPartial.new("", actual) partial = Spectator::Expectations::ValueExpectationPartial.new(actual)
partial.label.should eq(actual.to_s) partial.label.should eq(actual.to_s)
end end
end end

View file

@ -85,6 +85,16 @@ describe Spectator::Matchers::EqualityMatcher do
matcher = Spectator::Matchers::EqualityMatcher.new(label, value) matcher = Spectator::Matchers::EqualityMatcher.new(label, value)
matcher.message(partial).should contain(label) matcher.message(partial).should contain(label)
end end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value1 = 42
value2 = 777
partial = Spectator::Expectations::ValueExpectationPartial.new(value1.to_s, value1)
matcher = Spectator::Matchers::EqualityMatcher.new(value2)
matcher.message(partial).should contain(value2.to_s)
end
end
end end
describe "#negated_message" do describe "#negated_message" do
@ -100,7 +110,7 @@ describe Spectator::Matchers::EqualityMatcher do
label = "everything" label = "everything"
partial = Spectator::Expectations::ValueExpectationPartial.new(label, value) partial = Spectator::Expectations::ValueExpectationPartial.new(label, value)
matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value)
matcher.message(partial).should contain(label) matcher.negated_message(partial).should contain(label)
end end
it "contains the expected label" do it "contains the expected label" do
@ -108,7 +118,17 @@ describe Spectator::Matchers::EqualityMatcher do
label = "everything" label = "everything"
partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value)
matcher = Spectator::Matchers::EqualityMatcher.new(label, value) matcher = Spectator::Matchers::EqualityMatcher.new(label, value)
matcher.message(partial).should contain(label) matcher.negated_message(partial).should contain(label)
end
context "when expected label is omitted" do
it "contains stringified form of expected value" do
value1 = 42
value2 = 777
partial = Spectator::Expectations::ValueExpectationPartial.new(value1.to_s, value1)
matcher = Spectator::Matchers::EqualityMatcher.new(value2)
matcher.negated_message(partial).should contain(value2.to_s)
end
end end
end end
end end

View file

@ -8,14 +8,17 @@ module Spectator::Expectations
getter actual getter actual
# Creates the expectation partial. # Creates the expectation partial.
# The label should be a string representation of the actual value.
# The actual value is stored for later use.
protected def initialize(label : String, @actual : ActualType) protected def initialize(label : String, @actual : ActualType)
super(label) super(label)
end end
# Returns the actual value as a string # Creates the expectation partial.
# if there's no label available. # The label is generated by calling `#to_s` on the actual value.
def label # The actual value is stored for later use.
super.empty? ? actual.to_s : super protected def initialize(@actual : ActualType)
super(@actual.to_s)
end end
# Asserts that the `#actual` value matches some criteria. # Asserts that the `#actual` value matches some criteria.

View file

@ -17,6 +17,13 @@ module Spectator::Matchers
super(label) super(label)
end end
# Creates the value matcher.
# The label is generated by calling `#to_s` on the expected value.
# The expected value is stored for later use.
def initialize(@expected : ExpectedType)
super(@expected.to_s)
end
# Determines whether the matcher is satisfied with the value given to it. # Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise. # True is returned if the match was successful, false otherwise.
abstract def match?(partial : ValueExpectationPartial(ActualType)) : Bool forall ActualType abstract def match?(partial : ValueExpectationPartial(ActualType)) : Bool forall ActualType

View file

@ -20,7 +20,7 @@ class Object
# First argument of the `Expectation` initializer is the expression label. # First argument of the `Expectation` initializer is the expression label.
# However, since this isn't a macro and we can't "look behind" this method call # However, since this isn't a macro and we can't "look behind" this method call
# to see what it was invoked on, the argument is an empty string. # to see what it was invoked on, the argument is an empty string.
expectation = ::Spectator::Expectation.new("", self) expectation = ::Spectator::Expectation.new(self)
unless matcher.match?(expectation) unless matcher.match?(expectation)
raise ::Spectator::ExpectationFailed.new(matcher.message(expectation)) raise ::Spectator::ExpectationFailed.new(matcher.message(expectation))
end end
@ -29,7 +29,7 @@ class Object
# Works the same as `#should` except the condition is inverted. # Works the same as `#should` except the condition is inverted.
# When `#should` succeeds, this method will fail, and vice-versa. # When `#should` succeeds, this method will fail, and vice-versa.
def should_not(matcher : ::Spectator::Matchers::Matcher) def should_not(matcher : ::Spectator::Matchers::Matcher)
expectation = ::Spectator::Expectation.new("", self) expectation = ::Spectator::Expectation.new(self)
if matcher.match?(expectation) if matcher.match?(expectation)
raise ::Spectator::ExpectationFailed.new(matcher.message(expectation)) raise ::Spectator::ExpectationFailed.new(matcher.message(expectation))
end end