|
| 1 | +import 'package:flutter/services.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:stelaris/util/formatter/min_value_fomatter.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + group('MinValueFormatter', () { |
| 7 | + const min = 1; |
| 8 | + final formatter = MinValueFormatter(min); |
| 9 | + |
| 10 | + TextEditingValue format(String oldText, String newText, {int? cursor}) { |
| 11 | + return formatter.formatEditUpdate( |
| 12 | + TextEditingValue( |
| 13 | + text: oldText, |
| 14 | + selection: TextSelection.collapsed(offset: oldText.length), |
| 15 | + ), |
| 16 | + TextEditingValue( |
| 17 | + text: newText, |
| 18 | + selection: TextSelection.collapsed(offset: cursor ?? newText.length), |
| 19 | + ), |
| 20 | + ); |
| 21 | + } |
| 22 | + |
| 23 | + test('empty input resets to min', () { |
| 24 | + final result = format('2', ''); |
| 25 | + expect(result.text, '1'); |
| 26 | + expect(result.selection.baseOffset, '1'.length); |
| 27 | + }); |
| 28 | + |
| 29 | + test('leading zero removed', () { |
| 30 | + final result = format('', '01'); |
| 31 | + expect(result.text, '1'); |
| 32 | + expect(result.selection.baseOffset, 1); |
| 33 | + }); |
| 34 | + |
| 35 | + test('below min resets to min', () { |
| 36 | + final result = format('', '0'); |
| 37 | + expect(result.text, '1'); |
| 38 | + expect(result.selection.baseOffset, 1); |
| 39 | + }); |
| 40 | + |
| 41 | + test('valid number stays as is', () { |
| 42 | + final result = format('', '21'); |
| 43 | + expect(result.text, '21'); |
| 44 | + expect(result.selection.baseOffset, 2); |
| 45 | + }); |
| 46 | + |
| 47 | + test('multiple leading zeros collapse', () { |
| 48 | + final result = format('', '0007'); |
| 49 | + expect(result.text, '7'); |
| 50 | + expect(result.selection.baseOffset, 1); |
| 51 | + }); |
| 52 | + }); |
| 53 | +} |
0 commit comments