diff --git a/spec/expectations/value_expectation_spec.cr b/spec/expectations/value_expectation_spec.cr new file mode 100644 index 0000000..dca0aee --- /dev/null +++ b/spec/expectations/value_expectation_spec.cr @@ -0,0 +1,152 @@ +require "../spec_helper" + +describe Spectator::Expectations::ValueExpectation do + describe "#eval" do + context "with a successful match" do + it "returns a successful result" do + value = 42 + partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + matcher.match?(partial).should be_true # Sanity check. + expectation.eval.successful?.should be_true + end + + it "reports a successful actual message" do + value = 42 + partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + matcher.match?(partial).should be_true # Sanity check. + expectation.eval.actual_message.should eq(expectation.message) + end + end + + context "with an unsuccessful match" do + it "returns an unsuccessful result" do + value1 = 42 + value2 = 777 + partial = Spectator::Expectations::ValueExpectationPartial.new(value1.to_s, value1) + matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + matcher.match?(partial).should be_false # Sanity check. + expectation.eval.successful?.should be_false + end + + it "reports an unsuccessful actual message" do + value1 = 42 + value2 = 777 + partial = Spectator::Expectations::ValueExpectationPartial.new(value1.to_s, value1) + matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + matcher.match?(partial).should be_false # Sanity check. + expectation.eval.actual_message.should eq(expectation.negated_message) + end + end + + it "reports a non-negated expected message" do + value = 42 + partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + expectation.eval.expected_message.should eq(expectation.message) + end + + context "negated" do + context "with a successful match" do + it "returns an unsuccessful result" do + value = 42 + partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + matcher.match?(partial).should be_true # Sanity check. + expectation.eval(true).successful?.should be_false + end + + it "reports an unsuccessful actual message" do + value = 42 + partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + matcher.match?(partial).should be_true # Sanity check. + expectation.eval(true).actual_message.should eq(expectation.negated_message) + end + end + + context "with an unsuccessful match" do + it "returns a successful result" do + value1 = 42 + value2 = 777 + partial = Spectator::Expectations::ValueExpectationPartial.new(value1.to_s, value1) + matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + matcher.match?(partial).should be_false # Sanity check. + expectation.eval(true).successful?.should be_true + end + + it "reports a successful actual message" do + value1 = 42 + value2 = 777 + partial = Spectator::Expectations::ValueExpectationPartial.new(value1.to_s, value1) + matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + matcher.match?(partial).should be_false # Sanity check. + expectation.eval(true).actual_message.should eq(expectation.message) + end + end + + it "reports a negated expected message" do + value = 42 + partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + expectation.eval(true).expected_message.should eq(expectation.negated_message) + end + end + end + + describe "#satisifed?" do + context "with a successful match" do + it "is true" do + value = 42 + partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + matcher.match?(partial).should be_true # Sanity check. + expectation.satisfied?.should be_true + end + end + + context "with an unsuccessful match" do + it "is false" do + value1 = 42 + value2 = 777 + partial = Spectator::Expectations::ValueExpectationPartial.new(value1.to_s, value1) + matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + matcher.match?(partial).should be_false # Sanity check. + expectation.satisfied?.should be_false + end + end + end + + describe "#message" do + it "equals the matcher's #message" do + value = 42 + partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + expectation.message.should eq(matcher.message(partial)) + end + end + + describe "#negated_message" do + it "equals the matcher's #negated_message" do + value = 42 + partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) + matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) + expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) + expectation.negated_message.should eq(matcher.negated_message(partial)) + end + end +end diff --git a/src/spectator/expectations/expectation.cr b/src/spectator/expectations/expectation.cr index 6236850..d51aa09 100644 --- a/src/spectator/expectations/expectation.cr +++ b/src/spectator/expectations/expectation.cr @@ -43,7 +43,7 @@ module Spectator::Expectations # Description of what actually happened when the expectation was evaluated. def actual_message - message(@successful) + message(!@successful) end # Retrieves the message or negated message from an expectation.