diff --git a/engine/src/flutter/shell/platform/fuchsia/dart/BUILD.gn b/engine/src/flutter/shell/platform/fuchsia/dart/BUILD.gn index c6c5244f9f..6586e16d4c 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart/BUILD.gn +++ b/engine/src/flutter/shell/platform/fuchsia/dart/BUILD.gn @@ -114,6 +114,7 @@ dart_library("litetest") { sources = [ "litetest.dart", + "src/async_minitest.dart", "src/matchers.dart", "src/test.dart", "src/test_suite.dart", diff --git a/engine/src/flutter/testing/litetest/lib/litetest.dart b/engine/src/flutter/testing/litetest/lib/litetest.dart index a4d9873d58..b7eb5b388b 100644 --- a/engine/src/flutter/testing/litetest/lib/litetest.dart +++ b/engine/src/flutter/testing/litetest/lib/litetest.dart @@ -2,10 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:expect/variations.dart' as v; + import 'src/test_suite.dart'; -export 'package:async_helper/async_minitest.dart' hide test; export 'package:expect/expect.dart'; + +export 'src/async_minitest.dart' hide test; export 'src/matchers.dart'; final TestSuite _testSuite = TestSuite(); @@ -21,3 +24,6 @@ void test( }) { _testSuite.test(name, body, skip: skip); } + +/// Whether asserts are enabled. +final bool assertStatementsEnabled = v.enabledAsserts; diff --git a/engine/src/flutter/testing/litetest/lib/src/async_minitest.dart b/engine/src/flutter/testing/litetest/lib/src/async_minitest.dart new file mode 100644 index 0000000000..91b3eba48e --- /dev/null +++ b/engine/src/flutter/testing/litetest/lib/src/async_minitest.dart @@ -0,0 +1,353 @@ +// Copyright 2018 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// ignore_for_file: public_member_api_docs + +/// A minimal, dependency-free emulation layer for a subset of the +/// unittest/test API used by the language and core library. +/// +/// Compared with `minitest.dart`, this library supports and expects +/// asynchronous tests. It uses a `Zone` per test to associate a test name with +/// the failure. +/// +/// It does not support `setUp` or `tearDown` methods, +/// and matchers are severely restricted. +/// +/// A number of our language and core library tests were written against the +/// unittest package, which is now deprecated in favor of the new test package. +/// The latter is much better feature-wise, but is also quite complex and has +/// many dependencies. For low-level language and core library tests, we don't +/// want to have to pull in a large number of dependencies and be able to run +/// them correctly in order to run a test, so we want to test them against +/// something simpler. +/// +/// When possible, we just use the tiny expect library. But to avoid rewriting +/// all of the existing tests that use unittest, they can instead use this, +/// which shims the unittest API with as little code as possible and calls into +/// expect. +/// +/// Eventually, it would be good to refactor those tests to use the expect +/// package directly and remove this. +library; + +import 'dart:async'; + +import 'package:async_helper/async_helper.dart'; +import 'package:expect/expect.dart'; + +void group(String name, void Function() body) { + final oldName = _pushName(name); + try { + body(); + } finally { + _popName(oldName); + } +} + +void test(String name, dynamic Function() body) { + final oldName = _pushName(name); + + asyncStart(); + final result = runZoned(body, zoneValues: {_testToken: _currentName}); + if (result is Future) { + result.then((_) { + asyncEnd(); + }); + } else { + // Ensure all tests get to be set up first. + scheduleMicrotask(asyncEnd); + } + + _popName(oldName); +} + +void expect(dynamic value, dynamic matcher, {String reason = ''}) { + Matcher m; + if (matcher is _Matcher) { + m = matcher.call; + } else if (matcher is! Matcher) { + m = equals(matcher); + } else { + m = matcher; + } + m(value); +} + +R Function() expectAsync0(R Function() f, {int count = 1}) { + asyncStart(count); + return () { + final result = f(); + asyncEnd(); + return result; + }; +} + +R Function(A) expectAsync1(R Function(A) f, {int count = 1}) { + asyncStart(count); + return (A a) { + final result = f(a); + asyncEnd(); + return result; + }; +} + +R Function(A, B) expectAsync2(R Function(A, B) f, {int count = 1}) { + asyncStart(count); + return (A a, B b) { + final result = f(a, b); + asyncEnd(); + return result; + }; +} + +dynamic expectAsync(Function f, {int count = 1}) { + final f2 = f; // Avoid type-promoting f, we want dynamic invocations. + if (f2 is void Function(Never, Never, Never, Never, Never)) { + asyncStart(count); + return ([Object? a, Object? b, Object? c, Object? d, Object? e]) { + final result = f(a, b, c, d, e); // ignore: avoid_dynamic_calls + asyncEnd(); + return result; + }; + } + if (f2 is void Function(Never, Never, Never, Never)) { + asyncStart(count); + return ([Object? a, Object? b, Object? c, Object? d]) { + final result = f(a, b, c, d); // ignore: avoid_dynamic_calls + asyncEnd(); + return result; + }; + } + if (f2 is void Function(Never, Never, Never)) { + asyncStart(count); + return ([Object? a, Object? b, Object? c]) { + final result = f(a, b, c); // ignore: avoid_dynamic_calls + asyncEnd(); + return result; + }; + } + if (f2 is void Function(Never, Never)) { + asyncStart(count); + return ([Object? a, Object? b]) { + final result = f(a, b); // ignore: avoid_dynamic_calls + asyncEnd(); + return result; + }; + } + if (f2 is void Function(Never)) { + asyncStart(count); + return ([Object? a]) { + final result = f(a); // ignore: avoid_dynamic_calls + asyncEnd(); + return result; + }; + } + if (f2 is void Function()) { + asyncStart(count); + return () { + final result = f2(); + asyncEnd(); + return result; + }; + } + throw UnsupportedError( + 'expectAsync only accepts up to five argument functions'); +} + +// Matchers +typedef Matcher = void Function(dynamic); + +Matcher same(dynamic o) => (v) { + Expect.identical(o, v); + }; + +Matcher equals(dynamic o) => (v) { + Expect.deepEquals(o, v); + }; + +Matcher greaterThan(num n) => (dynamic v) { + Expect.type(v); + final num value = v as num; + if (value > n) { + return; + } + Expect.fail('$v is not greater than $n'); + }; + +Matcher greaterThanOrEqualTo(num n) => (dynamic v) { + Expect.type(v); + final num value = v as num; + if (value >= n) { + return; + } + Expect.fail('$v is not greater than $n'); + }; + +Matcher lessThan(num n) => (dynamic v) { + Expect.type(v); + final num value = v as num; + if (value < n) { + return; + } + Expect.fail('$v is not less than $n'); + }; + +Matcher lessThanOrEqualTo(num n) => (dynamic v) { + Expect.type(v); + final num value = v as num; + if (value <= n) { + return; + } + Expect.fail('$v is not less than $n'); + }; + +Matcher predicate(bool Function(dynamic value) fn, [String description = '']) => + (dynamic v) { + Expect.isTrue(fn(v), description); + }; + +Matcher anyOf(List expected) => (dynamic actual) { + for (final string in expected) { + if (actual == string) { + return; + } + } + + Expect.fail('Expected $actual to be one of $expected.'); + }; + +void isTrue(dynamic v) { + Expect.isTrue(v); +} + +void isFalse(dynamic v) { + Expect.isFalse(v); +} + +void isNull(dynamic o) { + Expect.isNull(o); +} + +void _checkThrow( + dynamic v, void Function(dynamic error) onError) { + if (v is Future) { + asyncStart(); + v.then((_) { + Expect.fail('Did not throw'); + }, onError: (Object e, s) { + if (e is! T) { + throw e; + } + onError(e); + asyncEnd(); + }); + return; + } + Expect.throws(v as void Function(), (dynamic e) { + onError(e); + return true; + }); +} + +void returnsNormally(dynamic o) { + try { + Expect.type(o); + (o as void Function())(); + } catch (error, trace) { + Expect.fail( + 'Expected function to return normally, but threw:\n$error\n\n$trace'); + } +} + +void throws(dynamic v) { + _checkThrow(v, (_) {}); +} + +Matcher throwsA(dynamic matcher) => (dynamic o) { + _checkThrow(o, (error) { + expect(error, matcher); + }); + }; + +Matcher completion(dynamic matcher) => (dynamic o) { + Expect.type>(o); + final Future future = o as Future; + asyncStart(); + future.then((value) { + expect(value, matcher); + asyncEnd(); + }); + }; + +void completes(dynamic o) { + Expect.type>(o); + final Future future = o as Future; + asyncStart(); + future.then(asyncSuccess); +} + +void isMap(dynamic o) { + Expect.type>(o); +} + +void isList(dynamic o) { + Expect.type>(o); +} + +void isNotNull(dynamic o) { + Expect.isNotNull(o); +} + +abstract class _Matcher { + void call(dynamic o); +} + +// ignore: camel_case_types +class isInstanceOf implements _Matcher { + @override + void call(dynamic o) { + Expect.type(o); + } +} + +void throwsArgumentError(dynamic v) { + _checkThrow(v, (_) {}); +} + +void throwsStateError(dynamic v) { + _checkThrow(v, (_) {}); +} + +void fail(String message) { + Expect.fail(message); +} + +/// Key for zone value holding current test name. +final _testToken = Object(); + +bool _initializedTestNameCallback = false; + +/// The current combined name of the nesting [group] or [test]. +String _currentName = ''; + +String _pushName(String newName) { + // Look up the current test name from the zone created for the test. + if (!_initializedTestNameCallback) { + ExpectException.setTestNameCallback( + () => Zone.current[_testToken] as String); + _initializedTestNameCallback = true; + } + + final oldName = _currentName; + if (oldName == '') { + _currentName = newName; + } else { + _currentName = '$oldName $newName'; + } + return oldName; +} + +void _popName(String oldName) { + _currentName = oldName; +} diff --git a/engine/src/flutter/testing/litetest/lib/src/matchers.dart b/engine/src/flutter/testing/litetest/lib/src/matchers.dart index ba30a1620c..a48f533e47 100644 --- a/engine/src/flutter/testing/litetest/lib/src/matchers.dart +++ b/engine/src/flutter/testing/litetest/lib/src/matchers.dart @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:async_helper/async_minitest.dart'; import 'package:expect/expect.dart'; +import 'async_minitest.dart'; /// The epsilon of tolerable double precision error. /// diff --git a/engine/src/flutter/testing/litetest/lib/src/test.dart b/engine/src/flutter/testing/litetest/lib/src/test.dart index 7a3f908bfa..ae48d0c3c2 100644 --- a/engine/src/flutter/testing/litetest/lib/src/test.dart +++ b/engine/src/flutter/testing/litetest/lib/src/test.dart @@ -6,8 +6,8 @@ import 'dart:async'; import 'dart:io' show stdout; import 'dart:isolate'; -import 'package:async_helper/async_minitest.dart' as m; import 'package:expect/expect.dart' as e; +import 'async_minitest.dart' as m; /// The state that each [Test] may be in. enum TestState { diff --git a/engine/src/flutter/testing/litetest/test/litetest_test.dart b/engine/src/flutter/testing/litetest/test/litetest_test.dart index 5401fa57c1..61e63df06f 100644 --- a/engine/src/flutter/testing/litetest/test/litetest_test.dart +++ b/engine/src/flutter/testing/litetest/test/litetest_test.dart @@ -7,7 +7,7 @@ import 'dart:collection'; import 'dart:isolate'; import 'package:async_helper/async_helper.dart'; -import 'package:async_helper/async_minitest.dart'; +import 'package:litetest/src/async_minitest.dart'; import 'package:litetest/src/test.dart'; import 'package:litetest/src/test_suite.dart';