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

Unified Diff: pkg/analyzer_plugin/test/utilities/analyzer_converter_test.dart

Issue 2873503007: Add support for converting elements to plugin API objects (Closed)
Patch Set: Created 3 years, 7 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
« no previous file with comments | « pkg/analyzer_plugin/test/support/abstract_context.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer_plugin/test/utilities/analyzer_converter_test.dart
diff --git a/pkg/analyzer_plugin/test/utilities/analyzer_converter_test.dart b/pkg/analyzer_plugin/test/utilities/analyzer_converter_test.dart
index d0355b531c39fa336e7f89f63982e598146c03be..1d6d34b3c354221274f2fb784684318a783eb7dd 100644
--- a/pkg/analyzer_plugin/test/utilities/analyzer_converter_test.dart
+++ b/pkg/analyzer_plugin/test/utilities/analyzer_converter_test.dart
@@ -2,27 +2,34 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
+import 'package:analyzer/dart/ast/ast.dart' as analyzer;
+import 'package:analyzer/dart/element/element.dart' as analyzer;
+import 'package:analyzer/dart/element/type.dart' as analyzer;
import 'package:analyzer/error/error.dart' as analyzer;
-import 'package:analyzer/file_system/memory_file_system.dart';
+import 'package:analyzer/exception/exception.dart' as analyzer;
import 'package:analyzer/source/error_processor.dart' as analyzer;
+import 'package:analyzer/src/dart/element/element.dart' as analyzer;
import 'package:analyzer/src/error/codes.dart' as analyzer;
import 'package:analyzer/src/generated/engine.dart' as analyzer;
import 'package:analyzer/src/generated/source.dart' as analyzer;
+import 'package:analyzer/src/generated/utilities_dart.dart' as analyzer;
import 'package:analyzer_plugin/protocol/protocol_constants.dart' as plugin;
import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
import 'package:analyzer_plugin/utilities/analyzer_converter.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
+import '../support/abstract_context.dart';
+
void main() {
defineReflectiveTests(AnalyzerConverterTest);
}
@reflectiveTest
-class AnalyzerConverterTest {
- MemoryResourceProvider resourceProvider = new MemoryResourceProvider();
+class AnalyzerConverterTest extends AbstractContextTest {
AnalyzerConverter converter = new AnalyzerConverter();
analyzer.Source source;
+ String testFile;
/**
* Assert that the given [pluginError] matches the given [analyzerError].
@@ -52,10 +59,13 @@ class AnalyzerConverterTest {
analyzer.AnalysisError createError(int offset) => new analyzer.AnalysisError(
source, offset, 5, analyzer.CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT);
+ @override
void setUp() {
- source = resourceProvider
- .newFile(resourceProvider.convertPath('/foo/bar.dart'), '')
+ super.setUp();
+ source = provider
+ .newFile(provider.convertPath('/foo/bar.dart'), '')
.createSource();
+ testFile = provider.convertPath('/test.dart');
}
test_convertAnalysisError_lineInfo_noSeverity() {
@@ -169,6 +179,390 @@ class AnalyzerConverterTest {
assertError(pluginErrors[1], analyzerErrors[1], severity: severity);
}
+ test_convertElement_class() async {
+ analyzer.Source source = addSource(
+ testFile,
+ '''
+@deprecated
+abstract class _A {}
+class B<K, V> {}''');
+ analyzer.CompilationUnit unit = await resolveLibraryUnit(source);
+ {
+ analyzer.ClassElement engineElement = findElementInUnit(unit, '_A');
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.CLASS);
+ expect(element.name, '_A');
+ expect(element.typeParameters, isNull);
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, 27);
+ expect(location.length, '_A'.length);
+ expect(location.startLine, 2);
+ expect(location.startColumn, 16);
+ }
+ expect(element.parameters, isNull);
+ expect(
+ element.flags,
+ plugin.Element.FLAG_ABSTRACT |
+ plugin.Element.FLAG_DEPRECATED |
+ plugin.Element.FLAG_PRIVATE);
+ }
+ {
+ analyzer.ClassElement engineElement = findElementInUnit(unit, 'B');
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.CLASS);
+ expect(element.name, 'B');
+ expect(element.typeParameters, '<K, V>');
+ expect(element.flags, 0);
+ }
+ }
+
+ test_convertElement_constructor() async {
+ analyzer.Source source = addSource(
+ testFile,
+ '''
+class A {
+ const A.myConstructor(int a, [String b]);
+}''');
+ analyzer.CompilationUnit unit = await resolveLibraryUnit(source);
+ analyzer.ConstructorElement engineElement =
+ findElementInUnit(unit, 'myConstructor');
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.CONSTRUCTOR);
+ expect(element.name, 'myConstructor');
+ expect(element.typeParameters, isNull);
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, 20);
+ expect(location.length, 'myConstructor'.length);
+ expect(location.startLine, 2);
+ expect(location.startColumn, 11);
+ }
+ expect(element.parameters, '(int a, [String b])');
+ expect(element.returnType, 'A');
+ expect(element.flags, plugin.Element.FLAG_CONST);
+ }
+
+ void test_convertElement_dynamic() {
+ var engineElement = analyzer.DynamicElementImpl.instance;
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.UNKNOWN);
+ expect(element.name, 'dynamic');
+ expect(element.location, isNull);
+ expect(element.parameters, isNull);
+ expect(element.returnType, isNull);
+ expect(element.flags, 0);
+ }
+
+ test_convertElement_enum() async {
+ analyzer.Source source = addSource(
+ testFile,
+ '''
+@deprecated
+enum _E1 { one, two }
+enum E2 { three, four }''');
+ analyzer.CompilationUnit unit = await resolveLibraryUnit(source);
+ {
+ analyzer.ClassElement engineElement = findElementInUnit(unit, '_E1');
+ expect(engineElement.isDeprecated, isTrue);
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.ENUM);
+ expect(element.name, '_E1');
+ expect(element.typeParameters, isNull);
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, 17);
+ expect(location.length, '_E1'.length);
+ expect(location.startLine, 2);
+ expect(location.startColumn, 6);
+ }
+ expect(element.parameters, isNull);
+ expect(
+ element.flags,
+ (engineElement.isDeprecated ? plugin.Element.FLAG_DEPRECATED : 0) |
+ plugin.Element.FLAG_PRIVATE);
+ }
+ {
+ analyzer.ClassElement engineElement = findElementInUnit(unit, 'E2');
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.ENUM);
+ expect(element.name, 'E2');
+ expect(element.typeParameters, isNull);
+ expect(element.flags, 0);
+ }
+ }
+
+ test_convertElement_enumConstant() async {
+ analyzer.Source source = addSource(
+ testFile,
+ '''
+@deprecated
+enum _E1 { one, two }
+enum E2 { three, four }''');
+ analyzer.CompilationUnit unit = await resolveLibraryUnit(source);
+ {
+ analyzer.FieldElement engineElement = findElementInUnit(unit, 'one');
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.ENUM_CONSTANT);
+ expect(element.name, 'one');
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, 23);
+ expect(location.length, 'one'.length);
+ expect(location.startLine, 2);
+ expect(location.startColumn, 12);
+ }
+ expect(element.parameters, isNull);
+ expect(element.returnType, '_E1');
+ // TODO(danrubel) determine why enum constant is not marked as deprecated
+ //analyzer.ClassElement classElement = engineElement.enclosingElement;
+ //expect(classElement.isDeprecated, isTrue);
+ expect(
+ element.flags,
+ // Element.FLAG_DEPRECATED |
+ plugin.Element.FLAG_CONST | plugin.Element.FLAG_STATIC);
+ }
+ {
+ analyzer.FieldElement engineElement = findElementInUnit(unit, 'three');
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.ENUM_CONSTANT);
+ expect(element.name, 'three');
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, 44);
+ expect(location.length, 'three'.length);
+ expect(location.startLine, 3);
+ expect(location.startColumn, 11);
+ }
+ expect(element.parameters, isNull);
+ expect(element.returnType, 'E2');
+ expect(element.flags,
+ plugin.Element.FLAG_CONST | plugin.Element.FLAG_STATIC);
+ }
+ {
+ analyzer.FieldElement engineElement = findElementInUnit(unit, 'index');
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.FIELD);
+ expect(element.name, 'index');
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, -1);
+ expect(location.length, 'index'.length);
+ expect(location.startLine, 1);
+ expect(location.startColumn, 0);
+ }
+ expect(element.parameters, isNull);
+ expect(element.returnType, 'int');
+ expect(element.flags, plugin.Element.FLAG_FINAL);
+ }
+ {
+ analyzer.FieldElement engineElement = findElementInUnit(unit, 'values');
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.FIELD);
+ expect(element.name, 'values');
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, -1);
+ expect(location.length, 'values'.length);
+ expect(location.startLine, 1);
+ expect(location.startColumn, 0);
+ }
+ expect(element.parameters, isNull);
+ expect(element.returnType, 'List<E2>');
+ expect(element.flags,
+ plugin.Element.FLAG_CONST | plugin.Element.FLAG_STATIC);
+ }
+ }
+
+ test_convertElement_field() async {
+ analyzer.Source source = addSource(
+ testFile,
+ '''
+class A {
+ static const myField = 42;
+}''');
+ analyzer.CompilationUnit unit = await resolveLibraryUnit(source);
+ analyzer.FieldElement engineElement = findElementInUnit(unit, 'myField');
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.FIELD);
+ expect(element.name, 'myField');
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, 25);
+ expect(location.length, 'myField'.length);
+ expect(location.startLine, 2);
+ expect(location.startColumn, 16);
+ }
+ expect(element.parameters, isNull);
+ expect(element.returnType, 'dynamic');
+ expect(
+ element.flags, plugin.Element.FLAG_CONST | plugin.Element.FLAG_STATIC);
+ }
+
+ test_convertElement_functionTypeAlias() async {
+ analyzer.Source source = addSource(
+ testFile,
+ '''
+typedef int F<T>(String x);
+''');
+ analyzer.CompilationUnit unit = await resolveLibraryUnit(source);
+ analyzer.FunctionTypeAliasElement engineElement =
+ findElementInUnit(unit, 'F');
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.FUNCTION_TYPE_ALIAS);
+ expect(element.name, 'F');
+ expect(element.typeParameters, '<T>');
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, 12);
+ expect(location.length, 'F'.length);
+ expect(location.startLine, 1);
+ expect(location.startColumn, 13);
+ }
+ expect(element.parameters, '(String x)');
+ expect(element.returnType, 'int');
+ expect(element.flags, 0);
+ }
+
+ test_convertElement_getter() async {
+ analyzer.Source source = addSource(
+ testFile,
+ '''
+class A {
+ String get myGetter => 42;
+}''');
+ analyzer.CompilationUnit unit = await resolveLibraryUnit(source);
+ analyzer.PropertyAccessorElement engineElement =
+ findElementInUnit(unit, 'myGetter', analyzer.ElementKind.GETTER);
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.GETTER);
+ expect(element.name, 'myGetter');
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, 23);
+ expect(location.length, 'myGetter'.length);
+ expect(location.startLine, 2);
+ expect(location.startColumn, 14);
+ }
+ expect(element.parameters, isNull);
+ expect(element.returnType, 'String');
+ expect(element.flags, 0);
+ }
+
+ test_convertElement_method() async {
+ analyzer.Source source = addSource(
+ testFile,
+ '''
+class A {
+ static List<String> myMethod(int a, {String b, int c}) {
+ return null;
+ }
+}''');
+ analyzer.CompilationUnit unit = await resolveLibraryUnit(source);
+ analyzer.MethodElement engineElement = findElementInUnit(unit, 'myMethod');
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.METHOD);
+ expect(element.name, 'myMethod');
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, 32);
+ expect(location.length, 'myGetter'.length);
+ expect(location.startLine, 2);
+ expect(location.startColumn, 23);
+ }
+ expect(element.parameters, '(int a, {String b, int c})');
+ expect(element.returnType, 'List<String>');
+ expect(element.flags, plugin.Element.FLAG_STATIC);
+ }
+
+ test_convertElement_setter() async {
+ analyzer.Source source = addSource(
+ testFile,
+ '''
+class A {
+ set mySetter(String x) {}
+}''');
+ analyzer.CompilationUnit unit = await resolveLibraryUnit(source);
+ analyzer.FieldElement engineFieldElement =
+ findElementInUnit(unit, 'mySetter', analyzer.ElementKind.FIELD);
+ analyzer.PropertyAccessorElement engineElement = engineFieldElement.setter;
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.SETTER);
+ expect(element.name, 'mySetter');
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, 16);
+ expect(location.length, 'mySetter'.length);
+ expect(location.startLine, 2);
+ expect(location.startColumn, 7);
+ }
+ expect(element.parameters, '(String x)');
+ expect(element.returnType, isNull);
+ expect(element.flags, 0);
+ }
+
+ void test_convertElementKind() {
+ expect(converter.convertElementKind(analyzer.ElementKind.CLASS),
+ plugin.ElementKind.CLASS);
+ expect(converter.convertElementKind(analyzer.ElementKind.COMPILATION_UNIT),
+ plugin.ElementKind.COMPILATION_UNIT);
+ expect(converter.convertElementKind(analyzer.ElementKind.CONSTRUCTOR),
+ plugin.ElementKind.CONSTRUCTOR);
+ expect(converter.convertElementKind(analyzer.ElementKind.FIELD),
+ plugin.ElementKind.FIELD);
+ expect(converter.convertElementKind(analyzer.ElementKind.FUNCTION),
+ plugin.ElementKind.FUNCTION);
+ expect(
+ converter.convertElementKind(analyzer.ElementKind.FUNCTION_TYPE_ALIAS),
+ plugin.ElementKind.FUNCTION_TYPE_ALIAS);
+ expect(converter.convertElementKind(analyzer.ElementKind.GETTER),
+ plugin.ElementKind.GETTER);
+ expect(converter.convertElementKind(analyzer.ElementKind.LABEL),
+ plugin.ElementKind.LABEL);
+ expect(converter.convertElementKind(analyzer.ElementKind.LIBRARY),
+ plugin.ElementKind.LIBRARY);
+ expect(converter.convertElementKind(analyzer.ElementKind.LOCAL_VARIABLE),
+ plugin.ElementKind.LOCAL_VARIABLE);
+ expect(converter.convertElementKind(analyzer.ElementKind.METHOD),
+ plugin.ElementKind.METHOD);
+ expect(converter.convertElementKind(analyzer.ElementKind.PARAMETER),
+ plugin.ElementKind.PARAMETER);
+ expect(converter.convertElementKind(analyzer.ElementKind.SETTER),
+ plugin.ElementKind.SETTER);
+ expect(
+ converter.convertElementKind(analyzer.ElementKind.TOP_LEVEL_VARIABLE),
+ plugin.ElementKind.TOP_LEVEL_VARIABLE);
+ expect(converter.convertElementKind(analyzer.ElementKind.TYPE_PARAMETER),
+ plugin.ElementKind.TYPE_PARAMETER);
+ }
+
test_convertErrorSeverity() {
for (analyzer.ErrorSeverity severity in analyzer.ErrorSeverity.values) {
if (severity != analyzer.ErrorSeverity.NONE) {
@@ -183,4 +577,33 @@ class AnalyzerConverterTest {
expect(converter.convertErrorType(type), isNotNull, reason: type.name);
}
}
+
+ test_fromElement_LABEL() async {
+ analyzer.Source source = addSource(
+ testFile,
+ '''
+main() {
+myLabel:
+ while (true) {
+ break myLabel;
+ }
+}''');
+ analyzer.CompilationUnit unit = await resolveLibraryUnit(source);
+ analyzer.LabelElement engineElement = findElementInUnit(unit, 'myLabel');
+ // create notification Element
+ plugin.Element element = converter.convertElement(engineElement);
+ expect(element.kind, plugin.ElementKind.LABEL);
+ expect(element.name, 'myLabel');
+ {
+ plugin.Location location = element.location;
+ expect(location.file, '/test.dart');
+ expect(location.offset, 9);
+ expect(location.length, 'myLabel'.length);
+ expect(location.startLine, 2);
+ expect(location.startColumn, 1);
+ }
+ expect(element.parameters, isNull);
+ expect(element.returnType, isNull);
+ expect(element.flags, 0);
+ }
}
« no previous file with comments | « pkg/analyzer_plugin/test/support/abstract_context.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698