|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:form_builder_validators/form_builder_validators.dart'; |
| 3 | + |
| 4 | +class _CustomClass {} |
| 5 | + |
| 6 | +void main() { |
| 7 | + final _CustomClass myObject = _CustomClass(); |
| 8 | + |
| 9 | + group('Validator: isNoEqual', () { |
| 10 | + final List< |
| 11 | + ({ |
| 12 | + String description, |
| 13 | + bool testFails, |
| 14 | + Object? referenceValue, |
| 15 | + Object? userInput |
| 16 | + })> testCases = <({ |
| 17 | + String description, |
| 18 | + Object? referenceValue, |
| 19 | + Object? userInput, |
| 20 | + bool testFails |
| 21 | + })>[ |
| 22 | + // Edge cases |
| 23 | + ( |
| 24 | + description: 'Should pass when the input is not null', |
| 25 | + referenceValue: null, |
| 26 | + userInput: 123, |
| 27 | + testFails: false |
| 28 | + ), |
| 29 | + ( |
| 30 | + description: 'Should fail when the value is null', |
| 31 | + referenceValue: null, |
| 32 | + userInput: null, |
| 33 | + testFails: true |
| 34 | + ), |
| 35 | + ( |
| 36 | + description: 'Should pass when the input is not the empty string', |
| 37 | + referenceValue: '', |
| 38 | + userInput: '\t', |
| 39 | + testFails: false |
| 40 | + ), |
| 41 | + ( |
| 42 | + description: 'Should fail when the value is the empty string', |
| 43 | + referenceValue: '', |
| 44 | + userInput: '', |
| 45 | + testFails: true |
| 46 | + ), |
| 47 | + // Domain cases |
| 48 | + ( |
| 49 | + description: 'Should pass when the input is not the integer 123', |
| 50 | + referenceValue: 123, |
| 51 | + userInput: 122, |
| 52 | + testFails: false |
| 53 | + ), |
| 54 | + ( |
| 55 | + description: |
| 56 | + 'Should pass when the input is not the string "Hello, World!"', |
| 57 | + referenceValue: 'Hello, World!', |
| 58 | + userInput: 'Hello, World', |
| 59 | + testFails: false |
| 60 | + ), |
| 61 | + ( |
| 62 | + description: 'Should fail when the input is "Hello, World!"', |
| 63 | + referenceValue: 'Hello, World!', |
| 64 | + userInput: 'Hello, World!', |
| 65 | + testFails: true |
| 66 | + ), |
| 67 | + ( |
| 68 | + description: |
| 69 | + 'Should pass when the input is not the same as a custom object', |
| 70 | + referenceValue: myObject, |
| 71 | + userInput: _CustomClass(), |
| 72 | + testFails: false |
| 73 | + ), |
| 74 | + ( |
| 75 | + description: |
| 76 | + 'Should fail when the input is the same as a custom class object', |
| 77 | + referenceValue: myObject, |
| 78 | + userInput: myObject, |
| 79 | + testFails: true |
| 80 | + ), |
| 81 | + ]; |
| 82 | + |
| 83 | + for (final ( |
| 84 | + description: String desc, |
| 85 | + referenceValue: Object? referenceValue, |
| 86 | + userInput: Object? userInput, |
| 87 | + testFails: bool testFails |
| 88 | + ) in testCases) { |
| 89 | + test(desc, () { |
| 90 | + final Validator<Object?> v = isNotEqual(referenceValue); |
| 91 | + |
| 92 | + expect( |
| 93 | + v(userInput), |
| 94 | + testFails |
| 95 | + ? equals(FormBuilderLocalizations.current |
| 96 | + .notEqualErrorText(referenceValue.toString())) |
| 97 | + : isNull); |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + test('Should return custom error message', () { |
| 102 | + const String ref = 'hello'; |
| 103 | + const String customErrorMessage = 'custom error'; |
| 104 | + final Validator<Object> v = |
| 105 | + isNotEqual(ref, isNotEqualMsg: (_) => customErrorMessage); |
| 106 | + |
| 107 | + // success |
| 108 | + expect(v(123), isNull); |
| 109 | + |
| 110 | + // failure |
| 111 | + expect(v(ref), customErrorMessage); |
| 112 | + }); |
| 113 | + }); |
| 114 | +} |
0 commit comments