|
| 1 | +import 'package:flutter/services.dart'; |
| 2 | +import 'package:flutter_multi_formatter/formatters/pos_input_formatter.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + const decimalSeparator = '.'; |
| 7 | + const formatter = PosInputFormatter( |
| 8 | + decimalSeparator: DecimalPosSeparator.dot, |
| 9 | + thousandsSeparator: ThousandsPosSeparator.space, |
| 10 | + mantissaLength: 2, |
| 11 | + ); |
| 12 | + |
| 13 | + group('Test PosInputFormatter text input formatter', () { |
| 14 | + group( |
| 15 | + 'Tests for adding zeroes at the beginning of the number and adding decimal separator', |
| 16 | + () { |
| 17 | + test('Add "0.0" at the beginning of the string', () { |
| 18 | + const oldValue = TextEditingValue(); |
| 19 | + const newValue = TextEditingValue( |
| 20 | + text: '1', |
| 21 | + selection: TextSelection.collapsed(offset: 1), |
| 22 | + ); |
| 23 | + const expectedValue = TextEditingValue( |
| 24 | + text: '0${decimalSeparator}01', |
| 25 | + selection: TextSelection.collapsed(offset: 4), |
| 26 | + ); |
| 27 | + |
| 28 | + expect(formatter.formatEditUpdate(oldValue, newValue), expectedValue); |
| 29 | + }); |
| 30 | + test('Add "0." at the beginning of the string', () { |
| 31 | + const oldValue = TextEditingValue( |
| 32 | + text: '1', |
| 33 | + selection: TextSelection.collapsed(offset: 1), |
| 34 | + ); |
| 35 | + const newValue = TextEditingValue( |
| 36 | + text: '12', |
| 37 | + selection: TextSelection.collapsed(offset: 2), |
| 38 | + ); |
| 39 | + const expectedValue = TextEditingValue( |
| 40 | + text: '0${decimalSeparator}12', |
| 41 | + selection: TextSelection.collapsed(offset: 4), |
| 42 | + ); |
| 43 | + |
| 44 | + expect(formatter.formatEditUpdate(oldValue, newValue), expectedValue); |
| 45 | + }); |
| 46 | + test('Add "." to the string', () { |
| 47 | + const oldValue = TextEditingValue( |
| 48 | + text: '12', |
| 49 | + selection: TextSelection.collapsed(offset: 2), |
| 50 | + ); |
| 51 | + const newValue = TextEditingValue( |
| 52 | + text: '123', |
| 53 | + selection: TextSelection.collapsed(offset: 3), |
| 54 | + ); |
| 55 | + const expectedValue = TextEditingValue( |
| 56 | + text: '1${decimalSeparator}23', |
| 57 | + selection: TextSelection.collapsed(offset: 4), |
| 58 | + ); |
| 59 | + |
| 60 | + expect(formatter.formatEditUpdate(oldValue, newValue), expectedValue); |
| 61 | + }); |
| 62 | + test('Add "." between 4 digits', () { |
| 63 | + const oldValue = TextEditingValue( |
| 64 | + text: '1.23', |
| 65 | + selection: TextSelection.collapsed(offset: 4), |
| 66 | + ); |
| 67 | + const newValue = TextEditingValue( |
| 68 | + text: '1.234', |
| 69 | + selection: TextSelection.collapsed(offset: 5), |
| 70 | + ); |
| 71 | + const expectedValue = TextEditingValue( |
| 72 | + text: '12${decimalSeparator}34', |
| 73 | + selection: TextSelection.collapsed(offset: 5), |
| 74 | + ); |
| 75 | + |
| 76 | + expect(formatter.formatEditUpdate(oldValue, newValue), expectedValue); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + group('Tests for adding the thousands separator', () { |
| 81 | + test('Not add space', () { |
| 82 | + const oldValue = TextEditingValue( |
| 83 | + text: '12.34', |
| 84 | + selection: TextSelection.collapsed(offset: 5), |
| 85 | + ); |
| 86 | + const newValue = TextEditingValue( |
| 87 | + text: '12.345', |
| 88 | + selection: TextSelection.collapsed(offset: 6), |
| 89 | + ); |
| 90 | + const expectedValue = TextEditingValue( |
| 91 | + text: '123${decimalSeparator}45', |
| 92 | + selection: TextSelection.collapsed(offset: 6), |
| 93 | + ); |
| 94 | + |
| 95 | + expect(formatter.formatEditUpdate(oldValue, newValue), expectedValue); |
| 96 | + }); |
| 97 | + test('Add space to the string', () { |
| 98 | + const oldValue = TextEditingValue( |
| 99 | + text: '123.45', |
| 100 | + selection: TextSelection.collapsed(offset: 5), |
| 101 | + ); |
| 102 | + const newValue = TextEditingValue( |
| 103 | + text: '123.456', |
| 104 | + selection: TextSelection.collapsed(offset: 7), |
| 105 | + ); |
| 106 | + const expectedValue = TextEditingValue( |
| 107 | + text: '1 234${decimalSeparator}56', |
| 108 | + selection: TextSelection.collapsed(offset: 8), |
| 109 | + ); |
| 110 | + |
| 111 | + expect(formatter.formatEditUpdate(oldValue, newValue), expectedValue); |
| 112 | + }); |
| 113 | + test('Add only one space to the string', () { |
| 114 | + const oldValue = TextEditingValue( |
| 115 | + text: '12 345.67', |
| 116 | + selection: TextSelection.collapsed(offset: 9), |
| 117 | + ); |
| 118 | + const newValue = TextEditingValue( |
| 119 | + text: '123 45.678', |
| 120 | + selection: TextSelection.collapsed(offset: 10), |
| 121 | + ); |
| 122 | + const expectedValue = TextEditingValue( |
| 123 | + text: '123 456${decimalSeparator}78', |
| 124 | + selection: TextSelection.collapsed(offset: 10), |
| 125 | + ); |
| 126 | + |
| 127 | + expect(formatter.formatEditUpdate(oldValue, newValue), expectedValue); |
| 128 | + }); |
| 129 | + test('Add two space to the string', () { |
| 130 | + const oldValue = TextEditingValue( |
| 131 | + text: '123 456.78', |
| 132 | + selection: TextSelection.collapsed(offset: 10), |
| 133 | + ); |
| 134 | + const newValue = TextEditingValue( |
| 135 | + text: '123 456.789', |
| 136 | + selection: TextSelection.collapsed(offset: 11), |
| 137 | + ); |
| 138 | + const expectedValue = TextEditingValue( |
| 139 | + text: '1 234 567${decimalSeparator}89', |
| 140 | + selection: TextSelection.collapsed(offset: 12), |
| 141 | + ); |
| 142 | + |
| 143 | + expect(formatter.formatEditUpdate(oldValue, newValue), expectedValue); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + test('Remove incorrect characters', () { |
| 148 | + const oldValue = TextEditingValue( |
| 149 | + text: '', |
| 150 | + selection: TextSelection.collapsed(offset: 0), |
| 151 | + ); |
| 152 | + const newValue = TextEditingValue( |
| 153 | + text: '123d45a.6', |
| 154 | + selection: TextSelection.collapsed(offset: 9), |
| 155 | + ); |
| 156 | + const expectedValue = TextEditingValue( |
| 157 | + text: '1 234${decimalSeparator}56', |
| 158 | + selection: TextSelection.collapsed(offset: 8), |
| 159 | + ); |
| 160 | + |
| 161 | + expect(formatter.formatEditUpdate(oldValue, newValue), expectedValue); |
| 162 | + }); |
| 163 | + }); |
| 164 | +} |
0 commit comments