@@ -6,33 +6,29 @@ import 'dart:convert' show JSON;
6
6
import 'package:github/src/common.dart' ;
7
7
import 'package:github/src/util.dart' ;
8
8
import "package:http/http.dart" as http;
9
- import 'package:mock/mock .dart' ;
9
+ import 'package:mockito/mockito .dart' ;
10
10
import 'package:test/test.dart' ;
11
11
12
- import 'helper.dart' ;
13
-
14
- class MockGitHub extends MockWithNamedArgs implements GitHub {}
12
+ class MockGitHub extends Mock implements GitHub {}
15
13
16
14
void main () {
17
15
MockGitHub github;
18
16
GitService git;
19
17
RepositorySlug repo;
18
+ var someSha = 'someSHA' ;
20
19
21
20
setUp (() {
22
21
github = new MockGitHub ();
23
22
git = new GitService (github);
24
23
repo = new RepositorySlug ('o' , 'n' );
25
24
});
26
25
27
- //
28
- // BLOB
29
- //
30
26
group ('getBlob()' , () {
31
27
test ('constructs correct path' , () {
32
28
git.getBlob (repo, 'sh' );
33
- github
34
- . getLogs ( callsTo ( 'getJSON' , ' /repos/o/n/git/blobs/sh'))
35
- . verify (happenedOnce );
29
+
30
+ verify (github. getJSON ( ' /repos/o/n/git/blobs/sh',
31
+ convert : GitBlob .fromJSON, statusCode : StatusCodes . OK ) );
36
32
});
37
33
});
38
34
@@ -41,33 +37,28 @@ void main() {
41
37
CreateGitBlob blob = new CreateGitBlob ('bbb' , 'utf-8' );
42
38
git.createBlob (repo, blob);
43
39
44
- github
45
- .getLogs (callsTo ('postJSON' , '/repos/o/n/git/blobs' ))
46
- .verify (happenedOnce);
40
+ verify (github.postJSON ('/repos/o/n/git/blobs' ,
41
+ convert: GitBlob .fromJSON,
42
+ statusCode: StatusCodes .CREATED ,
43
+ body: blob.toJSON ()));
47
44
});
48
45
49
46
test ('creates valid JSON body' , () {
50
47
CreateGitBlob blob = new CreateGitBlob ('bbb' , 'utf-8' );
51
- git.createBlob (repo, blob);
52
-
53
- LogEntryNamedArgs entry = github.getLogs ().first;
54
- Map body = JSON .decode (entry.namedArgs[#body]);
55
48
49
+ git.createBlob (repo, blob);
50
+ var body = captureSentBody (github);
56
51
expect (body['content' ], equals ('bbb' ));
57
52
expect (body['encoding' ], equals ('utf-8' ));
58
53
});
59
54
});
60
55
61
- //
62
- // COMMIT
63
- //
64
56
group ('getCommit()' , () {
65
57
test ('constructs correct path' , () {
66
58
git.getCommit (repo, 'sh' );
67
59
68
- github
69
- .getLogs (callsTo ('getJSON' , '/repos/o/n/git/commits/sh' ))
70
- .verify (happenedOnce);
60
+ verify (github.getJSON ('/repos/o/n/git/commits/sh' ,
61
+ convert: GitCommit .fromJSON, statusCode: StatusCodes .OK ));
71
62
});
72
63
});
73
64
@@ -76,9 +67,10 @@ void main() {
76
67
CreateGitCommit commit = new CreateGitCommit ('aMessage' , 'aTreeSha' );
77
68
git.createCommit (repo, commit);
78
69
79
- github
80
- .getLogs (callsTo ('postJSON' , '/repos/o/n/git/commits' ))
81
- .verify (happenedOnce);
70
+ verify (github.postJSON ('/repos/o/n/git/commits' ,
71
+ convert: GitCommit .fromJSON,
72
+ statusCode: StatusCodes .CREATED ,
73
+ body: commit.toJSON ()));
82
74
});
83
75
84
76
test ('creates valid JSON body' , () {
@@ -94,9 +86,7 @@ void main() {
94
86
git.createCommit (repo, commit);
95
87
96
88
// then
97
- LogEntryNamedArgs entry = github.getLogs ().first;
98
- Map body = JSON .decode (entry.namedArgs[#body]);
99
-
89
+ var body = captureSentBody (github);
100
90
expect (body['message' ], equals ('aMessage' ));
101
91
expect (body['tree' ], equals ('aTreeSha' ));
102
92
expect (body['parents' ], equals (['parentSha1' , 'parentSha2' ]));
@@ -109,73 +99,71 @@ void main() {
109
99
});
110
100
});
111
101
112
- //
113
- // REFERENCE
114
- //
115
102
group ('getReference()' , () {
116
103
test ('constructs correct path' , () {
117
104
git.getReference (repo, 'heads/b' );
118
105
119
- github
120
- .getLogs (callsTo ('getJSON' , '/repos/o/n/git/refs/heads/b' ))
121
- .verify (happenedOnce);
106
+ verify (github.getJSON ('/repos/o/n/git/refs/heads/b' ,
107
+ convert: GitReference .fromJSON, statusCode: StatusCodes .OK ));
122
108
});
123
109
});
124
110
125
111
group ('createReference()' , () {
112
+ var someRef = 'refs/heads/b' ;
126
113
test ('constructs correct path' , () {
127
- git.createReference (repo, 'refs/heads/b' , 'someSHA' );
114
+ git.createReference (repo, someRef, someSha );
128
115
129
- github
130
- .getLogs (callsTo ('postJSON' , '/repos/o/n/git/refs' ))
131
- .verify (happenedOnce);
116
+ verify (github.postJSON ('/repos/o/n/git/refs' ,
117
+ convert: GitReference .fromJSON,
118
+ statusCode: StatusCodes .CREATED ,
119
+ body: JSON .encode ({'ref' : someRef, 'sha' : someSha})));
132
120
});
133
121
134
122
test ('creates valid JSON body' , () {
135
- git.createReference (repo, 'refs/heads/b' , 'someSHA' );
136
-
137
- // then
138
- LogEntryNamedArgs entry = github.getLogs ().first;
139
- Map body = JSON .decode (entry.namedArgs[#body]);
123
+ git.createReference (repo, someRef, someSha);
140
124
141
- expect (body['ref' ], equals ('refs/heads/b' ));
142
- expect (body['sha' ], equals ('someSHA' ));
125
+ var body = captureSentBody (github);
126
+ expect (body['ref' ], equals (someRef));
127
+ expect (body['sha' ], equals (someSha));
143
128
});
144
129
});
145
130
146
131
group ('editReference()' , () {
147
132
test ('constructs correct path' , () {
148
133
// given
149
- http.Response res = new http.Response ('{}' , 200 );
150
- github
151
- .when (callsTo ('request' , anything, anything))
152
- .alwaysReturn (new Future .value (res));
134
+ http.Response expectedResponse = new http.Response ('{}' , 200 );
135
+
136
+ when (github.request (any, any,
137
+ body: any, headers: typed (any, named: 'headers' )))
138
+ .thenReturn (new Future .value (expectedResponse));
153
139
154
140
// when
155
- git.editReference (repo, 'heads/b' , 'someSHA' );
141
+ git.editReference (repo, 'heads/b' , someSha );
156
142
157
143
// then
158
- github
159
- .getLogs (callsTo ('request' , 'PATCH' , '/repos/o/n/git/refs/heads/b' ))
160
- .verify (happenedOnce);
144
+ verify (github.request ('PATCH' , '/repos/o/n/git/refs/heads/b' ,
145
+ headers: typed (any, named: 'headers' ), body: any));
161
146
});
162
147
163
148
test ('creates valid JSON body' , () {
164
149
// given
165
- http.Response res = new http.Response ('{}' , 200 );
166
- github
167
- . when ( callsTo ( 'request' , anything, anything ))
168
- .alwaysReturn (new Future .value (res ));
150
+ http.Response expectedResponse = new http.Response ('{}' , 200 );
151
+ when ( github. request (any, any,
152
+ body : any, headers : typed (any, named : 'headers' ) ))
153
+ .thenReturn (new Future .value (expectedResponse ));
169
154
170
155
// when
171
- git.editReference (repo, 'heads/b' , 'someSHA' , force: true );
156
+ git.editReference (repo, 'heads/b' , someSha , force: true );
172
157
173
158
// then
174
- LogEntryNamedArgs entry = github.getLogs ().first;
175
- Map body = JSON . decode (entry.namedArgs[#body]);
176
- Map headers = entry.namedArgs[#headers] ;
159
+ var captured = verify ( github.request (any, any,
160
+ body: captureAny, headers : typed (captureAny, named : 'headers' )))
161
+ .captured ;
177
162
178
- expect (body['sha' ], equals ('someSHA' ));
163
+ var body = JSON .decode (captured[0 ]);
164
+ var headers = captured[1 ];
165
+
166
+ expect (body['sha' ], equals (someSha));
179
167
expect (body['force' ], equals (true ));
180
168
expect (headers['content-length' ], equals ('30' ));
181
169
});
@@ -184,96 +172,79 @@ void main() {
184
172
group ('deleteReference()' , () {
185
173
test ('constructs correct path' , () {
186
174
// given
187
- http.Response res = new http.Response ('{}' , 200 );
188
- github
189
- .when (callsTo ('request' , anything, anything))
190
- .alwaysReturn (new Future .value (res));
175
+ http.Response expectedResponse = new http.Response ('{}' , 200 );
176
+ when (github.request (any, any))
177
+ .thenReturn (new Future .value (expectedResponse));
191
178
192
179
// when
193
180
git.deleteReference (repo, 'heads/b' );
194
181
195
182
// then
196
- github
197
- .getLogs (callsTo ('request' , 'DELETE' , '/repos/o/n/git/refs/heads/b' ))
198
- .verify (happenedOnce);
183
+ verify (github.request ('DELETE' , '/repos/o/n/git/refs/heads/b' ));
199
184
});
200
185
});
201
186
202
- //
203
- // TAG
204
- //
205
187
group ('getTag()' , () {
206
188
test ('constructs correct path' , () {
207
- git.getTag (repo, 'someSHA' );
189
+ git.getTag (repo, someSha );
208
190
209
- github
210
- .getLogs (callsTo ('getJSON' , '/repos/o/n/git/tags/someSHA' ))
211
- .verify (happenedOnce);
191
+ verify (github.getJSON ('/repos/o/n/git/tags/someSHA' ,
192
+ convert: GitTag .fromJSON, statusCode: StatusCodes .OK ));
212
193
});
213
194
});
214
195
215
196
group ('createTag()' , () {
197
+ var createGitTag = new CreateGitTag ('v0.0.1' , 'a message' , someSha,
198
+ 'commit' , new GitCommitUser ('aName' , 'aEmail' , new DateTime .now ()));
199
+
216
200
test ('constructs correct path' , () {
217
- git.createTag (
218
- repo,
219
- new CreateGitTag ('v0.0.1' , 'a message' , 'someSHA' , 'commit' ,
220
- new GitCommitUser ('aName' , 'aEmail' , new DateTime .now ())));
221
-
222
- github
223
- .getLogs (callsTo ('postJSON' , '/repos/o/n/git/tags' ))
224
- .verify (happenedOnce);
201
+ git.createTag (repo, createGitTag);
202
+
203
+ verify (github.postJSON ('/repos/o/n/git/tags' ,
204
+ convert: GitTag .fromJSON,
205
+ statusCode: StatusCodes .CREATED ,
206
+ body: createGitTag.toJSON ()));
225
207
});
226
208
227
209
test ('creates valid JSON body' , () {
228
- git.createTag (
229
- repo,
230
- new CreateGitTag ('v0.0.1' , 'a message' , 'someSHA' , 'commit' ,
231
- new GitCommitUser ('aName' , 'aEmail' , new DateTime .now ())));
232
-
233
- LogEntryNamedArgs entry = github.getLogs ().first;
234
- Map body = JSON .decode (entry.namedArgs[#body]);
210
+ git.createTag (repo, createGitTag);
235
211
212
+ var body = captureSentBody (github);
236
213
expect (body['tag' ], equals ('v0.0.1' ));
237
214
expect (body['message' ], equals ('a message' ));
238
- expect (body['object' ], equals ('someSHA' ));
215
+ expect (body['object' ], equals (someSha ));
239
216
expect (body['type' ], equals ('commit' ));
240
217
expect (body['tagger' ]['name' ], equals ('aName' ));
241
218
});
242
219
});
243
220
244
- //
245
- // TREE
246
- //
247
- //
248
- // TREE
249
- //
250
221
group ('getTree()' , () {
251
222
test ('constructs correct path' , () {
252
223
git.getTree (repo, 'sh' );
253
224
254
- github
255
- .getLogs (callsTo ('getJSON' , '/repos/o/n/git/trees/sh' ))
256
- .verify (happenedOnce);
225
+ verify (github.getJSON ('/repos/o/n/git/trees/sh' ,
226
+ convert: GitTree .fromJSON, statusCode: StatusCodes .OK ));
257
227
});
258
228
});
259
229
260
230
group ('getTree(recursive: true)' , () {
261
231
test ('constructs correct path' , () {
262
232
git.getTree (repo, 'sh' , recursive: true );
263
233
264
- github
265
- .getLogs (callsTo ('getJSON' , '/repos/o/n/git/trees/sh?recursive=1' ))
266
- .verify (happenedOnce);
234
+ verify (github.getJSON ('/repos/o/n/git/trees/sh?recursive=1' ,
235
+ convert: GitTree .fromJSON, statusCode: StatusCodes .OK ));
267
236
});
268
237
});
269
238
270
239
group ('createTree()' , () {
271
240
test ('constructs correct path' , () {
272
- git.createTree (repo, new CreateGitTree ([]));
241
+ var createGitTree = new CreateGitTree ([]);
242
+ git.createTree (repo, createGitTree);
273
243
274
- github
275
- .getLogs (callsTo ('postJSON' , '/repos/o/n/git/trees' ))
276
- .verify (happenedOnce);
244
+ verify (github.postJSON ('/repos/o/n/git/trees' ,
245
+ convert: GitTree .fromJSON,
246
+ statusCode: StatusCodes .CREATED ,
247
+ body: createGitTree.toJSON ()));
277
248
});
278
249
279
250
test ('with sha creates valid JSON body' , () {
@@ -287,9 +258,7 @@ void main() {
287
258
git.createTree (repo, tree);
288
259
289
260
// then
290
- LogEntryNamedArgs entry = github.getLogs ().first;
291
- Map body = JSON .decode (entry.namedArgs[#body]);
292
-
261
+ var body = captureSentBody (github);
293
262
expect (body['tree' ], isNotNull);
294
263
expect (body['tree' ][0 ]['path' ], equals ('file.rb' ));
295
264
expect (body['tree' ][0 ]['mode' ], equals ('100644' ));
@@ -310,9 +279,7 @@ void main() {
310
279
git.createTree (repo, tree);
311
280
312
281
// then
313
- LogEntryNamedArgs entry = github.getLogs ().first;
314
- Map body = JSON .decode (entry.namedArgs[#body]);
315
-
282
+ var body = captureSentBody (github);
316
283
expect (body['tree' ], isNotNull);
317
284
expect (body['tree' ][0 ]['path' ], equals ('file.rb' ));
318
285
expect (body['tree' ][0 ]['mode' ], equals ('100644' ));
@@ -322,3 +289,13 @@ void main() {
322
289
});
323
290
});
324
291
}
292
+
293
+ Map <String , dynamic > captureSentBody (MockGitHub github) {
294
+ var bodyString = verify (
295
+ github.postJSON (any, convert: any, statusCode: any, body: captureAny))
296
+ .captured
297
+ .single;
298
+
299
+ var body = JSON .decode (bodyString) as Map <String , dynamic >;
300
+ return body;
301
+ }
0 commit comments