| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library test.services.src.index.store.codec; | |
| 6 | |
| 7 import 'package:analysis_server/src/provisional/index/index_core.dart'; | |
| 8 import 'package:analysis_server/src/services/index/index.dart'; | |
| 9 import 'package:analysis_server/src/services/index/indexable_element.dart'; | |
| 10 import 'package:analysis_server/src/services/index/store/codec.dart'; | |
| 11 import 'package:analyzer/dart/element/element.dart'; | |
| 12 import 'package:analyzer/src/generated/engine.dart'; | |
| 13 import 'package:test_reflective_loader/test_reflective_loader.dart'; | |
| 14 import 'package:unittest/unittest.dart'; | |
| 15 | |
| 16 import '../../../abstract_single_unit.dart'; | |
| 17 import '../../../mocks.dart'; | |
| 18 import '../../../utils.dart'; | |
| 19 | |
| 20 main() { | |
| 21 initializeTestEnvironment(); | |
| 22 defineReflectiveTests(_ContextCodecTest); | |
| 23 defineReflectiveTests(_ElementCodecTest); | |
| 24 defineReflectiveTests(_RelationshipCodecTest); | |
| 25 defineReflectiveTests(_StringCodecTest); | |
| 26 } | |
| 27 | |
| 28 @reflectiveTest | |
| 29 class _ContextCodecTest { | |
| 30 ContextCodec codec = new ContextCodec(); | |
| 31 | |
| 32 void test_encode_decode() { | |
| 33 AnalysisContext contextA = new MockAnalysisContext('contextA'); | |
| 34 AnalysisContext contextB = new MockAnalysisContext('contextB'); | |
| 35 int idA = codec.encode(contextA); | |
| 36 int idB = codec.encode(contextB); | |
| 37 expect(idA, codec.encode(contextA)); | |
| 38 expect(idB, codec.encode(contextB)); | |
| 39 expect(codec.decode(idA), contextA); | |
| 40 expect(codec.decode(idB), contextB); | |
| 41 } | |
| 42 | |
| 43 void test_remove() { | |
| 44 // encode | |
| 45 { | |
| 46 AnalysisContext context = new MockAnalysisContext('context'); | |
| 47 // encode | |
| 48 int id = codec.encode(context); | |
| 49 expect(id, 0); | |
| 50 expect(codec.decode(id), context); | |
| 51 // remove | |
| 52 codec.remove(context); | |
| 53 expect(codec.decode(id), isNull); | |
| 54 } | |
| 55 // encode again | |
| 56 { | |
| 57 AnalysisContext context = new MockAnalysisContext('context'); | |
| 58 // encode | |
| 59 int id = codec.encode(context); | |
| 60 expect(id, 1); | |
| 61 expect(codec.decode(id), context); | |
| 62 } | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 @reflectiveTest | |
| 67 class _ElementCodecTest extends AbstractSingleUnitTest { | |
| 68 ElementCodec codec; | |
| 69 AnalysisContext context = new MockAnalysisContext('context'); | |
| 70 StringCodec stringCodec = new StringCodec(); | |
| 71 | |
| 72 void setUp() { | |
| 73 super.setUp(); | |
| 74 codec = new ElementCodec(stringCodec); | |
| 75 } | |
| 76 | |
| 77 void test_encode_CompilationUnitElement() { | |
| 78 addSource( | |
| 79 '/my_part.dart', | |
| 80 ''' | |
| 81 part of my_lib; | |
| 82 '''); | |
| 83 resolveTestUnit(''' | |
| 84 library my_lib; | |
| 85 part 'my_part.dart'; | |
| 86 '''); | |
| 87 // defining unit | |
| 88 { | |
| 89 Element element = testLibraryElement.definingCompilationUnit; | |
| 90 expect(element.source.fullName, '/test.dart'); | |
| 91 IndexableObject indexable = new IndexableElement(element); | |
| 92 int id1 = codec.encode1(indexable); | |
| 93 int id2 = codec.encode2(indexable); | |
| 94 int id3 = codec.encode3(indexable); | |
| 95 expect(id1, isNonNegative); | |
| 96 expect(id2, -1); | |
| 97 expect(id3, IndexableElementKind.forElement(element).index); | |
| 98 validateDecode(id1, id2, id3, element); | |
| 99 } | |
| 100 // part | |
| 101 { | |
| 102 Element element = testLibraryElement.parts[0]; | |
| 103 expect(element.source.fullName, '/my_part.dart'); | |
| 104 IndexableObject indexable = new IndexableElement(element); | |
| 105 int id1 = codec.encode1(indexable); | |
| 106 int id2 = codec.encode2(indexable); | |
| 107 int id3 = codec.encode3(indexable); | |
| 108 expect(id1, isNonNegative); | |
| 109 expect(id2, -1); | |
| 110 expect(id3, IndexableElementKind.forElement(element).index); | |
| 111 validateDecode(id1, id2, id3, element); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 void test_encode_ConstructorElement_default_real() { | |
| 116 resolveTestUnit(''' | |
| 117 class A { | |
| 118 A(); | |
| 119 } | |
| 120 '''); | |
| 121 ClassElement classA = findElement('A'); | |
| 122 ConstructorElement element = classA.constructors[0]; | |
| 123 IndexableObject indexable = new IndexableElement(element); | |
| 124 int id1 = codec.encode1(indexable); | |
| 125 int id2 = codec.encode2(indexable); | |
| 126 int id3 = codec.encode3(indexable); | |
| 127 expect(id1, isNonNegative); | |
| 128 expect(id2, classA.nameOffset); | |
| 129 expect(id3, IndexableElementKind.forElement(element).index); | |
| 130 validateDecode(id1, id2, id3, element); | |
| 131 } | |
| 132 | |
| 133 void test_encode_ConstructorElement_default_synthetic() { | |
| 134 resolveTestUnit(''' | |
| 135 class A { | |
| 136 } | |
| 137 '''); | |
| 138 ClassElement classA = findElement('A'); | |
| 139 ConstructorElement element = classA.constructors[0]; | |
| 140 IndexableObject indexable = new IndexableElement(element); | |
| 141 int id1 = codec.encode1(indexable); | |
| 142 int id2 = codec.encode2(indexable); | |
| 143 int id3 = codec.encode3(indexable); | |
| 144 expect(id1, isNonNegative); | |
| 145 expect(id2, classA.nameOffset); | |
| 146 expect(id3, IndexableElementKind.forElement(element).index); | |
| 147 validateDecode(id1, id2, id3, element); | |
| 148 } | |
| 149 | |
| 150 void test_encode_ConstructorElement_named_real() { | |
| 151 resolveTestUnit(''' | |
| 152 class A { | |
| 153 A.aaa(); | |
| 154 A.bbb(); | |
| 155 } | |
| 156 '''); | |
| 157 ClassElement classA = findElement('A'); | |
| 158 // A.aaa() | |
| 159 { | |
| 160 ConstructorElement element = classA.getNamedConstructor('aaa'); | |
| 161 IndexableObject indexable = new IndexableElement(element); | |
| 162 int id1 = codec.encode1(indexable); | |
| 163 int id2 = codec.encode2(indexable); | |
| 164 int id3 = codec.encode3(indexable); | |
| 165 expect(id1, isNonNegative); | |
| 166 expect(id2, classA.nameOffset); | |
| 167 expect(id3, IndexableElementKind.forElement(element).index); | |
| 168 validateDecode(id1, id2, id3, element); | |
| 169 } | |
| 170 // A.bbb() | |
| 171 { | |
| 172 ConstructorElement element = classA.getNamedConstructor('bbb'); | |
| 173 IndexableObject indexable = new IndexableElement(element); | |
| 174 int id1 = codec.encode1(indexable); | |
| 175 int id2 = codec.encode2(indexable); | |
| 176 int id3 = codec.encode3(indexable); | |
| 177 expect(id1, isNonNegative); | |
| 178 expect(id2, classA.nameOffset); | |
| 179 expect(id3, IndexableElementKind.forElement(element).index); | |
| 180 validateDecode(id1, id2, id3, element); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 void test_encode_ConstructorElement_named_synthetic() { | |
| 185 resolveTestUnit(''' | |
| 186 class A { | |
| 187 A.aaa(); | |
| 188 A.bbb(); | |
| 189 } | |
| 190 class M {} | |
| 191 class X = A with M; | |
| 192 '''); | |
| 193 ClassElement classX = findElement('X'); | |
| 194 // X.aaa() | |
| 195 { | |
| 196 ConstructorElement element = classX.getNamedConstructor('aaa'); | |
| 197 IndexableObject indexable = new IndexableElement(element); | |
| 198 int id1 = codec.encode1(indexable); | |
| 199 int id2 = codec.encode2(indexable); | |
| 200 int id3 = codec.encode3(indexable); | |
| 201 expect(id1, isNonNegative); | |
| 202 expect(id2, classX.nameOffset); | |
| 203 expect(id3, IndexableElementKind.forElement(element).index); | |
| 204 validateDecode(id1, id2, id3, element); | |
| 205 } | |
| 206 // X.bbb() | |
| 207 { | |
| 208 ConstructorElement element = classX.getNamedConstructor('bbb'); | |
| 209 IndexableObject indexable = new IndexableElement(element); | |
| 210 int id1 = codec.encode1(indexable); | |
| 211 int id2 = codec.encode2(indexable); | |
| 212 int id3 = codec.encode3(indexable); | |
| 213 expect(id1, isNonNegative); | |
| 214 expect(id2, classX.nameOffset); | |
| 215 expect(id3, IndexableElementKind.forElement(element).index); | |
| 216 validateDecode(id1, id2, id3, element); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 void test_encode_getter_real() { | |
| 221 resolveTestUnit(''' | |
| 222 class A { | |
| 223 int get test => 42; | |
| 224 } | |
| 225 '''); | |
| 226 PropertyAccessorElement element = findElement('test', ElementKind.GETTER); | |
| 227 IndexableObject indexable = new IndexableElement(element); | |
| 228 int id1 = codec.encode1(indexable); | |
| 229 int id2 = codec.encode2(indexable); | |
| 230 int id3 = codec.encode3(indexable); | |
| 231 expect(id1, isNonNegative); | |
| 232 expect(id2, element.nameOffset); | |
| 233 expect(id3, IndexableElementKind.forElement(element).index); | |
| 234 validateDecode(id1, id2, id3, element); | |
| 235 } | |
| 236 | |
| 237 void test_encode_getter_synthetic() { | |
| 238 resolveTestUnit(''' | |
| 239 class A { | |
| 240 int test; | |
| 241 } | |
| 242 '''); | |
| 243 FieldElement field = findElement('test', ElementKind.FIELD); | |
| 244 PropertyAccessorElement element = field.getter; | |
| 245 IndexableObject indexable = new IndexableElement(element); | |
| 246 int id1 = codec.encode1(indexable); | |
| 247 int id2 = codec.encode2(indexable); | |
| 248 int id3 = codec.encode3(indexable); | |
| 249 expect(id1, isNonNegative); | |
| 250 expect(id2, element.nameOffset); | |
| 251 expect(id3, IndexableElementKind.forElement(element).index); | |
| 252 validateDecode(id1, id2, id3, element); | |
| 253 } | |
| 254 | |
| 255 void test_encode_IndexableName() { | |
| 256 IndexableName indexable = new IndexableName('test'); | |
| 257 int id1 = codec.encode1(indexable); | |
| 258 int id2 = codec.encode2(indexable); | |
| 259 int id3 = codec.encode3(indexable); | |
| 260 expect(id1, -1); | |
| 261 expect(id2, isNonNegative); | |
| 262 expect(id3, IndexableNameKind.INSTANCE.index); | |
| 263 expect(codec.decode(context, id1, id2, id3), indexable); | |
| 264 } | |
| 265 | |
| 266 void test_encode_LibraryElement() { | |
| 267 resolveTestUnit(''' | |
| 268 class A { | |
| 269 test() {} | |
| 270 } | |
| 271 '''); | |
| 272 Element element = testLibraryElement; | |
| 273 IndexableObject indexable = new IndexableElement(element); | |
| 274 int id1 = codec.encode1(indexable); | |
| 275 int id2 = codec.encode2(indexable); | |
| 276 int id3 = codec.encode3(indexable); | |
| 277 expect(id1, isNonNegative); | |
| 278 expect(id2, -1); | |
| 279 expect(id3, IndexableElementKind.forElement(element).index); | |
| 280 validateDecode(id1, id2, id3, element); | |
| 281 } | |
| 282 | |
| 283 void test_encode_MethodElement() { | |
| 284 resolveTestUnit(''' | |
| 285 class A { | |
| 286 test() {} | |
| 287 } | |
| 288 '''); | |
| 289 Element element = findElement('test'); | |
| 290 IndexableObject indexable = new IndexableElement(element); | |
| 291 int id1 = codec.encode1(indexable); | |
| 292 int id2 = codec.encode2(indexable); | |
| 293 int id3 = codec.encode3(indexable); | |
| 294 expect(id1, isNonNegative); | |
| 295 expect(id2, element.nameOffset); | |
| 296 expect(id3, IndexableElementKind.forElement(element).index); | |
| 297 validateDecode(id1, id2, id3, element); | |
| 298 } | |
| 299 | |
| 300 void test_encode_nullLibraryElement() { | |
| 301 resolveTestUnit(''' | |
| 302 test() {} | |
| 303 '''); | |
| 304 Element element = findElement('test'); | |
| 305 IndexableObject indexable = new IndexableElement(element); | |
| 306 int id1 = codec.encode1(indexable); | |
| 307 int id2 = codec.encode2(indexable); | |
| 308 int id3 = codec.encode3(indexable); | |
| 309 context.setContents(testSource, ''); | |
| 310 IndexableObject object2 = codec.decode(context, id1, id2, id3); | |
| 311 expect(object2, isNull); | |
| 312 } | |
| 313 | |
| 314 void test_encode_setter_real() { | |
| 315 resolveTestUnit(''' | |
| 316 class A { | |
| 317 void set test(x) {} | |
| 318 } | |
| 319 '''); | |
| 320 PropertyAccessorElement element = findElement('test=', ElementKind.SETTER); | |
| 321 IndexableObject indexable = new IndexableElement(element); | |
| 322 int id1 = codec.encode1(indexable); | |
| 323 int id2 = codec.encode2(indexable); | |
| 324 int id3 = codec.encode3(indexable); | |
| 325 expect(id1, isNonNegative); | |
| 326 expect(id2, element.nameOffset); | |
| 327 expect(id3, IndexableElementKind.forElement(element).index); | |
| 328 validateDecode(id1, id2, id3, element); | |
| 329 } | |
| 330 | |
| 331 void test_encode_setter_synthetic() { | |
| 332 resolveTestUnit(''' | |
| 333 class A { | |
| 334 int test; | |
| 335 } | |
| 336 '''); | |
| 337 FieldElement field = findElement('test', ElementKind.FIELD); | |
| 338 PropertyAccessorElement element = field.setter; | |
| 339 IndexableObject indexable = new IndexableElement(element); | |
| 340 int id1 = codec.encode1(indexable); | |
| 341 int id2 = codec.encode2(indexable); | |
| 342 int id3 = codec.encode3(indexable); | |
| 343 expect(id1, isNonNegative); | |
| 344 expect(id2, element.nameOffset); | |
| 345 expect(id3, IndexableElementKind.forElement(element).index); | |
| 346 validateDecode(id1, id2, id3, element); | |
| 347 } | |
| 348 | |
| 349 void test_encodeHash_notLocal() { | |
| 350 resolveTestUnit(''' | |
| 351 class A { | |
| 352 void mainA() { | |
| 353 int foo; // A | |
| 354 } | |
| 355 void mainB() { | |
| 356 int foo; // B | |
| 357 int bar; | |
| 358 } | |
| 359 } | |
| 360 '''); | |
| 361 MethodElement mainA = findElement('mainA'); | |
| 362 MethodElement mainB = findElement('mainB'); | |
| 363 Element fooA = mainA.localVariables[0]; | |
| 364 Element fooB = mainB.localVariables[0]; | |
| 365 Element bar = mainB.localVariables[1]; | |
| 366 int id_fooA = codec.encodeHash(new IndexableElement(fooA)); | |
| 367 int id_fooB = codec.encodeHash(new IndexableElement(fooB)); | |
| 368 int id_bar = codec.encodeHash(new IndexableElement(bar)); | |
| 369 expect(id_fooA == id_fooB, isTrue); | |
| 370 expect(id_fooA == id_bar, isFalse); | |
| 371 } | |
| 372 | |
| 373 void validateDecode(int id1, int id2, int id3, Element element) { | |
| 374 IndexableObject object2 = codec.decode(context, id1, id2, id3); | |
| 375 expect(object2, new isInstanceOf<IndexableElement>()); | |
| 376 Element element2 = (object2 as IndexableElement).element; | |
| 377 expect(element2, element); | |
| 378 } | |
| 379 } | |
| 380 | |
| 381 @reflectiveTest | |
| 382 class _RelationshipCodecTest { | |
| 383 StringCodec stringCodec = new StringCodec(); | |
| 384 RelationshipCodec codec; | |
| 385 | |
| 386 void setUp() { | |
| 387 codec = new RelationshipCodec(stringCodec); | |
| 388 } | |
| 389 | |
| 390 void test_all() { | |
| 391 RelationshipImpl relationship = | |
| 392 RelationshipImpl.getRelationship('my-relationship'); | |
| 393 int id = codec.encode(relationship); | |
| 394 expect(codec.decode(id), relationship); | |
| 395 } | |
| 396 } | |
| 397 | |
| 398 @reflectiveTest | |
| 399 class _StringCodecTest { | |
| 400 StringCodec codec = new StringCodec(); | |
| 401 | |
| 402 void test_all() { | |
| 403 int idA = codec.encode('aaa'); | |
| 404 int idB = codec.encode('bbb'); | |
| 405 expect(codec.decode(idA), 'aaa'); | |
| 406 expect(codec.decode(idB), 'bbb'); | |
| 407 } | |
| 408 } | |
| OLD | NEW |