Files
CookIT/test/recipes/text_breaks_test.dart
2026-10-28 22:55:09 +01:00

38 lines
1.7 KiB
Dart
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:cookit/src/recipes/text_breaks.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
String shown(String text) => withNaturalBreaks(text).replaceAll('​', '|');
test('breaks after hyphens, ampersands and slashes, and between words written together', () {
expect(shown('ApfelWeißkohlPfanne&Schupfi'), 'Apfel|Weißkohl|Pfanne&|Schupfi');
expect(shown('Brokkoli-Sahne-Nudeln'), 'Brokkoli-|Sahne-|Nudeln');
expect(shown('AvocadoMinze Smoothie'), 'Avocado|Minze Smoothie');
expect(shown('Salz/Pfeffer'), 'Salz/|Pfeffer');
expect(shown('Öl-Ärger'), 'Öl-|Ärger');
});
test('leaves ordinary names and abbreviations alone', () {
expect(shown('Tomatensoße mit Spätzle'), 'Tomatensoße mit Spätzle');
expect(shown('BBQ Burger'), 'BBQ Burger');
expect(shown('Gef. Hähnchenfilet'), 'Gef. Hähnchenfilet');
expect(shown('Rösti - knusprig'), 'Rösti - knusprig');
expect(withNaturalBreaks('ApfelWeißkohl').replaceAll('​', ''), 'ApfelWeißkohl');
});
test('a line now ends at a natural break, not mid-word', () {
// Test font: every glyph is fontSize wide. Room for 15 glyphs.
List<double> lineWidths(String text) {
final p = TextPainter(
text: TextSpan(text: text, style: const TextStyle(fontSize: 10)),
textDirection: TextDirection.ltr,
)..layout(maxWidth: 155);
return [for (final l in p.computeLineMetrics()) l.width];
}
expect(lineWidths('ApfelWeißkohlPfanne'), [150, 40]); // "ApfelWeißkohlPf" | "anne"
expect(lineWidths(withNaturalBreaks('ApfelWeißkohlPfanne')), [130, 60]); // "ApfelWeißkohl" | "Pfanne"
});
}