expect function
Asserts that actual satisfies matcher.
matcher may be a Matcher or a raw value (wrapped via wrapMatcher,
so expect(x, 3) means expect(x, equals(3))). Throws a TestFailure
with a formatted message on mismatch. Unlike package:test's expect,
this works outside a test runner zone.
Implementation
void expect(dynamic actual, dynamic matcher, {String? reason}) {
final m = wrapMatcher(matcher);
final matchState = <dynamic, dynamic>{};
if (m.matches(actual, matchState)) return;
final description = StringDescription()
..add('Expected: ')
..addDescriptionOf(m)
..add('\n Actual: ')
..addDescriptionOf(actual);
final mismatch = StringDescription();
m.describeMismatch(actual, mismatch, matchState, false);
if (mismatch.length > 0) {
description
..add('\n Which: ')
..add(mismatch.toString());
}
if (reason != null) {
description.add('\n$reason');
}
fail(description.toString());
}