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

Unified Diff: pkg/analyzer/test/src/dart/element/element_test.dart

Issue 1927103002: Add checks for type bounds on generic methods (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 4 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/analyzer/test/src/dart/element/element_test.dart
diff --git a/pkg/analyzer/test/src/dart/element/element_test.dart b/pkg/analyzer/test/src/dart/element/element_test.dart
index ebee2d6444783f8b82409dc79484fec8bd63acb9..4886955e2224c71ded3eeea8425ae8fb4d61b9a6 100644
--- a/pkg/analyzer/test/src/dart/element/element_test.dart
+++ b/pkg/analyzer/test/src/dart/element/element_test.dart
@@ -2083,6 +2083,130 @@ class FunctionTypeImplTest extends EngineTestCase {
expect(f.type.toString(), '() \u2192 C<...>');
}
+ void test_typeParameters_genericLocalFunction_genericMethod_genericClass() {
+ //
+ // class C<S> {
+ // Object m<T>() {
+ // U f<U>() => null;
+ // }
+ // }
+ //
+ ClassElementImpl classElement =
+ ElementFactory.classElement('C', null, ['S']);
+ MethodElementImpl method = new MethodElementImpl('m', 0);
+ method.enclosingElement = classElement;
+ method.returnType = ElementFactory.objectType;
+ method.typeParameters = ElementFactory.typeParameters(['T']);
+ method.type = new FunctionTypeImpl(method);
+ FunctionElementImpl function = ElementFactory.functionElement('f');
+ function.enclosingElement = method;
+ function.typeParameters = ElementFactory.typeParameters(['T']);
scheglov 2016/04/28 17:14:52 'U' maybe?
Brian Wilkerson 2016/04/28 20:46:11 Done
+ function.returnType = function.typeParameters[0].type;
+ function.type = new FunctionTypeImpl(function);
+
+ List<TypeParameterElement> inheritedParameters = <TypeParameterElement>[];
+ inheritedParameters.addAll(method.typeParameters);
+ inheritedParameters.addAll(classElement.typeParameters);
+ expect(function.type.typeArguments,
+ unorderedEquals(_toTypes(inheritedParameters)));
+ expect(function.type.typeFormals, unorderedEquals(function.typeParameters));
+ expect(function.type.typeParameters, unorderedEquals(inheritedParameters));
+ }
+
+ void test_typeParameters_genericMethod_genericClass() {
+ //
+ // class C<S> {
+ // Object m<T>() => null;
+ // }
+ //
+ ClassElementImpl classElement =
+ ElementFactory.classElement('C', null, ['S']);
+ MethodElementImpl method = new MethodElementImpl('m', 0);
+ method.enclosingElement = classElement;
+ method.returnType = ElementFactory.objectType;
+ method.typeParameters = ElementFactory.typeParameters(['T']);
+ method.type = new FunctionTypeImpl(method);
+
+ expect(method.type.typeArguments,
+ unorderedEquals(_toTypes(classElement.typeParameters)));
+ expect(method.type.typeFormals, unorderedEquals(method.typeParameters));
+ expect(method.type.typeParameters,
+ unorderedEquals(classElement.typeParameters));
+ }
+
+ void test_typeParameters_genericMethod_simpleClass() {
+ //
+ // class C<S> {
+ // Object m<T>() => null;
+ // }
+ //
+ ClassElementImpl classElement = ElementFactory.classElement2('C');
+ MethodElementImpl method = new MethodElementImpl('m', 0);
+ method.enclosingElement = classElement;
+ method.returnType = ElementFactory.objectType;
+ method.typeParameters = ElementFactory.typeParameters(['T']);
+ method.type = new FunctionTypeImpl(method);
+
+ expect(method.type.typeArguments,
+ unorderedEquals(_toTypes(classElement.typeParameters)));
+ expect(method.type.typeFormals, unorderedEquals(method.typeParameters));
+ expect(method.type.typeParameters,
+ unorderedEquals(classElement.typeParameters));
+ }
+
+ void test_typeParameters_genericTopLevelFunction() {
+ //
+ // Object f<T>() => null;
+ //
+ FunctionElementImpl function = ElementFactory.functionElement('f');
+ function.returnType = ElementFactory.objectType;
+ function.typeParameters = ElementFactory.typeParameters(['T']);
+ function.type = new FunctionTypeImpl(function);
+
+ expect(function.type.typeArguments, isEmpty);
+ expect(function.type.typeFormals, unorderedEquals(function.typeParameters));
+ expect(function.type.typeParameters, isEmpty);
+ }
+
+ void test_typeParameters_simpleMethod_genericClass() {
+ //
+ // class C<S> {
+ // Object m<T>() => null;
+ // }
+ //
+ ClassElementImpl classElement =
+ ElementFactory.classElement('C', null, ['S']);
+ MethodElementImpl method = new MethodElementImpl('m', 0);
+ method.enclosingElement = classElement;
+ method.returnType = ElementFactory.objectType;
+ method.type = new FunctionTypeImpl(method);
scheglov 2016/04/28 17:14:52 Why don't we set <T> anywhere in this and the next
Brian Wilkerson 2016/04/28 20:46:11 Oversight. Done.
+
+ expect(method.type.typeArguments,
+ unorderedEquals(_toTypes(classElement.typeParameters)));
+ expect(method.type.typeFormals, unorderedEquals(method.typeParameters));
+ expect(method.type.typeParameters,
+ unorderedEquals(classElement.typeParameters));
+ }
+
+ void test_typeParameters_simpleMethod_simpleClass() {
+ //
+ // class C<S> {
+ // Object m<T>() => null;
+ // }
+ //
+ ClassElementImpl classElement = ElementFactory.classElement2('C');
+ MethodElementImpl method = new MethodElementImpl('m', 0);
+ method.enclosingElement = classElement;
+ method.returnType = ElementFactory.objectType;
+ method.type = new FunctionTypeImpl(method);
+
+ expect(method.type.typeArguments,
+ unorderedEquals(_toTypes(classElement.typeParameters)));
+ expect(method.type.typeFormals, unorderedEquals(method.typeParameters));
+ expect(method.type.typeParameters,
+ unorderedEquals(classElement.typeParameters));
+ }
+
void test_withTypeArguments() {
ClassElementImpl enclosingClass = ElementFactory.classElement2("C", ["E"]);
MethodElementImpl methodElement =
@@ -2094,6 +2218,10 @@ class FunctionTypeImplTest extends EngineTestCase {
expect(arguments, hasLength(1));
expect(arguments[0], expectedType);
}
+
+ Iterable<DartType> _toTypes(List<TypeParameterElement> typeParameters) {
+ return typeParameters.map((TypeParameterElement element) => element.type);
+ }
}
@reflectiveTest

Powered by Google App Engine
This is Rietveld 408576698