Fix specs to work with new Expectation type

This commit is contained in:
Michael Miller 2018-11-14 02:15:55 -07:00
parent 35b59854ec
commit f0204a32ea
9 changed files with 379 additions and 395 deletions

View file

@ -0,0 +1,207 @@
require "../spec_helper"
describe Spectator::Expectations::ExampleExpectations do
describe "#each" do
it "yields all expectations" do
tuple = generate_expectations(5, 5)
expectations = [] of Spectator::Expectations::Expectation
tuple[:reporter].expectations.each { |expectation| expectations << expectation }
# Expectations might not be in the same order.
# Just check if if the arrays contain the same items.
expectations.size.should eq(tuple[:expectations].size)
(expectations - tuple[:expectations]).empty?.should be_true
end
end
describe "#satisfied" do
it "returns only satisfied expectations" do
tuple = generate_expectations(5, 5)
expectations = tuple[:reporter].expectations
expectations.satisfied.all?(&.satisfied?).should be_true
end
it "returns the correct expectations" do
tuple = generate_expectations(5, 5)
expectations = tuple[:reporter].expectations
satisfied = expectations.satisfied.to_a
satisfied.size.should eq(5)
(satisfied - tuple[:satisfied]).empty?.should be_true
end
context "with all satisfied expectations" do
it "returns all expectations" do
tuple = generate_expectations(5, 0)
expectations = tuple[:reporter].expectations
expectations.satisfied.size.should eq(tuple[:satisfied].size)
end
end
context "with all unsatisfied expectations" do
it "returns an empty collection" do
tuple = generate_expectations(0, 5)
expectations = tuple[:reporter].expectations
expectations.satisfied.size.should eq(0)
end
end
end
describe "#each_satisfied" do
it "yields only satisfied expectations" do
tuple = generate_expectations(5, 5)
expectations = [] of Spectator::Expectations::Expectation
tuple[:reporter].expectations.each_satisfied { |expectation| expectations << expectation }
# Expectations might not be in the same order.
# Just check if if the arrays contain the same items.
expectations.size.should eq(tuple[:satisfied].size)
(expectations - tuple[:satisfied]).empty?.should be_true
end
context "with all satisfied expectations" do
it "yields all expectations" do
tuple = generate_expectations(0, 5)
expectations = [] of Spectator::Expectations::Expectation
tuple[:reporter].expectations.each_satisfied { |expectation| expectations << expectation }
expectations.size.should eq(tuple[:satisfied].size)
end
end
context "with all unsatisfied expectations" do
it "yields nothing" do
tuple = generate_expectations(0, 5)
expectations = [] of Spectator::Expectations::Expectation
tuple[:reporter].expectations.each_satisfied { |expectation| expectations << expectation }
expectations.empty?.should be_true
end
end
end
describe "#unsatisfied" do
it "returns only unsatisfied expectations" do
tuple = generate_expectations(5, 5)
expectations = tuple[:reporter].expectations
expectations.unsatisfied.all?(&.satisfied?).should be_false
end
it "returns the correct expectations" do
tuple = generate_expectations(5, 5)
expectations = tuple[:reporter].expectations
unsatisfied = expectations.unsatisfied.to_a
unsatisfied.size.should eq(5)
(unsatisfied - tuple[:unsatisfied]).empty?.should be_true
end
context "with all satisfied expectations" do
it "returns an empty collection" do
tuple = generate_expectations(5, 0)
expectations = tuple[:reporter].expectations
expectations.unsatisfied.size.should eq(0)
end
end
context "with all unsatisfied expectations" do
it "returns all expectations" do
tuple = generate_expectations(0, 5)
expectations = tuple[:reporter].expectations
expectations.unsatisfied.size.should eq(tuple[:unsatisfied].size)
end
end
end
describe "#each_unsatisfied" do
it "yields only unsatisfied expectations" do
tuple = generate_expectations(5, 5)
expectations = [] of Spectator::Expectations::Expectation
tuple[:reporter].expectations.each_unsatisfied { |expectation| expectations << expectation }
# Expectations might not be in the same order.
# Just check if if the arrays contain the same items.
expectations.size.should eq(tuple[:unsatisfied].size)
(expectations - tuple[:unsatisfied]).empty?.should be_true
end
context "with all satisfied expectations" do
it "yields nothing" do
tuple = generate_expectations(5, 0)
expectations = [] of Spectator::Expectations::Expectation
tuple[:reporter].expectations.each_unsatisfied { |expectation| expectations << expectation }
expectations.empty?.should be_true
end
end
context "with all unsatisfied expectations" do
it "yields all expectations" do
tuple = generate_expectations(0, 5)
expectations = [] of Spectator::Expectations::Expectation
tuple[:reporter].expectations.each_unsatisfied { |expectation| expectations << expectation }
expectations.size.should eq(tuple[:unsatisfied].size)
end
end
end
describe "#successful?" do
context "with all satisfied expectations" do
it "is true" do
tuple = generate_expectations(5, 0)
expectations = tuple[:reporter].expectations
expectations.successful?.should be_true
end
end
context "with one unsatisfied expectation" do
it "is false" do
tuple = generate_expectations(5, 1)
expectations = tuple[:reporter].expectations
expectations.successful?.should be_false
end
end
context "with one satisfied expectation" do
it "is false" do
tuple = generate_expectations(1, 5)
expectations = tuple[:reporter].expectations
expectations.successful?.should be_false
end
end
context "with all unsatisfied expectations" do
it "is false" do
tuple = generate_expectations(0, 5)
expectations = tuple[:reporter].expectations
expectations.successful?.should be_false
end
end
end
describe "#failed?" do
context "with all satisfied expectations" do
it "is false" do
tuple = generate_expectations(5, 0)
expectations = tuple[:reporter].expectations
expectations.failed?.should be_false
end
end
context "with one unsatisfied expectation" do
it "is true" do
tuple = generate_expectations(5, 1)
expectations = tuple[:reporter].expectations
expectations.failed?.should be_true
end
end
context "with one satisfied expectation" do
it "is true" do
tuple = generate_expectations(1, 5)
expectations = tuple[:reporter].expectations
expectations.failed?.should be_true
end
end
context "with all unsatisfied expectations" do
it "is true" do
tuple = generate_expectations(0, 5)
expectations = tuple[:reporter].expectations
expectations.failed?.should be_true
end
end
end
end

View file

@ -3,77 +3,77 @@ require "../spec_helper"
describe Spectator::Expectations::ExpectationReporter do describe Spectator::Expectations::ExpectationReporter do
describe "#report" do describe "#report" do
context "with raise flag set" do context "with raise flag set" do
context "given a successful result" do context "given a satisfied expectation" do
it "stores the result" do it "stores the result" do
result = new_successful_result expectation = new_satisfied_expectation
reporter = Spectator::Expectations::ExpectationReporter.new(true) reporter = Spectator::Expectations::ExpectationReporter.new(true)
reporter.report(result) reporter.report(expectation)
reporter.results.should contain(result) reporter.expectations.should contain(expectation)
end end
end end
context "given a failed result" do context "given a unsatisfied expectation" do
it "raises and error" do it "raises and error" do
result = new_failure_result expectation = new_unsatisfied_expectation
reporter = Spectator::Expectations::ExpectationReporter.new(true) reporter = Spectator::Expectations::ExpectationReporter.new(true)
expect_raises(Spectator::ExpectationFailed) { reporter.report(result) } expect_raises(Spectator::ExpectationFailed) { reporter.report(expectation) }
end end
it "stores the result" do it "stores the expectation" do
result = new_failure_result expectation = new_unsatisfied_expectation
reporter = Spectator::Expectations::ExpectationReporter.new(true) reporter = Spectator::Expectations::ExpectationReporter.new(true)
begin begin
reporter.report(result) reporter.report(expectation)
rescue rescue
# Ignore error, not testing that in this example. # Ignore error, not testing that in this example.
end end
reporter.results.should contain(result) reporter.expectations.should contain(expectation)
end end
end end
end end
context "with raise flag not set" do context "with raise flag not set" do
context "given a successful result" do context "given a satisfied expectation" do
it "stores the result" do it "stores the expectation" do
result = new_successful_result expectation = new_satisfied_expectation
reporter = Spectator::Expectations::ExpectationReporter.new(false) reporter = Spectator::Expectations::ExpectationReporter.new(false)
reporter.report(result) reporter.report(expectation)
reporter.results.should contain(result) reporter.expectations.should contain(expectation)
end end
end end
context "given a failed result" do context "given a unsatisfied expectation" do
it "stores the result" do it "stores the expectation" do
result = new_failure_result expectation = new_unsatisfied_expectation
reporter = Spectator::Expectations::ExpectationReporter.new(false) reporter = Spectator::Expectations::ExpectationReporter.new(false)
reporter.report(result) reporter.report(expectation)
reporter.results.should contain(result) reporter.expectations.should contain(expectation)
end end
end end
end end
end end
describe "#results" do describe "#expectations" do
context "with no expectations" do context "with no expectations" do
it "is empty" do it "is empty" do
reporter = Spectator::Expectations::ExpectationReporter.new reporter = Spectator::Expectations::ExpectationReporter.new
reporter.results.size.should eq(0) reporter.expectations.size.should eq(0)
end end
end end
context "with multiple expectations" do context "with multiple expectations" do
it "contains all expectations" do it "contains all expectations" do
result1 = new_successful_result expectation1 = new_satisfied_expectation
result2 = new_failure_result expectation2 = new_unsatisfied_expectation
reporter = Spectator::Expectations::ExpectationReporter.new(false) reporter = Spectator::Expectations::ExpectationReporter.new(false)
begin begin
reporter.report(result1) reporter.report(expectation1)
reporter.report(result2) reporter.report(expectation2)
rescue rescue
# Ignore errors for this test. # Ignore errors for this test.
end end
reporter.results.should contain(result1) reporter.expectations.should contain(expectation1)
reporter.results.should contain(result2) reporter.expectations.should contain(expectation2)
end end
end end
end end

View file

@ -1,207 +0,0 @@
require "../spec_helper"
describe Spectator::Expectations::ExpectationResults do
describe "#each" do
it "yields all results" do
tuple = generate_results(5, 5)
results = [] of Spectator::Expectations::Expectation::Result
tuple[:reporter].results.each { |result| results << result }
# Results might not be in the same order.
# Just check if if the arrays contain the same items.
results.size.should eq(tuple[:results].size)
(results - tuple[:results]).empty?.should be_true
end
end
describe "#successes" do
it "returns only successful results" do
tuple = generate_results(5, 5)
results = tuple[:reporter].results
results.successes.all?(&.successful?).should be_true
end
it "returns the correct results" do
tuple = generate_results(5, 5)
results = tuple[:reporter].results
successful = results.successes.to_a
successful.size.should eq(5)
(successful - tuple[:successful]).empty?.should be_true
end
context "with all successful results" do
it "returns all results" do
tuple = generate_results(5, 0)
results = tuple[:reporter].results
results.successes.size.should eq(tuple[:successful].size)
end
end
context "with all failure results" do
it "returns an empty collection" do
tuple = generate_results(0, 5)
results = tuple[:reporter].results
results.successes.size.should eq(0)
end
end
end
describe "#each_success" do
it "yields only successful results" do
tuple = generate_results(5, 5)
results = [] of Spectator::Expectations::Expectation::Result
tuple[:reporter].results.each_success { |result| results << result }
# Results might not be in the same order.
# Just check if if the arrays contain the same items.
results.size.should eq(tuple[:successful].size)
(results - tuple[:successful]).empty?.should be_true
end
context "with all successful results" do
it "yields all results" do
tuple = generate_results(0, 5)
results = [] of Spectator::Expectations::Expectation::Result
tuple[:reporter].results.each_success { |result| results << result }
results.size.should eq(tuple[:successful].size)
end
end
context "with all failure results" do
it "yields nothing" do
tuple = generate_results(0, 5)
results = [] of Spectator::Expectations::Expectation::Result
tuple[:reporter].results.each_success { |result| results << result }
results.empty?.should be_true
end
end
end
describe "#failures" do
it "returns only failed results" do
tuple = generate_results(5, 5)
results = tuple[:reporter].results
results.failures.all?(&.failure?).should be_true
end
it "returns the correct results" do
tuple = generate_results(5, 5)
results = tuple[:reporter].results
failures = results.failures.to_a
failures.size.should eq(5)
(failures - tuple[:failures]).empty?.should be_true
end
context "with all successful results" do
it "returns an empty collection" do
tuple = generate_results(5, 0)
results = tuple[:reporter].results
results.failures.size.should eq(0)
end
end
context "with all failure results" do
it "returns all results" do
tuple = generate_results(0, 5)
results = tuple[:reporter].results
results.failures.size.should eq(tuple[:failures].size)
end
end
end
describe "#each_failure" do
it "yields only failed results" do
tuple = generate_results(5, 5)
results = [] of Spectator::Expectations::Expectation::Result
tuple[:reporter].results.each_failure { |result| results << result }
# Results might not be in the same order.
# Just check if if the arrays contain the same items.
results.size.should eq(tuple[:failures].size)
(results - tuple[:failures]).empty?.should be_true
end
context "with all successful results" do
it "yields nothing" do
tuple = generate_results(5, 0)
results = [] of Spectator::Expectations::Expectation::Result
tuple[:reporter].results.each_failure { |result| results << result }
results.empty?.should be_true
end
end
context "with all failure results" do
it "yields all results" do
tuple = generate_results(0, 5)
results = [] of Spectator::Expectations::Expectation::Result
tuple[:reporter].results.each_failure { |result| results << result }
results.size.should eq(tuple[:failures].size)
end
end
end
describe "#successful?" do
context "with all successful results" do
it "is true" do
tuple = generate_results(5, 0)
results = tuple[:reporter].results
results.successful?.should be_true
end
end
context "with one failure result" do
it "is false" do
tuple = generate_results(5, 1)
results = tuple[:reporter].results
results.successful?.should be_false
end
end
context "with one successful result" do
it "is false" do
tuple = generate_results(1, 5)
results = tuple[:reporter].results
results.successful?.should be_false
end
end
context "with all failure results" do
it "is false" do
tuple = generate_results(0, 5)
results = tuple[:reporter].results
results.successful?.should be_false
end
end
end
describe "#failed?" do
context "with all successful results" do
it "is false" do
tuple = generate_results(5, 0)
results = tuple[:reporter].results
results.failed?.should be_false
end
end
context "with one failure result" do
it "is true" do
tuple = generate_results(5, 1)
results = tuple[:reporter].results
results.failed?.should be_true
end
end
context "with one successful result" do
it "is true" do
tuple = generate_results(1, 5)
results = tuple[:reporter].results
results.failed?.should be_true
end
end
context "with all failure results" do
it "is true" do
tuple = generate_results(0, 5)
results = tuple[:reporter].results
results.failed?.should be_true
end
end
end
end

View file

@ -1,120 +1,27 @@
require "../spec_helper" require "../spec_helper"
describe Spectator::Expectations::ValueExpectation do 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)
matcher = Spectator::Matchers::EqualityMatcher.new(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)
matcher = Spectator::Matchers::EqualityMatcher.new(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)
matcher = Spectator::Matchers::EqualityMatcher.new(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)
matcher = Spectator::Matchers::EqualityMatcher.new(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)
matcher = Spectator::Matchers::EqualityMatcher.new(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)
matcher = Spectator::Matchers::EqualityMatcher.new(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)
matcher = Spectator::Matchers::EqualityMatcher.new(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)
matcher = Spectator::Matchers::EqualityMatcher.new(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)
matcher = Spectator::Matchers::EqualityMatcher.new(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)
matcher = Spectator::Matchers::EqualityMatcher.new(value)
expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher)
expectation.eval(true).expected_message.should eq(expectation.negated_message)
end
end
end
describe "#satisifed?" do describe "#satisifed?" do
context "with a successful match" do context "with a successful match" do
it "is true" do it "is true" do
value = 42 value = 42
partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value)
matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value)
expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) expectation = Spectator::Expectations::ValueExpectation.new(true, false, partial, matcher)
matcher.match?(partial).should be_true # Sanity check. matcher.match?(partial).should be_true # Sanity check.
expectation.satisfied?.should be_true expectation.satisfied?.should be_true
end end
context "when negated" do
it "is false" 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(true, true, partial, matcher)
matcher.match?(partial).should be_true # Sanity check.
expectation.satisfied?.should be_false
end
end
end end
context "with an unsuccessful match" do context "with an unsuccessful match" do
@ -123,30 +30,118 @@ describe Spectator::Expectations::ValueExpectation do
value2 = 777 value2 = 777
partial = Spectator::Expectations::ValueExpectationPartial.new(value1.to_s, value1) partial = Spectator::Expectations::ValueExpectationPartial.new(value1.to_s, value1)
matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2) matcher = Spectator::Matchers::EqualityMatcher.new(value2.to_s, value2)
expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) expectation = Spectator::Expectations::ValueExpectation.new(false, false, partial, matcher)
matcher.match?(partial).should be_false # Sanity check. matcher.match?(partial).should be_false # Sanity check.
expectation.satisfied?.should be_false expectation.satisfied?.should be_false
end end
context "when negated" do
it "is true" 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(false, true, partial, matcher)
matcher.match?(partial).should be_false # Sanity check.
expectation.satisfied?.should be_true
end
end
end end
end end
describe "#message" do describe "#actual_message" do
it "equals the matcher's #message" do context "with a successful match" do
value = 42 it "equals the matcher's #message" do
partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) value = 42
matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value)
expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value)
expectation.message.should eq(matcher.message(partial)) expectation = Spectator::Expectations::ValueExpectation.new(true, false, partial, matcher)
matcher.match?(partial).should be_true # Sanity check.
expectation.actual_message.should eq(matcher.message(partial))
end
context "when negated" 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(true, true, partial, matcher)
matcher.match?(partial).should be_true # Sanity check.
expectation.actual_message.should eq(matcher.negated_message(partial))
end
end
end
context "with an unsuccessful match" do
it "equals the matcher's #negated_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(false, false, partial, matcher)
matcher.match?(partial).should be_false # Sanity check.
expectation.actual_message.should eq(matcher.negated_message(partial))
end
context "when negated" do
it "equals the matcher's #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(false, true, partial, matcher)
matcher.match?(partial).should be_false # Sanity check.
expectation.actual_message.should eq(matcher.message(partial))
end
end
end end
end end
describe "#negated_message" do describe "#expected_message" do
it "equals the matcher's #negated_message" do context "with a successful match" do
value = 42 it "equals the matcher's #message" do
partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value) value = 42
matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value) partial = Spectator::Expectations::ValueExpectationPartial.new(value.to_s, value)
expectation = Spectator::Expectations::ValueExpectation.new(partial, matcher) matcher = Spectator::Matchers::EqualityMatcher.new(value.to_s, value)
expectation.negated_message.should eq(matcher.negated_message(partial)) expectation = Spectator::Expectations::ValueExpectation.new(true, false, partial, matcher)
matcher.match?(partial).should be_true # Sanity check.
expectation.expected_message.should eq(matcher.message(partial))
end
context "when negated" 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(true, true, partial, matcher)
matcher.match?(partial).should be_true # Sanity check.
expectation.expected_message.should eq(matcher.negated_message(partial))
end
end
end
context "with an unsuccessful match" do
it "equals the matcher's #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(false, false, partial, matcher)
matcher.match?(partial).should be_false # Sanity check.
expectation.expected_message.should eq(matcher.message(partial))
end
context "when negated" do
it "equals the matcher's #negated_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(false, true, partial, matcher)
matcher.match?(partial).should be_false # Sanity check.
expectation.expected_message.should eq(matcher.negated_message(partial))
end
end
end end
end end
end end

View file

@ -19,46 +19,35 @@ end
def new_expectation(expected : ExpectedType = 123, actual : ActualType = 123) forall ExpectedType, ActualType def new_expectation(expected : ExpectedType = 123, actual : ActualType = 123) forall ExpectedType, ActualType
partial = new_partial("foo", actual) partial = new_partial("foo", actual)
matcher = new_matcher("bar", expected) matcher = new_matcher("bar", expected)
Spectator::Expectations::ValueExpectation.new(partial, matcher) matched = matcher.match?(partial)
Spectator::Expectations::ValueExpectation.new(matched, false, partial, matcher)
end end
def new_met_expectation(value : T = 123) forall T def new_satisfied_expectation(value : T = 123) forall T
new_expectation(value, value).tap do |expectation| new_expectation(value, value).tap do |expectation|
expectation.satisfied?.should be_true # Sanity check. expectation.satisfied?.should be_true # Sanity check.
end end
end end
def new_unmet_expectation(expected : ExpectedType = 123, actual : ActualType = 456) forall ExpectedType, ActualType def new_unsatisfied_expectation(expected : ExpectedType = 123, actual : ActualType = 456) forall ExpectedType, ActualType
new_expectation(expected, actual).tap do |expectation| new_expectation(expected, actual).tap do |expectation|
expectation.satisfied?.should be_false # Sanity check. expectation.satisfied?.should be_false # Sanity check.
end end
end end
def new_successful_result def generate_expectations(success_count = 1, failure_count = 0)
new_met_expectation.eval.tap do |result| satisfied = Array.new(success_count) { new_satisfied_expectation }
result.successful?.should be_true # Sanity check. unsatisfied = Array.new(failure_count) { new_unsatisfied_expectation }
end expectations = (satisfied + unsatisfied).shuffle
end
def new_failure_result
new_unmet_expectation.eval.tap do |result|
result.successful?.should be_false # Sanity check.
end
end
def generate_results(success_count = 1, failure_count = 0)
successful = Array.new(success_count) { new_successful_result }
failures = Array.new(failure_count) { new_failure_result }
results = (successful + failures).shuffle
reporter = Spectator::Expectations::ExpectationReporter.new(false) reporter = Spectator::Expectations::ExpectationReporter.new(false)
results.each do |result| expectations.each do |expectation|
reporter.report(result) reporter.report(expectation)
end end
{successful: successful, failures: failures, results: results, reporter: reporter} {satisfied: satisfied, unsatisfied: unsatisfied, expectations: expectations, reporter: reporter}
end end
def report_results(success_count = 1, failure_count = 0) def report_expectations(success_count = 1, failure_count = 0)
harness = Spectator::Internals::Harness.current harness = Spectator::Internals::Harness.current
success_count.times { harness.report_expectation(new_successful_result) } success_count.times { harness.report_expectation(new_satisfied_expectation) }
failure_count.times { harness.report_expectation(new_failure_result) } failure_count.times { harness.report_expectation(new_unsatisfied_expectation) }
end end

View file

@ -7,7 +7,7 @@ class FailingExample < Spectator::RunnableExample
# Run the example that always fails. # Run the example that always fails.
private def run_instance private def run_instance
report_results(0, 1) report_expectations(0, 1)
end end
# Creates a failing example. # Creates a failing example.

View file

@ -21,7 +21,7 @@ class PassingExample < Spectator::RunnableExample
# Run the example that always passes. # Run the example that always passes.
# If this doesn't something broke. # If this doesn't something broke.
private def run_instance private def run_instance
report_results(1, 0) report_expectations(1, 0)
end end
# Creates a passing example. # Creates a passing example.

View file

@ -54,14 +54,14 @@ describe Spectator::Internals::Harness do
context "with a successful result" do context "with a successful result" do
it "stores the result" do it "stores the result" do
error = nil.as(Exception?) error = nil.as(Exception?)
success = new_successful_result expectation = new_satisfied_expectation
spy = SpyExample.create do spy = SpyExample.create do
harness = Spectator::Internals::Harness.current harness = Spectator::Internals::Harness.current
harness.report_expectation(success) harness.report_expectation(expectation)
end end
result = Spectator::Internals::Harness.run(spy) result = Spectator::Internals::Harness.run(spy)
result.should be_a(Spectator::SuccessfulResult) result.should be_a(Spectator::SuccessfulResult)
result.as(Spectator::SuccessfulResult).expectations.should contain(success) result.as(Spectator::SuccessfulResult).expectations.should contain(expectation)
end end
end end
@ -71,7 +71,7 @@ describe Spectator::Internals::Harness do
spy = SpyExample.create do spy = SpyExample.create do
harness = Spectator::Internals::Harness.current harness = Spectator::Internals::Harness.current
begin begin
harness.report_expectation(new_failure_result) harness.report_expectation(new_unsatisfied_expectation)
rescue ex rescue ex
error = ex error = ex
end end
@ -82,30 +82,30 @@ describe Spectator::Internals::Harness do
it "stores the result" do it "stores the result" do
error = nil.as(Exception?) error = nil.as(Exception?)
failure = new_failure_result expectation = new_unsatisfied_expectation
spy = SpyExample.create do spy = SpyExample.create do
harness = Spectator::Internals::Harness.current harness = Spectator::Internals::Harness.current
harness.report_expectation(failure) harness.report_expectation(expectation)
end end
result = Spectator::Internals::Harness.run(spy) result = Spectator::Internals::Harness.run(spy)
result.should be_a(Spectator::FailedResult) result.should be_a(Spectator::FailedResult)
result.as(Spectator::FailedResult).expectations.should contain(failure) result.as(Spectator::FailedResult).expectations.should contain(expectation)
end end
end end
end end
describe "#expectation_results" do describe "#expectation_results" do
it "contains the reported results" do it "contains the reported results" do
results = [new_successful_result, new_failure_result] expectations = [new_satisfied_expectation, new_unsatisfied_expectation]
spy = SpyExample.create do spy = SpyExample.create do
harness = Spectator::Internals::Harness.current harness = Spectator::Internals::Harness.current
results.each do |result| expectations.each do |expectation|
harness.report_expectation(result) harness.report_expectation(expectation)
end end
end end
result = Spectator::Internals::Harness.run(spy) result = Spectator::Internals::Harness.run(spy)
reported_results = result.as(Spectator::FailedResult).expectations.to_a reported_results = result.as(Spectator::FailedResult).expectations.to_a
(results - reported_results).size.should eq(0) (expectations - reported_results).size.should eq(0)
end end
end end
end end

View file

@ -23,7 +23,7 @@ module Spectator::Expectations
# Text that indicates what the outcome was. # Text that indicates what the outcome was.
def actual_message def actual_message
@matched ? message : negated_message satisfied? ? message : negated_message
end end
# Describes the condition that must be met for the matcher to be satisifed. # Describes the condition that must be met for the matcher to be satisifed.