Skip to content

Commit

Permalink
General cleanup (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo authored Mar 1, 2021
1 parent af82671 commit 0dd731c
Show file tree
Hide file tree
Showing 24 changed files with 78 additions and 56 deletions.
3 changes: 0 additions & 3 deletions _test_yaml/test/yaml_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ line 4, column 21 of file.yaml: Unsupported value for "configLocation". Illegal
╵'''
};

// ignore: deprecated_member_use
final throwsCastError = throwsA(isA<CastError>());

T roundTripObject<T>(
T object,
T Function(Map<String, dynamic> json) factory, {
Expand Down
24 changes: 13 additions & 11 deletions example/test/json_convert_example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,20 @@ void main() {
final encoded = _encode(collection);

expect(
() => GenericCollection<CustomResult>.fromJson(
jsonDecode(encoded) as Map<String, dynamic>),
_throwsCastError);
() => GenericCollection<CustomResult>.fromJson(
jsonDecode(encoded) as Map<String, dynamic>),
_throwsTypeError,
);
expect(
() => GenericCollection<int>.fromJson(
jsonDecode(encoded) as Map<String, dynamic>),
_throwsCastError);
() => GenericCollection<int>.fromJson(
jsonDecode(encoded) as Map<String, dynamic>),
_throwsTypeError,
);
expect(
() => GenericCollection<String>.fromJson(
jsonDecode(encoded) as Map<String, dynamic>),
_throwsCastError);
() => GenericCollection<String>.fromJson(
jsonDecode(encoded) as Map<String, dynamic>),
_throwsTypeError,
);

final collection2 =
GenericCollection.fromJson(jsonDecode(encoded) as Map<String, dynamic>);
Expand All @@ -96,8 +99,7 @@ void main() {
});
}

// ignore: deprecated_member_use
final _throwsCastError = throwsA(isA<CastError>());
final _throwsTypeError = throwsA(isA<TypeError>());

String _encode(Object object) =>
const JsonEncoder.withIndent(' ').convert(object);
13 changes: 7 additions & 6 deletions json_serializable/test/generic_files/generic_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ void main() {
});
test('with bad arguments', () {
expect(
() => GenericClass<double, String>()
..fieldT = (true as dynamic) as double,
throwsCastError);
() => GenericClass<double, String>()
..fieldT = (true as dynamic) as double,
throwsTypeError,
);
});
test('with bad arguments', () {
expect(
() =>
GenericClass<double, String>()..fieldS = (5 as dynamic) as String,
throwsCastError);
() => GenericClass<double, String>()..fieldS = (5 as dynamic) as String,
throwsTypeError,
);
});
});

Expand Down
2 changes: 1 addition & 1 deletion json_serializable/test/integration/integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void main() {
'ints': [3.14, 0],
};

expect(() => Numbers.fromJson(value), throwsCastError);
expect(() => Numbers.fromJson(value), throwsTypeError);
});
});

Expand Down
16 changes: 9 additions & 7 deletions json_serializable/test/kitchen_sink/kitchen_sink_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void _nonNullableTests(KitchenSinkFactory factory) {
if (factory.checked) {
matcher = checkedMatcher('set');
} else {
matcher = isA<TypeError>();
matcher = isTypeError;
}
expect(() => factory.fromJson(<String, dynamic>{}), throwsA(matcher));
});
Expand Down Expand Up @@ -122,7 +122,9 @@ void _nullableTests(KitchenSinkFactory factory) {
expect(
encoded,
containsPair(
key, _nonNullableFields.contains(key) ? isNotNull : isNull),
key,
_nonNullableFields.contains(key) ? isNotNull : isNull,
),
);
}
}
Expand Down Expand Up @@ -228,7 +230,7 @@ void _sharedTests(KitchenSinkFactory factory) {
});
}

const _nonNullableFields = [
const _nonNullableFields = {
'dynamicIterable',
'objectIterable',
'intIterable',
Expand All @@ -251,11 +253,11 @@ const _nonNullableFields = [
'val',
'simpleObject',
'strictKeysObject'
];
};

const _encodedAsMapKeys = ['simpleObject', 'strictKeysObject'];
const _encodedAsMapKeys = {'simpleObject', 'strictKeysObject'};

const _iterableMapKeys = [
const _iterableMapKeys = {
'bigIntMap',
'crazyComplex',
'datetime-iterable',
Expand All @@ -280,4 +282,4 @@ const _iterableMapKeys = [
'set',
'stringStringMap',
generatedLocalVarName,
];
};
21 changes: 21 additions & 0 deletions json_serializable/test/kitchen_sink/kitchen_sink_test_shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ const invalidValueTypes = {
'validatedPropertyNo42': true
};

const disallowNullKeys = {
'set',
'dynamicSet',
'objectSet',
'intSet',
'dateTimeSet',
'list',
'dynamicList',
'objectList',
'intList',
'dateTimeList',
'map',
'stringStringMap',
'dynamicIntMap',
'objectDateTimeMap',
'crazyComplex',
generatedLocalVarName,
'simpleObject',
'strictKeysObject',
};

Matcher checkedMatcher(String? expectedKey) => isA<CheckedFromJsonException>()
.having((e) => e.className, 'className', 'KitchenSink')
.having((e) => e.key, 'key', expectedKey);
Expand Down
15 changes: 6 additions & 9 deletions json_serializable/test/kitchen_sink/kitchen_sink_yaml_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ void _anyMapTests(KitchenSinkFactory factory) {
for (final e in invalidValueTypes.entries) {
_testBadValue(e.key, e.value, factory, false);
}
for (final e in disallowNullKeys) {
_testBadValue(e, null, factory, false);
}
for (final e in _invalidCheckedValues.entries) {
_testBadValue(e.key, e.value, factory, true);
}
});
}

void _testBadValue(String key, Object badValue, KitchenSinkFactory factory,
void _testBadValue(String key, Object? badValue, KitchenSinkFactory factory,
bool checkedAssignment) {
final matcher = _getMatcher(factory.checked, key, checkedAssignment);

Expand Down Expand Up @@ -65,8 +68,7 @@ Matcher _getMatcher(bool checked, String? expectedKey, bool checkedAssignment) {
innerMatcher = checkedMatcher(expectedKey);
} else {
innerMatcher = anyOf(
_isACastError,
_isATypeError,
isTypeError,
_isAUnrecognizedKeysException(
'Unrecognized keys: [invalid_key]; supported keys: '
'[value, custom_field]',
Expand All @@ -86,7 +88,7 @@ Matcher _getMatcher(bool checked, String? expectedKey, bool checkedAssignment) {
break;
case 'intIterable':
case 'datetime-iterable':
innerMatcher = _isACastError;
innerMatcher = isTypeError;
break;
default:
throw StateError('Not expected! - $expectedKey');
Expand All @@ -97,11 +99,6 @@ Matcher _getMatcher(bool checked, String? expectedKey, bool checkedAssignment) {
return throwsA(innerMatcher);
}

final _isATypeError = isA<TypeError>();

// ignore: deprecated_member_use
final _isACastError = isA<CastError>();

Matcher _isAUnrecognizedKeysException(expectedMessage) =>
isA<UnrecognizedKeysException>()
.having((e) => e.message, 'message', expectedMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<ArgumentError>()),
throwsArgumentError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
test('round trip null', () {
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<TypeError>()),
throwsTypeError,
);
});

Expand Down
5 changes: 3 additions & 2 deletions json_serializable/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import 'dart:convert';
import 'package:stack_trace/stack_trace.dart';
import 'package:test/test.dart';

// ignore: deprecated_member_use
final throwsCastError = throwsA(isA<CastError>());
final throwsTypeError = throwsA(isTypeError);

final isTypeError = isA<TypeError>();

T roundTripObject<T>(T object, T Function(Map<String, dynamic> json) factory) {
final data = loudEncode(object);
Expand Down
5 changes: 3 additions & 2 deletions json_serializable/tool/test_type_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ class TestTypeData {
.replaceAll('non-nullable', 'nullable')
.replaceAll('SimpleClass', 'SimpleClassNullable');

final thrownError = type == customEnumType ? 'ArgumentError' : 'TypeError';
final thrownError =
type == customEnumType ? 'throwsArgumentError' : 'throwsTypeError';

final newGroupContent = groupContent.replaceAll(
r'''
Expand All @@ -154,7 +155,7 @@ class TestTypeData {
'''
expect(
() => loudEncode(SimpleClass.fromJson({})),
throwsA(isA<$thrownError>()),
$thrownError,
);''',
);

Expand Down

0 comments on commit 0dd731c

Please sign in to comment.