0190d20bd6
Change-Id: I0a37352c903bf7139af25aab255fe6a5331ce98b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194020 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@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.
|
|
class 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<Null>) {
|
|
// x is a unary predicate, but expects a specific type
|
|
// so wrap it.
|
|
return predicate((a) => (x as dynamic)(a));
|
|
} else {
|
|
return equals(x);
|
|
}
|
|
}
|
|
}
|