|
| 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: minLength', () { |
| 6 | + group('Validators with default error message', () { |
| 7 | + test('Should validate the input when it is expected to have minLength 0', |
| 8 | + () { |
| 9 | + final Validator<List<int>> v = minLength(0); |
| 10 | + final Validator<Object> v2 = minLength(0); |
| 11 | + |
| 12 | + expect(v(<int>[]), isNull); |
| 13 | + expect(v2(<int>[12]), isNull); |
| 14 | + expect(v2(12), isNull); |
| 15 | + }); |
| 16 | + test('Should validate the input when it is expected to have minLength 1', |
| 17 | + () { |
| 18 | + final Validator<List<int>> v = minLength(1); |
| 19 | + final Validator<Object> v2 = minLength(1); |
| 20 | + |
| 21 | + expect( |
| 22 | + v(<int>[]), FormBuilderLocalizations.current.minLengthErrorText(1)); |
| 23 | + expect(v(<int>[0]), isNull); |
| 24 | + expect(v(<int>[1, 2]), isNull); |
| 25 | + |
| 26 | + expect(v2(<String>[]), |
| 27 | + FormBuilderLocalizations.current.minLengthErrorText(1)); |
| 28 | + expect(v2(<String>['0']), isNull); |
| 29 | + expect(v2(0), isNull); |
| 30 | + expect(v2(<Object>[1, '3']), isNull); |
| 31 | + }); |
| 32 | + test('Should validate the input when it is expected to have minLength 4', |
| 33 | + () { |
| 34 | + final Validator<List<String>> v = minLength(4); |
| 35 | + final Validator<String> v2 = minLength(4); |
| 36 | + |
| 37 | + expect(v(<String>[]), |
| 38 | + FormBuilderLocalizations.current.minLengthErrorText(4)); |
| 39 | + expect(v(<String>['1']), |
| 40 | + FormBuilderLocalizations.current.minLengthErrorText(4)); |
| 41 | + expect(v(<String>['1', '2']), |
| 42 | + FormBuilderLocalizations.current.minLengthErrorText(4)); |
| 43 | + expect(v(<String>['1', '2', '3']), |
| 44 | + FormBuilderLocalizations.current.minLengthErrorText(4)); |
| 45 | + expect(v(<String>['1', '2', '3', '4']), isNull); |
| 46 | + expect(v(<String>['1', '2', '3', '4', '5']), isNull); |
| 47 | + expect(v(<String>['1', '2', '3', '4', '5', '6']), isNull); |
| 48 | + |
| 49 | + expect(v2(''), FormBuilderLocalizations.current.minLengthErrorText(4)); |
| 50 | + expect(v2('1'), FormBuilderLocalizations.current.minLengthErrorText(4)); |
| 51 | + expect( |
| 52 | + v2('12'), FormBuilderLocalizations.current.minLengthErrorText(4)); |
| 53 | + expect( |
| 54 | + v2('123'), FormBuilderLocalizations.current.minLengthErrorText(4)); |
| 55 | + expect(v2('1234'), isNull); |
| 56 | + expect(v2('12345'), isNull); |
| 57 | + expect(v2('123456'), isNull); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + test('Should validate input returning custom message error when invalid', |
| 62 | + () { |
| 63 | + const String customMsg = 'custom msg'; |
| 64 | + final Validator<Object> v = minLength(3, minLengthMsg: (_) => customMsg); |
| 65 | + |
| 66 | + expect(v(89), equals(customMsg)); |
| 67 | + expect(v(<Object>[1, '2', 3, 4]), isNull); |
| 68 | + }); |
| 69 | + test('Should throw AssertionError when minLength is negative', () { |
| 70 | + expect(() => minLength(-2), throwsAssertionError); |
| 71 | + }); |
| 72 | + }); |
| 73 | +} |
0 commit comments