todo-app/test/text_free_guard.dart
Krzysztof kuhy Rudnicki 7153630a2e
Some checks are pending
file-length / file length <= 250 lines (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Audit icons: add 1-word hover tooltips
Todo: fe70b845
2026-11-22 18:30:40 +01:00

55 lines
2.2 KiB
Dart

/// The "no words" guard: kuhy's textgate rule says nothing he sees carries
/// app-written text. This walks every painted string in the widget tree and
/// fails on any run of two letters that is not note content.
///
/// Deliberately NOT named `*_test.dart`: it is a helper with no `main()`.
library;
import 'package:material_ui/material_ui.dart';
import 'package:flutter_test/flutter_test.dart';
final RegExp _word = RegExp('[A-Za-z]{2,}');
/// Finds a node whose spoken (semantics) label contains [part] literally.
/// The old visible sentences live on as spoken labels, so tests that used
/// `find.textContaining` now look here.
Finder spoken(String part) =>
find.bySemanticsLabel(RegExp(RegExp.escape(part)));
/// textgate one-word tooltip: no whitespace, <= 20 chars, no . ! ? , ; :
bool isOneWordTooltip(String s) =>
s.length <= 20 && !RegExp(r'[\s.!?,;:]').hasMatch(s);
/// Every string the tree paints: each [RichText] (which is what [Text] builds),
/// and every non-empty [Tooltip] message that is not an allowed one-word
/// tooltip. Text being typed lives in an [EditableText] and is the user's, so
/// it is never painted through a [RichText] and is not collected.
List<String> paintedStrings(WidgetTester tester) => [
for (final w in tester.widgetList<RichText>(find.byType(RichText)))
w.text.toPlainText(),
for (final t in tester.widgetList<Tooltip>(find.byType(Tooltip)))
if ((t.message ?? '').isNotEmpty && !isOneWordTooltip(t.message!))
t.message!,
];
/// Fails if any painted string still holds a word once every [content]
/// string (note text, typed values: things kuhy wrote) is removed from it.
///
/// An empty tree is a failure too: a guard that saw nothing proves nothing.
void expectNoChromeWords(
WidgetTester tester, {
List<String> content = const [],
}) {
final painted = paintedStrings(tester);
expect(painted, isNotEmpty, reason: 'guard saw no painted strings');
final offenders = <String>[];
for (final s in painted) {
var rest = s;
for (final c in content) {
rest = rest.replaceAll(c, '');
}
if (_word.hasMatch(rest)) offenders.add(s);
}
expect(offenders, isEmpty, reason: 'app-written words are painted');
}