-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathjson_convert_example_test.dart
101 lines (82 loc) · 2.7 KB
/
json_convert_example_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// 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.
import 'dart:convert';
import 'package:_json_serial_shared_test/shared_test.dart';
import 'package:example/json_converter_example.dart';
import 'package:test/test.dart';
void main() {
test('DateTime epoch', () {
const value = 42;
final epochDateTime = DateTime.fromMillisecondsSinceEpoch(value);
final instance = DateTimeExample(epochDateTime);
final json = loudEncode(instance);
expect(json, '''{
"when": $value
}''');
final copy =
DateTimeExample.fromJson(jsonDecode(json) as Map<String, dynamic>);
expect(copy.when, epochDateTime);
});
test('trivial case', () {
final collection = GenericCollection<int>(
page: 0, totalPages: 3, totalResults: 10, results: [1, 2, 3]);
final encoded = loudEncode(collection);
final collection2 = GenericCollection<int>.fromJson(
jsonDecode(encoded) as Map<String, dynamic>);
expect(collection2.results, [1, 2, 3]);
expect(loudEncode(collection2), encoded);
});
test('custom result', () {
final collection = GenericCollection<CustomResult>(
page: 0,
totalPages: 3,
totalResults: 10,
results: [CustomResult('bob', 42)]);
final encoded = loudEncode(collection);
final collection2 = GenericCollection<CustomResult>.fromJson(
jsonDecode(encoded) as Map<String, dynamic>);
expect(collection2.results, [CustomResult('bob', 42)]);
expect(loudEncode(collection2), encoded);
});
test('mixed values in generic collection', () {
final collection =
GenericCollection(page: 0, totalPages: 3, totalResults: 10, results: [
1,
3.14,
null,
'bob',
['list'],
{'map': 'map'},
CustomResult('bob', 42)
]);
final encoded = loudEncode(collection);
expect(
() => GenericCollection<CustomResult>.fromJson(
jsonDecode(encoded) as Map<String, dynamic>),
throwsTypeError,
);
expect(
() => GenericCollection<int>.fromJson(
jsonDecode(encoded) as Map<String, dynamic>),
throwsTypeError,
);
expect(
() => GenericCollection<String>.fromJson(
jsonDecode(encoded) as Map<String, dynamic>),
throwsTypeError,
);
final collection2 = GenericCollection<dynamic>.fromJson(
jsonDecode(encoded) as Map<String, dynamic>);
expect(collection2.results, [
1,
3.14,
null,
'bob',
['list'],
{'map': 'map'},
CustomResult('bob', 42)
]);
expect(loudEncode(collection2), encoded);
});
}