blob: ab472d553af6b705c7fec949164d350003f54102 [file] [log] [blame]
// Copyright (c) 2015, 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.
library test.src.serialization.elements_test;
import 'dart:async';
import 'package:analyzer/dart/analysis/declared_variables.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/standard_resolution_map.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/handle.dart';
import 'package:analyzer/src/dart/element/member.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/resolver.dart' show Namespace;
import 'package:analyzer/src/generated/sdk.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/testing/ast_test_factory.dart';
import 'package:analyzer/src/summary/idl.dart';
import 'package:analyzer/src/summary/resynthesize.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../generated/test_support.dart';
import '../abstract_single_unit.dart';
import '../context/abstract_context.dart';
import 'element_text.dart';
import 'test_strategies.dart';
/**
* Abstract base class for resynthesizing and comparing elements.
*
* The return type separator: →
*/
abstract class AbstractResynthesizeTest extends AbstractSingleUnitTest {
Set<Source> otherLibrarySources = new Set<Source>();
/**
* Tests may set this to `true` to indicate that a missing file at the time of
* summary resynthesis shouldn't trigger an error.
*/
bool allowMissingFiles = false;
void addLibrary(String uri) {
otherLibrarySources.add(context.sourceFactory.forUri(uri));
}
Source addLibrarySource(String filePath, String contents) {
Source source = addSource(filePath, contents);
otherLibrarySources.add(source);
return source;
}
void assertNoErrors(Source source) {
GatheringErrorListener errorListener = new GatheringErrorListener();
for (AnalysisError error in context.computeErrors(source)) {
expect(error.source, source);
ErrorCode errorCode = error.errorCode;
if (errorCode == HintCode.UNUSED_ELEMENT ||
errorCode == HintCode.UNUSED_FIELD) {
continue;
}
if (errorCode == HintCode.UNUSED_CATCH_CLAUSE ||
errorCode == HintCode.UNUSED_CATCH_STACK ||
errorCode == HintCode.UNUSED_LOCAL_VARIABLE) {
continue;
}
errorListener.onError(error);
}
errorListener.assertNoErrors();
}
/**
* Verify that the [resynthesizer] didn't do any unnecessary work when
* resynthesizing [library].
*/
void checkMinimalResynthesisWork(
TestSummaryResynthesizer resynthesizer, LibraryElement library) {
// Check that no other summaries needed to be resynthesized to resynthesize
// the library element.
expect(resynthesizer.resynthesisCount, 3);
// Check that the only linked summary consulted was that for [uri].
expect(resynthesizer.linkedSummariesRequested, hasLength(1));
expect(resynthesizer.linkedSummariesRequested.first,
library.source.uri.toString());
// Check that the only unlinked summaries consulted were those for the
// library in question.
Set<String> expectedCompilationUnitUris = library.units
.map((CompilationUnitElement unit) => unit.source.uri.toString())
.toSet();
for (String requestedUri in resynthesizer.unlinkedSummariesRequested) {
expect(expectedCompilationUnitUris, contains(requestedUri));
}
}
DartSdk createDartSdk() => AbstractContextTest.SHARED_MOCK_SDK;
/**
* Create the analysis options that should be used for this test.
*/
AnalysisOptionsImpl createOptions() => new AnalysisOptionsImpl();
@override
void setUp() {
super.setUp();
prepareAnalysisContext(createOptions());
}
}
/// Mixin containing test cases exercising summary resynthesis. Intended to be
/// applied to a class implementing [ResynthesizeTestStrategy], along with the
/// mixin [ResynthesizeTestHelpers].
abstract class ResynthesizeTestCases implements ResynthesizeTestHelpers {
test_class_abstract() async {
var library = await checkLibrary('abstract class C {}');
checkElementText(library, r'''
abstract class C {
}
''');
}
test_class_alias() async {
var library = await checkLibrary('''
class C = D with E, F, G;
class D {}
class E {}
class F {}
class G {}
''');
checkElementText(library, r'''
class alias C extends D with E, F, G {
synthetic C() = D;
}
class D {
}
class E {
}
class F {
}
class G {
}
''');
}
test_class_alias_abstract() async {
var library = await checkLibrary('''
abstract class C = D with E;
class D {}
class E {}
''');
checkElementText(library, r'''
abstract class alias C extends D with E {
synthetic C() = D;
}
class D {
}
class E {
}
''');
}
test_class_alias_documented() async {
var library = await checkLibrary('''
/**
* Docs
*/
class C = D with E;
class D {}
class E {}
''');
checkElementText(library, r'''
/**
* Docs
*/
class alias C extends D with E {
synthetic C() = D;
}
class D {
}
class E {
}
''');
}
test_class_alias_documented_tripleSlash() async {
var library = await checkLibrary('''
/// aaa
/// b
/// cc
class C = D with E;
class D {}
class E {}
''');
checkElementText(library, r'''
/// aaa
/// b
/// cc
class alias C extends D with E {
synthetic C() = D;
}
class D {
}
class E {
}
''');
}
test_class_alias_documented_withLeadingNonDocumentation() async {
var library = await checkLibrary('''
// Extra comment so doc comment offset != 0
/**
* Docs
*/
class C = D with E;
class D {}
class E {}''');
checkElementText(library, r'''
/**
* Docs
*/
class alias C extends D with E {
synthetic C() = D;
}
class D {
}
class E {
}
''');
}
test_class_alias_generic() async {
var library = await checkLibrary('''
class Z = A with B<int>, C<double>;
class A {}
class B<B1> {}
class C<C1> {}
''');
checkElementText(library, r'''
class alias Z extends A with B<int>, C<double> {
synthetic Z() = A;
}
class A {
}
class B<B1> {
}
class C<C1> {
}
''');
}
test_class_alias_with_forwarding_constructors() async {
addLibrarySource('/a.dart', '''
class Base {
Base._priv();
Base();
Base.noArgs();
Base.requiredArg(x);
Base.positionalArg([x]);
Base.namedArg({x});
factory Base.fact() => null;
factory Base.fact2() = Base.noArgs;
}
''');
var library = await checkLibrary('''
import "a.dart";
class M {}
class MixinApp = Base with M;
''');
checkElementText(library, r'''
import 'a.dart';
class M {
}
class alias MixinApp extends Base with M {
synthetic MixinApp() = Base;
synthetic MixinApp.noArgs() = Base.noArgs;
synthetic MixinApp.requiredArg(dynamic x) = Base.requiredArg;
synthetic MixinApp.fact() = Base.fact;
synthetic MixinApp.fact2() = Base.fact2;
}
''');
}
test_class_alias_with_forwarding_constructors_type_substitution() async {
var library = await checkLibrary('''
class Base<T> {
Base.ctor(T t, List<T> l);
}
class M {}
class MixinApp = Base with M;
''');
checkElementText(library, r'''
class Base<T> {
Base.ctor(T t, List<T> l);
}
class M {
}
class alias MixinApp extends Base<dynamic> with M {
synthetic MixinApp.ctor(dynamic t, List<dynamic> l) = Base<T>.ctor;
}
''');
}
test_class_alias_with_forwarding_constructors_type_substitution_complex() async {
var library = await checkLibrary('''
class Base<T> {
Base.ctor(T t, List<T> l);
}
class M {}
class MixinApp<U> = Base<List<U>> with M;
''');
checkElementText(library, r'''
class Base<T> {
Base.ctor(T t, List<T> l);
}
class M {
}
class alias MixinApp<U> extends Base<List<U>> with M {
synthetic MixinApp.ctor(List<U> t, List<List<U>> l) = Base<T>.ctor;
}
''');
}
test_class_alias_with_mixin_members() async {
var library = await checkLibrary('''
class C = D with E;
class D {}
class E {
int get a => null;
void set b(int i) {}
void f() {}
int x;
}''');
checkElementText(library, r'''
class alias C extends D with E {
synthetic C() = D;
}
class D {
}
class E {
int x;
int get a {}
void set b(int i) {}
void f() {}
}
''');
}
test_class_constructor_const() async {
var library = await checkLibrary('class C { const C(); }');
checkElementText(library, r'''
class C {
const C();
}
''');
}
test_class_constructor_const_external() async {
var library = await checkLibrary('class C { external const C(); }');
checkElementText(library, r'''
class C {
external const C();
}
''');
}
test_class_constructor_explicit_named() async {
var library = await checkLibrary('class C { C.foo(); }');
checkElementText(library, r'''
class C {
C.foo();
}
''');
}
test_class_constructor_explicit_type_params() async {
var library = await checkLibrary('class C<T, U> { C(); }');
checkElementText(library, r'''
class C<T, U> {
C();
}
''');
}
test_class_constructor_explicit_unnamed() async {
var library = await checkLibrary('class C { C(); }');
checkElementText(library, r'''
class C {
C();
}
''');
}
test_class_constructor_external() async {
var library = await checkLibrary('class C { external C(); }');
checkElementText(library, r'''
class C {
external C();
}
''');
}
test_class_constructor_factory() async {
var library = await checkLibrary('class C { factory C() => null; }');
checkElementText(library, r'''
class C {
factory C();
}
''');
}
test_class_constructor_field_formal_dynamic_dynamic() async {
var library =
await checkLibrary('class C { dynamic x; C(dynamic this.x); }');
checkElementText(library, r'''
class C {
dynamic x;
C(dynamic this.x);
}
''');
}
test_class_constructor_field_formal_dynamic_typed() async {
var library = await checkLibrary('class C { dynamic x; C(int this.x); }');
checkElementText(library, r'''
class C {
dynamic x;
C(int this.x);
}
''');
}
test_class_constructor_field_formal_dynamic_untyped() async {
var library = await checkLibrary('class C { dynamic x; C(this.x); }');
checkElementText(library, r'''
class C {
dynamic x;
C(dynamic this.x);
}
''');
}
test_class_constructor_field_formal_multiple_matching_fields() async {
// This is a compile-time error but it should still analyze consistently.
var library = await checkLibrary('class C { C(this.x); int x; String x; }',
allowErrors: true);
checkElementText(library, r'''
class C {
int x;
String x;
C(int this.x);
}
''');
}
test_class_constructor_field_formal_no_matching_field() async {
// This is a compile-time error but it should still analyze consistently.
var library =
await checkLibrary('class C { C(this.x); }', allowErrors: true);
checkElementText(library, r'''
class C {
C(dynamic this.x);
}
''');
}
test_class_constructor_field_formal_typed_dynamic() async {
var library = await checkLibrary('class C { num x; C(dynamic this.x); }',
allowErrors: true);
checkElementText(library, r'''
class C {
num x;
C(dynamic this.x);
}
''');
}
test_class_constructor_field_formal_typed_typed() async {
var library = await checkLibrary('class C { num x; C(int this.x); }');
checkElementText(library, r'''
class C {
num x;
C(int this.x);
}
''');
}
test_class_constructor_field_formal_typed_untyped() async {
var library = await checkLibrary('class C { num x; C(this.x); }');
checkElementText(library, r'''
class C {
num x;
C(num this.x);
}
''');
}
test_class_constructor_field_formal_untyped_dynamic() async {
var library = await checkLibrary('class C { var x; C(dynamic this.x); }');
checkElementText(library, r'''
class C {
dynamic x;
C(dynamic this.x);
}
''');
}
test_class_constructor_field_formal_untyped_typed() async {
var library = await checkLibrary('class C { var x; C(int this.x); }');
checkElementText(library, r'''
class C {
dynamic x;
C(int this.x);
}
''');
}
test_class_constructor_field_formal_untyped_untyped() async {
var library = await checkLibrary('class C { var x; C(this.x); }');
checkElementText(library, r'''
class C {
dynamic x;
C(dynamic this.x);
}
''');
}
test_class_constructor_fieldFormal_named_noDefault() async {
var library = await checkLibrary('class C { int x; C({this.x}); }');
checkElementText(library, r'''
class C {
int x;
C({int this.x});
}
''');
}
test_class_constructor_fieldFormal_named_withDefault() async {
var library = await checkLibrary('class C { int x; C({this.x: 42}); }');
checkElementText(library, r'''
class C {
int x;
C({int this.x: 42});
}
''');
}
test_class_constructor_fieldFormal_optional_noDefault() async {
var library = await checkLibrary('class C { int x; C([this.x]); }');
checkElementText(library, r'''
class C {
int x;
C([int this.x]);
}
''');
}
test_class_constructor_fieldFormal_optional_withDefault() async {
var library = await checkLibrary('class C { int x; C([this.x = 42]); }');
checkElementText(library, r'''
class C {
int x;
C([int this.x = 42]);
}
''');
}
test_class_constructor_implicit() async {
var library = await checkLibrary('class C {}');
checkElementText(library, r'''
class C {
}
''');
}
test_class_constructor_implicit_type_params() async {
var library = await checkLibrary('class C<T, U> {}');
checkElementText(library, r'''
class C<T, U> {
}
''');
}
test_class_constructor_params() async {
var library = await checkLibrary('class C { C(x, int y); }');
checkElementText(library, r'''
class C {
C(dynamic x, int y);
}
''');
}
test_class_constructors() async {
var library = await checkLibrary('class C { C.foo(); C.bar(); }');
checkElementText(library, r'''
class C {
C.foo();
C.bar();
}
''');
}
test_class_documented() async {
var library = await checkLibrary('''
/**
* Docs
*/
class C {}''');
checkElementText(library, r'''
/**
* Docs
*/
class C {
}
''');
}
test_class_documented_mix() async {
var library = await checkLibrary('''
/**
* aaa
*/
/**
* bbb
*/
class A {}
/**
* aaa
*/
/// bbb
/// ccc
class B {}
/// aaa
/// bbb
/**
* ccc
*/
class C {}
/// aaa
/// bbb
/**
* ccc
*/
/// ddd
class D {}
/**
* aaa
*/
// bbb
class E {}
''');
checkElementText(library, r'''
/**
* bbb
*/
class A {
}
/// bbb
/// ccc
class B {
}
/**
* ccc
*/
class C {
}
/// ddd
class D {
}
/**
* aaa
*/
class E {
}
''');
}
test_class_documented_tripleSlash() async {
var library = await checkLibrary('''
/// aaa
/// bbbb
/// cc
class C {}''');
checkElementText(library, r'''
/// aaa
/// bbbb
/// cc
class C {
}
''');
}
test_class_documented_with_references() async {
var library = await checkLibrary('''
/**
* Docs referring to [D] and [E]
*/
class C {}
class D {}
class E {}''');
checkElementText(library, r'''
/**
* Docs referring to [D] and [E]
*/
class C {
}
class D {
}
class E {
}
''');
}
test_class_documented_with_windows_line_endings() async {
var library = await checkLibrary('/**\r\n * Docs\r\n */\r\nclass C {}');
checkElementText(library, r'''
/**
* Docs
*/
class C {
}
''');
}
test_class_documented_withLeadingNotDocumentation() async {
var library = await checkLibrary('''
// Extra comment so doc comment offset != 0
/**
* Docs
*/
class C {}''');
checkElementText(library, r'''
/**
* Docs
*/
class C {
}
''');
}
test_class_documented_withMetadata() async {
var library = await checkLibrary('''
/// Comment 1
/// Comment 2
@Annotation()
class BeforeMeta {}
/// Comment 1
/// Comment 2
@Annotation.named()
class BeforeMetaNamed {}
@Annotation()
/// Comment 1
/// Comment 2
class AfterMeta {}
/// Comment 1
@Annotation()
/// Comment 2
class AroundMeta {}
/// Doc comment.
@Annotation()
// Not doc comment.
class DocBeforeMetaNotDocAfter {}
class Annotation {
const Annotation();
const Annotation.named();
}
''');
checkElementText(
library,
r'''
/// Comment 1
/// Comment 2
@Annotation()
class BeforeMeta {
}
/// Comment 1
/// Comment 2
@Annotation.named()
class BeforeMetaNamed {
}
/// Comment 1
/// Comment 2
@Annotation()
class AfterMeta {
}
/// Comment 2
@Annotation()
class AroundMeta {
}
/// Doc comment.
@Annotation()
class DocBeforeMetaNotDocAfter {
}
class Annotation {
const Annotation();
const Annotation.named();
}
''',
withConstElements: false);
}
test_class_field_const() async {
var library = await checkLibrary('class C { static const int i = 0; }');
checkElementText(library, r'''
class C {
static const int i = 0;
}
''');
}
test_class_field_implicit_type() async {
var library = await checkLibrary('class C { var x; }');
checkElementText(library, r'''
class C {
dynamic x;
}
''');
}
test_class_field_static() async {
var library = await checkLibrary('class C { static int i; }');
checkElementText(library, r'''
class C {
static int i;
}
''');
}
test_class_fields() async {
var library = await checkLibrary('class C { int i; int j; }');
checkElementText(library, r'''
class C {
int i;
int j;
}
''');
}
test_class_getter_abstract() async {
var library = await checkLibrary('abstract class C { int get x; }');
checkElementText(library, r'''
abstract class C {
int get x;
}
''');
}
test_class_getter_external() async {
var library = await checkLibrary('class C { external int get x; }');
checkElementText(library, r'''
class C {
external int get x;
}
''');
}
test_class_getter_implicit_return_type() async {
var library = await checkLibrary('class C { get x => null; }');
checkElementText(library, r'''
class C {
dynamic get x {}
}
''');
}
test_class_getter_static() async {
var library = await checkLibrary('class C { static int get x => null; }');
checkElementText(library, r'''
class C {
static int get x {}
}
''');
}
test_class_getters() async {
var library =
await checkLibrary('class C { int get x => null; get y => null; }');
checkElementText(library, r'''
class C {
int get x {}
dynamic get y {}
}
''');
}
test_class_implicitField_getterFirst() async {
var library = await checkLibrary('''
class C {
int get x => 0;
void set x(int value) {}
}
''');
checkElementText(library, r'''
class C {
int get x {}
void set x(int value) {}
}
''');
}
test_class_implicitField_setterFirst() async {
var library = await checkLibrary('''
class C {
void set x(int value) {}
int get x => 0;
}
''');
checkElementText(library, r'''
class C {
void set x(int value) {}
int get x {}
}
''');
}
test_class_interfaces() async {
var library = await checkLibrary('''
class C implements D, E {}
class D {}
class E {}
''');
checkElementText(library, r'''
class C implements D, E {
}
class D {
}
class E {
}
''');
}
test_class_interfaces_unresolved() async {
var library = await checkLibrary(
'class C implements X, Y, Z {} class X {} class Z {}',
allowErrors: true);
checkElementText(library, r'''
class C implements X, Z {
}
class X {
}
class Z {
}
''');
}
test_class_method_abstract() async {
var library = await checkLibrary('abstract class C { f(); }');
checkElementText(library, r'''
abstract class C {
dynamic f();
}
''');
}
test_class_method_external() async {
var library = await checkLibrary('class C { external f(); }');
checkElementText(library, r'''
class C {
external dynamic f() {}
}
''');
}
test_class_method_params() async {
var library = await checkLibrary('class C { f(x, y) {} }');
checkElementText(library, r'''
class C {
dynamic f(dynamic x, dynamic y) {}
}
''');
}
test_class_method_static() async {
var library = await checkLibrary('class C { static f() {} }');
checkElementText(library, r'''
class C {
static dynamic f() {}
}
''');
}
test_class_methods() async {
var library = await checkLibrary('class C { f() {} g() {} }');
checkElementText(library, r'''
class C {
dynamic f() {}
dynamic g() {}
}
''');
}
test_class_mixins() async {
var library = await checkLibrary('''
class C extends D with E, F, G {}
class D {}
class E {}
class F {}
class G {}
''');
checkElementText(library, r'''
class C extends D with E, F, G {
synthetic C();
}
class D {
}
class E {
}
class F {
}
class G {
}
''');
}
test_class_mixins_generic() async {
var library = await checkLibrary('''
class Z extends A with B<int>, C<double> {}
class A {}
class B<B1> {}
class C<C1> {}
''');
checkElementText(library, r'''
class Z extends A with B<int>, C<double> {
synthetic Z();
}
class A {
}
class B<B1> {
}
class C<C1> {
}
''');
}
test_class_mixins_unresolved() async {
var library = await checkLibrary(
'class C extends Object with X, Y, Z {} class X {} class Z {}',
allowErrors: true);
checkElementText(library, r'''
class C extends Object with X, Z {
synthetic C();
}
class X {
}
class Z {
}
''');
}
test_class_setter_abstract() async {
var library =
await checkLibrary('abstract class C { void set x(int value); }');
checkElementText(library, r'''
abstract class C {
void set x(int value);
}
''');
}
test_class_setter_external() async {
var library =
await checkLibrary('class C { external void set x(int value); }');
checkElementText(library, r'''
class C {
external void set x(int value);
}
''');
}
test_class_setter_implicit_param_type() async {
var library = await checkLibrary('class C { void set x(value) {} }');
checkElementText(library, r'''
class C {
void set x(dynamic value) {}
}
''');
}
test_class_setter_implicit_return_type() async {
var library = await checkLibrary('class C { set x(int value) {} }');
checkElementText(library, r'''
class C {
void set x(int value) {}
}
''');
}
test_class_setter_invalid_named_parameter() async {
var library = await checkLibrary('class C { void set x({a}) {} }');
checkElementText(library, r'''
class C {
void set x({dynamic a}) {}
}
''');
}
test_class_setter_invalid_no_parameter() async {
var library = await checkLibrary('class C { void set x() {} }');
checkElementText(library, r'''
class C {
void set x() {}
}
''');
}
test_class_setter_invalid_optional_parameter() async {
var library = await checkLibrary('class C { void set x([a]) {} }');
checkElementText(library, r'''
class C {
void set x([dynamic a]) {}
}
''');
}
test_class_setter_invalid_too_many_parameters() async {
var library = await checkLibrary('class C { void set x(a, b) {} }');
checkElementText(library, r'''
class C {
void set x(dynamic a, dynamic b) {}
}
''');
}
test_class_setter_static() async {
var library =
await checkLibrary('class C { static void set x(int value) {} }');
checkElementText(library, r'''
class C {
static void set x(int value) {}
}
''');
}
test_class_setters() async {
var library = await checkLibrary('''
class C {
void set x(int value) {}
set y(value) {}
}
''');
checkElementText(library, r'''
class C {
void set x(int value) {}
void set y(dynamic value) {}
}
''');
}
test_class_supertype() async {
var library = await checkLibrary('''
class C extends D {}
class D {}
''');
checkElementText(library, r'''
class C extends D {
}
class D {
}
''');
}
test_class_supertype_typeArguments() async {
var library = await checkLibrary('''
class C extends D<int, double> {}
class D<T1, T2> {}
''');
checkElementText(library, r'''
class C extends D<int, double> {
}
class D<T1, T2> {
}
''');
}
test_class_supertype_unresolved() async {
var library = await checkLibrary('class C extends D {}', allowErrors: true);
checkElementText(library, r'''
class C {
}
''');
}
test_class_type_parameters() async {
var library = await checkLibrary('class C<T, U> {}');
checkElementText(library, r'''
class C<T, U> {
}
''');
}
test_class_type_parameters_bound() async {
var library = await checkLibrary('''
class C<T extends Object, U extends D> {}
class D {}
''');
checkElementText(library, r'''
class C<T, U extends D> {
}
class D {
}
''');
}
test_class_type_parameters_f_bound_complex() async {
var library = await checkLibrary('class C<T extends List<U>, U> {}');
checkElementText(library, r'''
class C<T extends List<U>, U> {
}
''');
}
test_class_type_parameters_f_bound_simple() async {
var library = await checkLibrary('class C<T extends U, U> {}');
checkElementText(library, r'''
class C<T extends U, U> {
}
''');
}
test_classes() async {
var library = await checkLibrary('class C {} class D {}');
checkElementText(library, r'''
class C {
}
class D {
}
''');
}
test_closure_executable_with_return_type_from_closure() async {
var library = await checkLibrary('''
f() {
print(() {});
print(() => () => 0);
}
''');
checkElementText(library, r'''
dynamic f() {}
''');
}
test_closure_generic() async {
var library = await checkLibrary(r'''
final f = <U, V>(U x, V y) => y;
''');
checkElementText(library, r'''
final <U,V>(U, V) → V f;
''');
}
test_closure_in_variable_declaration_in_part() async {
addSource('/a.dart', 'part of lib; final f = (int i) => i.toDouble();');
var library = await checkLibrary('''
library lib;
part "a.dart";
''');
checkElementText(library, r'''
library lib;
part 'a.dart';
--------------------
unit: a.dart
final (int) → double f;
''');
}
test_codeRange_class() async {
var library = await checkLibrary('''
class Raw {}
/// Comment 1.
/// Comment 2.
class HasDocComment {}
@Object()
class HasAnnotation {}
@Object()
/// Comment 1.
/// Comment 2.
class AnnotationThenComment {}
/// Comment 1.
/// Comment 2.
@Object()
class CommentThenAnnotation {}
/// Comment 1.
@Object()
/// Comment 2.
class CommentAroundAnnotation {}
''');
checkElementText(
library,
r'''
class Raw/*codeOffset=0, codeLength=12*/ {
}
/// Comment 1.
/// Comment 2.
class HasDocComment/*codeOffset=14, codeLength=52*/ {
}
@Object()
class HasAnnotation/*codeOffset=68, codeLength=32*/ {
}
/// Comment 1.
/// Comment 2.
@Object()
class AnnotationThenComment/*codeOffset=102, codeLength=70*/ {
}
/// Comment 1.
/// Comment 2.
@Object()
class CommentThenAnnotation/*codeOffset=174, codeLength=70*/ {
}
/// Comment 2.
@Object()
class CommentAroundAnnotation/*codeOffset=261, codeLength=57*/ {
}
''',
withCodeRanges: true,
withConstElements: false);
}
test_codeRange_class_namedMixin() async {
var library = await checkLibrary('''
class A {}
class B {}
class Raw = Object with A, B;
/// Comment 1.
/// Comment 2.
class HasDocComment = Object with A, B;
@Object()
class HasAnnotation = Object with A, B;
@Object()
/// Comment 1.
/// Comment 2.
class AnnotationThenComment = Object with A, B;
/// Comment 1.
/// Comment 2.
@Object()
class CommentThenAnnotation = Object with A, B;
/// Comment 1.
@Object()
/// Comment 2.
class CommentAroundAnnotation = Object with A, B;
''');
checkElementText(
library,
r'''
class A/*codeOffset=0, codeLength=10*/ {
}
class B/*codeOffset=12, codeLength=10*/ {
}
class alias Raw/*codeOffset=28, codeLength=29*/ extends Object with A, B {
synthetic Raw() = Object;
}
/// Comment 1.
/// Comment 2.
class alias HasDocComment/*codeOffset=59, codeLength=69*/ extends Object with A, B {
synthetic HasDocComment() = Object;
}
@Object()
class alias HasAnnotation/*codeOffset=130, codeLength=49*/ extends Object with A, B {
synthetic HasAnnotation() = Object;
}
/// Comment 1.
/// Comment 2.
@Object()
class alias AnnotationThenComment/*codeOffset=181, codeLength=87*/ extends Object with A, B {
synthetic AnnotationThenComment() = Object;
}
/// Comment 1.
/// Comment 2.
@Object()
class alias CommentThenAnnotation/*codeOffset=270, codeLength=87*/ extends Object with A, B {
synthetic CommentThenAnnotation() = Object;
}
/// Comment 2.
@Object()
class alias CommentAroundAnnotation/*codeOffset=374, codeLength=74*/ extends Object with A, B {
synthetic CommentAroundAnnotation() = Object;
}
''',
withCodeRanges: true,
withConstElements: false);
}
test_codeRange_constructor() async {
var library = await checkLibrary('''
class C {
C();
C.raw() {}
/// Comment 1.
/// Comment 2.
C.hasDocComment() {}
@Object()
C.hasAnnotation() {}
@Object()
/// Comment 1.
/// Comment 2.
C.annotationThenComment() {}
/// Comment 1.
/// Comment 2.
@Object()
C.commentThenAnnotation() {}
/// Comment 1.
@Object()
/// Comment 2.
C.commentAroundAnnotation() {}
}
''');
checkElementText(
library,
r'''
class C/*codeOffset=0, codeLength=362*/ {
C/*codeOffset=12, codeLength=4*/();
C.raw/*codeOffset=20, codeLength=10*/();
/// Comment 1.
/// Comment 2.
C.hasDocComment/*codeOffset=34, codeLength=54*/();
@Object()
C.hasAnnotation/*codeOffset=92, codeLength=32*/();
/// Comment 1.
/// Comment 2.
@Object()
C.annotationThenComment/*codeOffset=128, codeLength=74*/();
/// Comment 1.
/// Comment 2.
@Object()
C.commentThenAnnotation/*codeOffset=206, codeLength=74*/();
/// Comment 2.
@Object()
C.commentAroundAnnotation/*codeOffset=301, codeLength=59*/();
}
''',
withCodeRanges: true,
withConstElements: false);
}
test_codeRange_constructor_factory() async {
var library = await checkLibrary('''
class C {
factory C() => null;
factory C.raw() => null;
/// Comment 1.
/// Comment 2.
factory C.hasDocComment() => null;
@Object()
factory C.hasAnnotation() => null;
@Object()
/// Comment 1.
/// Comment 2.
factory C.annotationThenComment() => null;
/// Comment 1.
/// Comment 2.
@Object()
factory C.commentThenAnnotation() => null;
/// Comment 1.
@Object()
/// Comment 2.
factory C.commentAroundAnnotation() => null;
}
''');
checkElementText(
library,
r'''
class C/*codeOffset=0, codeLength=462*/ {
factory C/*codeOffset=12, codeLength=20*/();
factory C.raw/*codeOffset=36, codeLength=24*/();
/// Comment 1.
/// Comment 2.
factory C.hasDocComment/*codeOffset=64, codeLength=68*/();
@Object()
factory C.hasAnnotation/*codeOffset=136, codeLength=46*/();
/// Comment 1.
/// Comment 2.
@Object()
factory C.annotationThenComment/*codeOffset=186, codeLength=88*/();
/// Comment 1.
/// Comment 2.
@Object()
factory C.commentThenAnnotation/*codeOffset=278, codeLength=88*/();
/// Comment 2.
@Object()
factory C.commentAroundAnnotation/*codeOffset=387, codeLength=73*/();
}
''',
withCodeRanges: true,
withConstElements: false);
}
test_codeRange_field() async {
var library = await checkLibrary('''
class C {
int withInit = 1;
int withoutInit;
int multiWithInit = 2, multiWithoutInit, multiWithInit2 = 3;
}
''');
checkElementText(
library,
r'''
class C/*codeOffset=0, codeLength=116*/ {
int withInit/*codeOffset=12, codeLength=16*/;
int withoutInit/*codeOffset=33, codeLength=15*/;
int multiWithInit/*codeOffset=53, codeLength=21*/;
int multiWithoutInit/*codeOffset=76, codeLength=16*/;
int multiWithInit2/*codeOffset=94, codeLength=18*/;
}
''',
withCodeRanges: true,
withConstElements: false);
}
test_codeRange_field_annotations() async {
var library = await checkLibrary('''
class C {
/// Comment 1.
/// Comment 2.
int hasDocComment, hasDocComment2;
@Object()
int hasAnnotation, hasAnnotation2;
@Object()
/// Comment 1.
/// Comment 2.
int annotationThenComment, annotationThenComment2;
/// Comment 1.
/// Comment 2.
@Object()
int commentThenAnnotation, commentThenAnnotation2;
/// Comment 1.
@Object()
/// Comment 2.
int commentAroundAnnotation, commentAroundAnnotation2;
}
''');
checkElementText(
library,
r'''
class C/*codeOffset=0, codeLength=436*/ {
/// Comment 1.
/// Comment 2.
int hasDocComment/*codeOffset=12, codeLength=51*/;
/// Comment 1.
/// Comment 2.
int hasDocComment2/*codeOffset=65, codeLength=14*/;
@Object()
int hasAnnotation/*codeOffset=84, codeLength=29*/;
@Object()
int hasAnnotation2/*codeOffset=115, codeLength=14*/;
/// Comment 1.
/// Comment 2.
@Object()
int annotationThenComment/*codeOffset=134, codeLength=71*/;
/// Comment 1.
/// Comment 2.
@Object()
int annotationThenComment2/*codeOffset=207, codeLength=22*/;
/// Comment 1.
/// Comment 2.
@Object()
int commentThenAnnotation/*codeOffset=234, codeLength=71*/;
/// Comment 1.
/// Comment 2.
@Object()
int commentThenAnnotation2/*codeOffset=307, codeLength=22*/;
/// Comment 2.
@Object()
int commentAroundAnnotation/*codeOffset=351, codeLength=56*/;
/// Comment 2.
@Object()
int commentAroundAnnotation2/*codeOffset=409, codeLength=24*/;
}
''',
withCodeRanges: true,
withConstElements: false);
}
test_codeRange_function() async {
var library = await checkLibrary('''
void raw() {}
/// Comment 1.
/// Comment 2.
void hasDocComment() {}
@Object()
void hasAnnotation() {}
@Object()
/// Comment 1.
/// Comment 2.
void annotationThenComment() {}
/// Comment 1.
/// Comment 2.
@Object()
void commentThenAnnotation() {}
/// Comment 1.
@Object()
/// Comment 2.
void commentAroundAnnotation() {}
''');
checkElementText(
library,
r'''
void raw/*codeOffset=0, codeLength=13*/() {}
/// Comment 1.
/// Comment 2.
void hasDocComment/*codeOffset=15, codeLength=53*/() {}
@Object()
void hasAnnotation/*codeOffset=70, codeLength=33*/() {}
/// Comment 1.
/// Comment 2.
@Object()
void annotationThenComment/*codeOffset=105, codeLength=71*/() {}
/// Comment 1.
/// Comment 2.
@Object()
void commentThenAnnotation/*codeOffset=178, codeLength=71*/() {}
/// Comment 2.
@Object()
void commentAroundAnnotation/*codeOffset=266, codeLength=58*/() {}
''',
withCodeRanges: true,
withConstElements: false);
}
test_codeRange_method() async {
var library = await checkLibrary('''
class C {
void raw() {}
/// Comment 1.
/// Comment 2.
void hasDocComment() {}
@Object()
void hasAnnotation() {}
@Object()
/// Comment 1.
/// Comment 2.
void annotationThenComment() {}
/// Comment 1.
/// Comment 2.
@Object()
void commentThenAnnotation() {}
/// Comment 1.
@Object()
/// Comment 2.
void commentAroundAnnotation() {}
}
''');
checkElementText(
library,
r'''
class C/*codeOffset=0, codeLength=372*/ {
void raw/*codeOffset=12, codeLength=13*/() {}
/// Comment 1.
/// Comment 2.
void hasDocComment/*codeOffset=29, codeLength=57*/() {}
@Object()
void hasAnnotation/*codeOffset=90, codeLength=35*/() {}
/// Comment 1.
/// Comment 2.
@Object()
void annotationThenComment/*codeOffset=129, codeLength=77*/() {}
/// Comment 1.
/// Comment 2.
@Object()
void commentThenAnnotation/*codeOffset=210, codeLength=77*/() {}
/// Comment 2.
@Object()
void commentAroundAnnotation/*codeOffset=308, codeLength=62*/() {}
}
''',
withCodeRanges: true,
withConstElements: false);
}
test_codeRange_parameter() async {
var library = await checkLibrary('''
main({int a = 1, int b, int c = 2}) {}
''');
checkElementText(
library,
'dynamic main/*codeOffset=0, codeLength=38*/('
'{int a/*codeOffset=6, codeLength=9*/: 1}, '
'{int b/*codeOffset=17, codeLength=5*/}, '
'{int c/*codeOffset=24, codeLength=9*/: 2}) {}\n',
withCodeRanges: true,
withConstElements: false);
}
test_codeRange_parameter_annotations() async {
var library = await checkLibrary('''
main(@Object() int a, int b, @Object() int c) {}
''');
checkElementText(
library,
'dynamic main/*codeOffset=0, codeLength=48*/('
'@Object() int a/*codeOffset=5, codeLength=15*/, '
'int b/*codeOffset=22, codeLength=5*/, '
'@Object() int c/*codeOffset=29, codeLength=15*/) {}\n',
withCodeRanges: true,
withConstElements: false);
}
test_codeRange_topLevelVariable() async {
var library = await checkLibrary('''
int withInit = 1 + 2 * 3;
int withoutInit;
int multiWithInit = 2, multiWithoutInit, multiWithInit2 = 3;
''');
checkElementText(
library,
r'''
int withInit/*codeOffset=0, codeLength=24*/;
int withoutInit/*codeOffset=27, codeLength=15*/;
int multiWithInit/*codeOffset=45, codeLength=21*/;
int multiWithoutInit/*codeOffset=68, codeLength=16*/;
int multiWithInit2/*codeOffset=86, codeLength=18*/;
''',
withCodeRanges: true,
withConstElements: false);
}
test_codeRange_topLevelVariable_annotations() async {
var library = await checkLibrary('''
/// Comment 1.
/// Comment 2.
int hasDocComment, hasDocComment2;
@Object()
int hasAnnotation, hasAnnotation2;
@Object()
/// Comment 1.
/// Comment 2.
int annotationThenComment, annotationThenComment2;
/// Comment 1.
/// Comment 2.
@Object()
int commentThenAnnotation, commentThenAnnotation2;
/// Comment 1.
@Object()
/// Comment 2.
int commentAroundAnnotation, commentAroundAnnotation2;
''');
checkElementText(
library,
r'''
/// Comment 1.
/// Comment 2.
int hasDocComment/*codeOffset=0, codeLength=47*/;
/// Comment 1.
/// Comment 2.
int hasDocComment2/*codeOffset=49, codeLength=14*/;
@Object()
int hasAnnotation/*codeOffset=66, codeLength=27*/;
@Object()
int hasAnnotation2/*codeOffset=95, codeLength=14*/;
/// Comment 1.
/// Comment 2.
@Object()
int annotationThenComment/*codeOffset=112, codeLength=65*/;
/// Comment 1.
/// Comment 2.
@Object()
int annotationThenComment2/*codeOffset=179, codeLength=22*/;
/// Comment 1.
/// Comment 2.
@Object()
int commentThenAnnotation/*codeOffset=204, codeLength=65*/;
/// Comment 1.
/// Comment 2.
@Object()
int commentThenAnnotation2/*codeOffset=271, codeLength=22*/;
/// Comment 2.
@Object()
int commentAroundAnnotation/*codeOffset=311, codeLength=52*/;
/// Comment 2.
@Object()
int commentAroundAnnotation2/*codeOffset=365, codeLength=24*/;
''',
withCodeRanges: true,
withConstElements: false);
}
test_const_constructor_inferred_args() async {
var library = await checkLibrary('''
class C<T> {
final T t;
const C(this.t);
const C.named(this.t);
}
const Object x = const C(0);
const Object y = const C.named(0);
''');
checkElementText(library, '''
class C<T> {
final T t;
const C(T this.t);
const C.named(T this.t);
}
const Object x = const
C/*location: test.dart;C*/(0);
const Object y = const
C/*location: test.dart;C*/.
named/*location: test.dart;C;named*/(0);
''');
TopLevelVariableElementImpl x =
library.definingCompilationUnit.topLevelVariables[0];
InstanceCreationExpression xExpr = x.constantInitializer;
var xType = xExpr.constructorName.staticElement.returnType;
expect(xType.toString(), 'C<int>');
TopLevelVariableElementImpl y =
library.definingCompilationUnit.topLevelVariables[0];
InstanceCreationExpression yExpr = y.constantInitializer;
var yType = yExpr.constructorName.staticElement.returnType;
expect(yType.toString(), 'C<int>');
}
test_const_finalField_hasConstConstructor() async {
var library = await checkLibrary(r'''
class C {
final int f = 42;
const C();
}
''');
checkElementText(library, r'''
class C {
final int f = 42;
const C();
}
''');
}
test_const_invalid_field_const() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
class C {
static const f = 1 + foo();
}
int foo() => 42;
''', allowErrors: true);
checkElementText(library, r'''
class C {
static const int f = 1 +
foo/*location: test.dart;foo*/();
}
int foo() {}
''');
}
test_const_invalid_field_final() async {
variablesWithNotConstInitializers.add('f');
var library = await checkLibrary(r'''
class C {
final f = 1 + foo();
}
int foo() => 42;
''', allowErrors: true);
checkElementText(library, r'''
class C {
final int f;
}
int foo() {}
''');
}
test_const_invalid_intLiteral() async {
var library = await checkLibrary(r'''
const int x = 0x;
''', allowErrors: true);
checkElementText(library, r'''
const int x = 0;
''');
}
test_const_invalid_topLevel() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
const v = 1 + foo();
int foo() => 42;
''', allowErrors: true);
checkElementText(library, r'''
const int v = 1 +
foo/*location: test.dart;foo*/();
int foo() {}
''');
}
test_const_invalid_typeMismatch() async {
var library = await checkLibrary(r'''
const int a = 0;
const bool b = a + 5;
''', allowErrors: true);
checkElementText(library, r'''
const int a = 0;
const bool b =
a/*location: test.dart;a?*/ + 5;
''');
}
test_const_invokeConstructor_generic_named() async {
var library = await checkLibrary(r'''
class C<K, V> {
const C.named(K k, V v);
}
const V = const C<int, String>.named(1, '222');
''');
checkElementText(library, r'''
class C<K, V> {
const C.named(K k, V v);
}
const C<int, String> V = const
C/*location: test.dart;C*/<
int/*location: dart:core;int*/,
String/*location: dart:core;String*/>.
named/*location: test.dart;C;named*/(1, '222');
''');
}
test_const_invokeConstructor_generic_named_imported() async {
addLibrarySource('/a.dart', r'''
class C<K, V> {
const C.named(K k, V v);
}
''');
var library = await checkLibrary(r'''
import 'a.dart';
const V = const C<int, String>.named(1, '222');
''');
checkElementText(library, r'''
import 'a.dart';
const C<int, String> V = const
C/*location: a.dart;C*/<
int/*location: dart:core;int*/,
String/*location: dart:core;String*/>.
named/*location: a.dart;C;named*/(1, '222');
''');
}
test_const_invokeConstructor_generic_named_imported_withPrefix() async {
addLibrarySource('/a.dart', r'''
class C<K, V> {
const C.named(K k, V v);
}
''');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const V = const p.C<int, String>.named(1, '222');
''');
checkElementText(library, r'''
import 'a.dart' as p;
const C<int, String> V = const
C/*location: a.dart;C*/<
int/*location: dart:core;int*/,
String/*location: dart:core;String*/>.
named/*location: a.dart;C;named*/(1, '222');
''');
}
test_const_invokeConstructor_generic_noTypeArguments() async {
var library = await checkLibrary(r'''
class C<K, V> {
const C();
}
const V = const C();
''');
checkElementText(library, r'''
class C<K, V> {
const C();
}
const C<dynamic, dynamic> V = const
C/*location: test.dart;C*/();
''');
}
test_const_invokeConstructor_generic_unnamed() async {
var library = await checkLibrary(r'''
class C<K, V> {
const C();
}
const V = const C<int, String>();
''');
checkElementText(library, r'''
class C<K, V> {
const C();
}
const C<int, String> V = const
C/*location: test.dart;C*/<
int/*location: dart:core;int*/,
String/*location: dart:core;String*/>();
''');
}
test_const_invokeConstructor_generic_unnamed_imported() async {
addLibrarySource('/a.dart', r'''
class C<K, V> {
const C();
}
''');
var library = await checkLibrary(r'''
import 'a.dart';
const V = const C<int, String>();
''');
checkElementText(library, r'''
import 'a.dart';
const C<int, String> V = const
C/*location: a.dart;C*/<
int/*location: dart:core;int*/,
String/*location: dart:core;String*/>();
''');
}
test_const_invokeConstructor_generic_unnamed_imported_withPrefix() async {
addLibrarySource('/a.dart', r'''
class C<K, V> {
const C();
}
''');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const V = const p.C<int, String>();
''');
checkElementText(library, r'''
import 'a.dart' as p;
const C<int, String> V = const
C/*location: a.dart;C*/<
int/*location: dart:core;int*/,
String/*location: dart:core;String*/>();
''');
}
test_const_invokeConstructor_named() async {
var library = await checkLibrary(r'''
class C {
const C.named(bool a, int b, int c, {String d, double e});
}
const V = const C.named(true, 1, 2, d: 'ccc', e: 3.4);
''');
checkElementText(library, r'''
class C {
const C.named(bool a, int b, int c, {String d}, {double e});
}
const C V = const
C/*location: test.dart;C*/.
named/*location: test.dart;C;named*/(true, 1, 2,
d/*location: null*/: 'ccc',
e/*location: null*/: 3.4);
''');
}
test_const_invokeConstructor_named_imported() async {
addLibrarySource('/a.dart', r'''
class C {
const C.named();
}
''');
var library = await checkLibrary(r'''
import 'a.dart';
const V = const C.named();
''');
checkElementText(library, r'''
import 'a.dart';
const C V = const
C/*location: a.dart;C*/.
named/*location: a.dart;C;named*/();
''');
}
test_const_invokeConstructor_named_imported_withPrefix() async {
addLibrarySource('/a.dart', r'''
class C {
const C.named();
}
''');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const V = const p.C.named();
''');
checkElementText(library, r'''
import 'a.dart' as p;
const C V = const
C/*location: a.dart;C*/.
named/*location: a.dart;C;named*/();
''');
}
test_const_invokeConstructor_named_unresolved() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
class C {}
const V = const C.named();
''', allowErrors: true);
checkElementText(library, r'''
class C {
}
const dynamic V = #invalidConst;
''');
}
test_const_invokeConstructor_named_unresolved2() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
const V = const C.named();
''', allowErrors: true);
checkElementText(library, r'''
const dynamic V = #invalidConst;
''');
}
test_const_invokeConstructor_named_unresolved3() async {
shouldCompareLibraryElements = false;
addLibrarySource('/a.dart', r'''
class C {
}
''');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const V = const p.C.named();
''', allowErrors: true);
checkElementText(library, r'''
import 'a.dart' as p;
const dynamic V = #invalidConst;
''');
}
test_const_invokeConstructor_named_unresolved4() async {
shouldCompareLibraryElements = false;
addLibrarySource('/a.dart', '');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const V = const p.C.named();
''', allowErrors: true);
checkElementText(library, r'''
import 'a.dart' as p;
const dynamic V = #invalidConst;
''');
}
test_const_invokeConstructor_named_unresolved5() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
const V = const p.C.named();
''', allowErrors: true);
checkElementText(library, r'''
const dynamic V = #invalidConst;
''');
}
test_const_invokeConstructor_named_unresolved6() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
class C<T> {}
const V = const C.named();
''', allowErrors: true);
checkElementText(library, r'''
class C<T> {
}
const dynamic V = #invalidConst;
''');
}
test_const_invokeConstructor_unnamed() async {
var library = await checkLibrary(r'''
class C {
const C();
}
const V = const C();
''');
checkElementText(library, r'''
class C {
const C();
}
const C V = const
C/*location: test.dart;C*/();
''');
}
test_const_invokeConstructor_unnamed_imported() async {
addLibrarySource('/a.dart', r'''
class C {
const C();
}
''');
var library = await checkLibrary(r'''
import 'a.dart';
const V = const C();
''');
checkElementText(library, r'''
import 'a.dart';
const C V = const
C/*location: a.dart;C*/();
''');
}
test_const_invokeConstructor_unnamed_imported_withPrefix() async {
addLibrarySource('/a.dart', r'''
class C {
const C();
}
''');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const V = const p.C();
''');
checkElementText(library, r'''
import 'a.dart' as p;
const C V = const
C/*location: a.dart;C*/();
''');
}
test_const_invokeConstructor_unnamed_unresolved() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
const V = const C();
''', allowErrors: true);
checkElementText(library, r'''
const dynamic V = #invalidConst;
''');
}
test_const_invokeConstructor_unnamed_unresolved2() async {
shouldCompareLibraryElements = false;
addLibrarySource('/a.dart', '');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const V = const p.C();
''', allowErrors: true);
checkElementText(library, r'''
import 'a.dart' as p;
const dynamic V = #invalidConst;
''');
}
test_const_invokeConstructor_unnamed_unresolved3() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
const V = const p.C();
''', allowErrors: true);
checkElementText(library, r'''
const dynamic V = #invalidConst;
''');
}
test_const_length_ofClassConstField() async {
var library = await checkLibrary(r'''
class C {
static const String F = '';
}
const int v = C.F.length;
''');
checkElementText(library, r'''
class C {
static const String F = '';
}
const int v =
C/*location: test.dart;C*/.
F/*location: test.dart;C;F?*/.
length/*location: dart:core;String;length?*/;
''');
}
test_const_length_ofClassConstField_imported() async {
addLibrarySource('/a.dart', r'''
class C {
static const String F = '';
}
''');
var library = await checkLibrary(r'''
import 'a.dart';
const int v = C.F.length;
''');
checkElementText(library, r'''
import 'a.dart';
const int v =
C/*location: a.dart;C*/.
F/*location: a.dart;C;F?*/.
length/*location: dart:core;String;length?*/;
''');
}
test_const_length_ofClassConstField_imported_withPrefix() async {
addLibrarySource('/a.dart', r'''
class C {
static const String F = '';
}
''');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const int v = p.C.F.length;
''');
checkElementText(library, r'''
import 'a.dart' as p;
const int v =
p/*location: test.dart;p*/.
C/*location: a.dart;C*/.
F/*location: a.dart;C;F?*/.
length/*location: dart:core;String;length?*/;
''');
}
test_const_length_ofStringLiteral() async {
var library = await checkLibrary(r'''
const v = 'abc'.length;
''');
checkElementText(library, r'''
const int v = 'abc'.
length/*location: dart:core;String;length?*/;
''');
}
test_const_length_ofTopLevelVariable() async {
var library = await checkLibrary(r'''
const String S = 'abc';
const v = S.length;
''');
checkElementText(library, r'''
const String S = 'abc';
const int v =
S/*location: test.dart;S?*/.
length/*location: dart:core;String;length?*/;
''');
}
test_const_length_ofTopLevelVariable_imported() async {
addLibrarySource('/a.dart', r'''
const String S = 'abc';
''');
var library = await checkLibrary(r'''
import 'a.dart';
const v = S.length;
''');
checkElementText(library, r'''
import 'a.dart';
const int v =
S/*location: a.dart;S?*/.
length/*location: dart:core;String;length?*/;
''');
}
test_const_length_ofTopLevelVariable_imported_withPrefix() async {
addLibrarySource('/a.dart', r'''
const String S = 'abc';
''');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const v = p.S.length;
''');
checkElementText(library, r'''
import 'a.dart' as p;
const int v =
p/*location: test.dart;p*/.
S/*location: a.dart;S?*/.
length/*location: dart:core;String;length?*/;
''');
}
test_const_length_staticMethod() async {
var library = await checkLibrary(r'''
class C {
static int length() => 42;
}
const v = C.length;
''');
checkElementText(library, r'''
class C {
static int length() {}
}
const () → int v =
C/*location: test.dart;C*/.
length/*location: test.dart;C;length*/;
''');
}
test_const_list_inferredType() async {
// The summary needs to contain enough information so that when the constant
// is resynthesized, the constant value can get the type that was computed
// by type inference.
var library = await checkLibrary('''
const Object x = const [1];
''');
checkElementText(library, '''
const Object x = const <
int/*location: dart:core;int*/>[1];
''');
}
test_const_map_inferredType() async {
// The summary needs to contain enough information so that when the constant
// is resynthesized, the constant value can get the type that was computed
// by type inference.
var library = await checkLibrary('''
const Object x = const {1: 1.0};
''');
checkElementText(library, '''
const Object x = const <
int/*location: dart:core;int*/,
double/*location: dart:core;double*/>{1: 1.0};
''');
}
test_const_parameterDefaultValue_initializingFormal_functionTyped() async {
var library = await checkLibrary(r'''
class C {
final x;
const C({this.x: foo});
}
int foo() => 42;
''');
checkElementText(library, r'''
class C {
final dynamic x;
const C({dynamic this.x:
foo/*location: test.dart;foo*/});
}
int foo() {}
''');
}
test_const_parameterDefaultValue_initializingFormal_named() async {
var library = await checkLibrary(r'''
class C {
final x;
const C({this.x: 1 + 2});
}
''');
checkElementText(library, r'''
class C {
final dynamic x;
const C({dynamic this.x: 1 + 2});
}
''');
}
test_const_parameterDefaultValue_initializingFormal_positional() async {
var library = await checkLibrary(r'''
class C {
final x;
const C([this.x = 1 + 2]);
}
''');
checkElementText(library, r'''
class C {
final dynamic x;
const C([dynamic this.x = 1 + 2]);
}
''');
}
test_const_parameterDefaultValue_normal() async {
var library = await checkLibrary(r'''
class C {
const C.positional([p = 1 + 2]);
const C.named({p: 1 + 2});
void methodPositional([p = 1 + 2]) {}
void methodPositionalWithoutDefault([p]) {}
void methodNamed({p: 1 + 2}) {}
void methodNamedWithoutDefault({p}) {}
}
''');
checkElementText(library, r'''
class C {
const C.positional([dynamic p = 1 + 2]);
const C.named({dynamic p: 1 + 2});
void methodPositional([dynamic p = 1 + 2]) {}
void methodPositionalWithoutDefault([dynamic p]) {}
void methodNamed({dynamic p: 1 + 2}) {}
void methodNamedWithoutDefault({dynamic p}) {}
}
''');
}
test_const_reference_staticField() async {
var library = await checkLibrary(r'''
class C {
static const int F = 42;
}
const V = C.F;
''');
checkElementText(library, r'''
class C {
static const int F = 42;
}
const int V =
C/*location: test.dart;C*/.
F/*location: test.dart;C;F?*/;
''');
}
test_const_reference_staticField_imported() async {
addLibrarySource('/a.dart', r'''
class C {
static const int F = 42;
}
''');
var library = await checkLibrary(r'''
import 'a.dart';
const V = C.F;
''');
checkElementText(library, r'''
import 'a.dart';
const int V =
C/*location: a.dart;C*/.
F/*location: a.dart;C;F?*/;
''');
}
test_const_reference_staticField_imported_withPrefix() async {
addLibrarySource('/a.dart', r'''
class C {
static const int F = 42;
}
''');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const V = p.C.F;
''');
checkElementText(library, r'''
import 'a.dart' as p;
const int V =
p/*location: test.dart;p*/.
C/*location: a.dart;C*/.
F/*location: a.dart;C;F?*/;
''');
}
test_const_reference_staticMethod() async {
var library = await checkLibrary(r'''
class C {
static int m(int a, String b) => 42;
}
const V = C.m;
''');
checkElementText(library, r'''
class C {
static int m(int a, String b) {}
}
const (int, String) → int V =
C/*location: test.dart;C*/.
m/*location: test.dart;C;m*/;
''');
}
test_const_reference_staticMethod_imported() async {
addLibrarySource('/a.dart', r'''
class C {
static int m(int a, String b) => 42;
}
''');
var library = await checkLibrary(r'''
import 'a.dart';
const V = C.m;
''');
checkElementText(library, r'''
import 'a.dart';
const (int, String) → int V =
C/*location: a.dart;C*/.
m/*location: a.dart;C;m*/;
''');
}
test_const_reference_staticMethod_imported_withPrefix() async {
addLibrarySource('/a.dart', r'''
class C {
static int m(int a, String b) => 42;
}
''');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const V = p.C.m;
''');
checkElementText(library, r'''
import 'a.dart' as p;
const (int, String) → int V =
p/*location: test.dart;p*/.
C/*location: a.dart;C*/.
m/*location: a.dart;C;m*/;
''');
}
test_const_reference_topLevelFunction() async {
var library = await checkLibrary(r'''
foo() {}
const V = foo;
''');
checkElementText(library, r'''
const () → dynamic V =
foo/*location: test.dart;foo*/;
dynamic foo() {}
''');
}
test_const_reference_topLevelFunction_generic() async {
var library = await checkLibrary(r'''
R foo<P, R>(P p) {}
const V = foo;
''');
checkElementText(library, r'''
const <P,R>(P) → R V =
foo/*location: test.dart;foo*/;
R foo<P, R>(P p) {}
''');
}
test_const_reference_topLevelFunction_imported() async {
addLibrarySource('/a.dart', r'''
foo() {}
''');
var library = await checkLibrary(r'''
import 'a.dart';
const V = foo;
''');
checkElementText(library, r'''
import 'a.dart';
const () → dynamic V =
foo/*location: a.dart;foo*/;
''');
}
test_const_reference_topLevelFunction_imported_withPrefix() async {
addLibrarySource('/a.dart', r'''
foo() {}
''');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const V = p.foo;
''');
checkElementText(library, r'''
import 'a.dart' as p;
const () → dynamic V =
p/*location: test.dart;p*/.
foo/*location: a.dart;foo*/;
''');
}
test_const_reference_topLevelVariable() async {
var library = await checkLibrary(r'''
const A = 1;
const B = A + 2;
''');
checkElementText(library, r'''
const int A = 1;
const int B =
A/*location: test.dart;A?*/ + 2;
''');
}
test_const_reference_topLevelVariable_imported() async {
addLibrarySource('/a.dart', r'''
const A = 1;
''');
var library = await checkLibrary(r'''
import 'a.dart';
const B = A + 2;
''');
checkElementText(library, r'''
import 'a.dart';
const int B =
A/*location: a.dart;A?*/ + 2;
''');
}
test_const_reference_topLevelVariable_imported_withPrefix() async {
addLibrarySource('/a.dart', r'''
const A = 1;
''');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const B = p.A + 2;
''');
checkElementText(library, r'''
import 'a.dart' as p;
const int B =
p/*location: test.dart;p*/.
A/*location: a.dart;A?*/ + 2;
''');
}
test_const_reference_type() async {
var library = await checkLibrary(r'''
class C {}
class D<T> {}
enum E {a, b, c}
typedef F(int a, String b);
const vDynamic = dynamic;
const vNull = Null;
const vObject = Object;
const vClass = C;
const vGenericClass = D;
const vEnum = E;
const vFunctionTypeAlias = F;
''');
checkElementText(library, r'''
typedef F = dynamic Function(int a, String b);
enum E {
synthetic final int index;
synthetic static const List<E> values;
static const E a;
static const E b;
static const E c;
String toString() {}
}
class C {
}
class D<T> {
}
const Type vDynamic =
dynamic/*location: dynamic*/;
const Type vNull =
Null/*location: dart:core;Null*/;
const Type vObject =
Object/*location: dart:core;Object*/;
const Type vClass =
C/*location: test.dart;C*/;
const Type vGenericClass =
D/*location: test.dart;D*/;
const Type vEnum =
E/*location: test.dart;E*/;
const Type vFunctionTypeAlias =
F/*location: test.dart;F*/;
''');
}
test_const_reference_type_functionType() async {
var library = await checkLibrary(r'''
typedef F();
class C {
final f = <F>[];
}
''');
checkElementText(library, r'''
typedef F = dynamic Function();
class C {
final List<() → dynamic> f;
}
''');
}
test_const_reference_type_imported() async {
addLibrarySource('/a.dart', r'''
class C {}
enum E {a, b, c}
typedef F(int a, String b);
''');
var library = await checkLibrary(r'''
import 'a.dart';
const vClass = C;
const vEnum = E;
const vFunctionTypeAlias = F;
''');
checkElementText(library, r'''
import 'a.dart';
const Type vClass =
C/*location: a.dart;C*/;
const Type vEnum =
E/*location: a.dart;E*/;
const Type vFunctionTypeAlias =
F/*location: a.dart;F*/;
''');
}
test_const_reference_type_imported_withPrefix() async {
addLibrarySource('/a.dart', r'''
class C {}
enum E {a, b, c}
typedef F(int a, String b);
''');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const vClass = p.C;
const vEnum = p.E;
const vFunctionTypeAlias = p.F;
''');
checkElementText(library, r'''
import 'a.dart' as p;
const Type vClass =
p/*location: test.dart;p*/.
C/*location: a.dart;C*/;
const Type vEnum =
p/*location: test.dart;p*/.
E/*location: a.dart;E*/;
const Type vFunctionTypeAlias =
p/*location: test.dart;p*/.
F/*location: a.dart;F*/;
''');
}
test_const_reference_type_typeParameter() async {
var library = await checkLibrary(r'''
class C<T> {
final f = <T>[];
}
''');
checkElementText(library, r'''
class C<T> {
final List<T> f;
}
''');
}
test_const_reference_unresolved_prefix0() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
const V = foo;
''', allowErrors: true);
checkElementText(library, r'''
const dynamic V = #invalidConst;
''');
}
test_const_reference_unresolved_prefix1() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
class C {}
const V = C.foo;
''', allowErrors: true);
checkElementText(library, r'''
class C {
}
const dynamic V = #invalidConst;
''');
}
test_const_reference_unresolved_prefix2() async {
shouldCompareLibraryElements = false;
addLibrarySource('/foo.dart', '''
class C {}
''');
var library = await checkLibrary(r'''
import 'foo.dart' as p;
const V = p.C.foo;
''', allowErrors: true);
checkElementText(library, r'''
import 'foo.dart' as p;
const dynamic V = #invalidConst;
''');
}
test_const_topLevel_binary() async {
var library = await checkLibrary(r'''
const vEqual = 1 == 2;
const vAnd = true && false;
const vOr = false || true;
const vBitXor = 1 ^ 2;
const vBitAnd = 1 & 2;
const vBitOr = 1 | 2;
const vBitShiftLeft = 1 << 2;
const vBitShiftRight = 1 >> 2;
const vAdd = 1 + 2;
const vSubtract = 1 - 2;
const vMiltiply = 1 * 2;
const vDivide = 1 / 2;
const vFloorDivide = 1 ~/ 2;
const vModulo = 1 % 2;
const vGreater = 1 > 2;
const vGreaterEqual = 1 >= 2;
const vLess = 1 < 2;
const vLessEqual = 1 <= 2;
''');
checkElementText(library, r'''
const bool vEqual = 1 == 2;
const bool vAnd = true && false;
const bool vOr = false || true;
const int vBitXor = 1 ^ 2;
const int vBitAnd = 1 & 2;
const int vBitOr = 1 | 2;
const int vBitShiftLeft = 1 << 2;
const int vBitShiftRight = 1 >> 2;
const int vAdd = 1 + 2;
const int vSubtract = 1 - 2;
const int vMiltiply = 1 * 2;
const double vDivide = 1 / 2;
const int vFloorDivide = 1 ~/ 2;
const int vModulo = 1 % 2;
const bool vGreater = 1 > 2;
const bool vGreaterEqual = 1 >= 2;
const bool vLess = 1 < 2;
const bool vLessEqual = 1 <= 2;
''');
}
test_const_topLevel_conditional() async {
var library = await checkLibrary(r'''
const vConditional = (1 == 2) ? 11 : 22;
''');
checkElementText(library, r'''
const int vConditional = 1 == 2 ? 11 : 22;
''');
}
test_const_topLevel_identical() async {
var library = await checkLibrary(r'''
const vIdentical = (1 == 2) ? 11 : 22;
''');
checkElementText(library, r'''
const int vIdentical = 1 == 2 ? 11 : 22;
''');
}
test_const_topLevel_ifNull() async {
var library = await checkLibrary(r'''
const vIfNull = 1 ?? 2.0;
''');
checkElementText(library, r'''
const num vIfNull = 1 ?? 2.0;
''');
}
test_const_topLevel_literal() async {
var library = await checkLibrary(r'''
const vNull = null;
const vBoolFalse = false;
const vBoolTrue = true;
const vInt = 1;
const vIntLong1 = 0x7FFFFFFFFFFFFFFF;
const vIntLong2 = 0xFFFFFFFFFFFFFFFF;
const vDouble = 2.3;
const vString = 'abc';
const vStringConcat = 'aaa' 'bbb';
const vStringInterpolation = 'aaa ${true} ${42} bbb';
const vSymbol = #aaa.bbb.ccc;
''');
checkElementText(library, r'''
const dynamic vNull = null;
const bool vBoolFalse = false;
const bool vBoolTrue = true;
const int vInt = 1;
const int vIntLong1 = 9223372036854775807;
const int vIntLong2 = -1;
const double vDouble = 2.3;
const String vString = 'abc';
const String vStringConcat = 'aaabbb';
const String vStringInterpolation = 'aaa ${true} ${42} bbb';
const Symbol vSymbol = #aaa.bbb.ccc;
''');
}
test_const_topLevel_parenthesis() async {
var library = await checkLibrary(r'''
const int v1 = (1 + 2) * 3;
const int v2 = -(1 + 2);
const int v3 = ('aaa' + 'bbb').length;
''');
checkElementText(library, r'''
const int v1 = (1 + 2) * 3;
const int v2 = -(1 + 2);
const int v3 = ('aaa' + 'bbb').
length/*location: dart:core;String;length?*/;
''');
}
test_const_topLevel_prefix() async {
var library = await checkLibrary(r'''
const vNotEqual = 1 != 2;
const vNot = !true;
const vNegate = -1;
const vComplement = ~1;
''');
checkElementText(library, r'''
const bool vNotEqual = 1 != 2;
const bool vNot = !true;
const int vNegate = -1;
const int vComplement = ~1;
''');
}
test_const_topLevel_super() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
const vSuper = super;
''');
checkElementText(library, r'''
const dynamic vSuper = #invalidConst;
''');
}
test_const_topLevel_this() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
const vThis = this;
''');
checkElementText(library, r'''
const dynamic vThis = #invalidConst;
''');
}
test_const_topLevel_throw() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
const c = throw 42;
''');
// This is a bug.
checkElementText(library, r'''
const dynamic c;
''');
}
test_const_topLevel_typedList() async {
var library = await checkLibrary(r'''
const vNull = const <Null>[];
const vDynamic = const <dynamic>[1, 2, 3];
const vInterfaceNoTypeParameters = const <int>[1, 2, 3];
const vInterfaceNoTypeArguments = const <List>[];
const vInterfaceWithTypeArguments = const <List<String>>[];
const vInterfaceWithTypeArguments2 = const <Map<int, List<String>>>[];
''');
checkElementText(library, r'''
const List<Null> vNull = const <
Null/*location: dart:core;Null*/>[];
const List<dynamic> vDynamic = const <
dynamic/*location: dynamic*/>[1, 2, 3];
const List<int> vInterfaceNoTypeParameters = const <
int/*location: dart:core;int*/>[1, 2, 3];
const List<List<dynamic>> vInterfaceNoTypeArguments = const <
List/*location: dart:core;List*/>[];
const List<List<String>> vInterfaceWithTypeArguments = const <
List/*location: dart:core;List*/<
String/*location: dart:core;String*/>>[];
const List<Map<int, List<String>>> vInterfaceWithTypeArguments2 = const <
Map/*location: dart:core;Map*/<
int/*location: dart:core;int*/,
List/*location: dart:core;List*/<
String/*location: dart:core;String*/>>>[];
''');
}
test_const_topLevel_typedList_imported() async {
addLibrarySource('/a.dart', 'class C {}');
var library = await checkLibrary(r'''
import 'a.dart';
const v = const <C>[];
''');
checkElementText(library, r'''
import 'a.dart';
const List<C> v = const <
C/*location: a.dart;C*/>[];
''');
}
test_const_topLevel_typedList_importedWithPrefix() async {
addLibrarySource('/a.dart', 'class C {}');
var library = await checkLibrary(r'''
import 'a.dart' as p;
const v = const <p.C>[];
''');
checkElementText(library, r'''
import 'a.dart' as p;
const List<C> v = const <
C/*location: a.dart;C*/>[];
''');
}
test_const_topLevel_typedList_typedefArgument() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary(r'''
typedef int F(String id);
const v = const <F>[];
''');
checkElementText(library, r'''
typedef F = int Function(String id);
const List<(String) → int> v = const <
null/*location: test.dart;F;-*/>[];
''');
}
test_const_topLevel_typedMap() async {
var library = await checkLibrary(r'''
const vDynamic1 = const <dynamic, int>{};
const vDynamic2 = const <int, dynamic>{};
const vInterface = const <int, String>{};
const vInterfaceWithTypeArguments = const <int, List<String>>{};
''');
checkElementText(library, r'''
const Map<dynamic, int> vDynamic1 = const <
dynamic/*location: dynamic*/,
int/*location: dart:core;int*/>{};
const Map<int, dynamic> vDynamic2 = const <
int/*location: dart:core;int*/,
dynamic/*location: dynamic*/>{};
const Map<int, String> vInterface = const <
int/*location: dart:core;int*/,
String/*location: dart:core;String*/>{};
const Map<int, List<String>> vInterfaceWithTypeArguments = const <
int/*location: dart:core;int*/,
List/*location: dart:core;List*/<
String/*location: dart:core;String*/>>{};
''');
}
test_const_topLevel_untypedList() async {
var library = await checkLibrary(r'''
const v = const [1, 2, 3];
''');
checkElementText(library, r'''
const List<int> v = const [1, 2, 3];
''');
}
test_const_topLevel_untypedMap() async {
var library = await checkLibrary(r'''
const v = const {0: 'aaa', 1: 'bbb', 2: 'ccc'};
''');
checkElementText(library, r'''
const Map<int, String> v = const {0: 'aaa', 1: 'bbb', 2: 'ccc'};
''');
}
test_constExpr_pushReference_enum_field() async {
var library = await checkLibrary('''
enum E {a, b, c}
final vValue = E.a;
final vValues = E.values;
final vIndex = E.a.index;
''');
checkElementText(library, r'''
enum E {
synthetic final int index;
synthetic static const List<E> values;
static const E a;
static const E b;
static const E c;
String toString() {}
}
final E vValue;
final List<E> vValues;
final int vIndex;
''');
}
test_constExpr_pushReference_enum_method() async {
var library = await checkLibrary('''
enum E {a}
final vToString = E.a.toString();
''');
checkElementText(library, r'''
enum E {
synthetic final int index;
synthetic static const List<E> values;
static const E a;
String toString() {}
}
final String vToString;
''');
}
test_constExpr_pushReference_field_simpleIdentifier() async {
var library = await checkLibrary('''
class C {
static const a = b;
static const b = null;
}
''');
checkElementText(library, r'''
class C {
static const dynamic a =
C/*location: test.dart;C*/.
b/*location: test.dart;C;b?*/;
static const dynamic b = null;
}
''');
}
test_constExpr_pushReference_staticMethod_simpleIdentifier() async {
var library = await checkLibrary('''
class C {
static const a = m;
static m() {}
}
''');
checkElementText(library, r'''
class C {
static const () → dynamic a =
C/*location: test.dart;C*/.
m/*location: test.dart;C;m*/;
static dynamic m() {}
}
''');
}
test_constructor_documented() async {
var library = await checkLibrary('''
class C {
/**
* Docs
*/
C();
}''');
checkElementText(library, r'''
class C {
/**
* Docs
*/
C();
}
''');
}
test_constructor_initializers_assertInvocation() async {
var library = await checkLibrary('''
class C {
const C(int x) : assert(x >= 42);
}
''');
checkElementText(library, r'''
class C {
const C(int x) : assert(
x/*location: test.dart;C;;x*/ >= 42);
}
''');
}
test_constructor_initializers_assertInvocation_message() async {
var library = await checkLibrary('''
class C {
const C(int x) : assert(x >= 42, 'foo');
}
''');
checkElementText(library, r'''
class C {
const C(int x) : assert(
x/*location: test.dart;C;;x*/ >= 42, 'foo');
}
''');
}
test_constructor_initializers_field() async {
var library = await checkLibrary('''
class C {
final x;
const C() : x = 42;
}
''');
checkElementText(library, r'''
class C {
final dynamic x;
const C() :
x/*location: test.dart;C;x*/ = 42;
}
''');
}
test_constructor_initializers_field_notConst() async {
shouldCompareLibraryElements = false;
var library = await checkLibrary('''
class C {
final x;
const C() : x = foo();
}
int foo() => 42;
''', allowErrors: true);
// It is OK to keep non-constant initializers.
checkElementText(library, r'''
class C {
final dynamic x;
const C() :
x/*location: test.dart;C;x*/ =
foo/*location: test.dart;foo*/();
}
int foo() {}
''');
}
test_constructor_initializers_field_withParameter() async {
var library = await checkLibrary('''
class C {
final x;
const C(int p) : x = 1 + p;
}
''');
checkElementText(library, r'''
class C {
final dynamic x;
const C(int p) :
x/*location: test.dart;C;x*/ = 1 +
p/*location: test.dart;C;;p*/;
}
''');
}
test_constructor_initializers_superInvocation_named() async {
var library = await checkLibrary('''
class A {
const A.aaa(int p);
}
class C extends A {
const C() : super.aaa(42);
}
''');
checkElementText(library, r'''
class A {
const A.aaa(int p);
}
class C extends A {
const C() : super.
aaa/*location: test.dart;A;aaa*/(42);
}
''');
}
test_constructor_initializers_superInvocation_named_underscore() async {
var library = await checkLibrary('''
class A {
const A._();
}
class B extends A {
const B() : super._();
}
''');
checkElementText(library, r'''
class A {
const A._();
}
class B extends A {
const B() : super.
_/*location: test.dart;A;_*/();
}
''');
}
test_constructor_initializers_superInvocation_namedExpression() async {
var library = await checkLibrary('''
class A {
const A.aaa(a, {int b});
}
class C extends A {
const C() : super.aaa(1, b: 2);
}
''');
checkElementText(library, r'''
class A {
const A.aaa(dynamic a, {int b});
}
class C extends A {