2018-10-19 19:54:32 +00:00
|
|
|
# Utility methods for creating expectations, partials, and matchers.
|
|
|
|
|
2019-02-15 00:04:59 +00:00
|
|
|
def new_partial(actual : T, label : String) forall T
|
|
|
|
Spectator::Expectations::ValueExpectationPartial.new(actual, label, __FILE__, __LINE__)
|
2018-10-19 19:54:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_partial(actual : T = 123) forall T
|
2019-02-15 00:04:59 +00:00
|
|
|
Spectator::Expectations::ValueExpectationPartial.new(actual, __FILE__, __LINE__)
|
2018-10-19 19:54:32 +00:00
|
|
|
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
|
2019-02-15 00:04:59 +00:00
|
|
|
partial = new_partial(actual, "foo")
|
2018-10-19 19:54:32 +00:00
|
|
|
matcher = new_matcher("bar", expected)
|
2018-11-14 09:15:55 +00:00
|
|
|
matched = matcher.match?(partial)
|
2019-01-31 20:30:09 +00:00
|
|
|
Spectator::Expectations::Expectation.new(matched, false, partial, matcher)
|
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
|