Skip to content

Commit

Permalink
Fix test in preparation of the Dart VM dropping support for language …
Browse files Browse the repository at this point in the history
…versions < 2.12.0 (#115176)

* Fix test in preparation of the Dart VM switching to being null safe by
default.

* Fix analyze error.

* Format.

* Remove print.

* Fix analyzer lint.

* Fix warnings.
  • Loading branch information
a-siva authored Nov 14, 2022
1 parent f2ec1c4 commit ea4e11d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart=2.9
// Running in unsound null-safety mode is intended to test for potential miscasts
// or invalid assertions.

import 'package:flutter/src/foundation/_isolates_io.dart';
import 'package:flutter/src/foundation/isolates.dart' as isolates;


int returnInt(int arg) {
int? returnInt(int? arg) {
return arg;
}

Future<int> returnIntAsync(int arg) {
Future<int?> returnIntAsync(int? arg) {
return Future<int>.value(arg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// @dart=2.9
// Running in unsound null-safety mode is intended to test for potential miscasts
// or invalid assertions.

import 'package:flutter/src/foundation/_isolates_io.dart';

int throwNull(int arg) {
throw null;
int throwNull(dynamic arg) {
throw arg as Object;
}

void main() async {
try {
await compute(throwNull, null);
} catch (e) {
if (e is! NullThrownError) {
if (e is! TypeError && e is! NullThrownError) {
throw Exception('compute returned bad result');
}
}
Expand Down
29 changes: 20 additions & 9 deletions packages/flutter/test/foundation/isolates_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,25 @@ Future<int> test5CallCompute(int value) {
return compute(test5, value);
}

Future<void> expectFileSuccessfullyCompletes(String filename) async {
Future<void> expectFileSuccessfullyCompletes(String filename,
[bool unsound = false]) async {
// Run a Dart script that calls compute().
// The Dart process will terminate only if the script exits cleanly with
// all isolate ports closed.
const FileSystem fs = LocalFileSystem();
const Platform platform = LocalPlatform();
final String flutterRoot = platform.environment['FLUTTER_ROOT']!;
final String dartPath = fs.path.join(flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', 'dart');
final String dartPath =
fs.path.join(flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', 'dart');
final String packageRoot = fs.path.dirname(fs.path.fromUri(platform.script));
final String scriptPath = fs.path.join(packageRoot, 'test', 'foundation', filename);
final String scriptPath =
fs.path.join(packageRoot, 'test', 'foundation', filename);
final String nullSafetyArg =
unsound ? '--no-sound-null-safety' : '--sound-null-safety';

// Enable asserts to also catch potentially invalid assertions.
final ProcessResult result = await Process.run(dartPath, <String>['run', '--enable-asserts', scriptPath]);
final ProcessResult result = await Process.run(
dartPath, <String>[nullSafetyArg, 'run', '--enable-asserts', scriptPath]);
expect(result.exitCode, 0);
}

Expand Down Expand Up @@ -194,7 +200,8 @@ void main() {
expect(await computeInstanceMethod(10), 100);
expect(computeInvalidInstanceMethod(10), throwsArgumentError);

expect(await compute(testDebugName, null, debugLabel: 'debug_name'), 'debug_name');
expect(await compute(testDebugName, null, debugLabel: 'debug_name'),
'debug_name');
expect(await compute(testReturnNull, null), null);
}, skip: kIsWeb); // [intended] isn't supported on the web.

Expand All @@ -203,22 +210,26 @@ void main() {
await expectFileSuccessfullyCompletes('_compute_caller.dart');
});
test('with invalid message', () async {
await expectFileSuccessfullyCompletes('_compute_caller_invalid_message.dart');
await expectFileSuccessfullyCompletes(
'_compute_caller_invalid_message.dart');
});
test('with valid error', () async {
await expectFileSuccessfullyCompletes('_compute_caller.dart');
});
test('with invalid error', () async {
await expectFileSuccessfullyCompletes('_compute_caller_invalid_message.dart');
await expectFileSuccessfullyCompletes(
'_compute_caller_invalid_message.dart');
});
}, skip: kIsWeb); // [intended] isn't supported on the web.

group('compute() works with unsound null safety caller', () {
test('returning', () async {
await expectFileSuccessfullyCompletes('_compute_caller_unsound_null_safety.dart');
await expectFileSuccessfullyCompletes(
'_compute_caller_unsound_null_safety.dart', true);
});
test('erroring', () async {
await expectFileSuccessfullyCompletes('_compute_caller_unsound_null_safety_error.dart');
await expectFileSuccessfullyCompletes(
'_compute_caller_unsound_null_safety_error.dart', true);
});
}, skip: kIsWeb); // [intended] isn't supported on the web.
}

0 comments on commit ea4e11d

Please sign in to comment.