Skip to content

More test cleanup on the way to "any map" and config support #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions json_serializable/lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

final toJsonMapVarName = 'val';
final toJsonMapHelperName = 'writeNotNull';
const toJsonMapVarName = 'val';
const toJsonMapHelperName = 'writeNotNull';
1 change: 1 addition & 0 deletions json_serializable/lib/src/generator_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class _GeneratorHelper {
.toList(),
unavailableReasons,
deserializeFun);
_buffer.writeln(';');

// If there are fields that are final – that are not set via the generated
// constructor, then don't output them when generating the `toJson` call.
Expand Down
5 changes: 1 addition & 4 deletions json_serializable/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,13 @@ Set<String> writeConstructorInvocation(
}), ', ');

buffer.write(')');
if (remainingFieldsForInvocationBody.isEmpty) {
buffer.writeln(';');
} else {
if (remainingFieldsForInvocationBody.isNotEmpty) {
for (var field in remainingFieldsForInvocationBody) {
buffer.writeln();
buffer.write(' ..$field = ');
buffer.write(deserializeForField(field));
usedCtorParamsAndFields.add(field);
}
buffer.writeln(';');
}
buffer.writeln();

Expand Down
2 changes: 1 addition & 1 deletion json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 0.5.3
version: 0.5.4-dev
author: Dart Team <misc@dartlang.org>
description: Generates utilities to aid in serializing to/from JSON.
homepage: https://github.com/dart-lang/json_serializable
Expand Down
122 changes: 70 additions & 52 deletions json_serializable/test/kitchen_sink_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ import 'test_files/kitchen_sink.wrapped.dart' as wrapped
import 'test_files/kitchen_sink_interface.dart';
import 'test_utils.dart';

final _isACastError = const isInstanceOf<CastError>();
final _isATypeError = const isInstanceOf<TypeError>();

void main() {
test('valid values covers all keys', () {
expect(_validValues.keys, orderedEquals(_expectedOrder));
});

test('inlavid values covers all keys', () {
expect(_invalidValues.keys, orderedEquals(_expectedOrder));
});

group('nullable', () {
group('unwrapped', () {
_nullableTests(nullable.testFactory, nullable.testFromJson);
Expand All @@ -41,7 +52,8 @@ void main() {
}

typedef KitchenSink KitchenSinkCtor(
{Iterable iterable,
{int ctorValidatedNo42,
Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Expand Down Expand Up @@ -166,72 +178,78 @@ void _sharedTests(
expect(json.keys, orderedEquals(_expectedOrder));
});

group('a bad value for', () {
final input = const {
'dateTime': '2018-05-10T14:20:58.927',
'iterable': const [],
'dynamicIterable': const [],
'objectIterable': const [],
'intIterable': const [],
'datetime-iterable': const [],
'list': const [],
'dynamicList': const [],
'objectList': const [],
'intList': const [],
'dateTimeList': const [],
'map': const <String, dynamic>{},
'stringStringMap': const {},
'stringIntMap': const {},
'stringDateTimeMap': const <String, dynamic>{},
'crazyComplex': const [],
'val': const {},
'writeNotNull': null,
r'$string': null
};

test('nothing succeeds', () {
expect(loudEncode(input), loudEncode(fromJson(input)));
});
test('valid values round-trip', () {
expect(loudEncode(_validValues), loudEncode(fromJson(_validValues)));
});

for (var e in {
'dateTime': true,
'iterable': true,
'dynamicIterable': true,
'intIterable': [true],
'datetime-iterable': [true],
'list': true,
'dynamicList': true,
'objectList': true,
'intList': [true],
'dateTimeList': [true],
'stringStringMap': {'key': 42},
'stringIntMap': {'key': 'value'},
'stringDateTimeMap': {'key': 42},
'crazyComplex': [true],
'val': {'key': 42},
'writeNotNull': 42,
r'$string': true,
}.entries) {
group('a bad value for', () {
for (var e in _invalidValues.entries) {
test('`${e.key}` fails', () {
var copy = new Map<String, dynamic>.from(input);
var copy = new Map<String, dynamic>.from(_validValues);
copy[e.key] = e.value;
expect(() => fromJson(copy), throwsA((e) {
return e is CastError || e is TypeError;
}));
expect(() => fromJson(copy),
throwsA(anyOf(_isATypeError, _isACastError, isArgumentError)));
});
}
});
}

final _excludeIfNullKeys = [
final _validValues = const {
'no-42': 0,
'dateTime': '2018-05-10T14:20:58.927',
'iterable': const [],
'dynamicIterable': const [],
'objectIterable': const [],
'intIterable': const [],
'datetime-iterable': const [],
'list': const [],
'dynamicList': const [],
'objectList': const [],
'intList': const [],
'dateTimeList': const [],
'map': const <String, dynamic>{},
'stringStringMap': const {},
'stringIntMap': const {},
'stringDateTimeMap': const <String, dynamic>{},
'crazyComplex': const [],
'val': const {},
'writeNotNull': null,
r'$string': null
};

final _invalidValues = const {
'no-42': 42,
'dateTime': true,
'iterable': true,
'dynamicIterable': true,
'objectIterable': true,
'intIterable': const [true],
'datetime-iterable': const [true],
'list': true,
'dynamicList': true,
'objectList': true,
'intList': const [true],
'dateTimeList': const [true],
'map': true,
'stringStringMap': const {'key': 42},
'stringIntMap': const {'key': 'value'},
'stringDateTimeMap': const {'key': 42},
'crazyComplex': const [true],
'val': const {'key': 42},
'writeNotNull': 42,
r'$string': true,
};

final _excludeIfNullKeys = const [
'dateTime',
'iterable',
'dateTimeList',
'crazyComplex',
toJsonMapVarName
];

final _expectedOrder = [
final _expectedOrder = const [
'no-42',
'dateTime',
'iterable',
'dynamicIterable',
Expand Down
17 changes: 14 additions & 3 deletions json_serializable/test/test_files/kitchen_sink.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ List<T> _defaultList<T>() => null;
Map _defaultMap() => null;

k.KitchenSink testFactory(
{Iterable iterable,
{int ctorValidatedNo42,
Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Iterable<DateTime> dateTimeIterable}) =>
new KitchenSink(
ctorValidatedNo42: ctorValidatedNo42,
iterable: iterable,
dynamicIterable: dynamicIterable,
objectIterable: objectIterable,
Expand All @@ -45,8 +47,12 @@ class KitchenSink extends Object
final Iterable<int> _intIterable;
final Iterable<DateTime> _dateTimeIterable;

@JsonKey(name: 'no-42')
final int ctorValidatedNo42;

KitchenSink(
{Iterable iterable,
{this.ctorValidatedNo42,
Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Expand All @@ -55,7 +61,12 @@ class KitchenSink extends Object
_dynamicIterable = dynamicIterable?.toList() ?? _defaultList(),
_objectIterable = objectIterable?.toList() ?? _defaultList(),
_intIterable = intIterable?.toList() ?? _defaultList(),
_dateTimeIterable = dateTimeIterable?.toList() ?? _defaultList();
_dateTimeIterable = dateTimeIterable?.toList() ?? _defaultList() {
if (ctorValidatedNo42 == 42) {
throw new ArgumentError.value(
42, 'ctorValidatedNo42', 'The value `42` is not allowed.');
}
}

factory KitchenSink.fromJson(Map<String, Object> json) =>
_$KitchenSinkFromJson(json);
Expand Down
6 changes: 5 additions & 1 deletion json_serializable/test/test_files/kitchen_sink.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ part of 'kitchen_sink.dart';
// **************************************************************************

KitchenSink _$KitchenSinkFromJson(Map<String, dynamic> json) => new KitchenSink(
ctorValidatedNo42: json['no-42'] as int,
iterable: json['iterable'] as List,
dynamicIterable: json['dynamicIterable'] as List,
objectIterable: json['objectIterable'] as List,
Expand Down Expand Up @@ -54,6 +55,7 @@ KitchenSink _$KitchenSinkFromJson(Map<String, dynamic> json) => new KitchenSink(
..string = json[r'$string'] as String;

abstract class _$KitchenSinkSerializerMixin {
int get ctorValidatedNo42;
DateTime get dateTime;
Iterable<dynamic> get iterable;
Iterable<dynamic> get dynamicIterable;
Expand All @@ -74,7 +76,9 @@ abstract class _$KitchenSinkSerializerMixin {
bool get writeNotNull;
String get string;
Map<String, dynamic> toJson() {
var val = <String, dynamic>{};
var val = <String, dynamic>{
'no-42': ctorValidatedNo42,
};

void writeNotNull(String key, dynamic value) {
if (value != null) {
Expand Down
17 changes: 14 additions & 3 deletions json_serializable/test/test_files/kitchen_sink.non_nullable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ List<T> _defaultList<T>() => <T>[];
Map<String, T> _defaultMap<T>() => <String, T>{};

k.KitchenSink testFactory(
{Iterable iterable,
{int ctorValidatedNo42,
Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Iterable<DateTime> dateTimeIterable}) =>
new KitchenSink(
ctorValidatedNo42: ctorValidatedNo42,
iterable: iterable,
dynamicIterable: dynamicIterable,
objectIterable: objectIterable,
Expand All @@ -51,8 +53,12 @@ class KitchenSink extends Object
final Iterable<int> _intIterable;
final Iterable<DateTime> _dateTimeIterable;

@JsonKey(name: 'no-42')
final int ctorValidatedNo42;

KitchenSink(
{Iterable iterable,
{this.ctorValidatedNo42,
Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Expand All @@ -61,7 +67,12 @@ class KitchenSink extends Object
_dynamicIterable = dynamicIterable?.toList() ?? _defaultList(),
_objectIterable = objectIterable?.toList() ?? _defaultList(),
_intIterable = intIterable?.toList() ?? _defaultList(),
_dateTimeIterable = dateTimeIterable?.toList() ?? _defaultList();
_dateTimeIterable = dateTimeIterable?.toList() ?? _defaultList() {
if (ctorValidatedNo42 == 42) {
throw new ArgumentError.value(
42, 'ctorValidatedNo42', 'The value `42` is not allowed.');
}
}

factory KitchenSink.fromJson(Map<String, Object> json) =>
_$KitchenSinkFromJson(json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ part of 'kitchen_sink.non_nullable.dart';
// **************************************************************************

KitchenSink _$KitchenSinkFromJson(Map<String, dynamic> json) => new KitchenSink(
ctorValidatedNo42: json['no-42'] as int,
iterable: json['iterable'] as List,
dynamicIterable: json['dynamicIterable'] as List,
objectIterable: json['objectIterable'] as List,
Expand Down Expand Up @@ -45,6 +46,7 @@ KitchenSink _$KitchenSinkFromJson(Map<String, dynamic> json) => new KitchenSink(
..string = json[r'$string'] as String;

abstract class _$KitchenSinkSerializerMixin {
int get ctorValidatedNo42;
DateTime get dateTime;
Iterable<dynamic> get iterable;
Iterable<dynamic> get dynamicIterable;
Expand All @@ -65,6 +67,7 @@ abstract class _$KitchenSinkSerializerMixin {
bool get writeNotNull;
String get string;
Map<String, dynamic> toJson() => <String, dynamic>{
'no-42': ctorValidatedNo42,
'dateTime': dateTime.toIso8601String(),
'iterable': iterable.toList(),
'dynamicIterable': dynamicIterable.toList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ List<T> _defaultList<T>() => <T>[];
Map<String, T> _defaultMap<T>() => <String, T>{};

k.KitchenSink testFactory(
{Iterable iterable,
{int ctorValidatedNo42,
Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Iterable<DateTime> dateTimeIterable}) =>
new KitchenSink(
ctorValidatedNo42: ctorValidatedNo42,
iterable: iterable,
dynamicIterable: dynamicIterable,
objectIterable: objectIterable,
Expand All @@ -57,8 +59,12 @@ class KitchenSink extends Object
final Iterable<int> _intIterable;
final Iterable<DateTime> _dateTimeIterable;

@JsonKey(name: 'no-42')
final int ctorValidatedNo42;

KitchenSink(
{Iterable iterable,
{this.ctorValidatedNo42,
Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Expand All @@ -67,7 +73,12 @@ class KitchenSink extends Object
_dynamicIterable = dynamicIterable?.toList() ?? _defaultList(),
_objectIterable = objectIterable?.toList() ?? _defaultList(),
_intIterable = intIterable?.toList() ?? _defaultList(),
_dateTimeIterable = dateTimeIterable?.toList() ?? _defaultList();
_dateTimeIterable = dateTimeIterable?.toList() ?? _defaultList() {
if (ctorValidatedNo42 == 42) {
throw new ArgumentError.value(
42, 'ctorValidatedNo42', 'The value `42` is not allowed.');
}
}

factory KitchenSink.fromJson(Map<String, Object> json) =>
_$KitchenSinkFromJson(json);
Expand Down
Loading