flutter/dev/integration_tests
Nate Wilson 5ecf10052f
pattern-matching refactor (#154753)
This pull request aims to improve code readability, based on feedback gathered in a recent design doc.

<br>

There are two factors that hugely impact how easy it is to understand a piece of code: **verbosity** and **complexity**.

Reducing **verbosity** is important, because boilerplate makes a project more difficult to navigate. It also has a tendency to make one's eyes gloss over, and subtle typos/bugs become more likely to slip through.

Reducing **complexity** makes the code more accessible to more people. This is especially important for open-source projects like Flutter, where the code is read by those who make contributions, as well as others who read through source code as they debug their own projects.

<hr>

<br>

The following examples show how pattern-matching might affect these two factors:

<details> <summary><h3>Example 1 (GOOD)</h3> [click to expand]</summary>

```dart
if (ancestor case InheritedElement(:final InheritedTheme widget)) {
  themes.add(widget);
}
```

Without using patterns, this might expand to

```dart
if (ancestor is InheritedElement) {
  final InheritedWidget widget = ancestor.widget;
  if (widget is InheritedTheme) {
    themes.add(widget);
  }
}
```

Had `ancestor` been a non-local variable, it would need to be "converted" as well:

```dart
final Element ancestor = this.ancestor;
if (ancestor is InheritedElement) {
  final InheritedWidget inheritedWidget = ancestor.widget;
  if (widget is InheritedTheme) {
    themes.add(theme);
  }
}
```

</details>

<details> <summary><h3>Example 2 (BAD) </h3> [click to expand]</summary>

```dart
if (widget case PreferredSizeWidget(preferredSize: Size(:final double height))) {
  return height;
}
```

Assuming `widget` is a non-local variable, this would expand to:

```dart
final Widget widget = this.widget;
if (widget is PreferredSizeWidget) {
  return widget.preferredSize.height;
}
```

<br>

</details>

In both of the examples above, an `if-case` statement simultaneously verifies that an object meets the specified criteria and performs a variable assignment accordingly.

But there are some differences: Example 2 uses a more deeply-nested pattern than Example 1 but makes fewer useful checks.

**Example 1:**
- checks that `ancestor` is an `InheritedElement`
- checks that the inherited element's `widget` is an `InheritedTheme`

**Example 2:**
- checks that `widget` is a `PreferredSizeWidget`
(every `PreferredSizeWidget` has a `size` field, and every `Size` has a `height` field)

<br>

<hr>

I feel hesitant to try presenting a set of cut-and-dry rules as to which scenarios should/shouldn't use pattern-matching, since there are an abundance of different types of patterns, and an abundance of different places where they might be used.

But hopefully the conversations we've had recently will help us converge toward a common intuition of how pattern-matching can best be utilized for improved readability.

<br><br>

- resolves https://github.com/flutter/flutter/issues/152313
- Design Doc: [flutter.dev/go/dart-patterns](https://flutter.dev/go/dart-patterns)
2024-10-03 18:21:04 +00:00
..
abstract_method_smoke_test Manual roll to 48ddaf578fb0c8326d5b4b680b0f49ea72e33216 (#155070) 2024-09-12 17:58:12 +00:00
android_custom_host_app Reland "Update template/test compileSdk, targetSdk, ndk versions" (#153795) 2024-08-21 18:27:00 +00:00
android_embedding_v2_smoke_test Roll pub packages (#155846) 2024-10-01 22:45:08 +00:00
android_host_app_v2_embedding Reland "Update template/test compileSdk, targetSdk, ndk versions" (#153795) 2024-08-21 18:27:00 +00:00
android_module_host_with_custom_build_v2_embedding/app/src/main/java/io/flutter/addtoapp Fix package declarations for add-to-app integration tests (#77648) 2021-03-30 14:19:02 -07:00
android_semantics_testing Roll pub packages (#155846) 2024-10-01 22:45:08 +00:00
android_verified_input Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
android_views Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
channels Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
deferred_components_test Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
external_textures Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
flavors Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
flutter_gallery pattern-matching refactor (#154753) 2024-10-03 18:21:04 +00:00
gradle_deprecated_settings Roll pub packages (#156105) 2024-10-02 19:18:54 +00:00
hybrid_android_views Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
ios_add2app_life_cycle Roll pub packages (#155846) 2024-10-01 22:45:08 +00:00
ios_app_with_extensions Roll pub packages (#155846) 2024-10-01 22:45:08 +00:00
ios_host_app Added missing code block language in docs (#147481) 2024-05-01 14:44:27 +00:00
ios_host_app_swift [iOS] Migrate @UIApplicationMain attribute to @main (#146707) 2024-04-16 22:13:03 +00:00
ios_platform_view_tests Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
link_hook Roll pub packages (#155846) 2024-10-01 22:45:08 +00:00
module_host_with_custom_build/.gradle Reland "Branch out android platform view perf testing #60238" 2nd time (#61748) 2020-07-22 18:36:11 -07:00
module_host_with_custom_build_v2_embedding Reland "Update template/test compileSdk, targetSdk, ndk versions" (#153795) 2024-08-21 18:27:00 +00:00
native_driver_test Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
new_gallery pattern-matching refactor (#154753) 2024-10-03 18:21:04 +00:00
non_nullable Roll pub packages (#155846) 2024-10-01 22:45:08 +00:00
platform_interaction Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
release_smoke_test Roll pub packages (#155846) 2024-10-01 22:45:08 +00:00
spell_check Roll pub packages (#155846) 2024-10-01 22:45:08 +00:00
ui Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
web Roll pub packages (#155640) 2024-09-25 00:03:57 +00:00
web_compile_tests Manual dependency bump (#152881) 2024-08-06 02:40:27 +00:00
web_e2e_tests Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
wide_gamut_test Roll pub packages (#155846) 2024-10-01 22:45:08 +00:00
windows_startup_test Roll pub packages (#156117) 2024-10-02 23:01:09 +00:00
README.md Added missing code block language in docs (#147481) 2024-05-01 14:44:27 +00:00

Automated Flutter integration test suites

Each suite consists of either a complete Flutter app and a flutter_driver specification that drives tests from the UI, or a native app that is meant to integrate with Flutter for testing.

Intended for use with devicelab tests.

If you want to run a driver test locally, to debug a problem with a test, you can use this command from the appropriate subdirectory:

flutter drive -t <test> --driver <driver>

For example:

flutter drive -t lib/keyboard_resize.dart --driver test_driver/keyboard_resize_test.dart