| OLD | NEW |
| 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/constants.dart'; | 9 import 'package:analysis_server/src/constants.dart'; |
| 10 import 'package:analysis_server/src/protocol.dart'; | 10 import 'package:analysis_server/src/protocol.dart'; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 runReflectiveTests(RequestErrorTest); | 22 runReflectiveTests(RequestErrorTest); |
| 23 runReflectiveTests(ResponseTest); | 23 runReflectiveTests(ResponseTest); |
| 24 } | 24 } |
| 25 | 25 |
| 26 | 26 |
| 27 @ReflectiveTestCase() | 27 @ReflectiveTestCase() |
| 28 class InvalidParameterResponseMatcher extends Matcher { | 28 class InvalidParameterResponseMatcher extends Matcher { |
| 29 static const String ERROR_CODE = 'INVALID_PARAMETER'; | 29 static const String ERROR_CODE = 'INVALID_PARAMETER'; |
| 30 | 30 |
| 31 @override | 31 @override |
| 32 Description describe(Description description) => description.add( | 32 Description describe(Description description) => |
| 33 "an 'invalid parameter' response (code $ERROR_CODE)"); | 33 description.add("an 'invalid parameter' response (code $ERROR_CODE)"); |
| 34 | 34 |
| 35 @override | 35 @override |
| 36 bool matches(item, Map matchState) { | 36 bool matches(item, Map matchState) { |
| 37 if (item is! RequestFailure) { | 37 if (item is! RequestFailure) { |
| 38 return false; | 38 return false; |
| 39 } | 39 } |
| 40 var response = item.response; | 40 var response = item.response; |
| 41 if (response is! Response) { | 41 if (response is! Response) { |
| 42 return false; | 42 return false; |
| 43 } | 43 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 56 @ReflectiveTestCase() | 56 @ReflectiveTestCase() |
| 57 class NotificationTest { | 57 class NotificationTest { |
| 58 void test_fromJson() { | 58 void test_fromJson() { |
| 59 Notification original = new Notification('foo'); | 59 Notification original = new Notification('foo'); |
| 60 Notification notification = new Notification.fromJson(original.toJson()); | 60 Notification notification = new Notification.fromJson(original.toJson()); |
| 61 expect(notification.event, equals('foo')); | 61 expect(notification.event, equals('foo')); |
| 62 expect(notification.toJson().keys, isNot(contains('params'))); | 62 expect(notification.toJson().keys, isNot(contains('params'))); |
| 63 } | 63 } |
| 64 | 64 |
| 65 void test_fromJson_withParams() { | 65 void test_fromJson_withParams() { |
| 66 Notification original = new Notification('foo', {'x': 'y'}); | 66 Notification original = new Notification('foo', { |
| 67 'x': 'y' |
| 68 }); |
| 67 Notification notification = new Notification.fromJson(original.toJson()); | 69 Notification notification = new Notification.fromJson(original.toJson()); |
| 68 expect(notification.event, equals('foo')); | 70 expect(notification.event, equals('foo')); |
| 69 expect(notification.toJson()['params'], equals({'x': 'y'})); | 71 expect(notification.toJson()['params'], equals({ |
| 72 'x': 'y' |
| 73 })); |
| 70 } | 74 } |
| 71 | 75 |
| 72 void test_toJson_withParams() { | 76 void test_toJson_withParams() { |
| 73 Notification notification = new Notification('foo', {'x': 'y'}); | 77 Notification notification = new Notification('foo', { |
| 78 'x': 'y' |
| 79 }); |
| 74 expect(notification.event, equals('foo')); | 80 expect(notification.event, equals('foo')); |
| 75 expect(notification.toJson()['params'], equals({'x': 'y'})); | 81 expect(notification.toJson()['params'], equals({ |
| 82 'x': 'y' |
| 83 })); |
| 76 expect(notification.toJson(), equals({ | 84 expect(notification.toJson(), equals({ |
| 77 'event': 'foo', | 85 'event': 'foo', |
| 78 'params': { | 86 'params': { |
| 79 'x': 'y' | 87 'x': 'y' |
| 80 } | 88 } |
| 81 })); | 89 })); |
| 82 } | 90 } |
| 83 | 91 |
| 84 void test_toJson_noParams() { | 92 void test_toJson_noParams() { |
| 85 Notification notification = new Notification('foo'); | 93 Notification notification = new Notification('foo'); |
| 86 expect(notification.event, equals('foo')); | 94 expect(notification.event, equals('foo')); |
| 87 expect(notification.toJson().keys, isNot(contains('params'))); | 95 expect(notification.toJson().keys, isNot(contains('params'))); |
| 88 expect(notification.toJson(), equals({ | 96 expect(notification.toJson(), equals({ |
| 89 'event': 'foo' | 97 'event': 'foo' |
| 90 })); | 98 })); |
| 91 } | 99 } |
| 92 } | 100 } |
| 93 | 101 |
| 94 | 102 |
| 95 @ReflectiveTestCase() | 103 @ReflectiveTestCase() |
| 96 class RequestErrorTest { | 104 class RequestErrorTest { |
| 97 void test_create() { | 105 void test_create() { |
| 98 RequestError error = new RequestError(RequestErrorCode.INVALID_REQUEST, 'msg
'); | 106 RequestError error = |
| 107 new RequestError(RequestErrorCode.INVALID_REQUEST, 'msg'); |
| 99 expect(error.code, RequestErrorCode.INVALID_REQUEST); | 108 expect(error.code, RequestErrorCode.INVALID_REQUEST); |
| 100 expect(error.message, "msg"); | 109 expect(error.message, "msg"); |
| 101 expect(error.toJson(), equals({ | 110 expect(error.toJson(), equals({ |
| 102 CODE: 'INVALID_REQUEST', | 111 CODE: 'INVALID_REQUEST', |
| 103 MESSAGE: "msg" | 112 MESSAGE: "msg" |
| 104 })); | 113 })); |
| 105 } | 114 } |
| 106 | 115 |
| 107 void test_fromJson() { | 116 void test_fromJson() { |
| 108 var trace = 'a stack trace\r\nfoo'; | 117 var trace = 'a stack trace\r\nfoo'; |
| 109 var json = { | 118 var json = { |
| 110 CODE: RequestErrorCode.INVALID_PARAMETER.name, | 119 CODE: RequestErrorCode.INVALID_PARAMETER.name, |
| 111 MESSAGE: 'foo', | 120 MESSAGE: 'foo', |
| 112 STACK_TRACE: trace | 121 STACK_TRACE: trace |
| 113 }; | 122 }; |
| 114 RequestError error = new RequestError.fromJson(new ResponseDecoder(null), ''
, | 123 RequestError error = |
| 115 json); | 124 new RequestError.fromJson(new ResponseDecoder(null), '', json); |
| 116 expect(error.code, RequestErrorCode.INVALID_PARAMETER); | 125 expect(error.code, RequestErrorCode.INVALID_PARAMETER); |
| 117 expect(error.message, "foo"); | 126 expect(error.message, "foo"); |
| 118 expect(error.stackTrace, trace); | 127 expect(error.stackTrace, trace); |
| 119 } | 128 } |
| 120 | 129 |
| 121 void test_toJson() { | 130 void test_toJson() { |
| 122 var trace = 'a stack trace\r\nbar'; | 131 var trace = 'a stack trace\r\nbar'; |
| 123 RequestError error = new RequestError( | 132 RequestError error = |
| 124 RequestErrorCode.UNKNOWN_REQUEST, 'msg', stackTrace: trace); | 133 new RequestError(RequestErrorCode.UNKNOWN_REQUEST, 'msg', stackTrace: tr
ace); |
| 125 expect(error.toJson(), { | 134 expect(error.toJson(), { |
| 126 CODE: 'UNKNOWN_REQUEST', | 135 CODE: 'UNKNOWN_REQUEST', |
| 127 MESSAGE: 'msg', | 136 MESSAGE: 'msg', |
| 128 STACK_TRACE: trace | 137 STACK_TRACE: trace |
| 129 }); | 138 }); |
| 130 } | 139 } |
| 131 } | 140 } |
| 132 | 141 |
| 133 | 142 |
| 134 @ReflectiveTestCase() | 143 @ReflectiveTestCase() |
| (...skipping 20 matching lines...) Expand all Loading... |
| 155 expect(request, isNull); | 164 expect(request, isNull); |
| 156 } | 165 } |
| 157 | 166 |
| 158 void test_fromJson_invalidParams() { | 167 void test_fromJson_invalidParams() { |
| 159 String json = '{"id":"one","method":"aMethod","params":"foobar"}'; | 168 String json = '{"id":"one","method":"aMethod","params":"foobar"}'; |
| 160 Request request = new Request.fromString(json); | 169 Request request = new Request.fromString(json); |
| 161 expect(request, isNull); | 170 expect(request, isNull); |
| 162 } | 171 } |
| 163 | 172 |
| 164 void test_fromJson_withParams() { | 173 void test_fromJson_withParams() { |
| 165 Request original = new Request('one', 'aMethod', {'foo': 'bar'}); | 174 Request original = new Request('one', 'aMethod', { |
| 175 'foo': 'bar' |
| 176 }); |
| 166 String json = JSON.encode(original.toJson()); | 177 String json = JSON.encode(original.toJson()); |
| 167 Request request = new Request.fromString(json); | 178 Request request = new Request.fromString(json); |
| 168 expect(request.id, equals('one')); | 179 expect(request.id, equals('one')); |
| 169 expect(request.method, equals('aMethod')); | 180 expect(request.method, equals('aMethod')); |
| 170 expect(request.toJson()['params'], equals({'foo': 'bar'})); | 181 expect(request.toJson()['params'], equals({ |
| 182 'foo': 'bar' |
| 183 })); |
| 171 } | 184 } |
| 172 | 185 |
| 173 void test_toJson() { | 186 void test_toJson() { |
| 174 Request request = new Request('one', 'aMethod'); | 187 Request request = new Request('one', 'aMethod'); |
| 175 expect(request.toJson(), equals({ | 188 expect(request.toJson(), equals({ |
| 176 Request.ID: 'one', | 189 Request.ID: 'one', |
| 177 Request.METHOD: 'aMethod' | 190 Request.METHOD: 'aMethod' |
| 178 })); | 191 })); |
| 179 } | 192 } |
| 180 | 193 |
| 181 void test_toJson_withParams() { | 194 void test_toJson_withParams() { |
| 182 Request request = new Request('one', 'aMethod', {'foo': 'bar'}); | 195 Request request = new Request('one', 'aMethod', { |
| 196 'foo': 'bar' |
| 197 }); |
| 183 expect(request.toJson(), equals({ | 198 expect(request.toJson(), equals({ |
| 184 Request.ID: 'one', | 199 Request.ID: 'one', |
| 185 Request.METHOD: 'aMethod', | 200 Request.METHOD: 'aMethod', |
| 186 Request.PARAMS: { | 201 Request.PARAMS: { |
| 187 'foo': 'bar' | 202 'foo': 'bar' |
| 188 } | 203 } |
| 189 })); | 204 })); |
| 190 } | 205 } |
| 191 } | 206 } |
| 192 | 207 |
| 193 | 208 |
| 194 @ReflectiveTestCase() | 209 @ReflectiveTestCase() |
| 195 class ResponseTest { | 210 class ResponseTest { |
| 196 void test_create_invalidRequestFormat() { | 211 void test_create_invalidRequestFormat() { |
| 197 Response response = new Response.invalidRequestFormat(); | 212 Response response = new Response.invalidRequestFormat(); |
| 198 expect(response.id, equals('')); | 213 expect(response.id, equals('')); |
| 199 expect(response.error, isNotNull); | 214 expect(response.error, isNotNull); |
| 200 expect(response.toJson(), equals({ | 215 expect(response.toJson(), equals({ |
| 201 Response.ID: '', | 216 Response.ID: '', |
| 202 Response.ERROR: { | 217 Response.ERROR: { |
| 203 'code': 'INVALID_REQUEST', | 218 'code': 'INVALID_REQUEST', |
| 204 'message': 'Invalid request' | 219 'message': 'Invalid request' |
| 205 } | 220 } |
| 206 })); | 221 })); |
| 207 } | 222 } |
| 208 | 223 |
| 209 void test_create_unanalyzedPriorityFiles() { | 224 void test_create_unanalyzedPriorityFiles() { |
| 210 Response response = new Response.unanalyzedPriorityFiles(new Request('0', | 225 Response response = |
| 211 ''), 'file list'); | 226 new Response.unanalyzedPriorityFiles(new Request('0', ''), 'file list'); |
| 212 expect(response.id, equals('0')); | 227 expect(response.id, equals('0')); |
| 213 expect(response.error, isNotNull); | 228 expect(response.error, isNotNull); |
| 214 expect(response.toJson(), equals({ | 229 expect(response.toJson(), equals({ |
| 215 Response.ID: '0', | 230 Response.ID: '0', |
| 216 Response.ERROR: { | 231 Response.ERROR: { |
| 217 'code': 'UNANALYZED_PRIORITY_FILES', | 232 'code': 'UNANALYZED_PRIORITY_FILES', |
| 218 'message': "Unanalyzed files cannot be a priority: 'file list'" | 233 'message': "Unanalyzed files cannot be a priority: 'file list'" |
| 219 } | 234 } |
| 220 })); | 235 })); |
| 221 } | 236 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 243 Response original = new Response.invalidRequestFormat(); | 258 Response original = new Response.invalidRequestFormat(); |
| 244 Response response = new Response.fromJson(original.toJson()); | 259 Response response = new Response.fromJson(original.toJson()); |
| 245 expect(response.id, equals('')); | 260 expect(response.id, equals('')); |
| 246 expect(response.error, isNotNull); | 261 expect(response.error, isNotNull); |
| 247 RequestError error = response.error; | 262 RequestError error = response.error; |
| 248 expect(error.code, equals(RequestErrorCode.INVALID_REQUEST)); | 263 expect(error.code, equals(RequestErrorCode.INVALID_REQUEST)); |
| 249 expect(error.message, equals('Invalid request')); | 264 expect(error.message, equals('Invalid request')); |
| 250 } | 265 } |
| 251 | 266 |
| 252 void test_fromJson_withResult() { | 267 void test_fromJson_withResult() { |
| 253 Response original = new Response('myId', result: {'foo': 'bar'}); | 268 Response original = new Response('myId', result: { |
| 269 'foo': 'bar' |
| 270 }); |
| 254 Response response = new Response.fromJson(original.toJson()); | 271 Response response = new Response.fromJson(original.toJson()); |
| 255 expect(response.id, equals('myId')); | 272 expect(response.id, equals('myId')); |
| 256 Map<String, Object> result = response.toJson()['result']; | 273 Map<String, Object> result = response.toJson()['result']; |
| 257 expect(result.length, equals(1)); | 274 expect(result.length, equals(1)); |
| 258 expect(result['foo'], equals('bar')); | 275 expect(result['foo'], equals('bar')); |
| 259 } | 276 } |
| 260 } | 277 } |
| OLD | NEW |