|
1 | 1 | import 'package:faker_dart/faker_dart.dart';
|
2 | 2 | import 'package:flutter_test/flutter_test.dart';
|
| 3 | +import 'package:form_builder_validators/form_builder_validators.dart'; |
3 | 4 |
|
4 | 5 | void main() {
|
5 | 6 | final Faker faker = Faker.instance;
|
6 | 7 | final String customErrorMessage = faker.lorem.sentence();
|
7 |
| - group('Email -', () {}); |
| 8 | + group('Has numeric chars -', () { |
| 9 | + test('should return null when the value has at least 1 numeric character', |
| 10 | + () { |
| 11 | + // Arrange |
| 12 | + final HasNumericCharsValidator validator = HasNumericCharsValidator(); |
| 13 | + const String value = 'abc123'; |
| 14 | + |
| 15 | + // Act |
| 16 | + final String? result = validator.validate(value); |
| 17 | + |
| 18 | + // Assert |
| 19 | + expect(result, isNull); |
| 20 | + }); |
| 21 | + |
| 22 | + test('should return null when the value has at least 3 numeric characters', |
| 23 | + () { |
| 24 | + // Arrange |
| 25 | + final HasNumericCharsValidator validator = |
| 26 | + HasNumericCharsValidator(atLeast: 3); |
| 27 | + const String value = 'a1b2c3'; |
| 28 | + |
| 29 | + // Act |
| 30 | + final String? result = validator.validate(value); |
| 31 | + |
| 32 | + // Assert |
| 33 | + expect(result, isNull); |
| 34 | + }); |
| 35 | + |
| 36 | + test( |
| 37 | + 'should return the error message when the value has no numeric characters', |
| 38 | + () { |
| 39 | + // Arrange |
| 40 | + final HasNumericCharsValidator validator = |
| 41 | + HasNumericCharsValidator(errorText: customErrorMessage); |
| 42 | + const String value = 'abc'; |
| 43 | + |
| 44 | + // Act |
| 45 | + final String? result = validator.validate(value); |
| 46 | + |
| 47 | + // Assert |
| 48 | + expect(result, equals(customErrorMessage)); |
| 49 | + }); |
| 50 | + |
| 51 | + test( |
| 52 | + 'should return the error message when the value has less than 3 numeric characters', |
| 53 | + () { |
| 54 | + // Arrange |
| 55 | + final HasNumericCharsValidator validator = |
| 56 | + HasNumericCharsValidator(atLeast: 3, errorText: customErrorMessage); |
| 57 | + const String value = 'a1b'; |
| 58 | + |
| 59 | + // Act |
| 60 | + final String? result = validator.validate(value); |
| 61 | + |
| 62 | + // Assert |
| 63 | + expect(result, equals(customErrorMessage)); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should return the error message when the value is empty', () { |
| 67 | + // Arrange |
| 68 | + final HasNumericCharsValidator validator = |
| 69 | + HasNumericCharsValidator(errorText: customErrorMessage); |
| 70 | + const String value = ''; |
| 71 | + |
| 72 | + // Act |
| 73 | + final String? result = validator.validate(value); |
| 74 | + |
| 75 | + // Assert |
| 76 | + expect(result, equals(customErrorMessage)); |
| 77 | + }); |
| 78 | + }); |
8 | 79 | }
|
0 commit comments