testing library
Test helpers for the catalog the generator produced.
A generated CatalogItem is a contract with a model: these components
exist, they take these properties, and this is what the app does with them.
The schema half of that contract is checked at build time. This library
checks the other half — what the rendered component exposes to the person
using it — by recording the semantics of each item's example and failing
when they change.
The recorded file is the same shape A2UI's rendering cases are written in, so it doubles as the answer to "what can the model make this app announce", which is not visible from a Dart diff.
// test/genui_semantics_test.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:genui_gen/testing.dart';
import 'package:my_app/genui_catalog.g.dart';
void main() {
testWidgets('the catalog exposes what it did before', (tester) async {
final recorded = <String, List<GenUiSemanticNode>>{};
for (final item in genUiCatalog.items) {
final handle = tester.ensureSemantics();
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: GenUiExampleSurface(catalog: genUiCatalog, item: item),
),
),
);
await tester.pumpAndSettle();
recorded[item.name] = genUiRenderedSemantics();
handle.dispose();
}
expect(
genUiSemanticsGolden(recorded, File('test/genui_semantics.json')),
isNull,
);
});
}
Record the file the first time, and after a deliberate change, by setting
GENUI_UPDATE_GOLDENS=1:
GENUI_UPDATE_GOLDENS=1 flutter test test/genui_semantics_test.dart
Classes
- GenUiExampleSurface
- Renders one catalog item's generated example, through a real surface.
- GenUiSemanticNode
- One node of what a surface exposes to assistive technology, in the shape A2UI's rendering cases are written in.
Functions
-
genUiRenderedSemantics(
) → List< GenUiSemanticNode> - The meaningful semantics of what is currently on screen.
-
genUiSemantics(
SemanticsNode root) → List< GenUiSemanticNode> -
The meaningful part of
root, in traversal order. -
genUiSemanticsDiff(
List< GenUiSemanticNode> expected, List<GenUiSemanticNode> actual) → String? -
Describes how
actualdiffers fromexpected, ornullwhen it does not. -
genUiSemanticsGolden(
Map< String, List< recorded, File golden, {bool? update}) → String?GenUiSemanticNode> > -
Compares
recordedagainstgolden, and returns what differs, ornullwhen nothing does.