|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:form_builder_validators/new_api_prototype/constants.dart'; |
| 3 | +import 'package:form_builder_validators/new_api_prototype/datetime_validators.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + group('Validator: isBefore', () { |
| 7 | + test('Validation for the year 1994', () { |
| 8 | + final DateTime reference = DateTime(1994); |
| 9 | + final DateTime eq = reference.copyWith(); |
| 10 | + final DateTime after10Years = DateTime(2004); |
| 11 | + final DateTime after1Ms = reference.add(const Duration(milliseconds: 1)); |
| 12 | + final DateTime before1Year = DateTime(1993); |
| 13 | + final DateTime before1Sec = |
| 14 | + reference.subtract(const Duration(seconds: 1)); |
| 15 | + final Validator<DateTime> v = isBefore(reference); |
| 16 | + final String errorMsg = tmpIsBeforeErrorMsg(reference); |
| 17 | + |
| 18 | + expect( |
| 19 | + v(eq), |
| 20 | + errorMsg, |
| 21 | + reason: 'Should return error against 1994', |
| 22 | + ); |
| 23 | + expect( |
| 24 | + v(after10Years), |
| 25 | + errorMsg, |
| 26 | + reason: 'Should return error against 2004', |
| 27 | + ); |
| 28 | + expect( |
| 29 | + v(after1Ms), |
| 30 | + errorMsg, |
| 31 | + reason: 'Should return error against 1994 + 1ms', |
| 32 | + ); |
| 33 | + expect( |
| 34 | + v(before1Year), |
| 35 | + isNull, |
| 36 | + reason: 'Should return null against 1993', |
| 37 | + ); |
| 38 | + expect( |
| 39 | + v(before1Sec), |
| 40 | + isNull, |
| 41 | + reason: 'Should return null against 1994 - 1s', |
| 42 | + ); |
| 43 | + }); |
| 44 | + test( |
| 45 | + 'Inclusive validation for datetime 2089, month 3, day 23, h 3, min 46, s 12, 233 ms', |
| 46 | + () { |
| 47 | + final DateTime reference = DateTime(2089, 3, 23, 3, 46, 12, 233); |
| 48 | + final DateTime eq = reference.copyWith(); |
| 49 | + final DateTime after10Years = reference.copyWith(year: 2099); |
| 50 | + final DateTime after1Ms = reference.add(const Duration(milliseconds: 1)); |
| 51 | + final DateTime before1Year = reference.copyWith(year: 2088); |
| 52 | + final DateTime before1Sec = |
| 53 | + reference.subtract(const Duration(seconds: 1)); |
| 54 | + final Validator<DateTime> v = isBefore(reference, inclusive: true); |
| 55 | + final String errorMsg = tmpIsBeforeErrorMsg(reference); |
| 56 | + |
| 57 | + expect( |
| 58 | + v(eq), |
| 59 | + isNull, |
| 60 | + reason: 'Should return null against the same datetime', |
| 61 | + ); |
| 62 | + expect( |
| 63 | + v(after10Years), |
| 64 | + errorMsg, |
| 65 | + reason: 'Should return error against the reference shifted +10 years', |
| 66 | + ); |
| 67 | + expect( |
| 68 | + v(after1Ms), |
| 69 | + errorMsg, |
| 70 | + reason: 'Should return error against the reference shifted +1 ms', |
| 71 | + ); |
| 72 | + expect( |
| 73 | + v(before1Year), |
| 74 | + isNull, |
| 75 | + reason: |
| 76 | + 'Should return null against a datetime 1 year before the reference', |
| 77 | + ); |
| 78 | + expect( |
| 79 | + v(before1Sec), |
| 80 | + isNull, |
| 81 | + reason: |
| 82 | + 'Should return null against a datetime 1 sec before the reference', |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + test('Should return a custom message after validating', () { |
| 87 | + const String errorMsg = 'error msg'; |
| 88 | + final DateTime reference = DateTime(2); |
| 89 | + final Validator<DateTime> v = |
| 90 | + isBefore(reference, isBeforeMsg: (_) => errorMsg); |
| 91 | + |
| 92 | + expect( |
| 93 | + v(reference.copyWith()), |
| 94 | + errorMsg, |
| 95 | + reason: |
| 96 | + 'Should return custom message when input is equal to the reference', |
| 97 | + ); |
| 98 | + expect( |
| 99 | + v(reference.subtract(const Duration(microseconds: 1))), |
| 100 | + isNull, |
| 101 | + reason: 'Should return null when input is before the reference', |
| 102 | + ); |
| 103 | + expect( |
| 104 | + v(reference.add(const Duration(days: 1))), |
| 105 | + errorMsg, |
| 106 | + reason: |
| 107 | + 'Should return custom message when the input is after the reference', |
| 108 | + ); |
| 109 | + }); |
| 110 | + }); |
| 111 | +} |
0 commit comments