genUiSemanticsGolden function
Compares recorded against golden, and returns what differs, or null
when nothing does.
Writes golden instead of comparing when the file does not exist yet, or
when GENUI_UPDATE_GOLDENS is set in the environment. Recording is
deliberately not the default: a file that rewrites itself on every run
cannot fail, and the point is to fail when a component stops exposing what
it used to.
Implementation
String? genUiSemanticsGolden(
Map<String, List<GenUiSemanticNode>> recorded,
File golden, {
bool? update,
}) {
final bool write = update ?? (!golden.existsSync() || _updateRequested());
if (write) {
golden
..parent.createSync(recursive: true)
..writeAsStringSync('${_encode(recorded)}\n');
return null;
}
final Map<String, List<GenUiSemanticNode>> expected = _decode(
golden.readAsStringSync(),
);
final out = StringBuffer();
for (final name in <String>{
...expected.keys,
...recorded.keys,
}.toList()..sort()) {
final List<GenUiSemanticNode>? want = expected[name];
final List<GenUiSemanticNode>? got = recorded[name];
if (want == null) {
out.writeln('$name is new; it is not in ${golden.path}.');
continue;
}
if (got == null) {
out.writeln('$name is in ${golden.path} but was not recorded.');
continue;
}
final String? difference = genUiSemanticsDiff(want, got);
if (difference != null) out.writeln('$name:\n$difference');
}
if (out.isEmpty) return null;
return '$out\nRe-record with '
'GENUI_UPDATE_GOLDENS=1 if this is the change you meant to make.';
}