|
| 1 | +import 'package:parameterized_test/src/errors/parameterized_error.dart'; |
| 2 | +import 'package:test/test.dart'; |
| 3 | + |
| 4 | +void main() { |
| 5 | + group('Test formatting of error messages', () { |
| 6 | + test('ParameterizedError from NoSuchMethodError', () { |
| 7 | + try { |
| 8 | + Function.apply((int value) => value, [1, 2]); |
| 9 | + //ignore: avoid_catching_errors |
| 10 | + } on NoSuchMethodError catch (e) { |
| 11 | + final result = ParameterizedError.fromNoSuchMethodError(e, [1, 2]); |
| 12 | + expect( |
| 13 | + result.message, |
| 14 | + "Provided value(s) didn't match the function arguments " |
| 15 | + 'count.\n' |
| 16 | + 'Amount of provided values: 2\n' |
| 17 | + 'Expected function arguments: 1\n' |
| 18 | + 'Test values: [1, 2]\n' |
| 19 | + 'Provided types: (int, int)\n' |
| 20 | + 'Expected types: (int)', |
| 21 | + ); |
| 22 | + } |
| 23 | + try { |
| 24 | + Function.apply((int value, int value2) => value + value2, [1]); |
| 25 | + //ignore: avoid_catching_errors |
| 26 | + } on NoSuchMethodError catch (e) { |
| 27 | + final result = ParameterizedError.fromNoSuchMethodError(e, [1]); |
| 28 | + expect( |
| 29 | + result.message, |
| 30 | + "Provided value(s) didn't match the function arguments " |
| 31 | + 'count.\n' |
| 32 | + 'Amount of provided values: 1\n' |
| 33 | + 'Expected function arguments: 2\n' |
| 34 | + 'Test values: [1]\n' |
| 35 | + 'Provided types: (int)\n' |
| 36 | + 'Expected types: (int, int)', |
| 37 | + ); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + test('ParameterizedError for TypeError', () { |
| 42 | + final result = ParameterizedError.forTypeError( |
| 43 | + [1], |
| 44 | + (String value) => value, |
| 45 | + ); |
| 46 | + expect( |
| 47 | + result.message, |
| 48 | + "Provided value(s) didn't match the function arguments types.\n" |
| 49 | + 'Test values: [1]\n' |
| 50 | + 'Provided types: (int)\n' |
| 51 | + 'Expected types: (String)', |
| 52 | + ); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + test('extractFunctionArgumentsSignature tests', () { |
| 57 | + final testValues = [ |
| 58 | + ((int value) => Null, 'int'), |
| 59 | + ((int value) => String, 'int'), |
| 60 | + ((int value, String string) => Null, 'int, String'), |
| 61 | + ((int value, String string) => double, 'int, String'), |
| 62 | + ((Map<int, String> value, String string) => Null, 'Map, String'), |
| 63 | + ( |
| 64 | + (Map<int, String> value, String string) => Map<double, bool>, |
| 65 | + 'Map, String' |
| 66 | + ), |
| 67 | + ( |
| 68 | + (Map<int, Map<double, bool>> value, String string) => Null, |
| 69 | + 'Map, String' |
| 70 | + ), |
| 71 | + ((int value, String string) async => Future<String>, 'int, String'), |
| 72 | + ]; |
| 73 | + |
| 74 | + for (final e in testValues) { |
| 75 | + final result = ParameterizedError.extractFunctionArgumentsSignature( |
| 76 | + e.$1.toString(), |
| 77 | + ); |
| 78 | + |
| 79 | + expect(result, e.$2); |
| 80 | + } |
| 81 | + }); |
| 82 | +} |
0 commit comments