Skip to content

Commit

Permalink
implement isBool validator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturAssisComp committed Nov 6, 2024
1 parent 4205fa9 commit b1e0dc9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
32 changes: 26 additions & 6 deletions lib/new_api_prototype/core_validators/type_validators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,37 @@ Validator<T> isNum<T extends Object>([Validator<num>? next, String? isNumMsg]) {
return (false, null);
}

Validator<T> isBool<T extends Object>(Validator<bool>? v,
{String? isBoolMsg, bool caseSensitive = false, bool trim = true}) {
const tmpIsBoolMsg = 'Value must be "true" or "false"';

/// This function returns a validator that checks if the user input is either a
/// `bool` or a `String` that is parsable to a `bool`.
/// If it checks positive, then it returns null when `next` is not provided. Otherwise,
/// if `next` is provided, it passes the transformed value as `bool` to the `next`
/// validator.
///
/// ## Parameters
/// For the following parameters, consider the information that the parsing process
/// only happens if the input is a `String`.
/// - `caseSensitive` (defaults to `false`): whether the parsing process is case
/// sensitive. If true, inputs like `FaLsE` would be accepted as a valid boolean.
/// - `trim` (defaults to `true`): whether the parsing process is preceded by a
/// trim of whitespaces (`\n`, `\t`, `' '`, etc). If true, inputs like `false \n` would
/// be accepted as valid `bool`.
Validator<T> isBool<T extends Object>(
[Validator<bool>? next,
String? isBoolMsg,
bool caseSensitive = false,
bool trim = true]) {
String? finalValidator(T value) {
final (isValid, typeTransformedValue) = _isBoolValidateAndConvert(value,
caseSensitive: caseSensitive, trim: trim);
final (bool isValid, bool? typeTransformedValue) =
_isBoolValidateAndConvert(value,
caseSensitive: caseSensitive, trim: trim);
if (!isValid) {
// TODO(ArturAssisComp): Add the default error message for the isBool validator.
return isBoolMsg ??
'default bool error msg'; //FormBuilderLocalizations.current.boolErrorText;
tmpIsBoolMsg; //FormBuilderLocalizations.current.boolErrorText;
}
return v?.call(typeTransformedValue!);
return next?.call(typeTransformedValue!);
}

return finalValidator;
Expand Down
56 changes: 56 additions & 0 deletions test/new_api_testing/core_validators/type_validators_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ void main() {
input.length > 3 ? null : errorMsg;
String? isEven(int input) => input % 2 == 0 ? null : errorMsg;
String? greaterThan9(num input) => input > 9 ? null : errorMsg;
String? isT(bool input) => input ? null : errorMsg;

group('Validator: isString', () {
test('Should only check if the input is a String', () {
Expand Down Expand Up @@ -115,4 +116,59 @@ void main() {
expect(v(-23.34), isNull);
});
});

group('Validator: isBool', () {
test('Should only check if the input is a bool/parsable to bool', () {
// defaults to case insensitive and trim
final Validator<Object> v = isBool();

expect(v('not a bool'), equals(tmpIsBoolMsg));
expect(v('T'), equals(tmpIsBoolMsg));
expect(v('isTrue'), equals(tmpIsBoolMsg));
expect(v('true.'), equals(tmpIsBoolMsg));
expect(v('true true'), equals(tmpIsBoolMsg));
expect(v(true), isNull);
expect(v(1 > 2), isNull);
expect(v(false), isNull);
expect(v('True'), isNull);
expect(v('TrUe'), isNull);
expect(v(' true'), isNull);
expect(v('true\n'), isNull);
});
test(
'Should only check if the input is a bool/parsable to bool without trim and with case sensitiveness',
() {
final Validator<Object> v = isBool(null, null, true, false);

expect(v('not a bool'), equals(tmpIsBoolMsg));
expect(v('T'), equals(tmpIsBoolMsg));
expect(v('isTrue'), equals(tmpIsBoolMsg));
expect(v('true.'), equals(tmpIsBoolMsg));
expect(v(true), isNull);
expect(v(1 > 2), isNull);
expect(v(false), isNull);
expect(v('True'), equals(tmpIsBoolMsg));
expect(v('TrUe'), equals(tmpIsBoolMsg));
expect(v(' true'), equals(tmpIsBoolMsg));
expect(v('true\n'), equals(tmpIsBoolMsg));
});
test('Should check if the input is true', () {
final Validator<Object> v = isBool(isT);

expect(v('not a bool'), equals(tmpIsBoolMsg));
expect(v(true), isNull);
expect(v(1 > 2), equals(errorMsg));
expect(v(false), equals(errorMsg));
expect(v('False'), equals(errorMsg));
expect(v('fAlSE \n '), equals(errorMsg));
});
test('Should check if the input is a bool using custom error', () {
const String customError = 'custom error';
final Validator<Object> v = isBool(null, customError);

expect(v('not num'), equals(customError));
expect(v(true), isNull);
expect(v(false), isNull);
});
});
}

0 comments on commit b1e0dc9

Please sign in to comment.