diff --git a/spec/expectations/expectation_reporter_spec.cr b/spec/expectations/expectation_reporter_spec.cr index e828af7..4ccfdff 100644 --- a/spec/expectations/expectation_reporter_spec.cr +++ b/spec/expectations/expectation_reporter_spec.cr @@ -5,12 +5,7 @@ describe Spectator::Expectations::ExpectationReporter do context "with raise flag set" do context "given a successful result" do it "stores the 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) - result = expectation.eval - result.successful?.should be_true # Sanity check. + result = new_successful_result reporter = Spectator::Expectations::ExpectationReporter.new(true) reporter.report(result) reporter.results.should contain(result) @@ -19,25 +14,13 @@ describe Spectator::Expectations::ExpectationReporter do context "given a failed result" do it "raises and error" do - value1 = 42 - value2 = 777 - partial = Spectator::Expectations::ValueExpectationPartial.new(value1) - matcher = Spectator::Matchers::EqualityMatcher.new(value2) - expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) - result = expectation.eval - result.successful?.should be_false # Sanity check. + result = new_failure_result reporter = Spectator::Expectations::ExpectationReporter.new(true) expect_raises(Spectator::ExpectationFailed) { reporter.report(result) } end it "stores the result" do - value1 = 42 - value2 = 777 - partial = Spectator::Expectations::ValueExpectationPartial.new(value1) - matcher = Spectator::Matchers::EqualityMatcher.new(value2) - expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) - result = expectation.eval - result.successful?.should be_false # Sanity check. + result = new_failure_result reporter = Spectator::Expectations::ExpectationReporter.new(true) begin reporter.report(result) @@ -52,12 +35,7 @@ describe Spectator::Expectations::ExpectationReporter do context "with raise flag not set" do context "given a successful result" do it "stores the result" do - value = 42 - partial = Spectator::Expectations::ValueExpectationPartial.new(value) - matcher = Spectator::Matchers::EqualityMatcher.new(value) - expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) - result = expectation.eval - result.successful?.should be_true # Sanity check. + result = new_successful_result reporter = Spectator::Expectations::ExpectationReporter.new(false) reporter.report(result) reporter.results.should contain(result) @@ -66,13 +44,7 @@ describe Spectator::Expectations::ExpectationReporter do context "given a failed result" do it "stores the result" do - value1 = 42 - value2 = 777 - partial = Spectator::Expectations::ValueExpectationPartial.new(value1) - matcher = Spectator::Matchers::EqualityMatcher.new(value2) - expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) - result = expectation.eval - result.successful?.should be_false # Sanity check. + result = new_failure_result reporter = Spectator::Expectations::ExpectationReporter.new(false) reporter.report(result) reporter.results.should contain(result) @@ -91,17 +63,8 @@ describe Spectator::Expectations::ExpectationReporter do context "with multiple expectations" do it "contains all expectations" do - value1 = 42 - value2 = 777 - partial = Spectator::Expectations::ValueExpectationPartial.new(value1) - matcher = Spectator::Matchers::EqualityMatcher.new(value1) - expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) - result1 = expectation.eval - partial = Spectator::Expectations::ValueExpectationPartial.new(value1) - matcher = Spectator::Matchers::EqualityMatcher.new(value2) - expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) - result2 = expectation.eval - + result1 = new_successful_result + result2 = new_failure_result reporter = Spectator::Expectations::ExpectationReporter.new(false) begin reporter.report(result1) diff --git a/spec/expectations_helper.cr b/spec/expectations_helper.cr new file mode 100644 index 0000000..165172f --- /dev/null +++ b/spec/expectations_helper.cr @@ -0,0 +1,43 @@ +# Utility methods for creating expectations, partials, and matchers. + +def new_partial(label : String, actual : T) forall T + Spectator::Expectations::ValueExpectationPartial.new(label, actual) +end + +def new_partial(actual : T = 123) forall T + new_partial(actual.to_s, actual) +end + +def new_matcher(label : String, expected : T) forall T + Spectator::Matchers::EqualityMatcher.new(label, expected) +end + +def new_matcher(expected : T = 123) forall T + new_matcher(expected.to_s, expected) +end + +def new_expectation(expected : ExpectedType = 123, actual : ActualType = 123) forall ExpectedType, ActualType + partial = new_partial("foo", actual) + matcher = new_matcher("bar", expected) + Spectator::Expectations::ValueExpectation.new(partial, matcher) +end + +def new_met_expectation(value : T = 123) forall T + new_expectation(value, value) +end + +def new_unmet_expectation(expected : ExpectedType = 123, actual : ActualType = 456) forall ExpectedType, ActualType + new_expectation(expected, actual) +end + +def new_successful_result + new_met_expectation.eval.tap do |result| + result.successful?.should be_true # Sanity check. + end +end + +def new_failure_result + new_unmet_expectation.eval.tap do |result| + result.successful?.should be_false # Sanity check. + end +end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index 2a4f0dd..a2c27ec 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -1,5 +1,6 @@ require "spec" require "../src/spectator" +require "./expectations_helper" # Prevent Spectator from trying to run tests. Spectator.autorun = false