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

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

Issue 227713004: Improve analysis_server/protocol.dart test coverage. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 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
« no previous file with comments | « pkg/analysis_server/lib/src/protocol.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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:unittest/unittest.dart'; 10 import 'package:unittest/unittest.dart';
11 11
12 main() { 12 main() {
13 group('Notification', () { 13 group('Notification', () {
14 test('getParameter_defined', NotificationTest.getParameter_defined); 14 test('getParameter_defined', NotificationTest.getParameter_defined);
15 test('getParameter_undefined', NotificationTest.getParameter_undefined); 15 test('getParameter_undefined', NotificationTest.getParameter_undefined);
16 test('fromJson', NotificationTest.fromJson); 16 test('fromJson', NotificationTest.fromJson);
17 test('fromJson_withParams', NotificationTest.fromJson_withParams); 17 test('fromJson_withParams', NotificationTest.fromJson_withParams);
18 }); 18 });
19 group('Request', () { 19 group('Request', () {
20 test('getParameter_defined', RequestTest.getParameter_defined); 20 test('getParameter_defined', RequestTest.getParameter_defined);
21 test('getParameter_undefined', RequestTest.getParameter_undefined); 21 test('getParameter_undefined', RequestTest.getParameter_undefined);
22 test('getRequiredParameter_defined', RequestTest.getRequiredParameter_define d); 22 test('getRequiredParameter_defined', RequestTest.getRequiredParameter_define d);
23 test('getRequiredParameter_undefined', RequestTest.getRequiredParameter_unde fined); 23 test('getRequiredParameter_undefined', RequestTest.getRequiredParameter_unde fined);
24 test('fromJson', RequestTest.fromJson); 24 test('fromJson', RequestTest.fromJson);
25 test('fromJson_invalidId', RequestTest.fromJson_invalidId); 25 test('fromJson_invalidId', RequestTest.fromJson_invalidId);
26 test('fromJson_invalidMethod', RequestTest.fromJson_invalidMethod); 26 test('fromJson_invalidMethod', RequestTest.fromJson_invalidMethod);
27 test('fromJson_invalidParams', RequestTest.fromJson_invalidParams); 27 test('fromJson_invalidParams', RequestTest.fromJson_invalidParams);
28 test('fromJson_withParams', RequestTest.fromJson_withParams); 28 test('fromJson_withParams', RequestTest.fromJson_withParams);
29 test('toBool', RequestTest.toBool);
30 test('toInt', RequestTest.toInt);
29 test('toJson', RequestTest.toJson); 31 test('toJson', RequestTest.toJson);
30 test('toJson_withParams', RequestTest.toJson_withParams); 32 test('toJson_withParams', RequestTest.toJson_withParams);
31 }); 33 });
34 group('RequestError', () {
35 test('create', RequestErrorTest.create);
36 test('create_methodNotFound', RequestErrorTest.create_methodNotFound);
37 test('create_invalidParameters', RequestErrorTest.create_invalidParameters);
38 test('create_invalidRequest', RequestErrorTest.create_invalidRequest);
39 test('create_internalError', RequestErrorTest.create_internalError);
40 test('create_parseError', RequestErrorTest.create_parseError);
41 test('fromJson', RequestErrorTest.fromJson);
42 test('toJson', RequestErrorTest.toJson);
43 });
32 group('Response', () { 44 group('Response', () {
33 test('create_contextDoesNotExist', ResponseTest.create_contextDoesNotExist); 45 test('create_contextDoesNotExist', ResponseTest.create_contextDoesNotExist);
34 test('create_invalidRequestFormat', ResponseTest.create_invalidRequestFormat ); 46 test('create_invalidRequestFormat', ResponseTest.create_invalidRequestFormat );
35 test('create_missingRequiredParameter', ResponseTest.create_missingRequiredP arameter); 47 test('create_missingRequiredParameter', ResponseTest.create_missingRequiredP arameter);
48 test('create_unknownAnalysisOption', ResponseTest.create_unknownAnalysisOpti on);
36 test('create_unknownRequest', ResponseTest.create_unknownRequest); 49 test('create_unknownRequest', ResponseTest.create_unknownRequest);
37 test('setResult', ResponseTest.setResult); 50 test('setResult', ResponseTest.setResult);
38 test('fromJson', ResponseTest.fromJson); 51 test('fromJson', ResponseTest.fromJson);
39 test('fromJson_withError', ResponseTest.fromJson_withError); 52 test('fromJson_withError', ResponseTest.fromJson_withError);
40 test('fromJson_withResult', ResponseTest.fromJson_withResult); 53 test('fromJson_withResult', ResponseTest.fromJson_withResult);
41 }); 54 });
42 } 55 }
43 56
44 class NotificationTest { 57 class NotificationTest {
45 static void getParameter_defined() { 58 static void getParameter_defined() {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 String name = 'name'; 114 String name = 'name';
102 String value = 'value'; 115 String value = 'value';
103 Request request = new Request('0', ''); 116 Request request = new Request('0', '');
104 request.setParameter(name, value); 117 request.setParameter(name, value);
105 expect(request.getRequiredParameter(name), equals(value)); 118 expect(request.getRequiredParameter(name), equals(value));
106 } 119 }
107 120
108 static void getRequiredParameter_undefined() { 121 static void getRequiredParameter_undefined() {
109 String name = 'name'; 122 String name = 'name';
110 Request request = new Request('0', ''); 123 Request request = new Request('0', '');
111 expect(() => request.getRequiredParameter(name), throwsA(new isInstanceOf<Re questFailure>())); 124 expect(() => request.getRequiredParameter(name), _throwsRequestFailure);
112 } 125 }
113 126
114 static void fromJson() { 127 static void fromJson() {
115 Request original = new Request('one', 'aMethod'); 128 Request original = new Request('one', 'aMethod');
116 String json = JSON.encode(original.toJson()); 129 String json = JSON.encode(original.toJson());
117 Request request = new Request.fromString(json); 130 Request request = new Request.fromString(json);
118 expect(request.id, equals('one')); 131 expect(request.id, equals('one'));
119 expect(request.method, equals('aMethod')); 132 expect(request.method, equals('aMethod'));
120 } 133 }
121 134
(...skipping 18 matching lines...) Expand all
140 static void fromJson_withParams() { 153 static void fromJson_withParams() {
141 Request original = new Request('one', 'aMethod'); 154 Request original = new Request('one', 'aMethod');
142 original.setParameter('foo', 'bar'); 155 original.setParameter('foo', 'bar');
143 String json = JSON.encode(original.toJson()); 156 String json = JSON.encode(original.toJson());
144 Request request = new Request.fromString(json); 157 Request request = new Request.fromString(json);
145 expect(request.id, equals('one')); 158 expect(request.id, equals('one'));
146 expect(request.method, equals('aMethod')); 159 expect(request.method, equals('aMethod'));
147 expect(request.getParameter('foo'), equals('bar')); 160 expect(request.getParameter('foo'), equals('bar'));
148 } 161 }
149 162
163 static void toBool() {
164 Request request = new Request('0', '');
165 expect(request.toBool(true), isTrue);
166 expect(request.toBool(false), isFalse);
167 expect(request.toBool('true'), isTrue);
168 expect(request.toBool('false'), isFalse);
Brian Wilkerson 2014/04/07 18:51:48 Might be good to add a test for a string that is n
scheglov 2014/04/07 19:01:41 Done.
169 expect(() => request.toBool(42), _throwsRequestFailure);
170 }
171
172 static void toInt() {
173 Request request = new Request('0', '');
174 expect(request.toInt(1), equals(1));
175 expect(request.toInt('2'), equals(2));
176 expect(() => request.toInt('xxx'), _throwsRequestFailure);
177 expect(() => request.toInt(request), _throwsRequestFailure);
178 }
179
150 static void toJson() { 180 static void toJson() {
151 Request request = new Request('one', 'aMethod'); 181 Request request = new Request('one', 'aMethod');
152 expect(request.toJson(), equals({ 182 expect(request.toJson(), equals({
153 Request.ID : 'one', 183 Request.ID : 'one',
154 Request.METHOD : 'aMethod' 184 Request.METHOD : 'aMethod'
155 })); 185 }));
156 } 186 }
157 187
158 static void toJson_withParams() { 188 static void toJson_withParams() {
159 Request request = new Request('one', 'aMethod'); 189 Request request = new Request('one', 'aMethod');
160 request.setParameter('foo', 'bar'); 190 request.setParameter('foo', 'bar');
161 expect(request.toJson(), equals({ 191 expect(request.toJson(), equals({
162 Request.ID : 'one', 192 Request.ID : 'one',
163 Request.METHOD : 'aMethod', 193 Request.METHOD : 'aMethod',
164 Request.PARAMS : {'foo' : 'bar'} 194 Request.PARAMS : {'foo' : 'bar'}
165 })); 195 }));
166 } 196 }
167 } 197 }
168 198
199 class RequestErrorTest {
200 static void create() {
201 RequestError error = new RequestError(42, 'msg');
202 expect(error.code, 42);
203 expect(error.message, "msg");
204 expect(error.toJson(), equals({
205 RequestError.CODE: 42,
206 RequestError.MESSAGE: "msg"
207 }));
208 }
209 static void create_parseError() {
Brian Wilkerson 2014/04/07 18:51:48 nit: We should probably be consistent with spacing
scheglov 2014/04/07 19:01:41 Agree, we need to put an empty line between member
210 RequestError error = new RequestError.parseError();
211 expect(error.code, RequestError.CODE_PARSE_ERROR);
212 expect(error.message, "Parse error");
213 }
214 static void create_methodNotFound() {
215 RequestError error = new RequestError.methodNotFound();
216 expect(error.code, RequestError.CODE_METHOD_NOT_FOUND);
217 expect(error.message, "Method not found");
218 }
219 static void create_invalidParameters() {
220 RequestError error = new RequestError.invalidParameters();
221 expect(error.code, RequestError.CODE_INVALID_PARAMS);
222 expect(error.message, "Invalid parameters");
223 }
224 static void create_invalidRequest() {
225 RequestError error = new RequestError.invalidRequest();
226 expect(error.code, RequestError.CODE_INVALID_REQUEST);
227 expect(error.message, "Invalid request");
228 }
229 static void create_internalError() {
230 RequestError error = new RequestError.internalError();
231 expect(error.code, RequestError.CODE_INTERNAL_ERROR);
232 expect(error.message, "Internal error");
233 }
234 static void fromJson() {
235 var json = {
236 RequestError.CODE: RequestError.CODE_PARSE_ERROR,
237 RequestError.MESSAGE: 'foo',
238 RequestError.DATA: {'ints': [1, 2, 3]}
239 };
240 RequestError error = new RequestError.fromJson(json);
241 expect(error.code, RequestError.CODE_PARSE_ERROR);
242 expect(error.message, "foo");
243 expect(error.data['ints'], [1, 2, 3]);
244 expect(error.getData('ints'), [1, 2, 3]);
245 }
246 static void toJson() {
247 RequestError error = new RequestError(0, 'msg');
248 error.setData('answer', 42);
249 error.setData('question', 'unknown');
250 expect(error.toJson(), {
251 RequestError.CODE: 0,
252 RequestError.MESSAGE: 'msg',
253 RequestError.DATA: {'answer': 42, 'question': 'unknown'}
254 });
255 }
256 }
257
169 class ResponseTest { 258 class ResponseTest {
170 static void create_contextDoesNotExist() { 259 static void create_contextDoesNotExist() {
171 Response response = new Response.contextDoesNotExist(new Request('0', '')); 260 Response response = new Response.contextDoesNotExist(new Request('0', ''));
172 expect(response.id, equals('0')); 261 expect(response.id, equals('0'));
173 expect(response.error, isNotNull); 262 expect(response.error, isNotNull);
174 expect(response.toJson(), equals({ 263 expect(response.toJson(), equals({
175 Response.ID: '0', 264 Response.ID: '0',
176 Response.ERROR: {'code': -1, 'message': 'Context does not exist'} 265 Response.ERROR: {'code': -1, 'message': 'Context does not exist'}
177 })); 266 }));
178 } 267 }
(...skipping 11 matching lines...) Expand all
190 static void create_missingRequiredParameter() { 279 static void create_missingRequiredParameter() {
191 Response response = new Response.missingRequiredParameter(new Request('0', ' '), 'x'); 280 Response response = new Response.missingRequiredParameter(new Request('0', ' '), 'x');
192 expect(response.id, equals('0')); 281 expect(response.id, equals('0'));
193 expect(response.error, isNotNull); 282 expect(response.error, isNotNull);
194 expect(response.toJson(), equals({ 283 expect(response.toJson(), equals({
195 Response.ID: '0', 284 Response.ID: '0',
196 Response.ERROR: {'code': -5, 'message': 'Missing required parameter: x'} 285 Response.ERROR: {'code': -5, 'message': 'Missing required parameter: x'}
197 })); 286 }));
198 } 287 }
199 288
289 static void create_unknownAnalysisOption() {
290 Response response = new Response.unknownAnalysisOption(new Request('0', ''), 'x');
291 expect(response.id, equals('0'));
292 expect(response.error, isNotNull);
293 expect(response.toJson(), equals({
294 Response.ID: '0',
295 Response.ERROR: {'code': -6, 'message': 'Unknown analysis option: "x"'}
296 }));
297 }
298
200 static void create_unknownRequest() { 299 static void create_unknownRequest() {
201 Response response = new Response.unknownRequest(new Request('0', '')); 300 Response response = new Response.unknownRequest(new Request('0', ''));
202 expect(response.id, equals('0')); 301 expect(response.id, equals('0'));
203 expect(response.error, isNotNull); 302 expect(response.error, isNotNull);
204 expect(response.toJson(), equals({ 303 expect(response.toJson(), equals({
205 Response.ID: '0', 304 Response.ID: '0',
206 Response.ERROR: {'code': -7, 'message': 'Unknown request'} 305 Response.ERROR: {'code': -7, 'message': 'Unknown request'}
207 })); 306 }));
208 } 307 }
209 308
210 static void setResult() { 309 static void setResult() {
211 String resultName = 'name'; 310 String resultName = 'name';
212 String resultValue = 'value'; 311 String resultValue = 'value';
213 Response response = new Response('0'); 312 Response response = new Response('0');
214 response.setResult(resultName, resultValue); 313 response.setResult(resultName, resultValue);
314 expect(response.getResult(resultName), same(resultValue));
215 expect(response.toJson(), equals({ 315 expect(response.toJson(), equals({
216 Response.ID: '0', 316 Response.ID: '0',
217 Response.ERROR: null, 317 Response.ERROR: null,
218 Response.RESULT: { 318 Response.RESULT: {
219 resultName: resultValue 319 resultName: resultValue
220 } 320 }
221 })); 321 }));
222 } 322 }
223 323
224 static void fromJson() { 324 static void fromJson() {
(...skipping 15 matching lines...) Expand all
240 static void fromJson_withResult() { 340 static void fromJson_withResult() {
241 Response original = new Response('myId'); 341 Response original = new Response('myId');
242 original.setResult('foo', 'bar'); 342 original.setResult('foo', 'bar');
243 Response response = new Response.fromJson(original.toJson()); 343 Response response = new Response.fromJson(original.toJson());
244 expect(response.id, equals('myId')); 344 expect(response.id, equals('myId'));
245 Map<String, Object> result = response.result; 345 Map<String, Object> result = response.result;
246 expect(result.length, equals(1)); 346 expect(result.length, equals(1));
247 expect(result['foo'], equals('bar')); 347 expect(result['foo'], equals('bar'));
248 } 348 }
249 } 349 }
350
351 Matcher _throwsRequestFailure = throwsA(new isInstanceOf<RequestFailure>());
OLDNEW
« no previous file with comments | « pkg/analysis_server/lib/src/protocol.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698