Skip to content

Commit 16fac8a

Browse files
implement debug print validator
1 parent 60f38e4 commit 16fac8a

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:flutter/widgets.dart';
2+
3+
import '../constants.dart';
4+
5+
/// Each time the user provides an `input`, this validator prints its [String]
6+
/// representation to `stdout` for debugging purposes.
7+
///
8+
/// The logging message can be customized providing the `logOnInput` parameter.
9+
///
10+
/// If the validator `next` is provided, it redirects user's input to it, returning
11+
/// its result. Otherwise, it never fails, thus, always returning `null`.
12+
Validator<T> debugPrintValidator<T extends Object?>(
13+
{Validator<T>? next, String Function(T)? logOnInput}) {
14+
return (T value) {
15+
debugPrint(logOnInput?.call(value) ?? value.toString());
16+
return next?.call(value);
17+
};
18+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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/core_validators/debug_print_validator.dart';
4+
5+
class _CustomClass {
6+
@override
7+
String toString() => 'this is a string';
8+
}
9+
10+
void main() {
11+
group('Validator: debugPrintValidator', () {
12+
final List<({String objectDescription, Object? value})> testCases =
13+
<({String objectDescription, Object? value})>[
14+
(objectDescription: 'empty string', value: ''),
15+
(objectDescription: 'null', value: null),
16+
(objectDescription: 'custom class', value: _CustomClass()),
17+
(objectDescription: 'string \'input 1\'', value: 'input 1'),
18+
];
19+
for (final (objectDescription: String dsc, value: Object? value)
20+
in testCases) {
21+
test('Should print the String representation for: $dsc', () {
22+
final Validator<Object?> v = debugPrintValidator();
23+
expect(() => v(value), prints(equals('$value\n')));
24+
expect(v(value), isNull);
25+
});
26+
}
27+
28+
test(
29+
'Should print the String representation of the input with next validator',
30+
() {
31+
const String errorMsg = 'error msg';
32+
final Validator<Object?> v = debugPrintValidator(
33+
next: (Object? v) => v is int && v % 2 == 0 ? null : errorMsg);
34+
35+
expect(() => v(13), prints(equals('13\n')),
36+
reason: 'check stdout when input is \'13\'');
37+
expect(v(13), errorMsg,
38+
reason: 'check return value when input is \'13\'');
39+
40+
expect(() => v('not int'), prints(equals('not int\n')),
41+
reason: 'check stdout when input is \'not int\'');
42+
expect(v('not int'), errorMsg,
43+
reason: 'check return value when input is \'not int\'');
44+
45+
expect(() => v(10), prints(equals('10\n')),
46+
reason: 'check stdout when input is \'10\'');
47+
expect(v(10), isNull, reason: 'check return value when input is \'10\'');
48+
});
49+
test('Should print the custom String representation of the input', () {
50+
String logOnInput(Object? v) {
51+
return 'Hello world $v';
52+
}
53+
54+
final Validator<Object?> v = debugPrintValidator(logOnInput: logOnInput);
55+
56+
expect(() => v(13), prints(equals('${logOnInput(13)}\n')),
57+
reason: 'check stdout when input is \'13\'');
58+
expect(v(13), isNull, reason: 'check return value when input is \'13\'');
59+
60+
final _CustomClass c = _CustomClass();
61+
expect(() => v(c), prints(equals('${logOnInput(c)}\n')),
62+
reason: 'check stdout when input is a custom object');
63+
expect(v(c), isNull,
64+
reason: 'check return value when input is a custom object');
65+
});
66+
});
67+
}

0 commit comments

Comments
 (0)