| // Copyright (c) 2022, 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'; | |
| import '../dart/resolution/node_text_expectations.dart'; | |
| main() { | |
| defineReflectiveSuite(() { | |
| defineReflectiveTests(DuplicateVariablePatternTest); | |
| defineReflectiveTests(UpdateNodeTextExpectations); | |
| }); | |
| } | |
| @reflectiveTest | |
| class DuplicateVariablePatternTest extends PubPackageResolutionTest { | |
| test_ifCase() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f(int x) { | |
| if (x case var a && var a) { | |
| // ^ | |
| // [context 1] The first definition of this name. | |
| // ^ | |
| // [diag.duplicateVariablePattern][context 1] The variable 'a' is already defined in this pattern. | |
| a; | |
| } | |
| } | |
| '''); | |
| var node = result.findNode.singleIfStatement; | |
| assertResolvedNodeText(node, r''' | |
| IfStatement | |
| ifKeyword: if | |
| leftParenthesis: ( | |
| expression2: UnqualifiedNameExpression | |
| name: x | |
| resolution: VariableReadResolution | |
| element: <testLibrary>::@function::f::@formalParameter::x | |
| type: int | |
| staticType: int | |
| expression(v1): SimpleIdentifier | |
| token: x | |
| element: <testLibrary>::@function::f::@formalParameter::x | |
| staticType: int | |
| caseClause: CaseClause | |
| caseKeyword: case | |
| guardedPattern: GuardedPattern | |
| pattern: LogicalAndPattern | |
| leftOperand: DeclaredVariablePattern | |
| keyword: var | |
| name: a | |
| declaredFragment: isPublic a@33 | |
| element: hasImplicitType isPublic | |
| type: int | |
| matchedValueType: int | |
| operator: && | |
| rightOperand: DeclaredVariablePattern | |
| keyword: var | |
| name: a | |
| declaredFragment: isPublic a@42 | |
| element: hasImplicitType isPublic | |
| type: int | |
| matchedValueType: int | |
| matchedValueType: int | |
| rightParenthesis: ) | |
| thenStatement: Block | |
| leftBracket: { | |
| statements | |
| ExpressionStatement | |
| expression2: UnqualifiedNameExpression | |
| name: a | |
| resolution: VariableReadResolution | |
| element: a@33 | |
| type: int | |
| staticType: int | |
| expression(v1): SimpleIdentifier | |
| token: a | |
| element: a@33 | |
| staticType: int | |
| semicolon: ; | |
| rightBracket: } | |
| '''); | |
| } | |
| test_switchStatement() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f(int x) { | |
| switch (x) { | |
| case var a && var a: | |
| // ^ | |
| // [context 1] The first definition of this name. | |
| // ^ | |
| // [diag.duplicateVariablePattern][context 1] The variable 'a' is already defined in this pattern. | |
| a; | |
| } | |
| } | |
| '''); | |
| var node = result.findNode.singleSwitchPatternCase; | |
| assertResolvedNodeText(node, r''' | |
| SwitchPatternCase | |
| keyword: case | |
| guardedPattern: GuardedPattern | |
| pattern: LogicalAndPattern | |
| leftOperand: DeclaredVariablePattern | |
| keyword: var | |
| name: a | |
| declaredFragment: isPublic a@44 | |
| element: hasImplicitType isPublic | |
| type: int | |
| matchedValueType: int | |
| operator: && | |
| rightOperand: DeclaredVariablePattern | |
| keyword: var | |
| name: a | |
| declaredFragment: isPublic a@53 | |
| element: hasImplicitType isPublic | |
| type: int | |
| matchedValueType: int | |
| matchedValueType: int | |
| colon: : | |
| statements | |
| ExpressionStatement | |
| expression2: UnqualifiedNameExpression | |
| name: a | |
| resolution: VariableReadResolution | |
| element: a@44 | |
| type: int | |
| staticType: int | |
| expression(v1): SimpleIdentifier | |
| token: a | |
| element: a@44 | |
| staticType: int | |
| semicolon: ; | |
| '''); | |
| } | |
| test_variableDeclaration() async { | |
| var result = await resolveTestCodeWithDiagnostics(r''' | |
| void f() { | |
| var [a, a] = [0, 1]; | |
| // ^ | |
| // [context 1] The first definition of this name. | |
| // ^ | |
| // [diag.duplicateVariablePattern][context 1] The variable 'a' is already defined in this pattern. | |
| a; | |
| } | |
| '''); | |
| var node = result.findNode.singleBlock; | |
| assertResolvedNodeText(node, r''' | |
| Block | |
| leftBracket: { | |
| statements | |
| PatternVariableDeclarationStatement | |
| declaration: PatternVariableDeclaration | |
| keyword: var | |
| pattern: ListPattern | |
| leftBracket: [ | |
| elements | |
| DeclaredVariablePattern | |
| name: a | |
| declaredFragment: isPublic a@18 | |
| element: hasImplicitType isPublic | |
| type: int | |
| matchedValueType: int | |
| DeclaredVariablePattern | |
| name: a | |
| declaredFragment: isPublic a@21 | |
| element: hasImplicitType isPublic | |
| type: int | |
| matchedValueType: int | |
| rightBracket: ] | |
| matchedValueType: List<int> | |
| requiredType: List<int> | |
| equals: = | |
| expression2: ListLiteral | |
| leftBracket: [ | |
| elements2 | |
| IntegerLiteral | |
| literal: 0 | |
| staticType: int | |
| IntegerLiteral | |
| literal: 1 | |
| staticType: int | |
| rightBracket: ] | |
| staticType: List<int> | |
| patternTypeSchema: List<_> | |
| semicolon: ; | |
| ExpressionStatement | |
| expression2: UnqualifiedNameExpression | |
| name: a | |
| resolution: VariableReadResolution | |
| element: a@18 | |
| type: int | |
| staticType: int | |
| expression(v1): SimpleIdentifier | |
| token: a | |
| element: a@18 | |
| staticType: int | |
| semicolon: ; | |
| rightBracket: } | |
| '''); | |
| } | |
| } |