|
| 1 | +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:analyzer/src/error/codes.dart'; |
| 6 | +import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 7 | + |
| 8 | +import '../dart/resolution/context_collection_resolution.dart'; |
| 9 | + |
| 10 | +main() { |
| 11 | + defineReflectiveSuite(() { |
| 12 | + defineReflectiveTests(DuplicateVariablePatternTest); |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +@reflectiveTest |
| 17 | +class DuplicateVariablePatternTest extends PatternsResolutionTest { |
| 18 | + test_ifCase() async { |
| 19 | + await assertErrorsInCode(r''' |
| 20 | +void f(int x) { |
| 21 | + if (x case var a & var a) {} |
| 22 | +} |
| 23 | +''', [ |
| 24 | + error(CompileTimeErrorCode.DUPLICATE_VARIABLE_PATTERN, 41, 1, |
| 25 | + contextMessages: [message('/home/test/lib/test.dart', 33, 1)]), |
| 26 | + ]); |
| 27 | + final node = findNode.caseClause('case').pattern; |
| 28 | + assertResolvedNodeText(node, r''' |
| 29 | +BinaryPattern |
| 30 | + leftOperand: VariablePattern |
| 31 | + keyword: var |
| 32 | + name: a |
| 33 | + declaredElement: hasImplicitType a@33 |
| 34 | + type: int |
| 35 | + operator: & |
| 36 | + rightOperand: VariablePattern |
| 37 | + keyword: var |
| 38 | + name: a |
| 39 | + declaredElement: a@33 |
| 40 | +'''); |
| 41 | + } |
| 42 | + |
| 43 | + test_switchStatement() async { |
| 44 | + await assertErrorsInCode(r''' |
| 45 | +void f(int x) { |
| 46 | + switch (x) { |
| 47 | + case var a & var a: |
| 48 | + break; |
| 49 | + } |
| 50 | +} |
| 51 | +''', [ |
| 52 | + error(CompileTimeErrorCode.DUPLICATE_VARIABLE_PATTERN, 52, 1, |
| 53 | + contextMessages: [message('/home/test/lib/test.dart', 44, 1)]), |
| 54 | + ]); |
| 55 | + final node = findNode.switchPatternCase('case').pattern; |
| 56 | + assertResolvedNodeText(node, r''' |
| 57 | +BinaryPattern |
| 58 | + leftOperand: VariablePattern |
| 59 | + keyword: var |
| 60 | + name: a |
| 61 | + declaredElement: hasImplicitType a@44 |
| 62 | + type: int |
| 63 | + operator: & |
| 64 | + rightOperand: VariablePattern |
| 65 | + keyword: var |
| 66 | + name: a |
| 67 | + declaredElement: a@44 |
| 68 | +'''); |
| 69 | + } |
| 70 | +} |
0 commit comments