diff --git a/pkgs/test/test/utils.dart b/pkgs/test/test/utils.dart index d42c3d734..23d5b7e14 100644 --- a/pkgs/test/test/utils.dart +++ b/pkgs/test/test/utils.dart @@ -11,13 +11,10 @@ import 'package:test/test.dart'; import 'package:test_api/src/backend/declarer.dart'; import 'package:test_api/src/backend/group.dart'; import 'package:test_api/src/backend/group_entry.dart'; -import 'package:test_api/src/backend/invoker.dart'; import 'package:test_api/src/backend/live_test.dart'; -import 'package:test_api/src/backend/metadata.dart'; import 'package:test_api/src/backend/platform_selector.dart'; import 'package:test_api/src/backend/runtime.dart'; import 'package:test_api/src/backend/state.dart'; -import 'package:test_api/src/backend/suite.dart'; import 'package:test_api/src/backend/suite_platform.dart'; import 'package:test_core/src/runner/application_exception.dart'; import 'package:test_core/src/runner/compiler_selection.dart'; @@ -35,7 +32,7 @@ import 'package:test_core/src/runner/suite.dart'; final suitePlatform = SuitePlatform(Runtime.vm, compiler: null); // The last state change detected via [expectStates]. -State? lastState; +State? _lastState; /// Asserts that exactly [states] will be emitted via [liveTest.onStateChange]. /// @@ -43,7 +40,7 @@ State? lastState; void expectStates(LiveTest liveTest, Iterable statesIter) { var states = Queue.of(statesIter); liveTest.onStateChange.listen(expectAsync1((state) { - lastState = state; + _lastState = state; expect(state, equals(states.removeFirst())); }, count: states.length, max: states.length)); } @@ -67,37 +64,16 @@ void expectSingleFailure(LiveTest liveTest) { expectErrors(liveTest, [ (error) { - expect(lastState!.status, equals(Status.complete)); - expect(error, isTestFailure('oh no')); + expect(_lastState!.status, equals(Status.complete)); + expect(error, _isTestFailure('oh no')); } ]); } -/// Asserts that [liveTest] will have a single error, the string `"oh no"`. -void expectSingleError(LiveTest liveTest) { - expectStates(liveTest, [ - const State(Status.running, Result.success), - const State(Status.complete, Result.error) - ]); - - expectErrors(liveTest, [ - (error) { - expect(lastState!.status, equals(Status.complete)); - expect(error, equals('oh no')); - } - ]); -} - -/// Returns a matcher that matches a callback or Future that throws a -/// [TestFailure] with the given [message]. -/// -/// [message] can be a string or a [Matcher]. -Matcher throwsTestFailure(Object message) => throwsA(isTestFailure(message)); - /// Returns a matcher that matches a [TestFailure] with the given [message]. /// /// [message] can be a string or a [Matcher]. -Matcher isTestFailure(Object message) => const TypeMatcher() +Matcher _isTestFailure(Object message) => const TypeMatcher() .having((e) => e.message, 'message', message); /// Returns a matcher that matches a [ApplicationException] with the given @@ -108,22 +84,6 @@ Matcher isApplicationException(Object message) => const TypeMatcher() .having((e) => e.message, 'message', message); -/// Returns a local [LiveTest] that runs [body]. -LiveTest createTest(dynamic Function() body) { - var test = LocalTest('test', Metadata(chainStackTraces: true), body); - var suite = Suite(Group.root([test]), suitePlatform, ignoreTimeouts: false); - return test.load(suite); -} - -/// Runs [body] as a test. -/// -/// Once it completes, returns the [LiveTest] used to run it. -Future runTestBody(dynamic Function() body) async { - var liveTest = createTest(body); - await liveTest.run(); - return liveTest; -} - /// Asserts that [liveTest] has completed and passed. /// /// If the test had any errors, they're surfaced nicely into the outer test. @@ -147,46 +107,7 @@ void expectTestFailed(LiveTest liveTest, Object message) { expect(liveTest.state.status, equals(Status.complete)); expect(liveTest.state.result, equals(Result.failure)); expect(liveTest.errors, hasLength(1)); - expect(liveTest.errors.first.error, isTestFailure(message)); -} - -/// Assert that the [test] callback causes a test to block until [stopBlocking] -/// is called at some later time. -/// -/// [stopBlocking] is passed the return value of [test]. -Future expectTestBlocks( - dynamic Function() test, dynamic Function(dynamic) stopBlocking) async { - late LiveTest liveTest; - late Future future; - liveTest = createTest(() { - var value = test(); - future = pumpEventQueue().then((_) { - expect(liveTest.state.status, equals(Status.running)); - stopBlocking(value); - }); - }); - - await liveTest.run(); - expectTestPassed(liveTest); - // Ensure that the outer test doesn't complete until the inner future - // completes. - return future; -} - -/// Runs [body] with a declarer, runs all the declared tests, and asserts that -/// they pass. -/// -/// This is typically used to run multiple tests where later tests make -/// assertions about the results of previous ones. -Future expectTestsPass(void Function() body) async { - var engine = declareEngine(body); - var success = await engine.run(); - - for (var test in engine.liveTests) { - expectTestPassed(test); - } - - expect(success, isTrue); + expect(liveTest.errors.first.error, _isTestFailure(message)); } /// Runs [body] with a declarer and returns the declared entries.