Skip to content

Commit b58c851

Browse files
implement isNotEqual
1 parent c4c829a commit b58c851

File tree

2 files changed

+134
-6
lines changed

2 files changed

+134
-6
lines changed

lib/new_api_prototype/core_validators/equality_validators.dart

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ import '../../localization/l10n.dart';
22
import '../constants.dart';
33

44
/// This function returns a validator that checks if the user input is equal (using
5-
/// the == operator) to either `value`. If the condition is satisfied,
5+
/// the == operator) to `value`. If the condition is satisfied,
66
/// the validator returns null, otherwise, it returns
77
/// `FormBuilderLocalizations.current.equalErrorText(userInput.toString())`, if
8-
/// `equalMsg` is not provided.
9-
///
10-
/// # Error
11-
/// - Throws [AssertionError] if not exactly one of `value` or `dynValue` was
12-
/// provided.
8+
/// `isEqualMsg` is not provided.
139
Validator<T> isEqual<T extends Object?>(
1410
T value, {
1511
String Function(String)? isEqualMsg,
@@ -22,3 +18,21 @@ Validator<T> isEqual<T extends Object?>(
2218
FormBuilderLocalizations.current.equalErrorText(valueString);
2319
};
2420
}
21+
22+
/// This function returns a validator that checks if the user input is not equal (using
23+
/// the != operator) to `value`. If the condition is satisfied,
24+
/// the validator returns null, otherwise, it returns
25+
/// `FormBuilderLocalizations.current.notEqualErrorText(userInput.toString())`, if
26+
/// `isNotEqualMsg` is not provided.
27+
Validator<T> isNotEqual<T extends Object?>(
28+
T value, {
29+
String Function(String)? isNotEqualMsg,
30+
}) {
31+
return (T input) {
32+
final String valueString = value.toString();
33+
return value != input
34+
? null
35+
: isNotEqualMsg?.call(valueString) ??
36+
FormBuilderLocalizations.current.notEqualErrorText(valueString);
37+
};
38+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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

Comments
 (0)