blob: 107edab3318f2119e3f38b9b3f8ce1f50f88a111 [file] [log] [blame]
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// 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 'dart:async';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/standard_resolution_map.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/parser.dart' show ParserErrorCode;
import 'package:analyzer/src/generated/source_io.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'resolver_test_case.dart';
import 'test_support.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(NonErrorResolverTest);
});
}
@reflectiveTest
class NonErrorResolverTest extends NonErrorResolverTestBase {
@override
@failingTest
test_constConstructorWithMixinWithField_withoutSuperMixins() {
return super.test_constConstructorWithMixinWithField_withoutSuperMixins();
}
@override
@failingTest // Does not work with old task model
test_infer_mixin() {
return super.test_infer_mixin();
}
@override
@failingTest // Does not work with old task model
test_infer_mixin_multiplyConstrained() {
return super.test_infer_mixin_multiplyConstrained();
}
@override
@failingTest // Does not work with old task model
test_infer_mixin_with_substitution() {
return super.test_infer_mixin_with_substitution();
}
@override
@failingTest // Does not work with old task model
test_infer_mixin_with_substitution_functionType() {
return super.test_infer_mixin_with_substitution_functionType();
}
@override
@failingTest
test_intLiteralInDoubleContext_const_exact() {
return super.test_intLiteralInDoubleContext_const_exact();
}
@override
@failingTest // Fails with the old task model
test_issue_32394() {
return super.test_issue_32394();
}
@override
@failingTest // Does not work with old task model
test_mixin_of_mixin_type_argument_inference_cascaded_mixin() {
return super.test_mixin_of_mixin_type_argument_inference_cascaded_mixin();
}
@override
@failingTest // Does not work with old task model
test_mixinInference_with_actual_mixins() {
return super.test_mixinInference_with_actual_mixins();
}
@override
@failingTest // Does not work with old task model
test_mixinInference_with_actual_mixins_supermixins_enabled() {
return super.test_mixinInference_with_actual_mixins_supermixins_enabled();
}
@override
@failingTest
test_null_callMethod() {
return super.test_null_callMethod();
}
@override
@failingTest
test_null_callOperator() {
return super.test_null_callOperator();
}
}
class NonErrorResolverTestBase extends ResolverTestCase {
@override
AnalysisOptions get defaultAnalysisOptions => new AnalysisOptionsImpl();
fail_undefinedEnumConstant() async {
Source source = addSource(r'''
enum E { ONE }
E e() {
return E.TWO;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_ambiguousExport() async {
Source source = addSource(r'''
library L;
export 'lib1.dart';
export 'lib2.dart';''');
addNamedSource("/lib1.dart", r'''
library lib1;
class M {}''');
addNamedSource("/lib2.dart", r'''
library lib2;
class N {}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_ambiguousExport_combinators_hide() async {
Source source = addSource(r'''
library L;
export 'lib1.dart';
export 'lib2.dart' hide B;''');
addNamedSource("/lib1.dart", r'''
library L1;
class A {}
class B {}''');
addNamedSource("/lib2.dart", r'''
library L2;
class B {}
class C {}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_ambiguousExport_combinators_show() async {
Source source = addSource(r'''
library L;
export 'lib1.dart';
export 'lib2.dart' show C;''');
addNamedSource("/lib1.dart", r'''
library L1;
class A {}
class B {}''');
addNamedSource("/lib2.dart", r'''
library L2;
class B {}
class C {}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_ambiguousExport_sameDeclaration() async {
Source source = addSource(r'''
library L;
export 'lib.dart';
export 'lib.dart';''');
addNamedSource("/lib.dart", r'''
library lib;
class N {}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_ambiguousImport_dart_implicitHide() async {
Source source = addSource(r'''
import 'dart:async';
import 'lib.dart';
main() {
print(Future.zero);
}
''');
addNamedSource('/lib.dart', r'''
class Future {
static const zero = 0;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
}
test_ambiguousImport_hideCombinator() async {
Source source = addSource(r'''
import 'lib1.dart';
import 'lib2.dart';
import 'lib3.dart' hide N;
main() {
new N1();
new N2();
new N3();
}''');
addNamedSource("/lib1.dart", r'''
library lib1;
class N {}
class N1 {}''');
addNamedSource("/lib2.dart", r'''
library lib2;
class N {}
class N2 {}''');
addNamedSource("/lib3.dart", r'''
library lib3;
class N {}
class N3 {}''');
await computeAnalysisResult(source);
assertNoErrors(source);
}
test_ambiguousImport_showCombinator() async {
Source source = addSource(r'''
import 'lib1.dart';
import 'lib2.dart' show N, N2;
main() {
new N1();
new N2();
}''');
addNamedSource("/lib1.dart", r'''
library lib1;
class N {}
class N1 {}''');
addNamedSource("/lib2.dart", r'''
library lib2;
class N {}
class N2 {}''');
await computeAnalysisResult(source);
assertErrors(source, [HintCode.UNUSED_SHOWN_NAME]);
}
test_annotated_partOfDeclaration() async {
Source source = addSource('library L; part "part.dart";');
addNamedSource('/part.dart', '@deprecated part of L;');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_argumentTypeNotAssignable_classWithCall_Function() async {
Source source = addSource(r'''
caller(Function callee) {
callee();
}
class CallMeBack {
call() => 0;
}
main() {
caller(new CallMeBack());
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_argumentTypeNotAssignable_fieldFormalParameterElement_member() async {
Source source = addSource(r'''
class ObjectSink<T> {
void sink(T object) {
new TimestampedObject<T>(object);
}
}
class TimestampedObject<E> {
E object2;
TimestampedObject(this.object2);
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_argumentTypeNotAssignable_invocation_functionParameter_generic() async {
Source source = addSource(r'''
class A<K> {
m(f(K k), K v) {
f(v);
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_argumentTypeNotAssignable_invocation_typedef_generic() async {
Source source = addSource(r'''
typedef A<T>(T p);
f(A<int> a) {
a(1);
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_argumentTypeNotAssignable_Object_Function() async {
Source source = addSource(r'''
main() {
process(() {});
}
process(Object x) {}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_argumentTypeNotAssignable_optionalNew() async {
resetWith(options: new AnalysisOptionsImpl()..previewDart2 = true);
Source source = addSource(r'''
class Widget { }
class MaterialPageRoute {
final Widget Function() builder;
const MaterialPageRoute({this.builder});
}
void main() {
print(MaterialPageRoute(
builder: () { return Widget(); }
));
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_argumentTypeNotAssignable_typedef_local() async {
Source source = addSource(r'''
typedef A(int p1, String p2);
A getA() => null;
f() {
A a = getA();
a(1, '2');
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_argumentTypeNotAssignable_typedef_parameter() async {
Source source = addSource(r'''
typedef A(int p1, String p2);
f(A a) {
a(1, '2');
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_assert_with_message_await() async {
Source source = addSource('''
import 'dart:async';
f() async {
assert(false, await g());
}
Future<String> g() => null;
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_assert_with_message_dynamic() async {
Source source = addSource('''
f() {
assert(false, g());
}
g() => null;
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_assert_with_message_non_string() async {
Source source = addSource('''
f() {
assert(false, 3);
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_assert_with_message_null() async {
Source source = addSource('''
f() {
assert(false, null);
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_assert_with_message_string() async {
Source source = addSource('''
f() {
assert(false, 'message');
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_assert_with_message_suppresses_unused_var_hint() async {
Source source = addSource('''
f() {
String message = 'msg';
assert(true, message);
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_assignability_function_expr_rettype_from_typedef_cls() async {
// In the code below, the type of (() => f()) has a return type which is
// a class, and that class is inferred from the return type of the typedef
// F.
Source source = addSource('''
class C {}
typedef C F();
F f;
main() {
F f2 = (() => f());
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_assignability_function_expr_rettype_from_typedef_typedef() async {
// In the code below, the type of (() => f()) has a return type which is
// a typedef, and that typedef is inferred from the return type of the
// typedef F.
Source source = addSource('''
typedef G F();
typedef G();
F f;
main() {
F f2 = (() => f());
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_assignmentToFinal_prefixNegate() async {
Source source = addSource(r'''
f() {
final x = 0;
-x;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_assignmentToFinalNoSetter_prefixedIdentifier() async {
Source source = addSource(r'''
class A {
int get x => 0;
set x(v) {}
}
main() {
A a = new A();
a.x = 0;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_assignmentToFinalNoSetter_propertyAccess() async {
Source source = addSource(r'''
class A {
int get x => 0;
set x(v) {}
}
class B {
static A a;
}
main() {
B.a.x = 0;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_assignmentToFinals_importWithPrefix() async {
Source source = addSource(r'''
library lib;
import 'lib1.dart' as foo;
main() {
foo.x = true;
}''');
addNamedSource("/lib1.dart", r'''
library lib1;
bool x = false;''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_dynamic_with_return() async {
Source source = addSource('''
dynamic f() async {
return;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_dynamic_with_return_value() async {
Source source = addSource('''
dynamic f() async {
return 5;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_dynamic_without_return() async {
Source source = addSource('''
dynamic f() async {}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_expression_function_type() async {
Source source = addSource('''
import 'dart:async';
typedef Future<int> F(int i);
main() {
F f = (int i) async => i;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_flattened() async {
Source source = addSource('''
import 'dart:async';
typedef Future<int> CreatesFutureInt();
main() {
CreatesFutureInt createFutureInt = () async => f();
Future<int> futureInt = createFutureInt();
futureInt.then((int i) => print(i));
}
Future<int> f() => null;
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_future_dynamic_with_return() async {
Source source = addSource('''
import 'dart:async';
Future<dynamic> f() async {
return;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_future_dynamic_with_return_value() async {
Source source = addSource('''
import 'dart:async';
Future<dynamic> f() async {
return 5;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_future_dynamic_without_return() async {
Source source = addSource('''
import 'dart:async';
Future<dynamic> f() async {}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_future_int_with_return_future_int() async {
Source source = addSource('''
import 'dart:async';
Future<int> f() async {
return new Future<int>.value(5);
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_future_int_with_return_value() async {
Source source = addSource('''
import 'dart:async';
Future<int> f() async {
return 5;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_future_null_with_return() async {
Source source = addSource('''
import 'dart:async';
Future<Null> f() async {
return;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_future_null_without_return() async {
Source source = addSource('''
import 'dart:async';
Future<Null> f() async {}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_future_object_with_return_value() async {
Source source = addSource('''
import 'dart:async';
Future<Object> f() async {
return 5;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_future_with_return() async {
Source source = addSource('''
import 'dart:async';
Future f() async {
return;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_future_with_return_value() async {
Source source = addSource('''
import 'dart:async';
Future f() async {
return 5;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_future_without_return() async {
Source source = addSource('''
import 'dart:async';
Future f() async {}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_with_return() async {
Source source = addSource('''
f() async {
return;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_with_return_value() async {
Source source = addSource('''
f() async {
return 5;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_async_without_return() async {
Source source = addSource('''
f() async {}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_asyncForInWrongContext_async() async {
Source source = addSource(r'''
f(list) async {
await for (var e in list) {
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_asyncForInWrongContext_asyncStar() async {
Source source = addSource(r'''
f(list) async* {
await for (var e in list) {
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_await_flattened() async {
Source source = addSource('''
import 'dart:async';
Future<Future<int>> ffi() => null;
f() async {
Future<int> b = await ffi();
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_await_simple() async {
Source source = addSource('''
import 'dart:async';
Future<int> fi() => null;
f() async {
int a = await fi();
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_awaitInWrongContext_async() async {
Source source = addSource(r'''
f(x, y) async {
return await x + await y;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_awaitInWrongContext_asyncStar() async {
Source source = addSource(r'''
f(x, y) async* {
yield await x + await y;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_breakWithoutLabelInSwitch() async {
Source source = addSource(r'''
class A {
void m(int i) {
switch (i) {
case 0:
break;
}
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_bug_24539_getter() async {
Source source = addSource('''
class C<T> {
List<Foo> get x => null;
}
typedef Foo(param);
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_bug_24539_setter() async {
Source source = addSource('''
class C<T> {
void set x(List<Foo> value) {}
}
typedef Foo(param);
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_builtInIdentifierAsType_dynamic() async {
Source source = addSource(r'''
f() {
dynamic x;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_caseBlockNotTerminated() async {
Source source = addSource(r'''
f(int p) {
for (int i = 0; i < 10; i++) {
switch (p) {
case 0:
break;
case 1:
continue;
case 2:
return;
case 3:
throw new Object();
case 4:
case 5:
return;
case 6:
default:
return;
}
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_caseBlockNotTerminated_lastCase() async {
Source source = addSource(r'''
f(int p) {
switch (p) {
case 0:
p = p + 1;
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_caseExpressionTypeImplementsEquals() async {
Source source = addSource(r'''
print(p) {}
abstract class B {
final id;
const B(this.id);
String toString() => 'C($id)';
/** Equality is identity equality, the id isn't used. */
bool operator==(Object other);
}
class C extends B {
const C(id) : super(id);
}
void doSwitch(c) {
switch (c) {
case const C(0): print('Switch: 0'); break;
case const C(1): print('Switch: 1'); break;
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_caseExpressionTypeImplementsEquals_int() async {
Source source = addSource(r'''
f(int i) {
switch(i) {
case(1) : return 1;
default: return 0;
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_caseExpressionTypeImplementsEquals_Object() async {
Source source = addSource(r'''
class IntWrapper {
final int value;
const IntWrapper(this.value);
}
f(IntWrapper intWrapper) {
switch(intWrapper) {
case(const IntWrapper(1)) : return 1;
default: return 0;
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_caseExpressionTypeImplementsEquals_String() async {
Source source = addSource(r'''
f(String s) {
switch(s) {
case('1') : return 1;
default: return 0;
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_class_type_alias_documentationComment() async {
Source source = addSource('''
/**
* Documentation
*/
class C = D with E;
class D {}
class E {}''');
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
ClassElement classC =
resolutionMap.elementDeclaredByCompilationUnit(unit).getType('C');
expect(classC.documentationComment, isNotNull);
}
test_commentReference_beforeConstructor() async {
String code = r'''
abstract class A {
/// [p]
A(int p) {}
}''';
Source source = addSource(code);
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
{
SimpleIdentifier ref =
EngineTestCase.findSimpleIdentifier(unit, code, "p]");
expect(ref.staticElement, new TypeMatcher<ParameterElement>());
}
}
test_commentReference_beforeEnum() async {
String code = r'''
/// This is the [Samurai] kind.
enum Samurai {
/// Use [int].
WITH_SWORD,
/// Like [WITH_SWORD], but only without one.
WITHOUT_SWORD
}''';
Source source = addSource(code);
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
{
SimpleIdentifier ref =
EngineTestCase.findSimpleIdentifier(unit, code, 'Samurai]');
ClassElement refElement = ref.staticElement;
expect(refElement, isNotNull);
expect(refElement.name, 'Samurai');
}
{
SimpleIdentifier ref =
EngineTestCase.findSimpleIdentifier(unit, code, 'int]');
ClassElement refElement = ref.staticElement;
expect(refElement, isNotNull);
expect(refElement.name, 'int');
}
{
SimpleIdentifier ref =
EngineTestCase.findSimpleIdentifier(unit, code, 'WITH_SWORD]');
PropertyAccessorElement refElement = ref.staticElement;
expect(refElement, isNotNull);
expect(refElement.name, 'WITH_SWORD');
}
}
test_commentReference_beforeFunction_blockBody() async {
String code = r'''
/// [p]
foo(int p) {
}''';
Source source = addSource(code);
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
SimpleIdentifier ref =
EngineTestCase.findSimpleIdentifier(unit, code, 'p]');
expect(ref.staticElement, new TypeMatcher<ParameterElement>());
}
test_commentReference_beforeFunction_expressionBody() async {
String code = r'''
/// [p]
foo(int p) => null;''';
Source source = addSource(code);
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
SimpleIdentifier ref =
EngineTestCase.findSimpleIdentifier(unit, code, 'p]');
expect(ref.staticElement, new TypeMatcher<ParameterElement>());
}
test_commentReference_beforeFunctionTypeAlias() async {
String code = r'''
/// [p]
typedef Foo(int p);
''';
Source source = addSource(code);
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
SimpleIdentifier ref =
EngineTestCase.findSimpleIdentifier(unit, code, 'p]');
expect(ref.staticElement, new TypeMatcher<ParameterElement>());
}
test_commentReference_beforeGenericTypeAlias() async {
String code = r'''
/// Can resolve [T], [S], and [p].
typedef Foo<T> = Function<S>(int p);
''';
Source source = addSource(code);
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
Element getElement(String search) {
return EngineTestCase.findSimpleIdentifier(unit, code, search)
.staticElement;
}
expect(getElement('T]'), new TypeMatcher<TypeParameterElement>());
expect(getElement('S]'), new TypeMatcher<TypeParameterElement>());
expect(getElement('p]'), new TypeMatcher<ParameterElement>());
}
test_commentReference_beforeGetter() async {
String code = r'''
abstract class A {
/// [int]
get g => null;
}''';
Source source = addSource(code);
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
{
SimpleIdentifier ref =
EngineTestCase.findSimpleIdentifier(unit, code, 'int]');
expect(ref.staticElement, isNotNull);
}
}
test_commentReference_beforeMethod() async {
String code = r'''
abstract class A {
/// [p1]
ma(int p1) {}
/// [p2]
mb(int p2);
/// [p3] and [p4]
mc(int p3, p4());
/// [p5]
md(int p5, {int p6});
}''';
Source source = addSource(code);
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
assertIsParameter(String search) {
SimpleIdentifier ref =
EngineTestCase.findSimpleIdentifier(unit, code, search);
expect(ref.staticElement, new TypeMatcher<ParameterElement>());
}
assertIsParameter('p1');
assertIsParameter('p2');
assertIsParameter('p3');
assertIsParameter('p4');
assertIsParameter('p5');
assertIsParameter('p6');
}
test_commentReference_class() async {
String code = r'''
/// [foo]
class A {
foo() {}
}''';
Source source = addSource(code);
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
SimpleIdentifier ref =
EngineTestCase.findSimpleIdentifier(unit, code, 'foo]');
expect(ref.staticElement, new TypeMatcher<MethodElement>());
}
test_commentReference_setter() async {
String code = r'''
class A {
/// [x] in A
mA() {}
set x(value) {}
}
class B extends A {
/// [x] in B
mB() {}
}
''';
Source source = addSource(code);
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
{
SimpleIdentifier ref =
EngineTestCase.findSimpleIdentifier(unit, code, "x] in A");
expect(ref.staticElement, new TypeMatcher<PropertyAccessorElement>());
}
{
SimpleIdentifier ref =
EngineTestCase.findSimpleIdentifier(unit, code, 'x] in B');
expect(ref.staticElement, new TypeMatcher<PropertyAccessorElement>());
}
}
test_concreteClassWithAbstractMember() async {
Source source = addSource(r'''
abstract class A {
m();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_concreteClassWithAbstractMember_inherited() async {
Source source = addSource(r'''
class A {
m() {}
}
class B extends A {
m();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_conflictingConstructorNameAndMember_setter() async {
Source source = addSource(r'''
class A {
A.x() {}
set x(_) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_conflictingStaticGetterAndInstanceSetter_thisClass() async {
Source source = addSource(r'''
class A {
static get x => 0;
static set x(int p) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_const_constructor_with_named_generic_parameter() async {
Source source = addSource('''
class C<T> {
const C({T t});
}
const c = const C(t: 1);
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_const_dynamic() async {
Source source = addSource('''
const Type d = dynamic;
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_const_imported_defaultParameterValue_withImportPrefix() async {
Source source = addNamedSource("/a.dart", r'''
import 'b.dart';
const b = const B();
''');
addNamedSource("/b.dart", r'''
import 'c.dart' as ccc;
class B {
const B([p = ccc.value]);
}
''');
addNamedSource("/c.dart", r'''
const int value = 12345;
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constConstructorWithMixinWithField_withoutSuperMixins() async {
Source source = addSource(r'''
class M {
}
class A extends Object with M {
const A();
}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.CONST_CONSTRUCTOR_IN_SUBCLASS_OF_MIXIN_APPLICATION
]);
verify([source]);
}
test_constConstructorWithMixinWithField_withSuperMixins() async {
resetWith(options: new AnalysisOptionsImpl()..enableSuperMixins = true);
Source source = addSource(r'''
class M {
}
class A extends Object with M {
const A();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constConstructorWithNonConstSuper_explicit() async {
Source source = addSource(r'''
class A {
const A();
}
class B extends A {
const B(): super();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constConstructorWithNonConstSuper_redirectingFactory() async {
Source source = addSource(r'''
class A {
A();
}
class B implements C {
const B();
}
class C extends A {
const factory C() = B;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constConstructorWithNonConstSuper_unresolved() async {
Source source = addSource(r'''
class A {
A.a();
}
class B extends A {
const B(): super();
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.UNDEFINED_CONSTRUCTOR_IN_INITIALIZER_DEFAULT]);
verify([source]);
}
test_constConstructorWithNonFinalField_finalInstanceVar() async {
Source source = addSource(r'''
class A {
final int x = 0;
const A();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constConstructorWithNonFinalField_static() async {
Source source = addSource(r'''
class A {
static int x;
const A();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constConstructorWithNonFinalField_syntheticField() async {
Source source = addSource(r'''
class A {
const A();
set x(value) {}
get x {return 0;}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constDeferredClass_new() async {
await resolveWithErrors(<String>[
r'''
library lib1;
class A {
const A.b();
}''',
r'''
library root;
import 'lib1.dart' deferred as a;
main() {
new a.A.b();
}'''
], <ErrorCode>[]);
}
test_constEval_functionTypeLiteral() async {
Source source = addSource(r'''
typedef F();
const C = F;''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constEval_propertyExtraction_fieldStatic_targetType() async {
addNamedSource("/math.dart", r'''
library math;
const PI = 3.14;''');
Source source = addSource(r'''
import 'math.dart' as math;
const C = math.PI;''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constEval_propertyExtraction_methodStatic_targetType() async {
Source source = addSource(r'''
class A {
const A();
static m() {}
}
const C = A.m;''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constEval_symbol() async {
addNamedSource("/math.dart", r'''
library math;
const PI = 3.14;''');
Source source = addSource(r'''
const C = #foo;
foo() {}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constEvalTypeBoolNumString_equal() async {
Source source = addSource(r'''
class B {
final v;
const B.a1(bool p) : v = p == true;
const B.a2(bool p) : v = p == false;
const B.a3(bool p) : v = p == 0;
const B.a4(bool p) : v = p == 0.0;
const B.a5(bool p) : v = p == '';
const B.b1(int p) : v = p == true;
const B.b2(int p) : v = p == false;
const B.b3(int p) : v = p == 0;
const B.b4(int p) : v = p == 0.0;
const B.b5(int p) : v = p == '';
const B.c1(String p) : v = p == true;
const B.c2(String p) : v = p == false;
const B.c3(String p) : v = p == 0;
const B.c4(String p) : v = p == 0.0;
const B.c5(String p) : v = p == '';
const B.n1(num p) : v = p == null;
const B.n2(num p) : v = null == p;
const B.n3(Object p) : v = p == null;
const B.n4(Object p) : v = null == p;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
}
test_constEvalTypeBoolNumString_notEqual() async {
Source source = addSource(r'''
class B {
final v;
const B.a1(bool p) : v = p != true;
const B.a2(bool p) : v = p != false;
const B.a3(bool p) : v = p != 0;
const B.a4(bool p) : v = p != 0.0;
const B.a5(bool p) : v = p != '';
const B.b1(int p) : v = p != true;
const B.b2(int p) : v = p != false;
const B.b3(int p) : v = p != 0;
const B.b4(int p) : v = p != 0.0;
const B.b5(int p) : v = p != '';
const B.c1(String p) : v = p != true;
const B.c2(String p) : v = p != false;
const B.c3(String p) : v = p != 0;
const B.c4(String p) : v = p != 0.0;
const B.c5(String p) : v = p != '';
const B.n1(num p) : v = p != null;
const B.n2(num p) : v = null != p;
const B.n3(Object p) : v = p != null;
const B.n4(Object p) : v = null != p;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constEvAlTypeNum_String() async {
Source source = addSource(r'''
const String A = 'a';
const String B = A + 'b';
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constMapKeyExpressionTypeImplementsEquals_abstract() async {
Source source = addSource(r'''
abstract class B {
final id;
const B(this.id);
String toString() => 'C($id)';
/** Equality is identity equality, the id isn't used. */
bool operator==(Object other);
}
class C extends B {
const C(id) : super(id);
}
Map getMap() {
return const { const C(0): 'Map: 0' };
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constNotInitialized_field() async {
Source source = addSource(r'''
class A {
static const int x = 0;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constNotInitialized_local() async {
Source source = addSource(r'''
main() {
const int x = 0;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constRedirectSkipsSupertype() async {
// Since C redirects to C.named, it doesn't implicitly refer to B's
// unnamed constructor. Therefore there is no cycle.
Source source = addSource('''
class B {
final x;
const B() : x = y;
const B.named() : x = null;
}
class C extends B {
const C() : this.named();
const C.named() : super.named();
}
const y = const C();
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constructorDeclaration_scope_signature() async {
Source source = addSource(r'''
const app = 0;
class A {
A(@app int app) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constWithNonConstantArgument_constField() async {
Source source = addSource(r'''
class A {
const A(x);
}
main() {
const A(double.INFINITY);
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constWithNonConstantArgument_literals() async {
Source source = addSource(r'''
class A {
const A(a, b, c, d);
}
f() { return const A(true, 0, 1.0, '2'); }''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constWithTypeParameters_direct() async {
Source source = addSource(r'''
class A<T> {
static const V = const A<int>();
const A();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constWithUndefinedConstructor() async {
Source source = addSource(r'''
class A {
const A.name();
}
f() {
return const A.name();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constWithUndefinedConstructorDefault() async {
Source source = addSource(r'''
class A {
const A();
}
f() {
return const A();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_defaultValueInFunctionTypeAlias() async {
Source source = addSource("typedef F([x]);");
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_defaultValueInFunctionTypedParameter_named() async {
Source source = addSource("f(g({p})) {}");
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_defaultValueInFunctionTypedParameter_optional() async {
Source source = addSource("f(g([p])) {}");
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_deprecatedMemberUse_hide() async {
Source source = addSource(r'''
library lib;
import 'lib1.dart' hide B;
A a = new A();''');
addNamedSource("/lib1.dart", r'''
library lib1;
class A {}
@deprecated
class B {}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_duplicateDefinition_emptyName() async {
// Note: This code has two FunctionElements '() {}' with an empty name,
// this tests that the empty string is not put into the scope
// (more than once).
Source source = addSource(r'''
Map _globalMap = {
'a' : () {},
'b' : () {}
};''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_duplicateDefinition_getter() async {
Source source = addSource("bool get a => true;");
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_duplicatePart() async {
addNamedSource('/part1.dart', 'part of lib;');
addNamedSource('/part2.dart', 'part of lib;');
Source source = addSource(r'''
library lib;
part 'part1.dart';
part 'part2.dart';
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_dynamicIdentifier() async {
Source source = addSource(r'''
main() {
var v = dynamic;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_empty_generator_async() async {
Source source = addSource('''
import 'dart:async';
Stream<int> f() async* {
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_empty_generator_sync() async {
Source source = addSource('''
Iterable<int> f() sync* {
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_expectedOneListTypeArgument() async {
Source source = addSource(r'''
main() {
<int> [];
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_expectedTwoMapTypeArguments() async {
Source source = addSource(r'''
main() {
<int, int> {};
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_exportDuplicatedLibraryUnnamed() async {
Source source = addSource(r'''
library test;
export 'lib1.dart';
export 'lib2.dart';''');
addNamedSource("/lib1.dart", "");
addNamedSource("/lib2.dart", "");
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_exportOfNonLibrary_libraryDeclared() async {
Source source = addSource(r'''
library L;
export 'lib1.dart';''');
addNamedSource("/lib1.dart", "library lib1;");
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_exportOfNonLibrary_libraryNotDeclared() async {
Source source = addSource(r'''
library L;
export 'lib1.dart';''');
addNamedSource("/lib1.dart", "");
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_extraPositionalArguments_function() async {
Source source = addSource(r'''
f(p1, p2) {}
main() {
f(1, 2);
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_extraPositionalArguments_Function() async {
Source source = addSource(r'''
f(Function a) {
a(1, 2);
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_extraPositionalArguments_typedef_local() async {
Source source = addSource(r'''
typedef A(p1, p2);
A getA() => null;
f() {
A a = getA();
a(1, 2);
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_extraPositionalArguments_typedef_parameter() async {
Source source = addSource(r'''
typedef A(p1, p2);
f(A a) {
a(1, 2);
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_fieldFormalParameter_functionTyped_named() async {
Source source = addSource(r'''
class C {
final Function field;
C({String this.field(int value)});
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_fieldFormalParameter_genericFunctionTyped() async {
Source source = addSource(r'''
class C {
final Object Function(int, double) field;
C(String Function(num, Object) this.field);
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_fieldFormalParameter_genericFunctionTyped_named() async {
Source source = addSource(r'''
class C {
final Object Function(int, double) field;
C({String Function(num, Object) this.field});
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_fieldInitializedByMultipleInitializers() async {
Source source = addSource(r'''
class A {
int x;
int y;
A() : x = 0, y = 0 {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_fieldInitializedInInitializerAndDeclaration_fieldNotFinal() async {
Source source = addSource(r'''
class A {
int x = 0;
A() : x = 1 {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_fieldInitializedInInitializerAndDeclaration_finalFieldNotSet() async {
Source source = addSource(r'''
class A {
final int x;
A() : x = 1 {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_fieldInitializerOutsideConstructor() async {
Source source = addSource(r'''
class A {
int x;
A(this.x) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_fieldInitializerOutsideConstructor_defaultParameters() async {
Source source = addSource(r'''
class A {
int x;
A([this.x]) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_fieldInitializerRedirectingConstructor_super() async {
Source source = addSource(r'''
class A {
A() {}
}
class B extends A {
int x;
B(this.x) : super();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_finalInitializedInDeclarationAndConstructor_initializer() async {
Source source = addSource(r'''
class A {
final x;
A() : x = 1 {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_finalInitializedInDeclarationAndConstructor_initializingFormal() async {
Source source = addSource(r'''
class A {
final x;
A(this.x) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_finalNotInitialized_atDeclaration() async {
Source source = addSource(r'''
class A {
final int x = 0;
A() {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_finalNotInitialized_fieldFormal() async {
Source source = addSource(r'''
class A {
final int x = 0;
A() {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_finalNotInitialized_functionTypedFieldFormal() async {
Source source = addSource(r'''
class A {
final Function x;
A(int this.x(int p)) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_finalNotInitialized_hasNativeClause_hasConstructor() async {
Source source = addSource(r'''
class A native 'something' {
final int x;
A() {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.NATIVE_CLAUSE_IN_NON_SDK_CODE]);
verify([source]);
}
test_finalNotInitialized_hasNativeClause_noConstructor() async {
Source source = addSource(r'''
class A native 'something' {
final int x;
}''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.NATIVE_CLAUSE_IN_NON_SDK_CODE]);
verify([source]);
}
test_finalNotInitialized_initializer() async {
Source source = addSource(r'''
class A {
final int x;
A() : x = 0 {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_finalNotInitialized_redirectingConstructor() async {
Source source = addSource(r'''
class A {
final int x;
A(this.x);
A.named() : this (42);
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_forEach_genericFunctionType() async {
Source source = addSource(r'''
main() {
for (Null Function<T>(T, Null) e in <dynamic>[]) {
e;
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_functionDeclaration_scope_returnType() async {
Source source = addSource("int f(int) { return 0; }");
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_functionDeclaration_scope_signature() async {
Source source = addSource(r'''
const app = 0;
f(@app int app) {}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_functionTypeAlias_scope_returnType() async {
Source source = addSource("typedef int f(int);");
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_functionTypeAlias_scope_signature() async {
Source source = addSource(r'''
const app = 0;
typedef int f(@app int app);''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_functionWithoutCall() async {
Source source = addSource(r'''
abstract class A implements Function {
}
class B implements A {
void call() {}
}
class C extends A {
void call() {}
}
class D extends C {
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_functionWithoutCall_doesNotImplementFunction() async {
Source source = addSource("class A {}");
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_functionWithoutCall_staticCallMethod() async {
Source source = addSource(r'''
class A { }
class B extends A {
static call() { }
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_functionWithoutCall_withNoSuchMethod() async {
// 16078
Source source = addSource(r'''
class A implements Function {
noSuchMethod(inv) {
return 42;
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_functionWithoutCall_withNoSuchMethod_mixin() async {
Source source = addSource(r'''
class A {
noSuchMethod(inv) {}
}
class B extends Object with A implements Function {
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_functionWithoutCall_withNoSuchMethod_superclass() async {
Source source = addSource(r'''
class A {
noSuchMethod(inv) {}
}
class B extends A implements Function {
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_genericTypeAlias_castsAndTypeChecks_hasTypeParameters() async {
Source source = addSource('''
typedef Foo<S> = S Function<T>(T x);
main(Object p) {
(p as Foo)<int>(3);
if (p is Foo) {
p<int>(3);
}
(p as Foo<String>)<int>(3);
if (p is Foo<String>) {
p<int>(3);
}
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_genericTypeAlias_castsAndTypeChecks_noTypeParameters() async {
Source source = addSource('''
typedef Foo = T Function<T>(T x);
main(Object p) {
(p as Foo)<int>(3);
if (p is Foo) {
p<int>(3);
}
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_genericTypeAlias_fieldAndReturnType_noTypeParameters() async {
Source source = addSource(r'''
typedef Foo = int Function<T>(T x);
int foo<T>(T x) => 3;
Foo bar() => foo;
void test1() {
bar()<String>("hello");
}
class A {
Foo f;
void test() {
f<String>("hello");
}
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_genericTypeAlias_fieldAndReturnType_typeParameters_arguments() async {
Source source = addSource(r'''
typedef Foo<S> = S Function<T>(T x);
int foo<T>(T x) => 3;
Foo<int> bar() => foo;
void test1() {
bar()<String>("hello");
}
class A {
Foo<int> f;
void test() {
f<String>("hello");
}
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_genericTypeAlias_fieldAndReturnType_typeParameters_noArguments() async {
Source source = addSource(r'''
typedef Foo<S> = S Function<T>(T x);
int foo<T>(T x) => 3;
Foo bar() => foo;
void test1() {
bar()<String>("hello");
}
class A {
Foo f;
void test() {
f<String>("hello");
}
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_genericTypeAlias_invalidGenericFunctionType() async {
Source source = addSource('''
typedef F = int;
main(p) {
p is F;
}
''');
await computeAnalysisResult(source);
// There is a parse error, but no crashes.
assertErrors(source, [ParserErrorCode.INVALID_GENERIC_FUNCTION_TYPE]);
verify([source]);
}
test_genericTypeAlias_noTypeParameters() async {
Source source = addSource(r'''
typedef Foo = int Function<T>(T x);
int foo<T>(T x) => 3;
void test1() {
Foo y = foo;
// These two should be equivalent
foo<String>("hello");
y<String>("hello");
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_genericTypeAlias_typeParameters() async {
Source source = addSource(r'''
typedef Foo<S> = S Function<T>(T x);
int foo<T>(T x) => 3;
void test1() {
Foo<int> y = foo;
// These two should be equivalent
foo<String>("hello");
y<String>("hello");
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_implicitThisReferenceInInitializer_constructorName() async {
Source source = addSource(r'''
class A {
A.named() {}
}
class B {
var v;
B() : v = new A.named();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_implicitThisReferenceInInitializer_prefixedIdentifier() async {
Source source = addSource(r'''
class A {
var f;
}
class B {
var v;
B(A a) : v = a.f;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_implicitThisReferenceInInitializer_qualifiedMethodInvocation() async {
Source source = addSource(r'''
class A {
f() {}
}
class B {
var v;
B() : v = new A().f();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_implicitThisReferenceInInitializer_qualifiedPropertyAccess() async {
Source source = addSource(r'''
class A {
var f;
}
class B {
var v;
B() : v = new A().f;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_implicitThisReferenceInInitializer_staticField_thisClass() async {
Source source = addSource(r'''
class A {
var v;
A() : v = f;
static var f;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_implicitThisReferenceInInitializer_staticGetter() async {
Source source = addSource(r'''
class A {
var v;
A() : v = f;
static get f => 42;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_implicitThisReferenceInInitializer_staticMethod() async {
Source source = addSource(r'''
class A {
var v;
A() : v = f();
static f() => 42;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_implicitThisReferenceInInitializer_topLevelField() async {
Source source = addSource(r'''
class A {
var v;
A() : v = f;
}
var f = 42;''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_implicitThisReferenceInInitializer_topLevelFunction() async {
Source source = addSource(r'''
class A {
var v;
A() : v = f();
}
f() => 42;''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_implicitThisReferenceInInitializer_topLevelGetter() async {
Source source = addSource(r'''
class A {
var v;
A() : v = f;
}
get f => 42;''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_implicitThisReferenceInInitializer_typeParameter() async {
Source source = addSource(r'''
class A<T> {
var v;
A(p) : v = (p is T);
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_importDuplicatedLibraryName() async {
Source source = addSource(r'''
library test;
import 'lib.dart';
import 'lib.dart';''');
addNamedSource("/lib.dart", "library lib;");
await computeAnalysisResult(source);
assertErrors(source, [
HintCode.UNUSED_IMPORT,
HintCode.UNUSED_IMPORT,
HintCode.DUPLICATE_IMPORT
]);
verify([source]);
}
test_importDuplicatedLibraryUnnamed() async {
Source source = addSource(r'''
library test;
import 'lib1.dart';
import 'lib2.dart';''');
addNamedSource("/lib1.dart", "");
addNamedSource("/lib2.dart", "");
await computeAnalysisResult(source);
assertErrors(source, [
// No warning on duplicate import (https://github.com/dart-lang/sdk/issues/24156)
HintCode.UNUSED_IMPORT,
HintCode.UNUSED_IMPORT
]);
verify([source]);
}
test_importOfNonLibrary_libraryDeclared() async {
Source source = addSource(r'''
library lib;
import 'part.dart';
A a;''');
addNamedSource("/part.dart", r'''
library lib1;
class A {}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_importOfNonLibrary_libraryNotDeclared() async {
Source source = addSource(r'''
library lib;
import 'part.dart';
A a;''');
addNamedSource("/part.dart", "class A {}");
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_importPrefixes_withFirstLetterDifference() async {
Source source = addSource(r'''
library L;
import 'lib1.dart' as math;
import 'lib2.dart' as path;
main() {
math.test1();
path.test2();
}''');
addNamedSource("/lib1.dart", r'''
library lib1;
test1() {}''');
addNamedSource("/lib2.dart", r'''
library lib2;
test2() {}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_inconsistentCaseExpressionTypes() async {
Source source = addSource(r'''
f(var p) {
switch (p) {
case 1:
break;
case 2:
break;
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_inconsistentMethodInheritance_accessors_typeParameter2() async {
Source source = addSource(r'''
abstract class A<E> {
E get x {return null;}
}
class B<E> {
E get x {return null;}
}
class C<E> extends A<E> implements B<E> {}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_inconsistentMethodInheritance_accessors_typeParameters1() async {
Source source = addSource(r'''
abstract class A<E> {
E get x;
}
abstract class B<E> {
E get x;
}
class C<E> implements A<E>, B<E> {
E get x => null;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_inconsistentMethodInheritance_accessors_typeParameters_diamond() async {
Source source = addSource(r'''
abstract class F<E> extends B<E> {}
class D<E> extends F<E> {
external E get g;
}
abstract class C<E> {
E get g;
}
abstract class B<E> implements C<E> {
E get g { return null; }
}
class A<E> extends B<E> implements D<E> {
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_inconsistentMethodInheritance_methods_typeParameter2() async {
Source source = addSource(r'''
class A<E> {
x(E e) {}
}
class B<E> {
x(E e) {}
}
class C<E> extends A<E> implements B<E> {
x(E e) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_inconsistentMethodInheritance_methods_typeParameters1() async {
Source source = addSource(r'''
class A<E> {
x(E e) {}
}
class B<E> {
x(E e) {}
}
class C<E> implements A<E>, B<E> {
x(E e) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_inconsistentMethodInheritance_simple() async {
Source source = addSource(r'''
abstract class A {
x();
}
abstract class B {
x();
}
class C implements A, B {
x() {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_infer_mixin() async {
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
options.enableSuperMixins = true;
resetWith(options: options);
Source source = addSource('''
abstract class A<T> {}
class B {}
class M<T> extends A<T> {}
class C extends A<B> with M {}
''');
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
ClassElement classC =
resolutionMap.elementDeclaredByCompilationUnit(unit).getType('C');
expect(classC.mixins, hasLength(1));
expect(classC.mixins[0].toString(), 'M<B>');
}
test_infer_mixin_multiplyConstrained() async {
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
options.enableSuperMixins = true;
resetWith(options: options);
Source source = addSource('''
abstract class A<T> {}
abstract class B<U> {}
class C {}
class D {}
class M<T, U> extends A<T> with B<U> {}
class E extends A<C> implements B<D> {}
class F extends E with M {}
''');
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
ClassElement classF =
resolutionMap.elementDeclaredByCompilationUnit(unit).getType('F');
expect(classF.mixins, hasLength(1));
expect(classF.mixins[0].toString(), 'M<C, D>');
}
test_infer_mixin_with_substitution() async {
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
options.enableSuperMixins = true;
resetWith(options: options);
Source source = addSource('''
abstract class A<T> {}
class B {}
class M<T> extends A<List<T>> {}
class C extends A<List<B>> with M {}
''');
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
CompilationUnit unit = analysisResult.unit;
ClassElement classC =
resolutionMap.elementDeclaredByCompilationUnit(unit).getType('C');
expect(classC.mixins, hasLength(1));
expect(classC.mixins[0].toString(), 'M<B>');
}
test_infer_mixin_with_substitution_functionType() async {
AnalysisOptionsImpl options = new AnalysisOptionsImpl();
options.enableSuperMixins = true;
resetWith(options: options);
Source source = addSource('''
abstract class A<T> {}
class B {}
class M<T, U> extends A<T Function(U)> {}
class C extends A<int Function(String)> with M {}
''');
TestAnalysisResult analysisResult = await computeAnalysisResult(source);
assertNoErrors(source);
CompilationUnit unit = analysisResult.unit;
ClassElement classC =
resolutionMap.elementDeclaredByCompilationUnit(unit).getType('C');
expect(classC.mixins, hasLength(1));
expect(classC.mixins[0].toString(), 'M<int, String>');
}
test_initializingFormalForNonExistentField() async {
Source source = addSource(r'''
class A {
int x;
A(this.x) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_instance_creation_inside_annotation() async {
Source source = addSource('''
class C {
const C();
}
class D {
final C c;
const D(this.c);
}
@D(const C())
f() {}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_instanceAccessToStaticMember_fromComment() async {
Source source = addSource(r'''
class A {
static m() {}
}
/// [A.m]
main() {
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_instanceAccessToStaticMember_topLevel() async {
Source source = addSource(r'''
m() {}
main() {
m();
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_instanceMemberAccessFromStatic_fromComment() async {
Source source = addSource(r'''
class A {
m() {}
/// [m]
static foo() {
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_instanceMethodNameCollidesWithSuperclassStatic_field() async {
Source source = addSource(r'''
import 'lib.dart';
class B extends A {
_m() {}
}''');
addNamedSource("/lib.dart", r'''
library L;
class A {
static var _m;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_instanceMethodNameCollidesWithSuperclassStatic_method() async {
Source source = addSource(r'''
import 'lib.dart';
class B extends A {
_m() {}
}''');
addNamedSource("/lib.dart", r'''
library L;
class A {
static _m() {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_integerLiteralOutOfRange_negative_leadingZeros() async {
Source source = addSource('int x = -000923372036854775809;');
await computeAnalysisResult(source);
assertNoErrors(source);
}
test_integerLiteralOutOfRange_negative_small() async {
Source source = addSource('int x = -42;');
await computeAnalysisResult(source);
assertErrors(source);
}
test_integerLiteralOutOfRange_negative_valid() async {
Source source = addSource('int x = -9223372036854775808;');
await computeAnalysisResult(source);
assertErrors(source);
}
test_integerLiteralOutOfRange_positive_leadingZeros() async {
Source source = addSource('int x = 000923372036854775808;');
await computeAnalysisResult(source);
assertNoErrors(source);
}
test_integerLiteralOutOfRange_positive_valid() async {
Source source = addSource('int x = 9223372036854775807;');
await computeAnalysisResult(source);
assertNoErrors(source);
}
test_integerLiteralOutOfRange_positive_zero() async {
Source source = addSource('int x = 0;');
await computeAnalysisResult(source);
assertNoErrors(source);
}
test_intLiteralInDoubleContext() async {
Source source = addSource(r'''
void takeDouble(double x) {}
void main() {
takeDouble(0);
takeDouble(-0);
takeDouble(0x0);
takeDouble(-0x0);
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_intLiteralInDoubleContext_const() async {
Source source = addSource(r'''
class C {
const C(double x)
: assert((x + 3) / 2 == 1.5)
, assert(x == 0.0);
}
@C(0)
@C(-0)
@C(0x0)
@C(-0x0)
void main() {
const C(0);
const C(-0);
const C(0x0);
const C(-0x0);
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_intLiteralInDoubleContext_const_exact() async {
// TODO(mfairhurst): get the commented out assertions to pass.
Source source = addSource(r'''
class C {
const C(double x)
: assert("$x" == "0.0")
, assert(identical(x, 0.0));
}
@C(0)
@C(-0)
@C(0x0)
@C(-0x0)
void main() {
const C(0);
const C(-0);
const C(0x0);
const C(-0x0);
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAnnotation_constantVariable_field() async {
Source source = addSource(r'''
@A.C
class A {
static const C = 0;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAnnotation_constantVariable_field_importWithPrefix() async {
addNamedSource("/lib.dart", r'''
library lib;
class A {
static const C = 0;
}''');
Source source = addSource(r'''
import 'lib.dart' as p;
@p.A.C
main() {
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAnnotation_constantVariable_topLevel() async {
Source source = addSource(r'''
const C = 0;
@C
main() {
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAnnotation_constantVariable_topLevel_importWithPrefix() async {
addNamedSource("/lib.dart", r'''
library lib;
const C = 0;''');
Source source = addSource(r'''
import 'lib.dart' as p;
@p.C
main() {
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAnnotation_constConstructor_importWithPrefix() async {
addNamedSource("/lib.dart", r'''
library lib;
class A {
const A(int p);
}''');
Source source = addSource(r'''
import 'lib.dart' as p;
@p.A(42)
main() {
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAnnotation_constConstructor_named_importWithPrefix() async {
addNamedSource("/lib.dart", r'''
library lib;
class A {
const A.named(int p);
}''');
Source source = addSource(r'''
import 'lib.dart' as p;
@p.A.named(42)
main() {
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAssignment() async {
Source source = addSource(r'''
f() {
var x;
var y;
x = y;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAssignment_compoundAssignment() async {
Source source = addSource(r'''
class byte {
int _value;
byte(this._value);
byte operator +(int val) { return this; }
}
void main() {
byte b = new byte(52);
b += 3;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAssignment_defaultValue_named() async {
Source source = addSource(r'''
f({String x: '0'}) {
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAssignment_defaultValue_optional() async {
Source source = addSource(r'''
f([String x = '0']) {
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAssignment_ifNullAssignment_compatibleType() async {
Source source = addSource('''
void f(int i) {
num n;
n ??= i;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAssignment_ifNullAssignment_sameType() async {
Source source = addSource('''
void f(int i) {
int j;
j ??= i;
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAssignment_implicitlyImplementFunctionViaCall_1() async {
// 18341
//
// This test and
// 'test_invalidAssignment_implicitlyImplementFunctionViaCall_2()'
// are closely related: here we see that 'I' checks as a subtype of
// 'IntToInt'.
Source source = addSource(r'''
class I {
int call(int x) => 0;
}
class C implements I {
noSuchMethod(_) => null;
}
typedef int IntToInt(int x);
IntToInt f = new I();''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAssignment_implicitlyImplementFunctionViaCall_2() async {
// 18341
//
// Here 'C' checks as a subtype of 'I', but 'C' does not
// check as a subtype of 'IntToInt'. Together with
// 'test_invalidAssignment_implicitlyImplementFunctionViaCall_1()' we see
// that subtyping is not transitive here.
Source source = addSource(r'''
class I {
int call(int x) => 0;
}
class C implements I {
noSuchMethod(_) => null;
}
typedef int IntToInt(int x);
IntToInt f = new C();''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAssignment_implicitlyImplementFunctionViaCall_3() async {
// 18341
//
// Like 'test_invalidAssignment_implicitlyImplementFunctionViaCall_2()',
// but uses type 'Function' instead of more precise type 'IntToInt' for 'f'.
Source source = addSource(r'''
class I {
int call(int x) => 0;
}
class C implements I {
noSuchMethod(_) => null;
}
typedef int IntToInt(int x);
Function f = new C();''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAssignment_implicitlyImplementFunctionViaCall_4() async {
// 18341
//
// Like 'test_invalidAssignment_implicitlyImplementFunctionViaCall_2()',
// but uses type 'VoidToInt' instead of more precise type 'IntToInt' for
// 'f'.
//
// Here 'C <: IntToInt <: VoidToInt', but the spec gives no transitivity
// rule for '<:'. However, many of the :/tools/test.py tests assume this
// transitivity for 'JsBuilder' objects, assigning them to
// '(String) -> dynamic'. The declared type of 'JsBuilder.call' is
// '(String, [dynamic]) -> Expression'.
Source source = addSource(r'''
class I {
int call([int x]) => 0;
}
class C implements I {
noSuchMethod(_) => null;
}
typedef int VoidToInt();
VoidToInt f = new C();''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidAssignment_toDynamic() async {
Source source = addSource(r'''
f() {
var g;
g = () => 0;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidFactoryNameNotAClass() async {
Source source = addSource(r'''
class A {
factory A() => null;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidIdentifierInAsync() async {
Source source = addSource(r'''
class A {
m() {
int async;
int await;
int yield;
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidMethodOverrideNamedParamType() async {
Source source = addSource(r'''
class A {
m({int a}) {}
}
class B implements A {
m({int a, int b}) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidOverrideDifferentDefaultValues_named() async {
Source source = addSource(r'''
class A {
m({int p : 0}) {}
}
class B extends A {
m({int p : 0}) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidOverrideDifferentDefaultValues_named_function() async {
Source source = addSource(r'''
nothing() => 'nothing';
class A {
thing(String a, {orElse : nothing}) {}
}
class B extends A {
thing(String a, {orElse : nothing}) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidOverrideDifferentDefaultValues_positional() async {
Source source = addSource(r'''
class A {
m([int p = 0]) {}
}
class B extends A {
m([int p = 0]) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidOverrideDifferentDefaultValues_positional_changedOrder() async {
Source source = addSource(r'''
class A {
m([int a = 0, String b = '0']) {}
}
class B extends A {
m([int b = 0, String a = '0']) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidOverrideDifferentDefaultValues_positional_function() async {
Source source = addSource(r'''
nothing() => 'nothing';
class A {
thing(String a, [orElse = nothing]) {}
}
class B extends A {
thing(String a, [orElse = nothing]) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidOverrideNamed_unorderedNamedParameter() async {
Source source = addSource(r'''
class A {
m({a, b}) {}
}
class B extends A {
m({b, a}) {}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_invalidOverrideRequired_less() async {
Source source = addSource(r'''
class A {