092728e8b5
Related to https://dart-review.googlesource.com/c/sdk/+/412383 (Note: presubmit required some re-formatting 🤷) Change-Id: Ib18c7c1c1fceaa0007d804fbcb6397055242fa42 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/412362 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Phil Quitslund <pquitslund@google.com> Auto-Submit: Phil Quitslund <pquitslund@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(Object? actual, Object? 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(Object? 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);
|
|
}
|
|
}
|
|
}
|