Jamil Saadeh 5e74afc4da
Migrate to null aware elements - Part 2 (#172306)
<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

Replace null checks with null aware elements - part 2

Part of #172188

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-07-30 18:32:14 +00:00

103 lines
3.9 KiB
Dart

// Copyright 2014 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.
/// @docImport 'package:integration_test/integration_test.dart';
library;
import 'dart:convert';
import 'dart:io';
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as path;
final String bat = Platform.isWindows ? '.bat' : '';
final String _flutterBin = path.join(Directory.current.parent.parent.path, 'bin', 'flutter$bat');
const String _integrationResultsPrefix = 'IntegrationTestWidgetsFlutterBinding test results:';
const String _failureExcerpt = r'Expected: <false>\n Actual: <true>';
Future<void> main() async {
group('Integration binding result', () {
test('when multiple tests pass', () async {
final Map<String, dynamic>? results = await _runTest(
path.join('test', 'data', 'pass_test_script.dart'),
);
expect(
results,
equals(<String, dynamic>{'passing test 1': 'success', 'passing test 2': 'success'}),
);
});
test('when multiple tests fail', () async {
final Map<String, dynamic>? results = await _runTest(
path.join('test', 'data', 'fail_test_script.dart'),
);
expect(results, hasLength(2));
expect(results, containsPair('failing test 1', contains(_failureExcerpt)));
expect(results, containsPair('failing test 2', contains(_failureExcerpt)));
});
test('when one test passes, then another fails', () async {
final Map<String, dynamic>? results = await _runTest(
path.join('test', 'data', 'pass_then_fail_test_script.dart'),
);
expect(results, hasLength(2));
expect(results, containsPair('passing test', equals('success')));
expect(results, containsPair('failing test', contains(_failureExcerpt)));
});
test('when one test fails, then another passes', () async {
final Map<String, dynamic>? results = await _runTest(
path.join('test', 'data', 'fail_then_pass_test_script.dart'),
);
expect(results, hasLength(2));
expect(results, containsPair('failing test', contains(_failureExcerpt)));
expect(results, containsPair('passing test', equals('success')));
});
});
}
/// Runs a test script and returns the [IntegrationTestWidgetsFlutterBinding.results].
///
/// [scriptPath] is relative to the package root.
Future<Map<String, dynamic>?> _runTest(String scriptPath) async {
final Process process = await Process.start(_flutterBin, <String>[
'test',
'--machine',
scriptPath,
]);
/// In the test [tearDownAll] block, the test results are encoded into JSON and
/// are printed with the [_integrationResultsPrefix] prefix.
///
/// See the following for the test event spec which we parse the printed lines
/// out of: https://github.com/dart-lang/test/blob/master/pkgs/test/doc/json_reporter.md
final String testResults =
(await process.stdout
.transform(utf8.decoder)
.expand((String text) => text.split('\n'))
.map<dynamic>((String line) {
try {
return jsonDecode(line);
} on FormatException {
// Only interested in test events which are JSON.
}
})
.expand<Map<String, dynamic>>((dynamic json) {
if (json is List<dynamic>) {
return json.cast();
}
return <Map<String, dynamic>>[?json as Map<String, dynamic>?];
})
.where((Map<String, dynamic> testEvent) => testEvent['type'] == 'print')
.map((Map<String, dynamic> printEvent) => printEvent['message'] as String)
.firstWhere((String message) => message.startsWith(_integrationResultsPrefix)))
.replaceAll(_integrationResultsPrefix, '');
return jsonDecode(testResults) as Map<String, dynamic>?;
}