Skip to content

Test cleanup #152

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 3 commits into from
May 12, 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
133 changes: 71 additions & 62 deletions json_serializable/test/kitchen_sink_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,36 @@ import 'package:test/test.dart';

import 'package:json_serializable/src/constants.dart';

import 'test_files/kitchen_sink.dart' as nullable;
import 'test_files/kitchen_sink.non_nullable.dart' as nn;
import 'test_files/kitchen_sink.non_nullable.wrapped.dart' as nnwrapped;
import 'test_files/kitchen_sink.wrapped.dart' as wrapped;
import 'test_files/kitchen_sink.dart' as nullable
show testFactory, testFromJson;
import 'test_files/kitchen_sink.non_nullable.dart' as nn
show testFactory, testFromJson;
import 'test_files/kitchen_sink.non_nullable.wrapped.dart' as nnwrapped
show testFactory, testFromJson;
import 'test_files/kitchen_sink.wrapped.dart' as wrapped
show testFactory, testFromJson;

import 'test_files/kitchen_sink_interface.dart';
import 'test_utils.dart';

void main() {
group('nullable', () {
group('unwrapped', () {
_nullableTests(
(
{Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Iterable<DateTime> dateTimeIterable}) =>
new nullable.KitchenSink(
iterable: iterable,
dynamicIterable: dynamicIterable,
objectIterable: objectIterable,
intIterable: intIterable,
dateTimeIterable: dateTimeIterable),
(j) => new nullable.KitchenSink.fromJson(j));
_nullableTests(nullable.testFactory, nullable.testFromJson);
});

group('wrapped', () {
_nullableTests(
(
{Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Iterable<DateTime> dateTimeIterable}) =>
new wrapped.KitchenSink(
iterable: iterable,
dynamicIterable: dynamicIterable,
objectIterable: objectIterable,
intIterable: intIterable,
dateTimeIterable: dateTimeIterable),
(j) => new wrapped.KitchenSink.fromJson(j));
_nullableTests(wrapped.testFactory, wrapped.testFromJson);
});
});

group('non-nullable', () {
group('unwrapped', () {
_nonNullableTests(
(
{Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Iterable<DateTime> dateTimeIterable}) =>
new nn.KitchenSink(
iterable: iterable,
dynamicIterable: dynamicIterable,
objectIterable: objectIterable,
intIterable: intIterable,
dateTimeIterable: dateTimeIterable),
(j) => new nn.KitchenSink.fromJson(j));
_nonNullableTests(nn.testFactory, nn.testFromJson);
});

group('wrapped', () {
_nonNullableTests(
(
{Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Iterable<DateTime> dateTimeIterable}) =>
new nnwrapped.KitchenSink(
iterable: iterable,
dynamicIterable: dynamicIterable,
objectIterable: objectIterable,
intIterable: intIterable,
dateTimeIterable: dateTimeIterable),
(j) => new nnwrapped.KitchenSink.fromJson(j));
_nonNullableTests(nnwrapped.testFactory, nnwrapped.testFromJson);
});
});
}
Expand All @@ -109,7 +62,7 @@ void _nonNullableTests(

void _nullableTests(
KitchenSinkCtor ctor, KitchenSink fromJson(Map<String, dynamic> json)) {
roundTripItem(KitchenSink p) {
void roundTripItem(KitchenSink p) {
roundTripObject(p, (json) => fromJson(json));
}

Expand Down Expand Up @@ -164,7 +117,7 @@ void _nullableTests(

void _sharedTests(
KitchenSinkCtor ctor, KitchenSink fromJson(Map<String, dynamic> json)) {
roundTripSink(KitchenSink p) {
void roundTripSink(KitchenSink p) {
roundTripObject(p, fromJson);
}

Expand Down Expand Up @@ -212,6 +165,62 @@ void _sharedTests(
var json = item.toJson();
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)));
});

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) {
test('`${e.key}` fails', () {
var copy = new Map<String, dynamic>.from(input);
copy[e.key] = e.value;
expect(() => fromJson(copy), throwsA((e) {
return e is CastError || e is TypeError;
}));
});
}
});
}

final _excludeIfNullKeys = [
Expand Down
5 changes: 4 additions & 1 deletion json_serializable/test/test_files/json_test_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Order extends Object with _$OrderSerializerMixin {
throw new UnimplementedError('not impld');
}

// Ignored getter without value set in ctor
int get price => items.fold(0, (total, item) => item.price + total);

@JsonKey(ignore: true)
Expand Down Expand Up @@ -147,7 +148,9 @@ class Numbers extends Object with _$NumbersSerializerMixin {
_deepEquals(ints, other.ints) &&
_deepEquals(nums, other.nums) &&
_deepEquals(doubles, other.doubles) &&
_deepEquals(nnDoubles, other.nnDoubles);
_deepEquals(nnDoubles, other.nnDoubles) &&
_deepEquals(duration, other.duration) &&
_deepEquals(date, other.date);
}

Duration _fromJson(int ms) => new Duration(milliseconds: ms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Order extends Object with _$OrderSerializerMixin {
throw new UnimplementedError('not impld');
}

// Ignored getter without value set in ctor
int get price => items.fold(0, (total, item) => item.price + total);

@JsonKey(ignore: true)
Expand Down Expand Up @@ -153,7 +154,9 @@ class Numbers extends Object with _$NumbersSerializerMixin {
_deepEquals(ints, other.ints) &&
_deepEquals(nums, other.nums) &&
_deepEquals(doubles, other.doubles) &&
_deepEquals(nnDoubles, other.nnDoubles);
_deepEquals(nnDoubles, other.nnDoubles) &&
_deepEquals(duration, other.duration) &&
_deepEquals(date, other.date);
}

Duration _fromJson(int ms) => new Duration(milliseconds: ms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Order extends Object with _$OrderSerializerMixin {
throw new UnimplementedError('not impld');
}

// Ignored getter without value set in ctor
int get price => items.fold(0, (total, item) => item.price + total);

@JsonKey(ignore: true)
Expand Down Expand Up @@ -159,7 +160,9 @@ class Numbers extends Object with _$NumbersSerializerMixin {
_deepEquals(ints, other.ints) &&
_deepEquals(nums, other.nums) &&
_deepEquals(doubles, other.doubles) &&
_deepEquals(nnDoubles, other.nnDoubles);
_deepEquals(nnDoubles, other.nnDoubles) &&
_deepEquals(duration, other.duration) &&
_deepEquals(date, other.date);
}

Duration _fromJson(int ms) => new Duration(milliseconds: ms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Order extends Object with _$OrderSerializerMixin {
throw new UnimplementedError('not impld');
}

// Ignored getter without value set in ctor
int get price => items.fold(0, (total, item) => item.price + total);

@JsonKey(ignore: true)
Expand Down Expand Up @@ -153,7 +154,9 @@ class Numbers extends Object with _$NumbersSerializerMixin {
_deepEquals(ints, other.ints) &&
_deepEquals(nums, other.nums) &&
_deepEquals(doubles, other.doubles) &&
_deepEquals(nnDoubles, other.nnDoubles);
_deepEquals(nnDoubles, other.nnDoubles) &&
_deepEquals(duration, other.duration) &&
_deepEquals(date, other.date);
}

Duration _fromJson(int ms) => new Duration(milliseconds: ms);
Expand Down
16 changes: 16 additions & 0 deletions json_serializable/test/test_files/kitchen_sink.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ part 'kitchen_sink.g.dart';
List<T> _defaultList<T>() => null;
Map _defaultMap() => null;

k.KitchenSink testFactory(
{Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Iterable<DateTime> dateTimeIterable}) =>
new KitchenSink(
iterable: iterable,
dynamicIterable: dynamicIterable,
objectIterable: objectIterable,
intIterable: intIterable,
dateTimeIterable: dateTimeIterable);

k.KitchenSink testFromJson(Map<String, dynamic> json) =>
new KitchenSink.fromJson(json);

@JsonSerializable()
class KitchenSink extends Object
with _$KitchenSinkSerializerMixin
Expand Down
18 changes: 17 additions & 1 deletion json_serializable/test/test_files/kitchen_sink.non_nullable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@ import 'kitchen_sink_interface.dart' as k;
part 'kitchen_sink.non_nullable.g.dart';

List<T> _defaultList<T>() => <T>[];
Map _defaultMap() => {};
Map<String, T> _defaultMap<T>() => <String, T>{};

k.KitchenSink testFactory(
{Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Iterable<DateTime> dateTimeIterable}) =>
new KitchenSink(
iterable: iterable,
dynamicIterable: dynamicIterable,
objectIterable: objectIterable,
intIterable: intIterable,
dateTimeIterable: dateTimeIterable);

k.KitchenSink testFromJson(Map<String, dynamic> json) =>
new KitchenSink.fromJson(json);

@JsonSerializable(nullable: false)
class KitchenSink extends Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,23 @@ import 'kitchen_sink_interface.dart' as k;
part 'kitchen_sink.non_nullable.wrapped.g.dart';

List<T> _defaultList<T>() => <T>[];
Map _defaultMap() => {};
Map<String, T> _defaultMap<T>() => <String, T>{};

k.KitchenSink testFactory(
{Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Iterable<DateTime> dateTimeIterable}) =>
new KitchenSink(
iterable: iterable,
dynamicIterable: dynamicIterable,
objectIterable: objectIterable,
intIterable: intIterable,
dateTimeIterable: dateTimeIterable);

k.KitchenSink testFromJson(Map<String, dynamic> json) =>
new KitchenSink.fromJson(json);

@JsonSerializable(nullable: false)
class KitchenSink extends Object
Expand Down
16 changes: 16 additions & 0 deletions json_serializable/test/test_files/kitchen_sink.wrapped.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ part 'kitchen_sink.wrapped.g.dart';
List<T> _defaultList<T>() => null;
Map _defaultMap() => null;

k.KitchenSink testFactory(
{Iterable iterable,
Iterable<dynamic> dynamicIterable,
Iterable<Object> objectIterable,
Iterable<int> intIterable,
Iterable<DateTime> dateTimeIterable}) =>
new KitchenSink(
iterable: iterable,
dynamicIterable: dynamicIterable,
objectIterable: objectIterable,
intIterable: intIterable,
dateTimeIterable: dateTimeIterable);

k.KitchenSink testFromJson(Map<String, dynamic> json) =>
new KitchenSink.fromJson(json);

@JsonSerializable()
class KitchenSink extends Object
with _$KitchenSinkSerializerMixin
Expand Down
14 changes: 13 additions & 1 deletion json_serializable/test/test_files/kitchen_sink_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,22 @@ bool sinkEquals(KitchenSink a, Object other) =>
a.dateTime == other.dateTime &&
_deepEquals(a.iterable, other.iterable) &&
_deepEquals(a.dynamicIterable, other.dynamicIterable) &&
// objectIterable
// intIterable
_deepEquals(a.dateTimeIterable, other.dateTimeIterable) &&
// list
// dynamicList
// objectList
// intList
_deepEquals(a.dateTimeList, other.dateTimeList) &&
// map
// stringStringMap
// stringIntMap
_deepEquals(a.stringDateTimeMap, other.stringDateTimeMap) &&
_deepEquals(a.crazyComplex, other.crazyComplex);
_deepEquals(a.crazyComplex, other.crazyComplex) &&
// val
a.writeNotNull == other.writeNotNull &&
a.string == other.string;

bool _deepEquals(Object a, Object b) =>
const DeepCollectionEquality().equals(a, b);
4 changes: 2 additions & 2 deletions json_serializable/tool/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class _NonNullableGenerator extends Generator {
replacements.addAll([
new _Replacement('List<T> _defaultList<T>() => null;',
'List<T> _defaultList<T>() => <T>[];'),
new _Replacement(
'Map _defaultMap() => null;', 'Map _defaultMap() => {};'),
new _Replacement('Map _defaultMap() => null;',
'Map<String, T> _defaultMap<T>() => <String, T>{};'),
new _Replacement('DateTime dateTime;',
'DateTime dateTime = new DateTime(1981, 6, 5);')
]);
Expand Down