Chromium Code Reviews| 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.services.src.index.store.codec; | 5 library test.services.src.index.store.codec; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/services/index/index.dart'; | 7 import 'package:analysis_server/src/services/index/index.dart'; |
| 8 import 'package:analysis_server/src/services/index/store/codec.dart'; | 8 import 'package:analysis_server/src/services/index/store/codec.dart'; |
| 9 import 'package:analyzer/src/generated/element.dart'; | 9 import 'package:analyzer/src/generated/element.dart'; |
| 10 import 'package:analyzer/src/generated/engine.dart'; | 10 import 'package:analyzer/src/generated/engine.dart'; |
| 11 import 'package:analyzer/src/generated/source.dart'; | |
| 12 import 'package:unittest/unittest.dart'; | 11 import 'package:unittest/unittest.dart'; |
| 13 | 12 |
| 14 import '../../../abstract_single_unit.dart'; | 13 import '../../../abstract_single_unit.dart'; |
| 15 import '../../../mocks.dart'; | 14 import '../../../mocks.dart'; |
| 16 import '../../../reflective_tests.dart'; | 15 import '../../../reflective_tests.dart'; |
| 17 | 16 |
| 18 main() { | 17 main() { |
| 19 groupSep = ' | '; | 18 groupSep = ' | '; |
| 20 runReflectiveTests(_ContextCodecTest); | 19 runReflectiveTests(_ContextCodecTest); |
| 21 runReflectiveTests(_ElementCodecTest); | 20 runReflectiveTests(_ElementCodecTest); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 class _ElementCodecTest extends AbstractSingleUnitTest { | 64 class _ElementCodecTest extends AbstractSingleUnitTest { |
| 66 ElementCodec codec; | 65 ElementCodec codec; |
| 67 AnalysisContext context = new MockAnalysisContext('context'); | 66 AnalysisContext context = new MockAnalysisContext('context'); |
| 68 StringCodec stringCodec = new StringCodec(); | 67 StringCodec stringCodec = new StringCodec(); |
| 69 | 68 |
| 70 void setUp() { | 69 void setUp() { |
| 71 super.setUp(); | 70 super.setUp(); |
| 72 codec = new ElementCodec(stringCodec); | 71 codec = new ElementCodec(stringCodec); |
| 73 } | 72 } |
| 74 | 73 |
| 74 void test_encode_CompilationUnitElement() { | |
| 75 addSource('/my_part.dart', ''' | |
| 76 part of my_lib; | |
| 77 '''); | |
| 78 resolveTestUnit(''' | |
| 79 library my_lib; | |
| 80 part 'my_part.dart'; | |
| 81 '''); | |
| 82 // defining unit | |
| 83 { | |
| 84 Element element = testLibraryElement.definingCompilationUnit; | |
| 85 expect(element.source.fullName, '/test.dart'); | |
| 86 int id1 = codec.encode1(element); | |
| 87 int id2 = codec.encode2(element); | |
| 88 int id3 = codec.encode3(element); | |
| 89 expect(id1, isNonNegative); | |
| 90 expect(id2, element.nameOffset); | |
| 91 expect(id3, ElementKind.COMPILATION_UNIT.ordinal); | |
| 92 // decode | |
| 93 Element element2 = codec.decode(context, id1, id2, id3); | |
| 94 expect(element2, element); | |
| 95 } | |
| 96 // part | |
| 97 { | |
| 98 Element element = testLibraryElement.parts[0]; | |
| 99 expect(element.source.fullName, '/my_part.dart'); | |
| 100 int id1 = codec.encode1(element); | |
| 101 int id2 = codec.encode2(element); | |
| 102 int id3 = codec.encode3(element); | |
| 103 expect(id1, isNonNegative); | |
| 104 expect(id2, element.nameOffset); | |
| 105 expect(id3, ElementKind.COMPILATION_UNIT.ordinal); | |
| 106 // decode | |
| 107 Element element2 = codec.decode(context, id1, id2, id3); | |
| 108 expect(element2, element); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void test_encode_ConstructorElement_default_real() { | |
| 113 resolveTestUnit(''' | |
| 114 class A { | |
| 115 A(); | |
| 116 } | |
| 117 '''); | |
| 118 ClassElement classA = findElement('A'); | |
| 119 ConstructorElement element = classA.constructors[0]; | |
| 120 int id1 = codec.encode1(element); | |
| 121 int id2 = codec.encode2(element); | |
| 122 int id3 = codec.encode3(element); | |
| 123 expect(id1, isNonNegative); | |
| 124 expect(id2, classA.nameOffset); | |
| 125 expect(id3, -100); | |
| 126 // decode | |
| 127 Element element2 = codec.decode(context, id1, id2, id3); | |
| 128 expect(element2, element); | |
| 129 } | |
| 130 | |
| 131 void test_encode_ConstructorElement_default_synthetic() { | |
|
Brian Wilkerson
2015/04/09 14:06:25
It would be good to have a test case for multiple
scheglov
2015/04/09 15:11:57
Done.
| |
| 132 resolveTestUnit(''' | |
| 133 class A { | |
| 134 } | |
| 135 '''); | |
| 136 ClassElement classA = findElement('A'); | |
| 137 ConstructorElement element = classA.constructors[0]; | |
| 138 int id1 = codec.encode1(element); | |
| 139 int id2 = codec.encode2(element); | |
| 140 int id3 = codec.encode3(element); | |
| 141 expect(id1, isNonNegative); | |
| 142 expect(id2, classA.nameOffset); | |
| 143 expect(id3, -100); | |
| 144 // decode | |
| 145 Element element2 = codec.decode(context, id1, id2, id3); | |
| 146 expect(element2, element); | |
| 147 } | |
| 148 | |
| 149 void test_encode_ConstructorElement_named_real() { | |
| 150 resolveTestUnit(''' | |
| 151 class A { | |
| 152 A.foo(); | |
| 153 A.bar(); | |
| 154 } | |
| 155 '''); | |
| 156 ClassElement classA = findElement('A'); | |
| 157 ConstructorElement element = classA.getNamedConstructor('bar'); | |
| 158 int id1 = codec.encode1(element); | |
| 159 int id2 = codec.encode2(element); | |
| 160 int id3 = codec.encode3(element); | |
| 161 expect(id1, isNonNegative); | |
| 162 expect(id2, classA.nameOffset); | |
| 163 expect(id3, -101); | |
| 164 // decode | |
| 165 Element element2 = codec.decode(context, id1, id2, id3); | |
| 166 expect(element2, element); | |
| 167 } | |
| 168 | |
| 169 void test_encode_getter_real() { | |
| 170 resolveTestUnit(''' | |
| 171 class A { | |
| 172 int get test => 42; | |
| 173 } | |
| 174 '''); | |
| 175 PropertyAccessorElement element = findElement('test', ElementKind.GETTER); | |
| 176 int id1 = codec.encode1(element); | |
| 177 int id2 = codec.encode2(element); | |
| 178 int id3 = codec.encode3(element); | |
| 179 expect(id1, isNonNegative); | |
| 180 expect(id2, element.nameOffset); | |
| 181 expect(id3, ElementKind.GETTER.ordinal); | |
| 182 // decode | |
| 183 Element element2 = codec.decode(context, id1, id2, id3); | |
| 184 expect(element2, element); | |
| 185 } | |
| 186 | |
| 187 void test_encode_getter_synthetic() { | |
| 188 resolveTestUnit(''' | |
| 189 class A { | |
| 190 int test; | |
| 191 } | |
| 192 '''); | |
| 193 FieldElement field = findElement('test', ElementKind.FIELD); | |
| 194 PropertyAccessorElement element = field.getter; | |
| 195 int id1 = codec.encode1(element); | |
| 196 int id2 = codec.encode2(element); | |
| 197 int id3 = codec.encode3(element); | |
| 198 expect(id1, isNonNegative); | |
| 199 expect(id2, element.nameOffset); | |
| 200 expect(id3, ElementKind.GETTER.ordinal); | |
| 201 // decode | |
| 202 Element element2 = codec.decode(context, id1, id2, id3); | |
| 203 expect(element2, element); | |
| 204 } | |
| 205 | |
| 206 void test_encode_LibraryElement() { | |
| 207 resolveTestUnit(''' | |
| 208 class A { | |
| 209 test() {} | |
| 210 } | |
| 211 '''); | |
| 212 Element element = testLibraryElement; | |
| 213 int id1 = codec.encode1(element); | |
| 214 int id2 = codec.encode2(element); | |
| 215 int id3 = codec.encode3(element); | |
| 216 expect(id1, isNonNegative); | |
| 217 expect(id2, element.nameOffset); | |
| 218 expect(id3, ElementKind.LIBRARY.ordinal); | |
| 219 // decode | |
| 220 Element element2 = codec.decode(context, id1, id2, id3); | |
| 221 expect(element2, element); | |
| 222 } | |
| 223 | |
| 224 void test_encode_MethodElement() { | |
| 225 resolveTestUnit(''' | |
| 226 class A { | |
| 227 test() {} | |
| 228 } | |
| 229 '''); | |
| 230 Element element = findElement('test'); | |
| 231 int id1 = codec.encode1(element); | |
| 232 int id2 = codec.encode2(element); | |
| 233 int id3 = codec.encode3(element); | |
| 234 expect(id1, isNonNegative); | |
| 235 expect(id2, element.nameOffset); | |
| 236 expect(id3, ElementKind.METHOD.ordinal); | |
| 237 // decode | |
| 238 Element element2 = codec.decode(context, id1, id2, id3); | |
| 239 expect(element2, element); | |
| 240 } | |
| 241 | |
| 242 void test_encode_NameElement() { | |
| 243 Element element = new NameElement('test'); | |
| 244 int id1 = codec.encode1(element); | |
| 245 int id2 = codec.encode2(element); | |
| 246 int id3 = codec.encode3(element); | |
| 247 expect(id1, ElementCodec.NAMED_FILE_ID); | |
| 248 expect(id2, isNonNegative); | |
| 249 expect(id3, ElementCodec.NAMED_KIND_ID); | |
| 250 } | |
| 251 | |
| 252 void test_encode_setter_real() { | |
| 253 resolveTestUnit(''' | |
| 254 class A { | |
| 255 void set test(x) {} | |
| 256 } | |
| 257 '''); | |
| 258 PropertyAccessorElement element = findElement('test=', ElementKind.SETTER); | |
| 259 int id1 = codec.encode1(element); | |
| 260 int id2 = codec.encode2(element); | |
| 261 int id3 = codec.encode3(element); | |
| 262 expect(id1, isNonNegative); | |
| 263 expect(id2, element.nameOffset); | |
| 264 expect(id3, ElementKind.SETTER.ordinal); | |
| 265 // decode | |
| 266 Element element2 = codec.decode(context, id1, id2, id3); | |
| 267 expect(element2, element); | |
| 268 } | |
| 269 | |
| 270 void test_encode_setter_synthetic() { | |
| 271 resolveTestUnit(''' | |
| 272 class A { | |
| 273 int test; | |
| 274 } | |
| 275 '''); | |
| 276 FieldElement field = findElement('test', ElementKind.FIELD); | |
| 277 PropertyAccessorElement element = field.setter; | |
| 278 int id1 = codec.encode1(element); | |
| 279 int id2 = codec.encode2(element); | |
| 280 int id3 = codec.encode3(element); | |
| 281 expect(id1, isNonNegative); | |
| 282 expect(id2, element.nameOffset); | |
| 283 expect(id3, ElementKind.SETTER.ordinal); | |
| 284 // decode | |
| 285 Element element2 = codec.decode(context, id1, id2, id3); | |
| 286 expect(element2, element); | |
| 287 } | |
| 288 | |
| 75 void test_encodeHash_notLocal() { | 289 void test_encodeHash_notLocal() { |
| 76 resolveTestUnit(''' | 290 resolveTestUnit(''' |
| 77 class A { | 291 class A { |
| 78 void mainA() { | 292 void mainA() { |
| 79 int foo; // A | 293 int foo; // A |
| 80 } | 294 } |
| 81 void mainB() { | 295 void mainB() { |
| 82 int foo; // B | 296 int foo; // B |
| 83 int bar; | 297 int bar; |
| 84 } | 298 } |
| 85 } | 299 } |
| 86 '''); | 300 '''); |
| 87 MethodElement mainA = findElement('mainA'); | 301 MethodElement mainA = findElement('mainA'); |
| 88 MethodElement mainB = findElement('mainB'); | 302 MethodElement mainB = findElement('mainB'); |
| 89 Element fooA = mainA.localVariables[0]; | 303 Element fooA = mainA.localVariables[0]; |
| 90 Element fooB = mainB.localVariables[0]; | 304 Element fooB = mainB.localVariables[0]; |
| 91 Element bar = mainB.localVariables[1]; | 305 Element bar = mainB.localVariables[1]; |
| 92 int id_fooA = codec.encodeHash(fooA); | 306 int id_fooA = codec.encodeHash(fooA); |
| 93 int id_fooB = codec.encodeHash(fooB); | 307 int id_fooB = codec.encodeHash(fooB); |
| 94 int id_bar = codec.encodeHash(bar); | 308 int id_bar = codec.encodeHash(bar); |
| 95 expect(id_fooA == id_fooB, isTrue); | 309 expect(id_fooA == id_fooB, isTrue); |
| 96 expect(id_fooA == id_bar, isFalse); | 310 expect(id_fooA == id_bar, isFalse); |
| 97 } | 311 } |
| 98 | 312 } |
| 99 void test_field() { | 313 |
| 100 resolveTestUnit(''' | |
| 101 class A { | |
| 102 int field; | |
| 103 } | |
| 104 '''); | |
| 105 FieldElement field = findElement('field', ElementKind.FIELD); | |
| 106 PropertyAccessorElement getter = field.getter; | |
| 107 PropertyAccessorElement setter = field.setter; | |
| 108 { | |
| 109 int id = codec.encode(getter, false); | |
| 110 expect(codec.decode(context, id), getter); | |
| 111 } | |
| 112 { | |
| 113 int id = codec.encode(setter, false); | |
| 114 expect(codec.decode(context, id), setter); | |
| 115 } | |
| 116 { | |
| 117 int id = codec.encode(field, false); | |
| 118 expect(codec.decode(context, id), field); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 void test_filePackage_uriMix() { | |
| 123 MethodElement buildMethodElement(Source source) { | |
| 124 CompilationUnitElementImpl unitElement = | |
| 125 new CompilationUnitElementImpl('file.dart'); | |
| 126 LibraryElementImpl libraryElement = | |
| 127 new LibraryElementImpl(null, 'lib', 0); | |
| 128 ClassElementImpl classElement = new ClassElementImpl('A', 0); | |
| 129 MethodElementImpl methodElement = new MethodElementImpl('m', 0); | |
| 130 unitElement.source = source; | |
| 131 libraryElement.definingCompilationUnit = unitElement; | |
| 132 unitElement.types = [classElement]; | |
| 133 classElement.methods = [methodElement]; | |
| 134 return methodElement; | |
| 135 } | |
| 136 // file: | |
| 137 int fileId; | |
| 138 { | |
| 139 Source source = new _TestSource('/my/file.dart', 'file:///my/file.dart'); | |
| 140 var methodElement = buildMethodElement(source); | |
| 141 fileId = codec.encode(methodElement, true); | |
| 142 } | |
| 143 // package: | |
| 144 int packageId; | |
| 145 { | |
| 146 Source source = | |
| 147 new _TestSource('/my/file.dart', 'package:my_pkg/file.dart'); | |
| 148 var methodElement = buildMethodElement(source); | |
| 149 packageId = codec.encode(methodElement, true); | |
| 150 } | |
| 151 // should be the same | |
| 152 expect(packageId, fileId); | |
| 153 } | |
| 154 | |
| 155 void test_localLocalVariable() { | |
| 156 resolveTestUnit(''' | |
| 157 main() { | |
| 158 { | |
| 159 foo() { | |
| 160 int bar; // A | |
| 161 } | |
| 162 } | |
| 163 { | |
| 164 foo() { | |
| 165 int bar; // B | |
| 166 } | |
| 167 } | |
| 168 } | |
| 169 '''); | |
| 170 { | |
| 171 LocalVariableElement element = findNodeElementAtString('bar; // A', null); | |
| 172 int id = codec.encode(element, false); | |
| 173 expect(codec.decode(context, id), element); | |
| 174 } | |
| 175 { | |
| 176 LocalVariableElement element = findNodeElementAtString('bar; // B', null); | |
| 177 int id = codec.encode(element, false); | |
| 178 expect(codec.decode(context, id), element); | |
| 179 } | |
| 180 // check strings, "foo" as a single string, no "foo@17" or "bar@35" | |
| 181 expect(stringCodec.nameToIndex, hasLength(4)); | |
| 182 expect(stringCodec.nameToIndex, containsPair('file:///test.dart', 0)); | |
| 183 expect(stringCodec.nameToIndex, containsPair('main', 1)); | |
| 184 expect(stringCodec.nameToIndex, containsPair('foo', 2)); | |
| 185 expect(stringCodec.nameToIndex, containsPair('bar', 3)); | |
| 186 } | |
| 187 | |
| 188 void test_localVariable() { | |
| 189 resolveTestUnit(''' | |
| 190 main() { | |
| 191 { | |
| 192 int foo; // A | |
| 193 } | |
| 194 { | |
| 195 int foo; // B | |
| 196 } | |
| 197 } | |
| 198 '''); | |
| 199 { | |
| 200 LocalVariableElement element = findNodeElementAtString('foo; // A', null); | |
| 201 int id = codec.encode(element, false); | |
| 202 expect(codec.decode(context, id), element); | |
| 203 } | |
| 204 { | |
| 205 LocalVariableElement element = findNodeElementAtString('foo; // B', null); | |
| 206 int id = codec.encode(element, false); | |
| 207 expect(codec.decode(context, id), element); | |
| 208 } | |
| 209 // check strings, "foo" as a single string, no "foo@21" or "foo@47" | |
| 210 expect(stringCodec.nameToIndex, hasLength(3)); | |
| 211 expect(stringCodec.nameToIndex, containsPair('file:///test.dart', 0)); | |
| 212 expect(stringCodec.nameToIndex, containsPair('main', 1)); | |
| 213 expect(stringCodec.nameToIndex, containsPair('foo', 2)); | |
| 214 } | |
| 215 | |
| 216 void test_notLocal() { | |
| 217 resolveTestUnit(''' | |
| 218 main() { | |
| 219 int foo; | |
| 220 } | |
| 221 '''); | |
| 222 LocalVariableElement element = findElement('foo'); | |
| 223 int id = codec.encode(element, false); | |
| 224 expect(codec.encode(element, false), id); | |
| 225 expect(codec.decode(context, id), element); | |
| 226 // check strings | |
| 227 expect(stringCodec.nameToIndex, hasLength(3)); | |
| 228 expect(stringCodec.nameToIndex, containsPair('file:///test.dart', 0)); | |
| 229 expect(stringCodec.nameToIndex, containsPair('main', 1)); | |
| 230 expect(stringCodec.nameToIndex, containsPair('foo', 2)); | |
| 231 } | |
| 232 } | |
| 233 | |
| 234 @reflectiveTest | 314 @reflectiveTest |
| 235 class _RelationshipCodecTest { | 315 class _RelationshipCodecTest { |
| 236 StringCodec stringCodec = new StringCodec(); | 316 StringCodec stringCodec = new StringCodec(); |
| 237 RelationshipCodec codec; | 317 RelationshipCodec codec; |
| 238 | 318 |
| 239 void setUp() { | 319 void setUp() { |
| 240 codec = new RelationshipCodec(stringCodec); | 320 codec = new RelationshipCodec(stringCodec); |
| 241 } | 321 } |
| 242 | 322 |
| 243 void test_all() { | 323 void test_all() { |
| 244 Relationship relationship = Relationship.getRelationship('my-relationship'); | 324 Relationship relationship = Relationship.getRelationship('my-relationship'); |
| 245 int id = codec.encode(relationship); | 325 int id = codec.encode(relationship); |
| 246 expect(codec.decode(id), relationship); | 326 expect(codec.decode(id), relationship); |
| 247 } | 327 } |
| 248 } | 328 } |
| 249 | 329 |
| 250 @reflectiveTest | 330 @reflectiveTest |
| 251 class _StringCodecTest { | 331 class _StringCodecTest { |
| 252 StringCodec codec = new StringCodec(); | 332 StringCodec codec = new StringCodec(); |
| 253 | 333 |
| 254 void test_all() { | 334 void test_all() { |
| 255 int idA = codec.encode('aaa'); | 335 int idA = codec.encode('aaa'); |
| 256 int idB = codec.encode('bbb'); | 336 int idB = codec.encode('bbb'); |
| 257 expect(codec.decode(idA), 'aaa'); | 337 expect(codec.decode(idA), 'aaa'); |
| 258 expect(codec.decode(idB), 'bbb'); | 338 expect(codec.decode(idB), 'bbb'); |
| 259 } | 339 } |
| 260 } | 340 } |
| 261 | |
| 262 class _TestSource implements Source { | |
| 263 final String fullName; | |
| 264 final String encoding; | |
| 265 | |
| 266 _TestSource(this.fullName, this.encoding); | |
| 267 | |
| 268 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
| 269 } | |
| OLD | NEW |