Skip to content

Commit b1e0dc9

Browse files
implement isBool validator tests
1 parent 4205fa9 commit b1e0dc9

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

lib/new_api_prototype/core_validators/type_validators.dart

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,37 @@ Validator<T> isNum<T extends Object>([Validator<num>? next, String? isNumMsg]) {
9494
return (false, null);
9595
}
9696

97-
Validator<T> isBool<T extends Object>(Validator<bool>? v,
98-
{String? isBoolMsg, bool caseSensitive = false, bool trim = true}) {
97+
const tmpIsBoolMsg = 'Value must be "true" or "false"';
98+
99+
/// This function returns a validator that checks if the user input is either a
100+
/// `bool` or a `String` that is parsable to a `bool`.
101+
/// If it checks positive, then it returns null when `next` is not provided. Otherwise,
102+
/// if `next` is provided, it passes the transformed value as `bool` to the `next`
103+
/// validator.
104+
///
105+
/// ## Parameters
106+
/// For the following parameters, consider the information that the parsing process
107+
/// only happens if the input is a `String`.
108+
/// - `caseSensitive` (defaults to `false`): whether the parsing process is case
109+
/// sensitive. If true, inputs like `FaLsE` would be accepted as a valid boolean.
110+
/// - `trim` (defaults to `true`): whether the parsing process is preceded by a
111+
/// trim of whitespaces (`\n`, `\t`, `' '`, etc). If true, inputs like `false \n` would
112+
/// be accepted as valid `bool`.
113+
Validator<T> isBool<T extends Object>(
114+
[Validator<bool>? next,
115+
String? isBoolMsg,
116+
bool caseSensitive = false,
117+
bool trim = true]) {
99118
String? finalValidator(T value) {
100-
final (isValid, typeTransformedValue) = _isBoolValidateAndConvert(value,
101-
caseSensitive: caseSensitive, trim: trim);
119+
final (bool isValid, bool? typeTransformedValue) =
120+
_isBoolValidateAndConvert(value,
121+
caseSensitive: caseSensitive, trim: trim);
102122
if (!isValid) {
103123
// TODO(ArturAssisComp): Add the default error message for the isBool validator.
104124
return isBoolMsg ??
105-
'default bool error msg'; //FormBuilderLocalizations.current.boolErrorText;
125+
tmpIsBoolMsg; //FormBuilderLocalizations.current.boolErrorText;
106126
}
107-
return v?.call(typeTransformedValue!);
127+
return next?.call(typeTransformedValue!);
108128
}
109129

110130
return finalValidator;

test/new_api_testing/core_validators/type_validators_test.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ void main() {
77
input.length > 3 ? null : errorMsg;
88
String? isEven(int input) => input % 2 == 0 ? null : errorMsg;
99
String? greaterThan9(num input) => input > 9 ? null : errorMsg;
10+
String? isT(bool input) => input ? null : errorMsg;
1011

1112
group('Validator: isString', () {
1213
test('Should only check if the input is a String', () {
@@ -115,4 +116,59 @@ void main() {
115116
expect(v(-23.34), isNull);
116117
});
117118
});
119+
120+
group('Validator: isBool', () {
121+
test('Should only check if the input is a bool/parsable to bool', () {
122+
// defaults to case insensitive and trim
123+
final Validator<Object> v = isBool();
124+
125+
expect(v('not a bool'), equals(tmpIsBoolMsg));
126+
expect(v('T'), equals(tmpIsBoolMsg));
127+
expect(v('isTrue'), equals(tmpIsBoolMsg));
128+
expect(v('true.'), equals(tmpIsBoolMsg));
129+
expect(v('true true'), equals(tmpIsBoolMsg));
130+
expect(v(true), isNull);
131+
expect(v(1 > 2), isNull);
132+
expect(v(false), isNull);
133+
expect(v('True'), isNull);
134+
expect(v('TrUe'), isNull);
135+
expect(v(' true'), isNull);
136+
expect(v('true\n'), isNull);
137+
});
138+
test(
139+
'Should only check if the input is a bool/parsable to bool without trim and with case sensitiveness',
140+
() {
141+
final Validator<Object> v = isBool(null, null, true, false);
142+
143+
expect(v('not a bool'), equals(tmpIsBoolMsg));
144+
expect(v('T'), equals(tmpIsBoolMsg));
145+
expect(v('isTrue'), equals(tmpIsBoolMsg));
146+
expect(v('true.'), equals(tmpIsBoolMsg));
147+
expect(v(true), isNull);
148+
expect(v(1 > 2), isNull);
149+
expect(v(false), isNull);
150+
expect(v('True'), equals(tmpIsBoolMsg));
151+
expect(v('TrUe'), equals(tmpIsBoolMsg));
152+
expect(v(' true'), equals(tmpIsBoolMsg));
153+
expect(v('true\n'), equals(tmpIsBoolMsg));
154+
});
155+
test('Should check if the input is true', () {
156+
final Validator<Object> v = isBool(isT);
157+
158+
expect(v('not a bool'), equals(tmpIsBoolMsg));
159+
expect(v(true), isNull);
160+
expect(v(1 > 2), equals(errorMsg));
161+
expect(v(false), equals(errorMsg));
162+
expect(v('False'), equals(errorMsg));
163+
expect(v('fAlSE \n '), equals(errorMsg));
164+
});
165+
test('Should check if the input is a bool using custom error', () {
166+
const String customError = 'custom error';
167+
final Validator<Object> v = isBool(null, customError);
168+
169+
expect(v('not num'), equals(customError));
170+
expect(v(true), isNull);
171+
expect(v(false), isNull);
172+
});
173+
});
118174
}

0 commit comments

Comments
 (0)