Skip to content

Commit 2c4d41d

Browse files
committed
cleanup(test): share all common members from json_test_example.*
1 parent 70e3a52 commit 2c4d41d

10 files changed

+117
-230
lines changed

json_serializable/test/integration/integration_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:test/test.dart';
66

77
import '../test_utils.dart';
8+
import 'json_test_common.dart' show Category, House, Platform;
89
import 'json_test_example.dart';
910

1011
void main() {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:collection/collection.dart';
6+
7+
enum House { gryffindor, hufflepuff, ravenclaw, slytherin }
8+
enum Category { top, bottom, strange, charmed, up, down }
9+
10+
Duration durationFromInt(int ms) => new Duration(milliseconds: ms);
11+
int durationToInt(Duration duration) => duration.inMilliseconds;
12+
13+
DateTime dateTimeFromEpochUs(int us) =>
14+
new DateTime.fromMicrosecondsSinceEpoch(us);
15+
int dateTimeToEpochUs(DateTime dateTime) => dateTime.microsecondsSinceEpoch;
16+
17+
bool deepEquals(a, b) => const DeepCollectionEquality().equals(a, b);
18+
19+
class Platform {
20+
final String description;
21+
22+
static const Platform foo = const Platform._('foo');
23+
static const Platform undefined = const Platform._('undefined');
24+
const Platform._(this.description);
25+
26+
factory Platform.fromJson(String value) {
27+
switch (value) {
28+
case 'foo':
29+
return foo;
30+
case 'undefined':
31+
return undefined;
32+
default:
33+
throw new ArgumentError.value(value, 'value', 'Not a supported value.');
34+
}
35+
}
36+
37+
String toJson() => description;
38+
}
39+
40+
abstract class ItemCore {
41+
final int price;
42+
43+
ItemCore(this.price);
44+
}

json_serializable/test/integration/json_test_example.dart

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
// ignore_for_file: annotate_overrides, hash_and_equals
66
import 'dart:collection';
77

8-
import 'package:collection/collection.dart';
98
import 'package:json_annotation/json_annotation.dart';
9+
import 'json_test_common.dart';
1010

1111
part 'json_test_example.g.dart';
1212

13-
enum House { gryffindor, hufflepuff, ravenclaw, slytherin }
14-
1513
@JsonSerializable()
1614
class Person extends Object with _$PersonSerializerMixin {
1715
final String firstName, middleName, lastName;
@@ -35,11 +33,9 @@ class Person extends Object with _$PersonSerializerMixin {
3533
lastName == other.lastName &&
3634
dateOfBirth == other.dateOfBirth &&
3735
house == other.house &&
38-
const MapEquality().equals(houseMap, other.houseMap);
36+
deepEquals(houseMap, other.houseMap);
3937
}
4038

41-
enum Category { top, bottom, strange, charmed, up, down }
42-
4339
@JsonSerializable()
4440
class Order extends Object with _$OrderSerializerMixin {
4541
/// Used to test that `disallowNullValues: true` forces `includeIfNull: false`
@@ -78,8 +74,8 @@ class Order extends Object with _$OrderSerializerMixin {
7874
other is Order &&
7975
count == other.count &&
8076
isRushed == other.isRushed &&
81-
_deepEquals(items, other.items) &&
82-
_deepEquals(altPlatforms, other.altPlatforms);
77+
deepEquals(items, other.items) &&
78+
deepEquals(altPlatforms, other.altPlatforms);
8379
}
8480

8581
@JsonSerializable()
@@ -97,36 +93,7 @@ class Item extends ItemCore with _$ItemSerializerMixin {
9793
other is Item &&
9894
price == other.price &&
9995
itemNumber == other.itemNumber &&
100-
_deepEquals(saleDates, other.saleDates);
101-
}
102-
103-
abstract class ItemCore {
104-
final int price;
105-
106-
ItemCore(this.price);
107-
}
108-
109-
bool _deepEquals(a, b) => const DeepCollectionEquality().equals(a, b);
110-
111-
class Platform {
112-
final String description;
113-
114-
static const Platform foo = const Platform._('foo');
115-
static const Platform undefined = const Platform._('undefined');
116-
const Platform._(this.description);
117-
118-
factory Platform.fromJson(String value) {
119-
switch (value) {
120-
case 'foo':
121-
return foo;
122-
case 'undefined':
123-
return undefined;
124-
default:
125-
throw new ArgumentError.value(value, 'value', 'Not a supported value.');
126-
}
127-
}
128-
129-
String toJson() => description;
96+
deepEquals(saleDates, other.saleDates);
13097
}
13198

13299
@JsonSerializable()
@@ -138,10 +105,10 @@ class Numbers extends Object with _$NumbersSerializerMixin {
138105
@JsonKey(nullable: false)
139106
List<double> nnDoubles;
140107

141-
@JsonKey(fromJson: _fromJson, toJson: _toJson)
108+
@JsonKey(fromJson: durationFromInt, toJson: durationToInt)
142109
Duration duration;
143110

144-
@JsonKey(fromJson: _dateTimeFromEpochUs, toJson: _dateTimeToEpochUs)
111+
@JsonKey(fromJson: dateTimeFromEpochUs, toJson: dateTimeToEpochUs)
145112
DateTime date;
146113

147114
Numbers();
@@ -151,17 +118,10 @@ class Numbers extends Object with _$NumbersSerializerMixin {
151118

152119
bool operator ==(Object other) =>
153120
other is Numbers &&
154-
_deepEquals(ints, other.ints) &&
155-
_deepEquals(nums, other.nums) &&
156-
_deepEquals(doubles, other.doubles) &&
157-
_deepEquals(nnDoubles, other.nnDoubles) &&
158-
_deepEquals(duration, other.duration) &&
159-
_deepEquals(date, other.date);
121+
deepEquals(ints, other.ints) &&
122+
deepEquals(nums, other.nums) &&
123+
deepEquals(doubles, other.doubles) &&
124+
deepEquals(nnDoubles, other.nnDoubles) &&
125+
deepEquals(duration, other.duration) &&
126+
deepEquals(date, other.date);
160127
}
161-
162-
Duration _fromJson(int ms) => new Duration(milliseconds: ms);
163-
int _toJson(Duration duration) => duration.inMilliseconds;
164-
165-
DateTime _dateTimeFromEpochUs(int us) =>
166-
new DateTime.fromMicrosecondsSinceEpoch(us);
167-
int _dateTimeToEpochUs(DateTime dateTime) => dateTime.microsecondsSinceEpoch;

json_serializable/test/integration/json_test_example.g.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ Numbers _$NumbersFromJson(Map<String, dynamic> json) {
131131
(json['doubles'] as List)?.map((e) => (e as num)?.toDouble())?.toList()
132132
..nnDoubles =
133133
(json['nnDoubles'] as List).map((e) => (e as num).toDouble()).toList()
134-
..duration =
135-
json['duration'] == null ? null : _fromJson(json['duration'] as int)
134+
..duration = json['duration'] == null
135+
? null
136+
: durationFromInt(json['duration'] as int)
136137
..date =
137-
json['date'] == null ? null : _dateTimeFromEpochUs(json['date'] as int);
138+
json['date'] == null ? null : dateTimeFromEpochUs(json['date'] as int);
138139
}
139140

140141
abstract class _$NumbersSerializerMixin {
@@ -149,7 +150,7 @@ abstract class _$NumbersSerializerMixin {
149150
'nums': nums,
150151
'doubles': doubles,
151152
'nnDoubles': nnDoubles,
152-
'duration': duration == null ? null : _toJson(duration),
153-
'date': date == null ? null : _dateTimeToEpochUs(date)
153+
'duration': duration == null ? null : durationToInt(duration),
154+
'date': date == null ? null : dateTimeToEpochUs(date)
154155
};
155156
}

json_serializable/test/integration/json_test_example.non_nullable.dart

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
// ignore_for_file: annotate_overrides, hash_and_equals
1212
import 'dart:collection';
1313

14-
import 'package:collection/collection.dart';
1514
import 'package:json_annotation/json_annotation.dart';
15+
import 'json_test_common.dart';
1616

1717
part 'json_test_example.non_nullable.g.dart';
1818

19-
enum House { gryffindor, hufflepuff, ravenclaw, slytherin }
20-
2119
@JsonSerializable(nullable: false)
2220
class Person extends Object with _$PersonSerializerMixin {
2321
final String firstName, middleName, lastName;
@@ -41,11 +39,9 @@ class Person extends Object with _$PersonSerializerMixin {
4139
lastName == other.lastName &&
4240
dateOfBirth == other.dateOfBirth &&
4341
house == other.house &&
44-
const MapEquality().equals(houseMap, other.houseMap);
42+
deepEquals(houseMap, other.houseMap);
4543
}
4644

47-
enum Category { top, bottom, strange, charmed, up, down }
48-
4945
@JsonSerializable(nullable: false)
5046
class Order extends Object with _$OrderSerializerMixin {
5147
/// Used to test that `disallowNullValues: true` forces `includeIfNull: false`
@@ -84,8 +80,8 @@ class Order extends Object with _$OrderSerializerMixin {
8480
other is Order &&
8581
count == other.count &&
8682
isRushed == other.isRushed &&
87-
_deepEquals(items, other.items) &&
88-
_deepEquals(altPlatforms, other.altPlatforms);
83+
deepEquals(items, other.items) &&
84+
deepEquals(altPlatforms, other.altPlatforms);
8985
}
9086

9187
@JsonSerializable(nullable: false)
@@ -103,36 +99,7 @@ class Item extends ItemCore with _$ItemSerializerMixin {
10399
other is Item &&
104100
price == other.price &&
105101
itemNumber == other.itemNumber &&
106-
_deepEquals(saleDates, other.saleDates);
107-
}
108-
109-
abstract class ItemCore {
110-
final int price;
111-
112-
ItemCore(this.price);
113-
}
114-
115-
bool _deepEquals(a, b) => const DeepCollectionEquality().equals(a, b);
116-
117-
class Platform {
118-
final String description;
119-
120-
static const Platform foo = const Platform._('foo');
121-
static const Platform undefined = const Platform._('undefined');
122-
const Platform._(this.description);
123-
124-
factory Platform.fromJson(String value) {
125-
switch (value) {
126-
case 'foo':
127-
return foo;
128-
case 'undefined':
129-
return undefined;
130-
default:
131-
throw new ArgumentError.value(value, 'value', 'Not a supported value.');
132-
}
133-
}
134-
135-
String toJson() => description;
102+
deepEquals(saleDates, other.saleDates);
136103
}
137104

138105
@JsonSerializable(nullable: false)
@@ -144,10 +111,10 @@ class Numbers extends Object with _$NumbersSerializerMixin {
144111
@JsonKey(nullable: false)
145112
List<double> nnDoubles;
146113

147-
@JsonKey(fromJson: _fromJson, toJson: _toJson)
114+
@JsonKey(fromJson: durationFromInt, toJson: durationToInt)
148115
Duration duration;
149116

150-
@JsonKey(fromJson: _dateTimeFromEpochUs, toJson: _dateTimeToEpochUs)
117+
@JsonKey(fromJson: dateTimeFromEpochUs, toJson: dateTimeToEpochUs)
151118
DateTime date;
152119

153120
Numbers();
@@ -157,17 +124,10 @@ class Numbers extends Object with _$NumbersSerializerMixin {
157124

158125
bool operator ==(Object other) =>
159126
other is Numbers &&
160-
_deepEquals(ints, other.ints) &&
161-
_deepEquals(nums, other.nums) &&
162-
_deepEquals(doubles, other.doubles) &&
163-
_deepEquals(nnDoubles, other.nnDoubles) &&
164-
_deepEquals(duration, other.duration) &&
165-
_deepEquals(date, other.date);
127+
deepEquals(ints, other.ints) &&
128+
deepEquals(nums, other.nums) &&
129+
deepEquals(doubles, other.doubles) &&
130+
deepEquals(nnDoubles, other.nnDoubles) &&
131+
deepEquals(duration, other.duration) &&
132+
deepEquals(date, other.date);
166133
}
167-
168-
Duration _fromJson(int ms) => new Duration(milliseconds: ms);
169-
int _toJson(Duration duration) => duration.inMilliseconds;
170-
171-
DateTime _dateTimeFromEpochUs(int us) =>
172-
new DateTime.fromMicrosecondsSinceEpoch(us);
173-
int _dateTimeToEpochUs(DateTime dateTime) => dateTime.microsecondsSinceEpoch;

json_serializable/test/integration/json_test_example.non_nullable.g.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ Numbers _$NumbersFromJson(Map<String, dynamic> json) {
103103
(json['doubles'] as List).map((e) => (e as num).toDouble()).toList()
104104
..nnDoubles =
105105
(json['nnDoubles'] as List).map((e) => (e as num).toDouble()).toList()
106-
..duration = _fromJson(json['duration'] as int)
107-
..date = _dateTimeFromEpochUs(json['date'] as int);
106+
..duration = durationFromInt(json['duration'] as int)
107+
..date = dateTimeFromEpochUs(json['date'] as int);
108108
}
109109

110110
abstract class _$NumbersSerializerMixin {
@@ -119,7 +119,7 @@ abstract class _$NumbersSerializerMixin {
119119
'nums': nums,
120120
'doubles': doubles,
121121
'nnDoubles': nnDoubles,
122-
'duration': _toJson(duration),
123-
'date': _dateTimeToEpochUs(date)
122+
'duration': durationToInt(duration),
123+
'date': dateTimeToEpochUs(date)
124124
};
125125
}

0 commit comments

Comments
 (0)