[checks_codegen] Generate full field types with imports (#2781)

Generated getters referenced the field type by the name of its element
only, so type arguments were never imported and function typed fields
were skipped entirely. Imports were also searched only among the test
library's direct imports, so types reached by the checked class through
a re-export fell back to `src/` libraries or uncompilable `dart:_*` SDK
libraries.

First walk the checked classes and their field types to collect every
referenced interface element and resolve an import URI for each, then
emit type references synchronously from the analyzer `DartType`:

- Interface types import the library for each element, including within
  type arguments.
- Function types, including generic function types with bounds, and
  record types are emitted structurally.
- `dynamic`, `void`, and `Never` are emitted directly.
- Type parameters of the checked class are replaced with their bounds by
  reading getter types from the class instantiated to bounds. The
  extension is on the raw type, so this matches the static type of
  `v.field`.
- Unresolvable types (`InvalidType`) fail naming the class and field,
  with a source span on the field and a hint to check for a missing
  build dependency.

Function typed fields now get getters. Fields without a getter, such as
those introduced by a lone setter, are skipped.

Import resolution searches for a library exporting each non-public-SDK
element in order:

1. The imports of the test library.
2. The imports of every library declaring a referenced element.
3. For elements declared in `dart:_*` libraries, public SDK libraries:
   first those reachable through imports and exports from the test and
   declaring libraries (so e.g. `HttpStatus` prefers `dart:io` over
   `dart:html` in a VM program), then any remaining public SDK
   libraries, each sorted by URI.

The first two groups prefer libraries outside `src/`. If no library
exports an element, fall back to its declaring library (which may be in
`src/`), or fail if that is a `dart:_*` library.

Note on conditional directives: `build_runner` resolves code with empty
`DeclaredVariables`, so conditional `import`/`export` directives always
follow their default URI during codegen, never an `if (dart.library.*)`
branch. When a barrel conditionally exports `src/` implementations,
codegen now imports the barrel rather than the default `src/` stub, so
test compilation evaluates the condition for the target platform. When a
declaring library uses a conditional `import` directly, codegen still
sees only the default branch: a stub default yields the stub's types as
before, while an IO default exposing types like `HttpHeaders` or
`HttpStatus` now emits `dart:io` (compiling on VM, instead of emitting
`dart:_http`/`dart:_internal` which failed everywhere) but still will
not compile for web unless the test library imports a compatible
exporter.
3 files changed
tree: 0bbe5a0532e17d68724a28e1fa889e081b4644e9
  1. .github/
  2. integration_tests/
  3. pkgs/
  4. tool/
  5. .gitattributes
  6. .gitignore
  7. analysis_options.yaml
  8. CODEOWNERS
  9. CONTRIBUTING.md
  10. mono_repo.yaml
  11. pubspec.yaml
  12. README.md
README.md

Dart CI OpenSSF Scorecard

What's here?

Welcome! package:test is the standard testing library for Dart and Flutter. If you have questions about Dart testing, please see the docs for package:test. package:test_api and package:test_core are implementation details and generally not user-facing.

package:checks is a relatively new library for expressing test expectations. It's a more modern version of package:matcher and features a literate API.

Packages

PackageDescriptionIssuesVersion
checksA framework for checking values against expectations and building custom expectations.issuespub package
fake_asyncFake asynchronous events such as timers and microtasks for deterministic testing.issuespub package
matcherSupport for specifying test expectations via an extensible Matcher class. Also includes a number of built-in Matcher implementations for common cases.issuespub package
testA full featured library for writing and running Dart tests across platforms.issuespub package
test_apiThe user facing API for structuring Dart tests and checking expectations.pub package
test_coreA basic library for writing tests and running them on the VM.pub package
test_descriptorAn API for defining and verifying files and directory structures.issuespub package
test_processTest processes: starting; validating stdout and stderr; checking exit code.issuespub package