blob: 102dd0909c69f1fef5159d9f2363cbde8c85749b [file]
// Copyright (c) 2020, 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 'package:test_reflective_loader/test_reflective_loader.dart';
import '../dart/resolution/context_collection_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(ConstWithNonConstTest);
});
}
@reflectiveTest
class ConstWithNonConstTest extends PubPackageResolutionTest {
test_inConstContext() async {
await resolveTestCodeWithDiagnostics(r'''
class A {
const A(x);
}
class B {
}
main() {
const A(B());
// ^^^
// [diag.constWithNonConst] The constructor being called isn't a const constructor.
}
''');
}
test_mixinApplication_constSuperConstructor() async {
await resolveTestCodeWithDiagnostics(r'''
mixin M {}
class A {
const A();
}
class B = A with M;
const b = const B();
''');
}
test_mixinApplication_constSuperConstructor_field() async {
await resolveTestCodeWithDiagnostics(r'''
mixin M {
int i = 0;
}
class A {
const A();
}
class B = A with M;
var b = const B();
// ^^^^^
// [diag.constWithNonConst] The constructor being called isn't a const constructor.
''');
}
test_mixinApplication_constSuperConstructor_getter() async {
await resolveTestCodeWithDiagnostics(r'''
mixin M {
int get i => 0;
}
class A {
const A();
}
class B = A with M;
var b = const B();
''');
}
test_mixinApplication_constSuperConstructor_setter() async {
await resolveTestCodeWithDiagnostics(r'''
mixin M {
set(int i) {}
}
class A {
const A();
}
class B = A with M;
var b = const B();
''');
}
test_nonConst_factory() async {
var result = await resolveTestCodeWithDiagnostics(r'''
class A {
factory A(int a) => throw 0;
}
void f() {
const A(0);
//^^^^^
// [diag.constWithNonConst] The constructor being called isn't a const constructor.
}
''');
var node = result.findNode.singleInstanceCreationExpression;
assertResolvedNodeText(node, r'''
InstanceCreationExpression
keyword: const
constructorName: ConstructorName
type: NamedType
name: A
element: <testLibrary>::@class::A
type: A
element: <testLibrary>::@class::A::@constructor::new
argumentList: ArgumentList
leftParenthesis: (
arguments
IntegerLiteral
literal: 0
correspondingParameter: <testLibrary>::@class::A::@constructor::new::@formalParameter::a
staticType: int
rightParenthesis: )
staticType: A
''');
}
test_nonConst_generative() async {
var result = await resolveTestCodeWithDiagnostics(r'''
class A {
A(int a);
}
void f() {
const A(0);
//^^^^^
// [diag.constWithNonConst] The constructor being called isn't a const constructor.
}
''');
var node = result.findNode.singleInstanceCreationExpression;
assertResolvedNodeText(node, r'''
InstanceCreationExpression
keyword: const
constructorName: ConstructorName
type: NamedType
name: A
element: <testLibrary>::@class::A
type: A
element: <testLibrary>::@class::A::@constructor::new
argumentList: ArgumentList
leftParenthesis: (
arguments
IntegerLiteral
literal: 0
correspondingParameter: <testLibrary>::@class::A::@constructor::new::@formalParameter::a
staticType: int
rightParenthesis: )
staticType: A
''');
}
}