Extension types. Tests for using in object pattern.

Change-Id: I3a6bd166cde3dfd6ea664310fdb524b379448e87
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/317842
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2023-08-03 02:44:59 +00:00 committed by Commit Queue
parent b035af2fdf
commit 7323c8af51

View file

@ -743,6 +743,119 @@ ObjectPattern
''');
}
test_extensionType_notGeneric_hasName_constant() async {
await assertNoErrorsInCode(r'''
extension type A(int it) {
int get foo => 0;
}
void f(x) {
switch (x) {
case A(foo: 0):
break;
}
}
''');
final node = findNode.singleGuardedPattern.pattern;
assertResolvedNodeText(node, r'''
ObjectPattern
type: NamedType
name: A
element: self::@extensionType::A
type: A
leftParenthesis: (
fields
PatternField
name: PatternFieldName
name: foo
colon: :
pattern: ConstantPattern
expression: IntegerLiteral
literal: 0
staticType: int
matchedValueType: int
element: self::@extensionType::A::@getter::foo
rightParenthesis: )
matchedValueType: dynamic
''');
}
test_extensionType_notGeneric_noName_variable() async {
await assertErrorsInCode(r'''
extension type A(int it) {
int get foo => 0;
}
void f(x) {
switch (x) {
case A(: final foo):
break;
}
}
''', [
error(WarningCode.UNUSED_LOCAL_VARIABLE, 96, 3),
]);
final node = findNode.singleGuardedPattern.pattern;
assertResolvedNodeText(node, r'''
ObjectPattern
type: NamedType
name: A
element: self::@extensionType::A
type: A
leftParenthesis: (
fields
PatternField
name: PatternFieldName
colon: :
pattern: DeclaredVariablePattern
keyword: final
name: foo
declaredElement: hasImplicitType isFinal foo@96
type: int
matchedValueType: int
element: self::@extensionType::A::@getter::foo
rightParenthesis: )
matchedValueType: dynamic
''');
}
test_extensionType_notGeneric_unresolved_hasName() async {
await assertErrorsInCode(r'''
extension type A(int it) {}
void f(x) {
switch (x) {
case A(foo: 0):
break;
}
}
''', [
error(CompileTimeErrorCode.UNDEFINED_GETTER, 67, 3),
]);
final node = findNode.singleGuardedPattern.pattern;
assertResolvedNodeText(node, r'''
ObjectPattern
type: NamedType
name: A
element: self::@extensionType::A
type: A
leftParenthesis: (
fields
PatternField
name: PatternFieldName
name: foo
colon: :
pattern: ConstantPattern
expression: IntegerLiteral
literal: 0
staticType: int
matchedValueType: dynamic
element: <null>
rightParenthesis: )
matchedValueType: dynamic
''');
}
test_typeAlias_nullable() async {
await assertErrorsInCode(r'''
typedef A = int?;