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
|
|
|
|
|
2019-02-28 22:17:12 +00:00
|
|
|
def new_matcher(expected : T, label : String) forall T
|
|
|
|
Spectator::Matchers::EqualityMatcher.new(expected, label)
|
2018-10-19 19:54:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_matcher(expected : T = 123) forall T
|
2019-02-28 22:17:12 +00:00
|
|
|
Spectator::Matchers::EqualityMatcher.new(expected)
|
2018-10-19 19:54:32 +00:00
|
|
|
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")
|
2019-02-28 22:17:12 +00:00
|
|
|
matcher = new_matcher(expected, "bar")
|
2019-02-27 03:29:04 +00:00
|
|
|
match_data = matcher.match(partial)
|
|
|
|
Spectator::Expectations::Expectation.new(match_data, false)
|
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
|
|
|
|
|
2019-02-28 05:20:57 +00:00
|
|
|
def create_expectations(success_count = 1, failure_count = 0)
|
2019-02-28 22:18:56 +00:00
|
|
|
satisfied = Array.new(success_count) { new_satisfied_expectation }
|
|
|
|
unsatisfied = Array.new(failure_count) { new_unsatisfied_expectation }
|
2019-02-28 05:20:57 +00:00
|
|
|
(satisfied + unsatisfied).shuffle
|
|
|
|
end
|
|
|
|
|
2018-11-14 09:15:55 +00:00
|
|
|
def generate_expectations(success_count = 1, failure_count = 0)
|
2019-02-28 22:18:56 +00:00
|
|
|
satisfied = Array.new(success_count) { new_satisfied_expectation }
|
|
|
|
unsatisfied = Array.new(failure_count) { new_unsatisfied_expectation }
|
2018-11-14 09:15:55 +00:00
|
|
|
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
|