37 lines
1.7 KiB
Dart
37 lines
1.7 KiB
Dart
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"
|
|||
|
|
});
|
|||
|
|
}
|