Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(307)

Side by Side Diff: pkg/analysis_server/test/protocol_test.dart

Issue 513853002: In the analysis server API, change RequestError.code to an enum. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library test.protocol; 5 library test.protocol;
6 6
7 import 'dart:convert'; 7 import 'dart:convert';
8 8
9 import 'package:analysis_server/src/protocol.dart'; 9 import 'package:analysis_server/src/protocol.dart';
10 import 'package:analysis_testing/reflective_tests.dart'; 10 import 'package:analysis_testing/reflective_tests.dart';
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 expect(notification.toJson(), equals({ 87 expect(notification.toJson(), equals({
88 'event': 'foo' 88 'event': 'foo'
89 })); 89 }));
90 } 90 }
91 } 91 }
92 92
93 93
94 @ReflectiveTestCase() 94 @ReflectiveTestCase()
95 class RequestErrorTest { 95 class RequestErrorTest {
96 void test_create() { 96 void test_create() {
97 RequestError error = new RequestError('ERROR_CODE', 'msg'); 97 RequestError error = new RequestError(RequestErrorCode.INVALID_REQUEST, 'msg ');
98 expect(error.code, 'ERROR_CODE'); 98 expect(error.code, RequestErrorCode.INVALID_REQUEST);
99 expect(error.message, "msg"); 99 expect(error.message, "msg");
100 expect(error.toJson(), equals({ 100 expect(error.toJson(), equals({
101 RequestError.CODE: 'ERROR_CODE', 101 RequestError.CODE: 'INVALID_REQUEST',
102 RequestError.MESSAGE: "msg" 102 RequestError.MESSAGE: "msg"
103 })); 103 }));
104 } 104 }
105 105
106 void test_create_internalError() {
107 RequestError error = new RequestError.internalError();
108 expect(error.code, RequestError.CODE_INTERNAL_ERROR);
109 expect(error.message, "Internal error");
110 }
111
112 void test_create_invalidParameters() {
113 RequestError error = new RequestError.invalidParameters();
114 expect(error.code, RequestError.CODE_INVALID_PARAMS);
115 expect(error.message, "Invalid parameters");
116 }
117
118 void test_create_invalidRequest() {
119 RequestError error = new RequestError.invalidRequest();
120 expect(error.code, RequestError.CODE_INVALID_REQUEST);
121 expect(error.message, "Invalid request");
122 }
123
124 void test_create_methodNotFound() {
125 RequestError error = new RequestError.methodNotFound();
126 expect(error.code, RequestError.CODE_METHOD_NOT_FOUND);
127 expect(error.message, "Method not found");
128 }
129
130 void test_create_parseError() {
131 RequestError error = new RequestError.parseError();
132 expect(error.code, RequestError.CODE_PARSE_ERROR);
133 expect(error.message, "Parse error");
134 }
135
136 void test_create_serverAlreadyStarted() { 106 void test_create_serverAlreadyStarted() {
137 RequestError error = new RequestError.serverAlreadyStarted(); 107 RequestError error = new RequestError.serverAlreadyStarted();
138 expect(error.code, RequestError.CODE_SERVER_ALREADY_STARTED); 108 expect(error.code, RequestErrorCode.SERVER_ALREADY_STARTED);
139 expect(error.message, "Server already started"); 109 expect(error.message, "Server already started");
140 } 110 }
141 111
142 void test_fromJson() { 112 void test_fromJson() {
143 var json = { 113 var json = {
144 RequestError.CODE: RequestError.CODE_PARSE_ERROR, 114 RequestError.CODE: RequestErrorCode.INVALID_PARAMETER.name,
145 RequestError.MESSAGE: 'foo', 115 RequestError.MESSAGE: 'foo',
146 RequestError.DATA: { 116 RequestError.DATA: {
147 'ints': [1, 2, 3] 117 'ints': [1, 2, 3]
148 } 118 }
149 }; 119 };
150 RequestError error = new RequestError.fromJson(json); 120 RequestError error = new RequestError.fromJson(json);
151 expect(error.code, RequestError.CODE_PARSE_ERROR); 121 expect(error.code, RequestErrorCode.INVALID_PARAMETER);
152 expect(error.message, "foo"); 122 expect(error.message, "foo");
153 expect(error.data['ints'], [1, 2, 3]); 123 expect(error.data['ints'], [1, 2, 3]);
154 expect(error.getData('ints'), [1, 2, 3]); 124 expect(error.getData('ints'), [1, 2, 3]);
155 } 125 }
156 126
157 void test_toJson() { 127 void test_toJson() {
158 RequestError error = new RequestError('ERROR_CODE', 'msg'); 128 RequestError error = new RequestError(RequestErrorCode.UNKNOWN_REQUEST, 'msg ');
159 error.setData('answer', 42); 129 error.setData('answer', 42);
160 error.setData('question', 'unknown'); 130 error.setData('question', 'unknown');
161 expect(error.toJson(), { 131 expect(error.toJson(), {
162 RequestError.CODE: 'ERROR_CODE', 132 RequestError.CODE: 'UNKNOWN_REQUEST',
163 RequestError.MESSAGE: 'msg', 133 RequestError.MESSAGE: 'msg',
164 RequestError.DATA: { 134 RequestError.DATA: {
165 'answer': 42, 135 'answer': 42,
166 'question': 'unknown' 136 'question': 'unknown'
167 } 137 }
168 }); 138 });
169 } 139 }
170 } 140 }
171 141
172 142
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 Request.PARAMS: { 195 Request.PARAMS: {
226 'foo': 'bar' 196 'foo': 'bar'
227 } 197 }
228 })); 198 }));
229 } 199 }
230 } 200 }
231 201
232 202
233 @ReflectiveTestCase() 203 @ReflectiveTestCase()
234 class ResponseTest { 204 class ResponseTest {
235 void test_create_contextDoesNotExist() {
236 Response response = new Response.contextDoesNotExist(new Request('0', ''));
237 expect(response.id, equals('0'));
238 expect(response.error, isNotNull);
239 expect(response.toJson(), equals({
240 Response.ID: '0',
241 Response.ERROR: {
242 'code': 'NONEXISTENT_CONTEXT',
243 'message': 'Context does not exist'
244 }
245 }));
246 }
247
248 void test_create_invalidRequestFormat() { 205 void test_create_invalidRequestFormat() {
249 Response response = new Response.invalidRequestFormat(); 206 Response response = new Response.invalidRequestFormat();
250 expect(response.id, equals('')); 207 expect(response.id, equals(''));
251 expect(response.error, isNotNull); 208 expect(response.error, isNotNull);
252 expect(response.toJson(), equals({ 209 expect(response.toJson(), equals({
253 Response.ID: '', 210 Response.ID: '',
254 Response.ERROR: { 211 Response.ERROR: {
255 'code': 'INVALID_REQUEST', 212 'code': 'INVALID_REQUEST',
256 'message': 'Invalid request' 213 'message': 'Invalid request'
257 } 214 }
258 })); 215 }));
259 } 216 }
260 217
261 void test_create_missingRequiredParameter() {
262 Response response = new Response.missingRequiredParameter(new Request('0',
263 ''), 'x');
264 expect(response.id, equals('0'));
265 expect(response.error, isNotNull);
266 expect(response.toJson(), equals({
267 Response.ID: '0',
268 Response.ERROR: {
269 'code': 'MISSING_PARAMETER',
270 'message': 'Missing required parameter: x'
271 }
272 }));
273 }
274
275 void test_create_unanalyzedPriorityFiles() { 218 void test_create_unanalyzedPriorityFiles() {
276 Response response = new Response.unanalyzedPriorityFiles(new Request('0', 219 Response response = new Response.unanalyzedPriorityFiles(new Request('0',
277 ''), 'file list'); 220 ''), 'file list');
278 expect(response.id, equals('0')); 221 expect(response.id, equals('0'));
279 expect(response.error, isNotNull); 222 expect(response.error, isNotNull);
280 expect(response.toJson(), equals({ 223 expect(response.toJson(), equals({
281 Response.ID: '0', 224 Response.ID: '0',
282 Response.ERROR: { 225 Response.ERROR: {
283 'code': 'UNANALYZED_PRIORITY_FILES', 226 'code': 'UNANALYZED_PRIORITY_FILES',
284 'message': "Unanalyzed files cannot be a priority: 'file list'" 227 'message': "Unanalyzed files cannot be a priority: 'file list'"
285 } 228 }
286 })); 229 }));
287 } 230 }
288 231
289 void test_create_unknownAnalysisOption() {
290 Response response = new Response.unknownAnalysisOption(new Request('0', ''),
291 'x');
292 expect(response.id, equals('0'));
293 expect(response.error, isNotNull);
294 expect(response.toJson(), equals({
295 Response.ID: '0',
296 Response.ERROR: {
297 'code': 'UNKNOWN_ANALYSIS_OPTION',
298 'message': 'Unknown analysis option: "x"'
299 }
300 }));
301 }
302
303 void test_create_unknownRequest() { 232 void test_create_unknownRequest() {
304 Response response = new Response.unknownRequest(new Request('0', '')); 233 Response response = new Response.unknownRequest(new Request('0', ''));
305 expect(response.id, equals('0')); 234 expect(response.id, equals('0'));
306 expect(response.error, isNotNull); 235 expect(response.error, isNotNull);
307 expect(response.toJson(), equals({ 236 expect(response.toJson(), equals({
308 Response.ID: '0', 237 Response.ID: '0',
309 Response.ERROR: { 238 Response.ERROR: {
310 'code': 'UNKNOWN_REQUEST', 239 'code': 'UNKNOWN_REQUEST',
311 'message': 'Unknown request' 240 'message': 'Unknown request'
312 } 241 }
313 })); 242 }));
314 } 243 }
315 244
316 void test_fromJson() { 245 void test_fromJson() {
317 Response original = new Response('myId'); 246 Response original = new Response('myId');
318 Response response = new Response.fromJson(original.toJson()); 247 Response response = new Response.fromJson(original.toJson());
319 expect(response.id, equals('myId')); 248 expect(response.id, equals('myId'));
320 } 249 }
321 250
322 void test_fromJson_withError() { 251 void test_fromJson_withError() {
323 Response original = new Response.invalidRequestFormat(); 252 Response original = new Response.invalidRequestFormat();
324 Response response = new Response.fromJson(original.toJson()); 253 Response response = new Response.fromJson(original.toJson());
325 expect(response.id, equals('')); 254 expect(response.id, equals(''));
326 expect(response.error, isNotNull); 255 expect(response.error, isNotNull);
327 RequestError error = response.error; 256 RequestError error = response.error;
328 expect(error.code, equals('INVALID_REQUEST')); 257 expect(error.code, equals(RequestErrorCode.INVALID_REQUEST));
329 expect(error.message, equals('Invalid request')); 258 expect(error.message, equals('Invalid request'));
330 } 259 }
331 260
332 void test_fromJson_withResult() { 261 void test_fromJson_withResult() {
333 Response original = new Response('myId', result: {'foo': 'bar'}); 262 Response original = new Response('myId', result: {'foo': 'bar'});
334 Response response = new Response.fromJson(original.toJson()); 263 Response response = new Response.fromJson(original.toJson());
335 expect(response.id, equals('myId')); 264 expect(response.id, equals('myId'));
336 Map<String, Object> result = response.toJson()['result']; 265 Map<String, Object> result = response.toJson()['result'];
337 expect(result.length, equals(1)); 266 expect(result.length, equals(1));
338 expect(result['foo'], equals('bar')); 267 expect(result['foo'], equals('bar'));
339 } 268 }
340 } 269 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698