|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:form_builder_validators/form_builder_validators.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('Validator: maxLength', () { |
| 6 | + group('Validators with default error message', () { |
| 7 | + test('Should validate the input when it is expected to have maxLength 0', |
| 8 | + () { |
| 9 | + final Validator<List<int>> v = maxLength(0); |
| 10 | + final Validator<Object> v2 = maxLength(0); |
| 11 | + |
| 12 | + expect(v(<int>[]), isNull); |
| 13 | + expect(v2(<int>[12]), |
| 14 | + FormBuilderLocalizations.current.maxLengthErrorText(0)); |
| 15 | + expect(v2(12), FormBuilderLocalizations.current.maxLengthErrorText(0)); |
| 16 | + }); |
| 17 | + test('Should validate the input when it is expected to have maxLength 1', |
| 18 | + () { |
| 19 | + final Validator<List<int>> v = maxLength(1); |
| 20 | + final Validator<Object> v2 = maxLength(1); |
| 21 | + |
| 22 | + expect(v(<int>[]), isNull); |
| 23 | + expect(v(<int>[0]), isNull); |
| 24 | + expect(v2(<String>['0']), isNull); |
| 25 | + expect(v2(0), isNull); |
| 26 | + expect(v(<int>[1, 2]), |
| 27 | + FormBuilderLocalizations.current.maxLengthErrorText(1)); |
| 28 | + expect(v2(<Object>[1, '3']), |
| 29 | + FormBuilderLocalizations.current.maxLengthErrorText(1)); |
| 30 | + }); |
| 31 | + test('Should validate the input when it is expected to have maxLength 4', |
| 32 | + () { |
| 33 | + final Validator<List<String>> v = maxLength(4); |
| 34 | + final Validator<String> v2 = maxLength(4); |
| 35 | + |
| 36 | + expect(v(<String>[]), isNull); |
| 37 | + expect(v(<String>['1']), isNull); |
| 38 | + expect(v(<String>['1', '2']), isNull); |
| 39 | + expect(v(<String>['1', '2', '3']), isNull); |
| 40 | + expect(v(<String>['1', '2', '3', '4']), isNull); |
| 41 | + |
| 42 | + expect(v2(''), isNull); |
| 43 | + expect(v2('1'), isNull); |
| 44 | + expect(v2('12'), isNull); |
| 45 | + expect(v2('123'), isNull); |
| 46 | + expect(v2('1234'), isNull); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + test('Should validate input returning custom message error when invalid', |
| 51 | + () { |
| 52 | + const String customMsg = 'custom msg'; |
| 53 | + final Validator<Object> v = maxLength(3, maxLengthMsg: (_) => customMsg); |
| 54 | + |
| 55 | + expect(v('hey'), isNull); |
| 56 | + expect(v(<Object>[1, '2', 3, 4]), equals(customMsg)); |
| 57 | + }); |
| 58 | + test('Should throw AssertionError when maxLength is negative', () { |
| 59 | + expect(() => maxLength(-2), throwsAssertionError); |
| 60 | + }); |
| 61 | + }); |
| 62 | +} |
0 commit comments