-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathgeneric_response_class_test.dart
82 lines (70 loc) · 1.99 KB
/
generic_response_class_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
// Copyright (c) 2020, 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 'package:example/generic_response_class_example.dart';
import 'package:test/test.dart';
const _jsonUser = {
'status': 200,
'msg': 'success',
'data': {'id': 1, 'email': 'test@test.com'}
};
final _jsonArticle = {
'status': 200,
'msg': 'success',
'data': {
'id': 2,
'title': 'title1',
'author': _jsonUser['data'],
'comments': [
{'content': 'comment context', 'id': 1},
{'content': 'comment context', 'id': 2},
]
}
};
final _jsonArticleList = {
'status': 200,
'msg': 'success',
'data': [
{'id': 1, 'title': 'title1'},
_jsonArticle['data'],
]
};
void _testResponse<T>(BaseResponse response, void Function(T) testFunction) {
expect(response.status, 200);
expect(response.msg, 'success');
testFunction(response.data as T);
}
void _testUser(User user) {
expect(user.email, 'test@test.com');
expect(user.id, 1);
}
void _testArticle(Article article) {
expect(article.id, 2);
expect(article.title, 'title1');
_testUser(article.author!);
expect(article.comments, hasLength(2));
}
void _testArticleList(List<Article> articles) {
expect(articles, hasLength(2));
_testArticle(articles[1]);
}
void main() {
test('user', () {
_testResponse(BaseResponse<User>.fromJson(_jsonUser), _testUser);
// without generic
_testResponse(BaseResponse.fromJson(_jsonUser), _testUser);
});
test('article', () {
_testResponse(BaseResponse<Article>.fromJson(_jsonArticle), _testArticle);
// without generic
_testResponse(BaseResponse.fromJson(_jsonArticle), _testArticle);
});
test('article list', () {
_testResponse(
BaseResponse<List<Article>>.fromJson(_jsonArticleList),
_testArticleList,
);
// without generic
_testResponse(BaseResponse.fromJson(_jsonArticleList), _testArticleList);
});
}