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