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

Unified Diff: pkg/analysis_server/test/services/index/store/codec_test.dart

Issue 1075773002: Use file/offset/kind triplet as elements id. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 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 side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/test/services/index/store/codec_test.dart
diff --git a/pkg/analysis_server/test/services/index/store/codec_test.dart b/pkg/analysis_server/test/services/index/store/codec_test.dart
index 1ad0e42cc3506900f96a20d3d501c4e86dc97ef7..4f12648f70ac6768b4fcb542f361d4d1695b3ff3 100644
--- a/pkg/analysis_server/test/services/index/store/codec_test.dart
+++ b/pkg/analysis_server/test/services/index/store/codec_test.dart
@@ -8,7 +8,6 @@ import 'package:analysis_server/src/services/index/index.dart';
import 'package:analysis_server/src/services/index/store/codec.dart';
import 'package:analyzer/src/generated/element.dart';
import 'package:analyzer/src/generated/engine.dart';
-import 'package:analyzer/src/generated/source.dart';
import 'package:unittest/unittest.dart';
import '../../../abstract_single_unit.dart';
@@ -72,162 +71,243 @@ class _ElementCodecTest extends AbstractSingleUnitTest {
codec = new ElementCodec(stringCodec);
}
- void test_encodeHash_notLocal() {
+ void test_encode_CompilationUnitElement() {
+ addSource('/my_part.dart', '''
+part of my_lib;
+''');
resolveTestUnit('''
-class A {
- void mainA() {
- int foo; // A
- }
- void mainB() {
- int foo; // B
- int bar;
+library my_lib;
+part 'my_part.dart';
+''');
+ // defining unit
+ {
+ Element element = testLibraryElement.definingCompilationUnit;
+ expect(element.source.fullName, '/test.dart');
+ int id1 = codec.encode1(element);
+ int id2 = codec.encode2(element);
+ int id3 = codec.encode3(element);
+ expect(id1, isNonNegative);
+ expect(id2, element.nameOffset);
+ expect(id3, ElementKind.COMPILATION_UNIT.ordinal);
+ // decode
+ Element element2 = codec.decode(context, id1, id2, id3);
+ expect(element2, element);
+ }
+ // part
+ {
+ Element element = testLibraryElement.parts[0];
+ expect(element.source.fullName, '/my_part.dart');
+ int id1 = codec.encode1(element);
+ int id2 = codec.encode2(element);
+ int id3 = codec.encode3(element);
+ expect(id1, isNonNegative);
+ expect(id2, element.nameOffset);
+ expect(id3, ElementKind.COMPILATION_UNIT.ordinal);
+ // decode
+ Element element2 = codec.decode(context, id1, id2, id3);
+ expect(element2, element);
+ }
}
+
+ void test_encode_ConstructorElement_default_real() {
+ resolveTestUnit('''
+class A {
+ A();
}
''');
- MethodElement mainA = findElement('mainA');
- MethodElement mainB = findElement('mainB');
- Element fooA = mainA.localVariables[0];
- Element fooB = mainB.localVariables[0];
- Element bar = mainB.localVariables[1];
- int id_fooA = codec.encodeHash(fooA);
- int id_fooB = codec.encodeHash(fooB);
- int id_bar = codec.encodeHash(bar);
- expect(id_fooA == id_fooB, isTrue);
- expect(id_fooA == id_bar, isFalse);
+ ClassElement classA = findElement('A');
+ ConstructorElement element = classA.constructors[0];
+ int id1 = codec.encode1(element);
+ int id2 = codec.encode2(element);
+ int id3 = codec.encode3(element);
+ expect(id1, isNonNegative);
+ expect(id2, classA.nameOffset);
+ expect(id3, -100);
+ // decode
+ Element element2 = codec.decode(context, id1, id2, id3);
+ expect(element2, element);
}
- void test_field() {
+ 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.
resolveTestUnit('''
class A {
- int field;
}
''');
- FieldElement field = findElement('field', ElementKind.FIELD);
- PropertyAccessorElement getter = field.getter;
- PropertyAccessorElement setter = field.setter;
- {
- int id = codec.encode(getter, false);
- expect(codec.decode(context, id), getter);
- }
- {
- int id = codec.encode(setter, false);
- expect(codec.decode(context, id), setter);
- }
- {
- int id = codec.encode(field, false);
- expect(codec.decode(context, id), field);
- }
+ ClassElement classA = findElement('A');
+ ConstructorElement element = classA.constructors[0];
+ int id1 = codec.encode1(element);
+ int id2 = codec.encode2(element);
+ int id3 = codec.encode3(element);
+ expect(id1, isNonNegative);
+ expect(id2, classA.nameOffset);
+ expect(id3, -100);
+ // decode
+ Element element2 = codec.decode(context, id1, id2, id3);
+ expect(element2, element);
}
- void test_filePackage_uriMix() {
- MethodElement buildMethodElement(Source source) {
- CompilationUnitElementImpl unitElement =
- new CompilationUnitElementImpl('file.dart');
- LibraryElementImpl libraryElement =
- new LibraryElementImpl(null, 'lib', 0);
- ClassElementImpl classElement = new ClassElementImpl('A', 0);
- MethodElementImpl methodElement = new MethodElementImpl('m', 0);
- unitElement.source = source;
- libraryElement.definingCompilationUnit = unitElement;
- unitElement.types = [classElement];
- classElement.methods = [methodElement];
- return methodElement;
- }
- // file:
- int fileId;
- {
- Source source = new _TestSource('/my/file.dart', 'file:///my/file.dart');
- var methodElement = buildMethodElement(source);
- fileId = codec.encode(methodElement, true);
- }
- // package:
- int packageId;
- {
- Source source =
- new _TestSource('/my/file.dart', 'package:my_pkg/file.dart');
- var methodElement = buildMethodElement(source);
- packageId = codec.encode(methodElement, true);
- }
- // should be the same
- expect(packageId, fileId);
+ void test_encode_ConstructorElement_named_real() {
+ resolveTestUnit('''
+class A {
+ A.foo();
+ A.bar();
+}
+''');
+ ClassElement classA = findElement('A');
+ ConstructorElement element = classA.getNamedConstructor('bar');
+ int id1 = codec.encode1(element);
+ int id2 = codec.encode2(element);
+ int id3 = codec.encode3(element);
+ expect(id1, isNonNegative);
+ expect(id2, classA.nameOffset);
+ expect(id3, -101);
+ // decode
+ Element element2 = codec.decode(context, id1, id2, id3);
+ expect(element2, element);
}
- void test_localLocalVariable() {
+ void test_encode_getter_real() {
resolveTestUnit('''
-main() {
- {
- foo() {
- int bar; // A
- }
+class A {
+ int get test => 42;
+}
+''');
+ PropertyAccessorElement element = findElement('test', ElementKind.GETTER);
+ int id1 = codec.encode1(element);
+ int id2 = codec.encode2(element);
+ int id3 = codec.encode3(element);
+ expect(id1, isNonNegative);
+ expect(id2, element.nameOffset);
+ expect(id3, ElementKind.GETTER.ordinal);
+ // decode
+ Element element2 = codec.decode(context, id1, id2, id3);
+ expect(element2, element);
}
- {
- foo() {
- int bar; // B
- }
+
+ void test_encode_getter_synthetic() {
+ resolveTestUnit('''
+class A {
+ int test;
+}
+''');
+ FieldElement field = findElement('test', ElementKind.FIELD);
+ PropertyAccessorElement element = field.getter;
+ int id1 = codec.encode1(element);
+ int id2 = codec.encode2(element);
+ int id3 = codec.encode3(element);
+ expect(id1, isNonNegative);
+ expect(id2, element.nameOffset);
+ expect(id3, ElementKind.GETTER.ordinal);
+ // decode
+ Element element2 = codec.decode(context, id1, id2, id3);
+ expect(element2, element);
}
+
+ void test_encode_LibraryElement() {
+ resolveTestUnit('''
+class A {
+ test() {}
}
''');
- {
- LocalVariableElement element = findNodeElementAtString('bar; // A', null);
- int id = codec.encode(element, false);
- expect(codec.decode(context, id), element);
- }
- {
- LocalVariableElement element = findNodeElementAtString('bar; // B', null);
- int id = codec.encode(element, false);
- expect(codec.decode(context, id), element);
- }
- // check strings, "foo" as a single string, no "foo@17" or "bar@35"
- expect(stringCodec.nameToIndex, hasLength(4));
- expect(stringCodec.nameToIndex, containsPair('file:///test.dart', 0));
- expect(stringCodec.nameToIndex, containsPair('main', 1));
- expect(stringCodec.nameToIndex, containsPair('foo', 2));
- expect(stringCodec.nameToIndex, containsPair('bar', 3));
+ Element element = testLibraryElement;
+ int id1 = codec.encode1(element);
+ int id2 = codec.encode2(element);
+ int id3 = codec.encode3(element);
+ expect(id1, isNonNegative);
+ expect(id2, element.nameOffset);
+ expect(id3, ElementKind.LIBRARY.ordinal);
+ // decode
+ Element element2 = codec.decode(context, id1, id2, id3);
+ expect(element2, element);
}
- void test_localVariable() {
+ void test_encode_MethodElement() {
resolveTestUnit('''
-main() {
- {
- int foo; // A
+class A {
+ test() {}
+}
+''');
+ Element element = findElement('test');
+ int id1 = codec.encode1(element);
+ int id2 = codec.encode2(element);
+ int id3 = codec.encode3(element);
+ expect(id1, isNonNegative);
+ expect(id2, element.nameOffset);
+ expect(id3, ElementKind.METHOD.ordinal);
+ // decode
+ Element element2 = codec.decode(context, id1, id2, id3);
+ expect(element2, element);
}
- {
- int foo; // B
+
+ void test_encode_NameElement() {
+ Element element = new NameElement('test');
+ int id1 = codec.encode1(element);
+ int id2 = codec.encode2(element);
+ int id3 = codec.encode3(element);
+ expect(id1, ElementCodec.NAMED_FILE_ID);
+ expect(id2, isNonNegative);
+ expect(id3, ElementCodec.NAMED_KIND_ID);
}
+
+ void test_encode_setter_real() {
+ resolveTestUnit('''
+class A {
+ void set test(x) {}
}
''');
- {
- LocalVariableElement element = findNodeElementAtString('foo; // A', null);
- int id = codec.encode(element, false);
- expect(codec.decode(context, id), element);
- }
- {
- LocalVariableElement element = findNodeElementAtString('foo; // B', null);
- int id = codec.encode(element, false);
- expect(codec.decode(context, id), element);
- }
- // check strings, "foo" as a single string, no "foo@21" or "foo@47"
- expect(stringCodec.nameToIndex, hasLength(3));
- expect(stringCodec.nameToIndex, containsPair('file:///test.dart', 0));
- expect(stringCodec.nameToIndex, containsPair('main', 1));
- expect(stringCodec.nameToIndex, containsPair('foo', 2));
+ PropertyAccessorElement element = findElement('test=', ElementKind.SETTER);
+ int id1 = codec.encode1(element);
+ int id2 = codec.encode2(element);
+ int id3 = codec.encode3(element);
+ expect(id1, isNonNegative);
+ expect(id2, element.nameOffset);
+ expect(id3, ElementKind.SETTER.ordinal);
+ // decode
+ Element element2 = codec.decode(context, id1, id2, id3);
+ expect(element2, element);
}
- void test_notLocal() {
+ void test_encode_setter_synthetic() {
resolveTestUnit('''
-main() {
- int foo;
+class A {
+ int test;
+}
+''');
+ FieldElement field = findElement('test', ElementKind.FIELD);
+ PropertyAccessorElement element = field.setter;
+ int id1 = codec.encode1(element);
+ int id2 = codec.encode2(element);
+ int id3 = codec.encode3(element);
+ expect(id1, isNonNegative);
+ expect(id2, element.nameOffset);
+ expect(id3, ElementKind.SETTER.ordinal);
+ // decode
+ Element element2 = codec.decode(context, id1, id2, id3);
+ expect(element2, element);
+ }
+
+ void test_encodeHash_notLocal() {
+ resolveTestUnit('''
+class A {
+ void mainA() {
+ int foo; // A
+ }
+ void mainB() {
+ int foo; // B
+ int bar;
+ }
}
''');
- LocalVariableElement element = findElement('foo');
- int id = codec.encode(element, false);
- expect(codec.encode(element, false), id);
- expect(codec.decode(context, id), element);
- // check strings
- expect(stringCodec.nameToIndex, hasLength(3));
- expect(stringCodec.nameToIndex, containsPair('file:///test.dart', 0));
- expect(stringCodec.nameToIndex, containsPair('main', 1));
- expect(stringCodec.nameToIndex, containsPair('foo', 2));
+ MethodElement mainA = findElement('mainA');
+ MethodElement mainB = findElement('mainB');
+ Element fooA = mainA.localVariables[0];
+ Element fooB = mainB.localVariables[0];
+ Element bar = mainB.localVariables[1];
+ int id_fooA = codec.encodeHash(fooA);
+ int id_fooB = codec.encodeHash(fooB);
+ int id_bar = codec.encodeHash(bar);
+ expect(id_fooA == id_fooB, isTrue);
+ expect(id_fooA == id_bar, isFalse);
}
}
@@ -258,12 +338,3 @@ class _StringCodecTest {
expect(codec.decode(idB), 'bbb');
}
}
-
-class _TestSource implements Source {
- final String fullName;
- final String encoding;
-
- _TestSource(this.fullName, this.encoding);
-
- noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
-}

Powered by Google App Engine
This is Rietveld 408576698