Skip to content

Commit

Permalink
Remove some unused testing utilities (#2413)
Browse files Browse the repository at this point in the history
This file was forked during the package split and some of the APIs are
unused in the test package tests. Remove or make private all members of
the utils library that are unused in other test suites.
  • Loading branch information
natebosch authored Oct 29, 2024
1 parent fe38650 commit 9a267de
Showing 1 changed file with 6 additions and 85 deletions.
91 changes: 6 additions & 85 deletions pkgs/test/test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -35,15 +32,15 @@ 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].
///
/// The most recent emitted state is stored in [_lastState].
void expectStates(LiveTest liveTest, Iterable<State> 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));
}
Expand All @@ -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<TestFailure>()
Matcher _isTestFailure(Object message) => const TypeMatcher<TestFailure>()
.having((e) => e.message, 'message', message);

/// Returns a matcher that matches a [ApplicationException] with the given
Expand All @@ -108,22 +84,6 @@ Matcher isApplicationException(Object message) =>
const TypeMatcher<ApplicationException>()
.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<LiveTest> 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.
Expand All @@ -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<void> expectTestBlocks(
dynamic Function() test, dynamic Function(dynamic) stopBlocking) async {
late LiveTest liveTest;
late Future<void> 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<void> 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.
Expand Down

0 comments on commit 9a267de

Please sign in to comment.