Skip to content

Commit 58f46ad

Browse files
committed
More sharing of test logic
1 parent 2dfffd0 commit 58f46ad

File tree

6 files changed

+24
-32
lines changed

6 files changed

+24
-32
lines changed

example/test/example_test.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:convert';
66

7+
import 'package:_json_serial_shared_test/shared_test.dart';
78
import 'package:example/example.dart';
89
import 'package:test/test.dart';
910

@@ -12,7 +13,7 @@ void main() {
1213
final person = Person('Inigo', 'Montoya', DateTime(1560, 5, 5))
1314
..orders = [Order(DateTime.now())..item = (Item()..count = 42)];
1415

15-
final personJson = _encode(person);
16+
final personJson = loudEncode(person);
1617

1718
final person2 =
1819
Person.fromJson(json.decode(personJson) as Map<String, dynamic>);
@@ -23,13 +24,10 @@ void main() {
2324
expect(person.orders.single.date, person2.orders.single.date);
2425
expect(person.orders.single.item!.count, 42);
2526

26-
expect(_encode(person2), equals(personJson));
27+
expect(loudEncode(person2), equals(personJson));
2728
});
2829

2930
test('JsonLiteral', () {
3031
expect(glossaryData, hasLength(1));
3132
});
3233
}
33-
34-
String _encode(Object object) =>
35-
const JsonEncoder.withIndent(' ').convert(object);

example/test/json_convert_example_test.dart

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:convert';
66

7+
import 'package:_json_serial_shared_test/shared_test.dart';
78
import 'package:example/json_converter_example.dart';
89
import 'package:test/test.dart';
910

@@ -13,7 +14,7 @@ void main() {
1314

1415
final epochDateTime = DateTime.fromMillisecondsSinceEpoch(value);
1516
final instance = DateTimeExample(epochDateTime);
16-
final json = _encode(instance);
17+
final json = loudEncode(instance);
1718
expect(json, '''{
1819
"when": $value
1920
}''');
@@ -27,13 +28,13 @@ void main() {
2728
final collection = GenericCollection<int>(
2829
page: 0, totalPages: 3, totalResults: 10, results: [1, 2, 3]);
2930

30-
final encoded = _encode(collection);
31+
final encoded = loudEncode(collection);
3132
final collection2 = GenericCollection<int>.fromJson(
3233
jsonDecode(encoded) as Map<String, dynamic>);
3334

3435
expect(collection2.results, [1, 2, 3]);
3536

36-
expect(_encode(collection2), encoded);
37+
expect(loudEncode(collection2), encoded);
3738
});
3839

3940
test('custom result', () {
@@ -43,13 +44,13 @@ void main() {
4344
totalResults: 10,
4445
results: [CustomResult('bob', 42)]);
4546

46-
final encoded = _encode(collection);
47+
final encoded = loudEncode(collection);
4748
final collection2 = GenericCollection<CustomResult>.fromJson(
4849
jsonDecode(encoded) as Map<String, dynamic>);
4950

5051
expect(collection2.results, [CustomResult('bob', 42)]);
5152

52-
expect(_encode(collection2), encoded);
53+
expect(loudEncode(collection2), encoded);
5354
});
5455

5556
test('mixed values in generic collection', () {
@@ -64,22 +65,22 @@ void main() {
6465
CustomResult('bob', 42)
6566
]);
6667

67-
final encoded = _encode(collection);
68+
final encoded = loudEncode(collection);
6869

6970
expect(
7071
() => GenericCollection<CustomResult>.fromJson(
7172
jsonDecode(encoded) as Map<String, dynamic>),
72-
_throwsTypeError,
73+
throwsTypeError,
7374
);
7475
expect(
7576
() => GenericCollection<int>.fromJson(
7677
jsonDecode(encoded) as Map<String, dynamic>),
77-
_throwsTypeError,
78+
throwsTypeError,
7879
);
7980
expect(
8081
() => GenericCollection<String>.fromJson(
8182
jsonDecode(encoded) as Map<String, dynamic>),
82-
_throwsTypeError,
83+
throwsTypeError,
8384
);
8485

8586
final collection2 =
@@ -95,11 +96,6 @@ void main() {
9596
CustomResult('bob', 42)
9697
]);
9798

98-
expect(_encode(collection2), encoded);
99+
expect(loudEncode(collection2), encoded);
99100
});
100101
}
101-
102-
final _throwsTypeError = throwsA(isA<TypeError>());
103-
104-
String _encode(Object object) =>
105-
const JsonEncoder.withIndent(' ').convert(object);

example/test/tuple_example_test.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:convert';
66

7+
import 'package:_json_serial_shared_test/shared_test.dart';
78
import 'package:example/tuple_example.dart';
89
import 'package:test/test.dart';
910

@@ -14,7 +15,7 @@ void main() {
1415
Tuple(const Duration(seconds: 42), BigInt.two),
1516
);
1617

17-
final encoded = _encode(instance);
18+
final encoded = loudEncode(instance);
1819

1920
const expected = r'''
2021
{
@@ -32,10 +33,7 @@ void main() {
3233
final decoded = ConcreteClass.fromJson(
3334
jsonDecode(encoded) as Map<String, dynamic>,
3435
);
35-
final encoded2 = _encode(decoded);
36+
final encoded2 = loudEncode(decoded);
3637
expect(encoded2, encoded);
3738
});
3839
}
39-
40-
String _encode(Object object) =>
41-
const JsonEncoder.withIndent(' ').convert(object);

json_serializable/test/annotation_version_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
@TestOn('vm')
66
@Tags(['presubmit-only'])
7-
import 'dart:convert';
87
import 'dart:io';
98

109
import 'package:json_serializable/src/check_dependencies.dart';
@@ -15,6 +14,8 @@ import 'package:test/test.dart';
1514
import 'package:test_descriptor/test_descriptor.dart' as d;
1615
import 'package:test_process/test_process.dart';
1716

17+
import 'test_utils.dart';
18+
1819
void main() {
1920
test('validate pubspec constraint', () {
2021
final annotationConstraint =
@@ -116,7 +117,7 @@ Future<void> _structurePackage({
116117
Map<String, dynamic> dependencies = const {},
117118
Map<String, dynamic> devDependencies = const {},
118119
}) async {
119-
final pubspec = const JsonEncoder.withIndent(' ').convert(
120+
final pubspec = loudEncode(
120121
{
121122
'name': '_test_pkg',
122123
'environment': {'sdk': '>=2.14.0 <3.0.0'},

json_serializable/test/test_utils.dart

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:collection/collection.dart';
6-
import 'package:test/test.dart';
76

87
export 'package:_json_serial_shared_test/shared_test.dart';
98

10-
final throwsTypeError = throwsA(isTypeError);
11-
12-
final isTypeError = isA<TypeError>();
13-
149
bool deepEquals(dynamic a, dynamic b) =>
1510
const DeepCollectionEquality().equals(a, b);

shared_test/lib/shared_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import 'dart:convert';
33
import 'package:stack_trace/stack_trace.dart';
44
import 'package:test/test.dart';
55

6+
final throwsTypeError = throwsA(isTypeError);
7+
8+
final isTypeError = isA<TypeError>();
9+
610
/// Prints out nested causes before throwing `JsonUnsupportedObjectError`.
711
String loudEncode(Object? object) {
812
try {

0 commit comments

Comments
 (0)