|
| 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