04c1d58a4a
Change-Id: I28fb5cf51c5dae887f19613d874d1ff264954be0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/290912 Reviewed-by: Samuel Rawlins <srawlins@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
39 lines
1.1 KiB
Dart
39 lines
1.1 KiB
Dart
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import 'package:matcher/matcher.dart';
|
|
|
|
typedef _Predicate<T> = bool Function(T value);
|
|
|
|
/// Lightweight expect that can be run outside of a test context.
|
|
mixin ExpectMixin {
|
|
void expect(actual, matcher, {String? reason}) {
|
|
matcher = _wrapMatcher(matcher);
|
|
var matchState = {};
|
|
try {
|
|
if ((matcher as Matcher).matches(actual, matchState)) {
|
|
return;
|
|
}
|
|
} catch (e, trace) {
|
|
reason ??= '$e at $trace';
|
|
}
|
|
throw Exception(reason);
|
|
}
|
|
|
|
static Matcher _wrapMatcher(x) {
|
|
if (x is Matcher) {
|
|
return x;
|
|
} else if (x is _Predicate<Object>) {
|
|
// x is already a predicate that can handle anything
|
|
return predicate(x);
|
|
} else if (x is _Predicate<void>) {
|
|
// x is a unary predicate, but expects a specific type
|
|
// so wrap it.
|
|
return predicate((a) => (x as dynamic)(a) as bool);
|
|
} else {
|
|
return equals(x);
|
|
}
|
|
}
|
|
}
|