| 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/protocol.dart'; | 9 import 'package:analysis_server/src/protocol.dart'; |
| 10 import 'package:analysis_testing/reflective_tests.dart'; |
| 10 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
| 11 | 12 |
| 12 import 'reflective_tests.dart'; | |
| 13 | |
| 14 | 13 |
| 15 Matcher _throwsRequestFailure = throwsA(new isInstanceOf<RequestFailure>()); | 14 Matcher _throwsRequestFailure = throwsA(new isInstanceOf<RequestFailure>()); |
| 16 | 15 |
| 17 | 16 |
| 18 main() { | 17 main() { |
| 19 groupSep = ' | '; | 18 groupSep = ' | '; |
| 20 group('Notification', () { | 19 group('Notification', () { |
| 21 runReflectiveTests(NotificationTest); | 20 runReflectiveTests(NotificationTest); |
| 22 }); | 21 }); |
| 23 group('Request', () { | 22 group('Request', () { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 138 |
| 140 void test_asList_emptyList() { | 139 void test_asList_emptyList() { |
| 141 expect(makeDatum([]).asList((datum) => datum.asString()), equals([])); | 140 expect(makeDatum([]).asList((datum) => datum.asString()), equals([])); |
| 142 } | 141 } |
| 143 | 142 |
| 144 void test_asList_nonEmptyList() { | 143 void test_asList_nonEmptyList() { |
| 145 expect(makeDatum(['foo', 'bar']).asList((datum) => datum.asString()), | 144 expect(makeDatum(['foo', 'bar']).asList((datum) => datum.asString()), |
| 146 equals(['foo', 'bar'])); | 145 equals(['foo', 'bar'])); |
| 147 } | 146 } |
| 148 | 147 |
| 149 void test_isList() { | |
| 150 expect(makeDatum(3).isList, isFalse); | |
| 151 expect(makeDatum(null).isList, isTrue); | |
| 152 expect(makeDatum([]).isList, isTrue); | |
| 153 expect(makeDatum(['foo', 'bar']).isList, isTrue); | |
| 154 } | |
| 155 | |
| 156 void test_asList_nonList() { | 148 void test_asList_nonList() { |
| 157 expect(() => makeDatum(3).asList((datum) => null), _throwsInvalidParameter); | 149 expect(() => makeDatum(3).asList((datum) => null), _throwsInvalidParameter); |
| 158 } | 150 } |
| 159 | 151 |
| 160 void test_asList_null() { | 152 void test_asList_null() { |
| 161 expect(makeDatum(null).asList((datum) => datum.asString()), equals([])); | 153 expect(makeDatum(null).asList((datum) => datum.asString()), equals([])); |
| 162 } | 154 } |
| 163 | 155 |
| 164 void test_asString() { | 156 void test_asString() { |
| 165 expect(makeDatum('foo').asString(), equals('foo')); | 157 expect(makeDatum('foo').asString(), equals('foo')); |
| 166 expect(() => makeDatum(3).asString(), _throwsInvalidParameter); | 158 expect(() => makeDatum(3).asString(), _throwsInvalidParameter); |
| 167 } | 159 } |
| 168 | 160 |
| 169 void test_isStringList() { | |
| 170 expect(makeDatum(['foo', 'bar']).isStringList, isTrue); | |
| 171 expect(makeDatum([]).isStringList, isTrue); | |
| 172 expect(makeDatum(null).isStringList, isTrue); | |
| 173 expect(makeDatum(['foo', 1]).isStringList, isFalse); | |
| 174 expect(makeDatum({}).isStringList, isFalse); | |
| 175 } | |
| 176 | |
| 177 void test_asStringList() { | 161 void test_asStringList() { |
| 178 expect(makeDatum(['foo', 'bar']).asStringList(), equals(['foo', 'bar'])); | 162 expect(makeDatum(['foo', 'bar']).asStringList(), equals(['foo', 'bar'])); |
| 179 expect(makeDatum([]).asStringList(), equals([])); | 163 expect(makeDatum([]).asStringList(), equals([])); |
| 180 expect(makeDatum(null).asStringList(), equals([])); | 164 expect(makeDatum(null).asStringList(), equals([])); |
| 181 expect(() => makeDatum(['foo', 1]).asStringList(), _throwsInvalidParameter); | 165 expect(() => makeDatum(['foo', 1]).asStringList(), _throwsInvalidParameter); |
| 182 expect(() => makeDatum({}).asStringList(), _throwsInvalidParameter); | 166 expect(() => makeDatum({}).asStringList(), _throwsInvalidParameter); |
| 183 } | 167 } |
| 184 | 168 |
| 185 void test_isStringListMap() { | |
| 186 expect(makeDatum({ | |
| 187 'key1': ['value11', 'value12'], | |
| 188 'key2': ['value21', 'value22'] | |
| 189 }).isStringListMap, isTrue); | |
| 190 expect(makeDatum({ | |
| 191 'key1': 10, | |
| 192 'key2': 20 | |
| 193 }).isStringListMap, isFalse); | |
| 194 expect(makeDatum({ | |
| 195 'key1': [11, 12], | |
| 196 'key2': [21, 22] | |
| 197 }).isStringListMap, isFalse); | |
| 198 expect(makeDatum({}).isStringListMap, isTrue); | |
| 199 expect(makeDatum(null).isStringListMap, isTrue); | |
| 200 expect(makeDatum(3).isStringListMap, isFalse); | |
| 201 } | |
| 202 | |
| 203 void test_asStringListMap() { | 169 void test_asStringListMap() { |
| 204 { | 170 { |
| 205 var map = { | 171 var map = { |
| 206 'key1': ['value11', 'value12'], | 172 'key1': ['value11', 'value12'], |
| 207 'key2': ['value21', 'value22'] | 173 'key2': ['value21', 'value22'] |
| 208 }; | 174 }; |
| 209 expect(makeDatum(map).asStringListMap(), map); | 175 expect(makeDatum(map).asStringListMap(), map); |
| 210 } | 176 } |
| 211 { | 177 { |
| 212 var map = { | 178 var map = { |
| 213 'key1': 10, | 179 'key1': 10, |
| 214 'key2': 20 | 180 'key2': 20 |
| 215 }; | 181 }; |
| 216 expect(() => makeDatum(map).asStringListMap(), _throwsInvalidParameter); | 182 expect(() => makeDatum(map).asStringListMap(), _throwsInvalidParameter); |
| 217 } | 183 } |
| 218 { | 184 { |
| 219 var map = { | 185 var map = { |
| 220 'key1': [11, 12], | 186 'key1': [11, 12], |
| 221 'key2': [21, 22] | 187 'key2': [21, 22] |
| 222 }; | 188 }; |
| 223 expect(() => makeDatum(map).asStringListMap(), _throwsInvalidParameter); | 189 expect(() => makeDatum(map).asStringListMap(), _throwsInvalidParameter); |
| 224 } | 190 } |
| 225 } | 191 } |
| 226 | 192 |
| 227 void test_isMap() { | |
| 228 expect(makeDatum({ | |
| 229 'key1': 'value1', | |
| 230 'key2': 'value2' | |
| 231 }).isMap, isTrue); | |
| 232 expect(makeDatum({}).isMap, isTrue); | |
| 233 expect(makeDatum(null).isMap, isTrue); | |
| 234 expect(makeDatum({ | |
| 235 'key1': 'value1', | |
| 236 'key2': 2 | |
| 237 }).isMap, isTrue); | |
| 238 expect(makeDatum({ | |
| 239 'key1': 1, | |
| 240 'key2': 2 | |
| 241 }).isMap, isTrue); | |
| 242 expect(makeDatum([]).isMap, isFalse); | |
| 243 } | |
| 244 | |
| 245 void test_isStringMap() { | |
| 246 expect(makeDatum({ | |
| 247 'key1': 'value1', | |
| 248 'key2': 'value2' | |
| 249 }).isStringMap, isTrue); | |
| 250 expect(makeDatum({}).isStringMap, isTrue); | |
| 251 expect(makeDatum(null).isStringMap, isTrue); | |
| 252 expect(makeDatum({ | |
| 253 'key1': 'value1', | |
| 254 'key2': 2 | |
| 255 }).isStringMap, isFalse); | |
| 256 expect(makeDatum({ | |
| 257 'key1': 1, | |
| 258 'key2': 2 | |
| 259 }).isStringMap, isFalse); | |
| 260 expect(makeDatum([]).isMap, isFalse); | |
| 261 } | |
| 262 | |
| 263 void test_asStringMap() { | 193 void test_asStringMap() { |
| 264 expect(makeDatum({ | 194 expect(makeDatum({ |
| 265 'key1': 'value1', | 195 'key1': 'value1', |
| 266 'key2': 'value2' | 196 'key2': 'value2' |
| 267 }).asStringMap(), equals({ | 197 }).asStringMap(), equals({ |
| 268 'key1': 'value1', | 198 'key1': 'value1', |
| 269 'key2': 'value2' | 199 'key2': 'value2' |
| 270 })); | 200 })); |
| 271 expect(makeDatum({}).asStringMap(), equals({})); | 201 expect(makeDatum({}).asStringMap(), equals({})); |
| 272 expect(() => makeDatum({ | 202 expect(() => makeDatum({ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 285 fail('Empty map should not be iterated'); | 215 fail('Empty map should not be iterated'); |
| 286 }); | 216 }); |
| 287 } | 217 } |
| 288 | 218 |
| 289 void test_forEachMap_nonMap() { | 219 void test_forEachMap_nonMap() { |
| 290 expect(() => makeDatum(1).forEachMap((key, value) { | 220 expect(() => makeDatum(1).forEachMap((key, value) { |
| 291 fail('Non-map should not be iterated'); | 221 fail('Non-map should not be iterated'); |
| 292 }), _throwsInvalidParameter); | 222 }), _throwsInvalidParameter); |
| 293 } | 223 } |
| 294 | 224 |
| 295 void test_forEachMap_null() { | 225 void test_forEachMap_null() { |
| 296 makeDatum(null).forEachMap((key, value) { | 226 makeDatum(null).forEachMap((key, value) { |
| 297 fail('Empty map should not be iterated'); | 227 fail('Empty map should not be iterated'); |
| 298 }); | 228 }); |
| 299 } | 229 } |
| 300 | 230 |
| 301 void test_forEachMap_oneElementMap() { | 231 void test_forEachMap_oneElementMap() { |
| 302 int callCount = 0; | 232 int callCount = 0; |
| 303 makeDatum({ | 233 makeDatum({ |
| 304 'key': 'value' | 234 'key': 'value' |
| 305 }).forEachMap((key, value) { | 235 }).forEachMap((key, value) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 333 }); | 263 }); |
| 334 expect(datum.hasKey('foo'), isTrue); | 264 expect(datum.hasKey('foo'), isTrue); |
| 335 expect(datum.hasKey('bar'), isFalse); | 265 expect(datum.hasKey('bar'), isFalse); |
| 336 expect(datum.hasKey('baz'), isFalse); | 266 expect(datum.hasKey('baz'), isFalse); |
| 337 } | 267 } |
| 338 | 268 |
| 339 void test_hasKey_null() { | 269 void test_hasKey_null() { |
| 340 expect(makeDatum(null).hasKey('foo'), isFalse); | 270 expect(makeDatum(null).hasKey('foo'), isFalse); |
| 341 } | 271 } |
| 342 | 272 |
| 343 void test_indexOperator_hasKey() { | 273 void test_indexOperator_hasKey() { |
| 344 var indexResult = makeDatum({ | 274 var indexResult = makeDatum({ |
| 345 'foo': 'bar' | 275 'foo': 'bar' |
| 346 })['foo']; | 276 })['foo']; |
| 347 expect(indexResult, isRequestDatum); | 277 expect(indexResult, isRequestDatum); |
| 348 expect(indexResult.datum, equals('bar')); | 278 expect(indexResult.datum, equals('bar')); |
| 349 expect(indexResult.path, equals('myPath.foo')); | 279 expect(indexResult.path, equals('myPath.foo')); |
| 350 } | 280 } |
| 351 | 281 |
| 352 void test_indexOperator_null() { | |
| 353 expect(() => makeDatum(null)['foo'], _throwsInvalidParameter); | |
| 354 } | |
| 355 | |
| 356 void test_indexOperator_missingKey() { | 282 void test_indexOperator_missingKey() { |
| 357 expect(() => makeDatum({ | 283 expect(() => makeDatum({ |
| 358 'foo': 'bar' | 284 'foo': 'bar' |
| 359 })['baz'], _throwsInvalidParameter); | 285 })['baz'], _throwsInvalidParameter); |
| 360 } | 286 } |
| 361 | 287 |
| 362 void test_indexOperator_nonMap() { | 288 void test_indexOperator_nonMap() { |
| 363 expect(() => makeDatum(1)['foo'], _throwsInvalidParameter); | 289 expect(() => makeDatum(1)['foo'], _throwsInvalidParameter); |
| 364 } | 290 } |
| 365 | 291 |
| 292 void test_indexOperator_null() { |
| 293 expect(() => makeDatum(null)['foo'], _throwsInvalidParameter); |
| 294 } |
| 295 |
| 296 void test_isList() { |
| 297 expect(makeDatum(3).isList, isFalse); |
| 298 expect(makeDatum(null).isList, isTrue); |
| 299 expect(makeDatum([]).isList, isTrue); |
| 300 expect(makeDatum(['foo', 'bar']).isList, isTrue); |
| 301 } |
| 302 |
| 303 void test_isMap() { |
| 304 expect(makeDatum({ |
| 305 'key1': 'value1', |
| 306 'key2': 'value2' |
| 307 }).isMap, isTrue); |
| 308 expect(makeDatum({}).isMap, isTrue); |
| 309 expect(makeDatum(null).isMap, isTrue); |
| 310 expect(makeDatum({ |
| 311 'key1': 'value1', |
| 312 'key2': 2 |
| 313 }).isMap, isTrue); |
| 314 expect(makeDatum({ |
| 315 'key1': 1, |
| 316 'key2': 2 |
| 317 }).isMap, isTrue); |
| 318 expect(makeDatum([]).isMap, isFalse); |
| 319 } |
| 320 |
| 321 void test_isStringList() { |
| 322 expect(makeDatum(['foo', 'bar']).isStringList, isTrue); |
| 323 expect(makeDatum([]).isStringList, isTrue); |
| 324 expect(makeDatum(null).isStringList, isTrue); |
| 325 expect(makeDatum(['foo', 1]).isStringList, isFalse); |
| 326 expect(makeDatum({}).isStringList, isFalse); |
| 327 } |
| 328 |
| 329 void test_isStringListMap() { |
| 330 expect(makeDatum({ |
| 331 'key1': ['value11', 'value12'], |
| 332 'key2': ['value21', 'value22'] |
| 333 }).isStringListMap, isTrue); |
| 334 expect(makeDatum({ |
| 335 'key1': 10, |
| 336 'key2': 20 |
| 337 }).isStringListMap, isFalse); |
| 338 expect(makeDatum({ |
| 339 'key1': [11, 12], |
| 340 'key2': [21, 22] |
| 341 }).isStringListMap, isFalse); |
| 342 expect(makeDatum({}).isStringListMap, isTrue); |
| 343 expect(makeDatum(null).isStringListMap, isTrue); |
| 344 expect(makeDatum(3).isStringListMap, isFalse); |
| 345 } |
| 346 |
| 347 void test_isStringMap() { |
| 348 expect(makeDatum({ |
| 349 'key1': 'value1', |
| 350 'key2': 'value2' |
| 351 }).isStringMap, isTrue); |
| 352 expect(makeDatum({}).isStringMap, isTrue); |
| 353 expect(makeDatum(null).isStringMap, isTrue); |
| 354 expect(makeDatum({ |
| 355 'key1': 'value1', |
| 356 'key2': 2 |
| 357 }).isStringMap, isFalse); |
| 358 expect(makeDatum({ |
| 359 'key1': 1, |
| 360 'key2': 2 |
| 361 }).isStringMap, isFalse); |
| 362 expect(makeDatum([]).isMap, isFalse); |
| 363 } |
| 364 |
| 366 static RequestDatum makeDatum(dynamic datum) { | 365 static RequestDatum makeDatum(dynamic datum) { |
| 367 return new RequestDatum(request, 'myPath', datum); | 366 return new RequestDatum(request, 'myPath', datum); |
| 368 } | 367 } |
| 369 } | 368 } |
| 370 | 369 |
| 371 | 370 |
| 372 @ReflectiveTestCase() | 371 @ReflectiveTestCase() |
| 373 class RequestErrorTest { | 372 class RequestErrorTest { |
| 374 void test_create() { | 373 void test_create() { |
| 375 RequestError error = new RequestError(42, 'msg'); | 374 RequestError error = new RequestError(42, 'msg'); |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 670 response.setResult(resultName, resultValue); | 669 response.setResult(resultName, resultValue); |
| 671 expect(response.getResult(resultName), same(resultValue)); | 670 expect(response.getResult(resultName), same(resultValue)); |
| 672 expect(response.toJson(), equals({ | 671 expect(response.toJson(), equals({ |
| 673 Response.ID: '0', | 672 Response.ID: '0', |
| 674 Response.RESULT: { | 673 Response.RESULT: { |
| 675 resultName: resultValue | 674 resultName: resultValue |
| 676 } | 675 } |
| 677 })); | 676 })); |
| 678 } | 677 } |
| 679 } | 678 } |
| OLD | NEW |