2018-10-19 19:54:32 +00:00
|
|
|
# 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)
|
2018-11-14 09:15:55 +00:00
|
|
|
matched = matcher.match?(partial)
|
2018-11-16 18:38:50 +00:00
|
|
|
Spectator::Expectations::ValueExpectation.new(matched, false, partial, matcher).as(Spectator::Expectations::Expectation)
|
2018-10-19 19:54:32 +00:00
|
|
|
end
|
|
|
|
|
2018-11-14 09:15:55 +00:00
|
|
|
def new_satisfied_expectation(value : T = 123) forall T
|
2018-10-20 00:20:42 +00:00
|
|
|
new_expectation(value, value).tap do |expectation|
|
|
|
|
expectation.satisfied?.should be_true # Sanity check.
|
|
|
|
end
|
2018-10-19 19:54:32 +00:00
|
|
|
end
|
|
|
|
|
2018-11-14 09:15:55 +00:00
|
|
|
def new_unsatisfied_expectation(expected : ExpectedType = 123, actual : ActualType = 456) forall ExpectedType, ActualType
|
2018-10-20 00:20:42 +00:00
|
|
|
new_expectation(expected, actual).tap do |expectation|
|
|
|
|
expectation.satisfied?.should be_false # Sanity check.
|
|
|
|
end
|
2018-10-19 19:54:32 +00:00
|
|
|
end
|
|
|
|
|
2018-11-14 09:15:55 +00:00
|
|
|
def generate_expectations(success_count = 1, failure_count = 0)
|
|
|
|
satisfied = Array.new(success_count) { new_satisfied_expectation }
|
|
|
|
unsatisfied = Array.new(failure_count) { new_unsatisfied_expectation }
|
|
|
|
expectations = (satisfied + unsatisfied).shuffle
|
2018-10-19 22:27:59 +00:00
|
|
|
reporter = Spectator::Expectations::ExpectationReporter.new(false)
|
2018-11-14 09:15:55 +00:00
|
|
|
expectations.each do |expectation|
|
|
|
|
reporter.report(expectation)
|
2018-10-19 22:27:59 +00:00
|
|
|
end
|
2018-11-14 09:15:55 +00:00
|
|
|
{satisfied: satisfied, unsatisfied: unsatisfied, expectations: expectations, reporter: reporter}
|
2018-10-19 22:27:59 +00:00
|
|
|
end
|
2018-10-21 14:10:59 +00:00
|
|
|
|
2018-11-14 09:15:55 +00:00
|
|
|
def report_expectations(success_count = 1, failure_count = 0)
|
2018-10-21 14:10:59 +00:00
|
|
|
harness = Spectator::Internals::Harness.current
|
2018-11-14 09:15:55 +00:00
|
|
|
success_count.times { harness.report_expectation(new_satisfied_expectation) }
|
|
|
|
failure_count.times { harness.report_expectation(new_unsatisfied_expectation) }
|
2018-10-21 14:10:59 +00:00
|
|
|
end
|