Skip to content

Commit f8086c8

Browse files
lrhnCommit Queue
authored andcommitted
Collect all test-related files in package:expect.
Collects files from `package:async_helper` and `tests/language` that are generally useful, so that all test-related helpers are in `package:expect`. Moves the two libraries from `package:async_helper` into `package:expect`, and the `tests/language/static_type_helper.dart` file too. Deprecates `async_minitest.dart`, to follow `minitest.dart`, expecting the Flutter use of it to have been fixed to not break on deprecation (I believe Flutter no longer breaks builds on deprecations at all). Patch 1 is the actual change. Patch 2+4+8 is changing all existing references to the files. Patch 6 ignores deprecation in files still using `async_minitest.dart`. 3+5+7+9 are updating this text to make the numbers match. Then it's just test-expectations and small tweaks from there. Change-Id: I1b665135b5fef9b9a0c3b340ffe9daf874d0174c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/373120 Reviewed-by: Nate Bosch <[email protected]> Reviewed-by: Devon Carew <[email protected]> Commit-Queue: Lasse Nielsen <[email protected]>
1 parent b68b86b commit f8086c8

File tree

1,302 files changed

+2339
-2343
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,302 files changed

+2339
-2343
lines changed

docs/Testing.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,12 @@ some failed way that the test runner can detect and determine that a runtime
495495
error occurred.
496496

497497
If you are writing asynchronous tests, there is a separate tiny
498-
["async_helper"][async pkg] package that talks to the test runner to ensure all
498+
["async_helper"][async helper] library that talks to the test runner to ensure all
499499
asynchronous operations performed by the test have a chance to complete.
500500

501-
[async pkg]: https://github.com/dart-lang/sdk/tree/main/pkg/async_helper
501+
[async helper]: https://github.com/dart-lang/sdk/tree/main/pkg/expect/lib/async_helper.dart
502502

503-
With these two packages, it's straightforward to write tests of expected correct
503+
With these two libraries, it's straightforward to write tests of expected correct
504504
runtime behavior. You can also write tests for validating runtime *failures* by
505505
using the helper functions for checking that certain exceptions are thrown:
506506

pkg/async_helper/lib/async_helper.dart

Lines changed: 3 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -2,156 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// This library is used for testing asynchronous tests.
6-
/// If a test is asynchronous, it needs to notify the testing driver
7-
/// about this (otherwise tests may get reported as passing after main()
8-
/// finished even if the asynchronous operations fail).
9-
///
10-
/// This library provides four methods
11-
/// - asyncStart(): Needs to be called before an asynchronous operation is
12-
/// scheduled.
13-
/// - asyncEnd(): Needs to be called as soon as the asynchronous operation
14-
/// ended.
15-
/// - asyncSuccess(_): Variant of asyncEnd useful together with Future.then.
16-
/// - asyncTest(f()): Helper method that wraps a computation that returns a
17-
/// Future with matching calls to asyncStart() and
18-
/// asyncSuccess(_).
19-
/// After every asyncStart() called is matched with a corresponding
20-
/// asyncEnd() or asyncSuccess(_) call, the testing driver will be notified that
21-
/// the tests is done.
5+
// Will be deprecated or removed when possible
6+
// TODO: @Deprecated('Use this library directly from package:expect instead')
227
library;
238

24-
import 'dart:async';
25-
26-
import 'package:expect/expect.dart';
27-
28-
bool _initialized = false;
29-
int _asyncLevel = 0;
30-
31-
Exception _buildException(String msg) {
32-
return Exception('Fatal: $msg. This is most likely a bug in your test.');
33-
}
34-
35-
/// Call this method before an asynchronous test is created.
36-
///
37-
/// If [count] is provided, expect [count] [asyncEnd] calls instead of just one.
38-
void asyncStart([int count = 1]) {
39-
if (count <= 0) return;
40-
if (_initialized && _asyncLevel == 0) {
41-
throw _buildException('asyncStart() was called even though we are done '
42-
'with testing.');
43-
}
44-
if (!_initialized) {
45-
print('unittest-suite-wait-for-done');
46-
_initialized = true;
47-
}
48-
_asyncLevel += count;
49-
}
50-
51-
/// Call this after an asynchronous test has ended successfully.
52-
void asyncEnd() {
53-
if (_asyncLevel <= 0) {
54-
if (!_initialized) {
55-
throw _buildException('asyncEnd() was called before asyncStart().');
56-
} else {
57-
throw _buildException('asyncEnd() was called more often than '
58-
'asyncStart().');
59-
}
60-
}
61-
_asyncLevel--;
62-
if (_asyncLevel == 0) {
63-
print('unittest-suite-success');
64-
}
65-
}
66-
67-
/// Call this after an asynchronous test has ended successfully. This is a helper
68-
/// for calling [asyncEnd].
69-
///
70-
/// This method intentionally has a signature that matches `Future.then` as a
71-
/// convenience for calling [asyncEnd] when a `Future` completes without error,
72-
/// like this:
73-
/// ```dart
74-
/// asyncStart();
75-
/// Future result = test();
76-
/// result.then(asyncSuccess);
77-
/// ```
78-
void asyncSuccess(void _) {
79-
asyncEnd();
80-
}
81-
82-
/// Helper method for performing asynchronous tests involving `Future`.
83-
///
84-
/// The function [test] must return a `Future` which completes without error
85-
/// when the test is successful.
86-
Future<void> asyncTest(Function() test) {
87-
asyncStart();
88-
return test().then(asyncSuccess);
89-
}
90-
91-
/// Verifies that the asynchronous [result] throws a [T].
92-
///
93-
/// Fails if [result] completes with a value, or it completes with
94-
/// an error which is not a [T].
95-
///
96-
/// Returns the accepted thrown object.
97-
/// For example, to check the content of the thrown object,
98-
/// you could write this:
99-
/// ```
100-
/// var e = await asyncExpectThrows<MyException>(asyncExpression)
101-
/// Expect.isTrue(e.myMessage.contains("WARNING"));
102-
/// ```
103-
/// If `result` completes with an [ExpectException] error from another
104-
/// failed test expectation, that error cannot be caught and accepted.
105-
Future<T> asyncExpectThrows<T extends Object>(Future<void> result,
106-
[String reason = ""]) {
107-
// Delay computing the header text until the test has failed.
108-
// The header computation uses complicated language features,
109-
// and language tests should avoid doing complicated things
110-
// until after the actual test has had a chance to succeed.
111-
String header() {
112-
// Handle null being passed in from legacy code
113-
// while also avoiding producing an unnecessary null check warning here.
114-
if ((reason as dynamic) == null) reason = "";
115-
// Only include the type in the message if it's not `Object`.
116-
var type = Object() is! T ? "<$T>" : "";
117-
return "asyncExpectThrows$type($reason):";
118-
}
119-
120-
// Unsound null-safety check.
121-
if ((result as dynamic) == null) {
122-
Expect.testError("${header()} result Future must not be null.");
123-
}
124-
125-
// TODO(rnystrom): It might useful to validate that T is not bound to
126-
// ExpectException since that won't work.
127-
128-
asyncStart();
129-
return result.then<T>((_) {
130-
throw ExpectException("${header()} Did not throw.");
131-
}, onError: (error, stack) {
132-
// A test failure doesn't count as throwing. Rethrow it.
133-
if (error is ExpectException) throw error;
134-
135-
if (error is! T) {
136-
// Throws something unexpected.
137-
throw ExpectException(
138-
"${header()} Unexpected '${Error.safeToString(error)}'\n$stack");
139-
}
140-
141-
asyncEnd();
142-
return error;
143-
});
144-
}
145-
146-
/// Checks that the asynchronous [result] throws a [T] if and only if
147-
/// [condition] is `true`.
148-
///
149-
/// When [condition] is `false`, [result] is expected to complete without
150-
/// errors.
151-
Future<T?> asyncExpectThrowsWhen<T extends Object>(
152-
bool condition, Future<void> result,
153-
[String reason = ""]) {
154-
return condition
155-
? asyncExpectThrows<T>(result, reason)
156-
: result.then<T?>((_) => null);
157-
}
9+
export 'package:expect/async_helper.dart';

0 commit comments

Comments
 (0)