Skip to content

Commit 41b5a27

Browse files
committed
New infra package to share code across the mono repo
And migrate last test in _test_yaml to null-safety
1 parent 4de8c1f commit 41b5a27

13 files changed

+87
-109
lines changed

_test_yaml/pubspec.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ environment:
55
sdk: '>=2.14.0 <3.0.0'
66

77
dev_dependencies:
8+
_json_serial_shared_test:
9+
path: ../shared_test
810
build_runner: ^2.0.0
911
build_verify: ^3.0.0
1012
checked_yaml: any

_test_yaml/test/src/build_config.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Config {
1212
final Map<String, Builder> builders;
1313

1414
// Verifying enum keys in map
15-
Map<AutoApply, int>? weights;
15+
Map<AutoApply, int?>? weights;
1616

1717
Config({required this.builders});
1818

@@ -29,7 +29,7 @@ class Config {
2929
class Builder {
3030
final String? target;
3131

32-
final String import;
32+
final String? import;
3333

3434
@JsonKey(name: 'is_optional')
3535
final bool? isOptional;
@@ -58,7 +58,7 @@ class Builder {
5858
final Map<String, List<String>>? buildExtensions;
5959

6060
Builder({
61-
required this.import,
61+
this.import,
6262
this.target,
6363
this.isOptional,
6464
this.autoApply,

_test_yaml/test/src/build_config.g.dart

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_test_yaml/test/yaml_test.dart

+1-38
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart=2.9
6-
75
@TestOn('vm')
8-
import 'dart:convert';
96
import 'dart:io';
107

8+
import 'package:_json_serial_shared_test/shared_test.dart';
119
import 'package:checked_yaml/checked_yaml.dart';
1210
import 'package:json_annotation/json_annotation.dart';
1311
import 'package:path/path.dart' as p;
@@ -159,38 +157,3 @@ line 4, column 21 of file.yaml: Unsupported value for "configLocation". Illegal
159157
│ ^^^^^^^^^^^^^^^^^^^^^^^
160158
╵'''
161159
};
162-
163-
T roundTripObject<T>(
164-
T object,
165-
T Function(Map<String, dynamic> json) factory, {
166-
bool skipObjectEquals = false,
167-
}) {
168-
final data = loudEncode(object);
169-
170-
final object2 = factory(json.decode(data) as Map<String, dynamic>);
171-
172-
if (!skipObjectEquals) {
173-
expect(object2, equals(object));
174-
}
175-
176-
final json2 = loudEncode(object2);
177-
178-
expect(json2, equals(data));
179-
return object2;
180-
}
181-
182-
/// Prints out nested causes before throwing `JsonUnsupportedObjectError`.
183-
String loudEncode(Object object) {
184-
try {
185-
return const JsonEncoder.withIndent(' ').convert(object);
186-
} on JsonUnsupportedObjectError catch (e) // ignore: avoid_catching_errors
187-
{
188-
var error = e;
189-
do {
190-
final cause = error.cause;
191-
print(cause);
192-
error = (cause is JsonUnsupportedObjectError) ? cause : null;
193-
} while (error != null);
194-
rethrow;
195-
}
196-
}

json_serializable/pubspec.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ dependencies:
2525
source_helper: ^1.3.0
2626

2727
dev_dependencies:
28+
_json_serial_shared_test:
29+
path: ../shared_test
2830
build_runner: ^2.0.0
2931
build_verify: ^3.0.0
3032
dart_style: ^2.0.0
3133
logging: ^1.0.0
3234
source_gen_test: ^1.0.0
33-
stack_trace: ^1.10.0
3435
test: ^1.16.0
3536
test_descriptor: ^2.0.0
3637
test_process: ^2.0.0

json_serializable/test/generic_files/generic_argument_factories_nullable.dart

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart=2.12
6-
75
import 'package:json_annotation/json_annotation.dart';
86

97
part 'generic_argument_factories_nullable.g.dart';

json_serializable/test/generic_files/generic_argument_factories_nullable.g.dart

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json_serializable/test/generic_files/generic_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ void main() {
318318
});
319319

320320
test('issue 980 regression test', () {
321-
validateRoundTrip(
321+
roundTripObject(
322322
Issue980ParentClass([
323323
Issue980GenericClass(45),
324324
Issue980GenericClass(42),

json_serializable/test/integration/integration_test.dart

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Matcher _throwsArgumentError(matcher) =>
1616
void main() {
1717
group('Person', () {
1818
void roundTripPerson(Person p) {
19-
validateRoundTrip(p, (json) => Person.fromJson(json));
19+
roundTripObject(p, (json) => Person.fromJson(json));
2020
}
2121

2222
test('now', () {
@@ -50,7 +50,7 @@ void main() {
5050

5151
group('Order', () {
5252
void roundTripOrder(Order p) {
53-
validateRoundTrip(p, (json) => Order.fromJson(json));
53+
roundTripObject(p, (json) => Order.fromJson(json));
5454
}
5555

5656
test('null', () {
@@ -190,7 +190,7 @@ void main() {
190190

191191
group('Item', () {
192192
void roundTripItem(Item p) {
193-
validateRoundTrip(p, (json) => Item.fromJson(json));
193+
roundTripObject(p, (json) => Item.fromJson(json));
194194
}
195195

196196
test('empty json', () {
@@ -231,7 +231,7 @@ void main() {
231231

232232
group('Numbers', () {
233233
void roundTripNumber(Numbers p) {
234-
validateRoundTrip(p, (json) => Numbers.fromJson(json));
234+
roundTripObject(p, (json) => Numbers.fromJson(json));
235235
}
236236

237237
test('simple', () {
@@ -276,7 +276,7 @@ void main() {
276276
..intIntMap = {3: 3}
277277
..uriIntMap = {Uri.parse('https://example.com'): 4};
278278

279-
validateRoundTrip(instance, (j) => MapKeyVariety.fromJson(j));
279+
roundTripObject(instance, (j) => MapKeyVariety.fromJson(j));
280280
});
281281

282282
test('UnknownEnumValue', () {
@@ -296,7 +296,7 @@ void main() {
296296
test('PrivateConstructor', () {
297297
final value = PrivateConstructor('test');
298298

299-
validateRoundTrip(value, (json) => PrivateConstructor.fromJson(json));
299+
roundTripObject(value, (json) => PrivateConstructor.fromJson(json));
300300
});
301301

302302
test('enum helpers', () {

json_serializable/test/kitchen_sink/kitchen_sink_test.dart

+6-6
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void _nonNullableTests(KitchenSinkFactory factory) {
8484

8585
void _nullableTests(KitchenSinkFactory factory) {
8686
void roundTripSink(KitchenSink p) {
87-
validateRoundTrip(p, factory.fromJson);
87+
roundTripObject(p, factory.fromJson);
8888
}
8989

9090
test('nullable values are allowed in the nullable version', () {
@@ -180,7 +180,7 @@ void _sharedTests(KitchenSinkFactory factory) {
180180

181181
test('empty', () {
182182
final item = factory.ctor();
183-
validateRoundTrip(item, factory.fromJson);
183+
roundTripObject(item, factory.fromJson);
184184
});
185185

186186
test('list and map of DateTime - not null', () {
@@ -189,7 +189,7 @@ void _sharedTests(KitchenSinkFactory factory) {
189189
..dateTimeList = <DateTime>[now, now]
190190
..objectDateTimeMap = <Object, DateTime>{'value': now};
191191

192-
validateRoundTrip(item, factory.fromJson);
192+
roundTripObject(item, factory.fromJson);
193193
});
194194

195195
test('complex nested type - not null', () {
@@ -207,7 +207,7 @@ void _sharedTests(KitchenSinkFactory factory) {
207207
}
208208
}
209209
];
210-
validateRoundTrip(item, factory.fromJson);
210+
roundTripObject(item, factory.fromJson);
211211
});
212212

213213
test('round trip valid, empty values', () {
@@ -226,7 +226,7 @@ void _sharedTests(KitchenSinkFactory factory) {
226226

227227
final validInstance = factory.fromJson(values);
228228

229-
validateRoundTrip(validInstance, factory.fromJson);
229+
roundTripObject(validInstance, factory.fromJson);
230230
});
231231

232232
test('JSON keys should be defined in field/property order', () {
@@ -240,7 +240,7 @@ void _sharedTests(KitchenSinkFactory factory) {
240240

241241
test('valid values round-trip - json', () {
242242
final validInstance = factory.fromJson(validValues);
243-
validateRoundTrip(validInstance, factory.fromJson);
243+
roundTripObject(validInstance, factory.fromJson);
244244
});
245245
}
246246

json_serializable/test/test_utils.dart

+2-48
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,14 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:convert';
6-
75
import 'package:collection/collection.dart';
8-
import 'package:stack_trace/stack_trace.dart';
96
import 'package:test/test.dart';
107

8+
export 'package:_json_serial_shared_test/shared_test.dart';
9+
1110
final throwsTypeError = throwsA(isTypeError);
1211

1312
final isTypeError = isA<TypeError>();
1413

1514
bool deepEquals(dynamic a, dynamic b) =>
1615
const DeepCollectionEquality().equals(a, b);
17-
18-
void validateRoundTrip<T>(
19-
T object,
20-
T Function(Map<String, dynamic> json) factory,
21-
) {
22-
final data = loudEncode(object);
23-
24-
final object2 = factory(json.decode(data) as Map<String, dynamic>);
25-
26-
expect(object2, equals(object));
27-
28-
final json2 = loudEncode(object2);
29-
30-
expect(json2, equals(data));
31-
}
32-
33-
/// Prints out nested causes before throwing `JsonUnsupportedObjectError`.
34-
String loudEncode(Object? object) {
35-
try {
36-
return const JsonEncoder.withIndent(' ').convert(object);
37-
} catch (e) {
38-
if (e is JsonUnsupportedObjectError) {
39-
Object? error = e;
40-
41-
var count = 1;
42-
43-
while (error is JsonUnsupportedObjectError) {
44-
print(
45-
'(${count++}) $error ${error.unsupportedObject} '
46-
'(${error.unsupportedObject.runtimeType}) !!!',
47-
);
48-
print(Trace.from(error.stackTrace!).terse);
49-
error = error.cause;
50-
}
51-
52-
if (error != null) {
53-
print('(${count++}) $error ???');
54-
if (error is Error) {
55-
print(Trace.from(error.stackTrace!).terse);
56-
}
57-
}
58-
}
59-
rethrow;
60-
}
61-
}

shared_test/lib/shared_test.dart

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'dart:convert';
2+
3+
import 'package:stack_trace/stack_trace.dart';
4+
import 'package:test/test.dart';
5+
6+
/// Prints out nested causes before throwing `JsonUnsupportedObjectError`.
7+
String loudEncode(Object? object) {
8+
try {
9+
return const JsonEncoder.withIndent(' ').convert(object);
10+
} catch (e) {
11+
if (e is JsonUnsupportedObjectError) {
12+
Object? error = e;
13+
14+
var count = 1;
15+
16+
while (error is JsonUnsupportedObjectError) {
17+
print(
18+
'(${count++}) $error ${error.unsupportedObject} '
19+
'(${error.unsupportedObject.runtimeType}) !!!',
20+
);
21+
print(Trace.from(error.stackTrace!).terse);
22+
error = error.cause;
23+
}
24+
25+
if (error != null) {
26+
print('(${count++}) $error ???');
27+
if (error is Error) {
28+
print(Trace.from(error.stackTrace!).terse);
29+
}
30+
}
31+
}
32+
rethrow;
33+
}
34+
}
35+
36+
T roundTripObject<T>(
37+
T object,
38+
T Function(Map<String, dynamic> json) factory, {
39+
bool skipObjectEquals = false,
40+
}) {
41+
final data = loudEncode(object);
42+
43+
final object2 = factory(json.decode(data) as Map<String, dynamic>);
44+
45+
if (!skipObjectEquals) {
46+
expect(object2, equals(object));
47+
}
48+
49+
final json2 = loudEncode(object2);
50+
51+
expect(json2, equals(data));
52+
return object2;
53+
}

shared_test/pubspec.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: _json_serial_shared_test
2+
publish_to: none
3+
environment:
4+
sdk: '>=2.14.0 <3.0.0'
5+
6+
dependencies:
7+
stack_trace: ^1.10.0
8+
test: ^1.6.0

0 commit comments

Comments
 (0)