Skip to content

Commit 435a56b

Browse files
committed
[analyzer] Dot shorthands: Tests with abstract classes, factory constructors, const ctors, and type arguments.
Testing the code paths of `InstanceCreationExpressionResolver.resolveDotShorthand`. I wanted to make sure we covered everything with these conditions so this is a sanity check to make sure the recent changes with https://dart-review.googlesource.com/c/sdk/+/462542 still kept the same semantics. The only big difference that I can see with the change above is that `test_abstractClass_const_typeArguments` has two diagnostics instead of one now. I checked with the equivalent non-shorthand code, and we are also producing two diagnostics, so I'm not too bothered by that. This is a table of what each unit test is testing. Abstract Factory Const Type args Analyzer Test FALSE FALSE FALSE FALSE Most existing tests FALSE FALSE FALSE TRUE test_typeParameters FALSE FALSE TRUE FALSE Existing const ctor tests FALSE FALSE TRUE TRUE test_typeParameters_const FALSE TRUE FALSE FALSE test_factory FALSE TRUE FALSE TRUE test_factory_typeArguments FALSE TRUE TRUE FALSE test_factory_const FALSE TRUE TRUE TRUE test_factory_const_typeArguments TRUE FALSE FALSE FALSE test_abstractClass TRUE FALSE FALSE TRUE test_abstractClass_typeArguments TRUE FALSE TRUE FALSE test_abstractClass_const TRUE FALSE TRUE TRUE test_abstractClass_const_typeArguments TRUE TRUE FALSE FALSE test_abstractClass_factory TRUE TRUE FALSE TRUE test_abstractClass_factory_typeArguments TRUE TRUE TRUE FALSE test_abstractClass_factory_const TRUE TRUE TRUE TRUE test_abstractClass_factory_const_typeArguments Bug: #61978 Change-Id: I6be2df562711cd7e8f52dacf62a422b823afdad1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/464764 Reviewed-by: Paul Berry <[email protected]>
1 parent 13351c1 commit 435a56b

File tree

1 file changed

+199
-12
lines changed

1 file changed

+199
-12
lines changed

pkg/analyzer/test/src/dart/resolution/dot_shorthand_constructor_invocation_test.dart

Lines changed: 199 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,67 @@ main() {
1818
@reflectiveTest
1919
class DotShorthandConstructorInvocationResolutionTest
2020
extends PubPackageResolutionTest {
21-
test_abstract_instantiation() async {
21+
test_abstractClass() async {
2222
await assertErrorsInCode(
2323
r'''
24-
Function getFunction() {
25-
return .new();
24+
abstract class Foo<T> {
25+
Foo();
26+
}
27+
28+
void main() {
29+
Foo _ = .new();
2630
}
2731
''',
28-
[error(diag.instantiateAbstractClass, 34, 6)],
32+
[error(diag.instantiateAbstractClass, 60, 6)],
2933
);
3034
}
3135

32-
test_abstract_instantiation_factory() async {
36+
test_abstractClass_const() async {
37+
await assertErrorsInCode(
38+
r'''
39+
abstract class C {
40+
static C fn() => CB.named(1);
41+
}
42+
43+
class CB implements C {
44+
final int x;
45+
CB.named(this.x);
46+
}
47+
48+
void main() {
49+
C c = const .fn(1);
50+
print(c);
51+
}
52+
''',
53+
[error(diag.constWithUndefinedConstructor, 145, 2)],
54+
);
55+
}
56+
57+
test_abstractClass_const_typeArguments() async {
58+
await assertErrorsInCode(
59+
r'''
60+
abstract class C {
61+
static C fn() => CB.named(1);
62+
}
63+
64+
class CB implements C {
65+
final int x;
66+
CB.named(this.x);
67+
}
68+
69+
void main() {
70+
C c = const .fn<int>(1);
71+
print(c);
72+
}
73+
''',
74+
[
75+
error(diag.constWithUndefinedConstructor, 145, 2),
76+
error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 147, 5),
77+
],
78+
);
79+
}
80+
81+
test_abstractClass_factory() async {
3382
await assertNoErrorsInCode(r'''
3483
void main() async {
3584
var iter = [1, 2];
@@ -65,18 +114,86 @@ DotShorthandConstructorInvocation
65114
''');
66115
}
67116

68-
test_abstractClass() async {
117+
test_abstractClass_factory_const() async {
118+
await assertNoErrorsInCode(r'''
119+
abstract class Foo<T> {
120+
const factory Foo.a() = _Foo;
121+
122+
const Foo();
123+
}
124+
125+
class _Foo<T> extends Foo<T> {
126+
const _Foo();
127+
}
128+
129+
Foo<T> bar<T>() => const .a();
130+
''');
131+
132+
var node = findNode.singleDotShorthandConstructorInvocation;
133+
assertResolvedNodeText(node, r'''
134+
DotShorthandConstructorInvocation
135+
constKeyword: const
136+
period: .
137+
constructorName: SimpleIdentifier
138+
token: a
139+
element: ConstructorMember
140+
baseElement: <testLibrary>::@class::Foo::@constructor::a
141+
substitution: {T: Never}
142+
staticType: null
143+
argumentList: ArgumentList
144+
leftParenthesis: (
145+
rightParenthesis: )
146+
isDotShorthand: true
147+
staticType: Foo<Never>
148+
''');
149+
}
150+
151+
test_abstractClass_factory_const_typeArguments() async {
69152
await assertErrorsInCode(
70153
r'''
71154
abstract class Foo<T> {
155+
const factory Foo.a() = _Foo;
156+
157+
const Foo();
158+
}
159+
160+
class _Foo<T> extends Foo<T> {
161+
const _Foo();
162+
}
163+
164+
Foo<int> bar<T>() => const .a<int>();
165+
''',
166+
[error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 154, 5)],
167+
);
168+
}
169+
170+
test_abstractClass_factory_typeArguments() async {
171+
await assertErrorsInCode(
172+
r'''
173+
abstract class Foo<T> {
174+
factory Foo.a() = _Foo;
175+
72176
Foo();
73177
}
74178
75-
void main() {
76-
Foo _ = .new();
179+
class _Foo<T> extends Foo<T> {
180+
_Foo();
77181
}
182+
183+
Foo<T> bar<T>() => .a<T>();
78184
''',
79-
[error(diag.instantiateAbstractClass, 60, 6)],
185+
[error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 128, 3)],
186+
);
187+
}
188+
189+
test_abstractClass_function() async {
190+
await assertErrorsInCode(
191+
r'''
192+
Function getFunction() {
193+
return .new();
194+
}
195+
''',
196+
[error(diag.instantiateAbstractClass, 34, 6)],
80197
);
81198
}
82199

@@ -506,7 +623,7 @@ DotShorthandConstructorInvocation
506623

507624
test_factory() async {
508625
await assertNoErrorsInCode(r'''
509-
abstract class Foo<T> {
626+
class Foo<T> {
510627
factory Foo.a() = _Foo;
511628
512629
Foo();
@@ -517,13 +634,83 @@ class _Foo<T> extends Foo<T> {
517634
}
518635
519636
Foo<T> bar<T>() => .a();
637+
''');
638+
639+
var node = findNode.singleDotShorthandConstructorInvocation;
640+
assertResolvedNodeText(node, r'''
641+
DotShorthandConstructorInvocation
642+
period: .
643+
constructorName: SimpleIdentifier
644+
token: a
645+
element: ConstructorMember
646+
baseElement: <testLibrary>::@class::Foo::@constructor::a
647+
substitution: {T: T}
648+
staticType: null
649+
argumentList: ArgumentList
650+
leftParenthesis: (
651+
rightParenthesis: )
652+
isDotShorthand: true
653+
staticType: Foo<T>
520654
''');
521655
}
522656

657+
test_factory_const() async {
658+
await assertNoErrorsInCode(r'''
659+
class Foo<T> {
660+
const factory Foo.a() = _Foo;
661+
662+
const Foo();
663+
}
664+
665+
class _Foo<T> extends Foo<T> {
666+
const _Foo();
667+
}
668+
669+
Foo<T> bar<T>() => const .a();
670+
''');
671+
672+
var node = findNode.singleDotShorthandConstructorInvocation;
673+
assertResolvedNodeText(node, r'''
674+
DotShorthandConstructorInvocation
675+
constKeyword: const
676+
period: .
677+
constructorName: SimpleIdentifier
678+
token: a
679+
element: ConstructorMember
680+
baseElement: <testLibrary>::@class::Foo::@constructor::a
681+
substitution: {T: Never}
682+
staticType: null
683+
argumentList: ArgumentList
684+
leftParenthesis: (
685+
rightParenthesis: )
686+
isDotShorthand: true
687+
staticType: Foo<Never>
688+
''');
689+
}
690+
691+
test_factory_const_typeArguments() async {
692+
await assertErrorsInCode(
693+
r'''
694+
class Foo<T> {
695+
const factory Foo.a() = _Foo;
696+
697+
const Foo();
698+
}
699+
700+
class _Foo<T> extends Foo<T> {
701+
const _Foo();
702+
}
703+
704+
Foo<int> bar<T>() => const .a<int>();
705+
''',
706+
[error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 145, 5)],
707+
);
708+
}
709+
523710
test_factory_typeArguments() async {
524711
await assertErrorsInCode(
525712
r'''
526-
abstract class Foo<T> {
713+
class Foo<T> {
527714
factory Foo.a() = _Foo;
528715
529716
Foo();
@@ -535,7 +722,7 @@ class _Foo<T> extends Foo<T> {
535722
536723
Foo<T> bar<T>() => .a<T>();
537724
''',
538-
[error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 128, 3)],
725+
[error(diag.wrongNumberOfTypeArgumentsDotShorthandConstructor, 119, 3)],
539726
);
540727
}
541728

0 commit comments

Comments
 (0)