| // 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 'context_collection_resolution.dart'; |
| |
| main() { |
| defineReflectiveSuite(() { |
| defineReflectiveTests(SwitchStatementPatternTest); |
| }); |
| } |
| |
| @reflectiveTest |
| class SwitchStatementPatternTest extends PatternsResolutionTest { |
| test_default() async { |
| await assertNoErrorsInCode(r''' |
| void f(Object? x) { |
| switch (x) { |
| case 0?: |
| break; |
| default: |
| break; |
| } |
| } |
| '''); |
| |
| final node = findNode.switchStatement('switch'); |
| assertResolvedNodeText(node, r''' |
| SwitchStatement |
| switchKeyword: switch |
| leftParenthesis: ( |
| expression: SimpleIdentifier |
| token: x |
| staticElement: self::@function::f::@parameter::x |
| staticType: Object? |
| rightParenthesis: ) |
| leftBracket: { |
| members |
| SwitchPatternCase |
| keyword: case |
| guardedPattern: GuardedPattern |
| pattern: PostfixPattern |
| operand: ConstantPattern |
| expression: IntegerLiteral |
| literal: 0 |
| staticType: int |
| operator: ? |
| colon: : |
| statements |
| BreakStatement |
| breakKeyword: break |
| semicolon: ; |
| SwitchDefault |
| keyword: default |
| colon: : |
| statements |
| BreakStatement |
| breakKeyword: break |
| semicolon: ; |
| rightBracket: } |
| '''); |
| } |
| |
| test_mergeCases() async { |
| await assertNoErrorsInCode(r''' |
| void f(Object? x) { |
| switch (x) { |
| case 0?: |
| case 1?: |
| break; |
| case 2?: |
| break; |
| } |
| } |
| '''); |
| |
| final node = findNode.switchStatement('switch'); |
| assertResolvedNodeText(node, r''' |
| SwitchStatement |
| switchKeyword: switch |
| leftParenthesis: ( |
| expression: SimpleIdentifier |
| token: x |
| staticElement: self::@function::f::@parameter::x |
| staticType: Object? |
| rightParenthesis: ) |
| leftBracket: { |
| members |
| SwitchPatternCase |
| keyword: case |
| guardedPattern: GuardedPattern |
| pattern: PostfixPattern |
| operand: ConstantPattern |
| expression: IntegerLiteral |
| literal: 0 |
| staticType: int |
| operator: ? |
| colon: : |
| SwitchPatternCase |
| keyword: case |
| guardedPattern: GuardedPattern |
| pattern: PostfixPattern |
| operand: ConstantPattern |
| expression: IntegerLiteral |
| literal: 1 |
| staticType: int |
| operator: ? |
| colon: : |
| statements |
| BreakStatement |
| breakKeyword: break |
| semicolon: ; |
| SwitchPatternCase |
| keyword: case |
| guardedPattern: GuardedPattern |
| pattern: PostfixPattern |
| operand: ConstantPattern |
| expression: IntegerLiteral |
| literal: 2 |
| staticType: int |
| operator: ? |
| colon: : |
| statements |
| BreakStatement |
| breakKeyword: break |
| semicolon: ; |
| rightBracket: } |
| '''); |
| } |
| |
| test_rewrite_pattern() async { |
| await assertNoErrorsInCode(r''' |
| void f(Object? x, int Function() a) { |
| switch (x) { |
| case const a(): |
| break; |
| } |
| } |
| '''); |
| |
| final node = findNode.switchStatement('switch'); |
| assertResolvedNodeText(node, r''' |
| SwitchStatement |
| switchKeyword: switch |
| leftParenthesis: ( |
| expression: SimpleIdentifier |
| token: x |
| staticElement: self::@function::f::@parameter::x |
| staticType: Object? |
| rightParenthesis: ) |
| leftBracket: { |
| members |
| SwitchPatternCase |
| keyword: case |
| guardedPattern: GuardedPattern |
| pattern: ConstantPattern |
| const: const |
| expression: FunctionExpressionInvocation |
| function: SimpleIdentifier |
| token: a |
| staticElement: self::@function::f::@parameter::a |
| staticType: int Function() |
| argumentList: ArgumentList |
| leftParenthesis: ( |
| rightParenthesis: ) |
| staticElement: <null> |
| staticInvokeType: int Function() |
| staticType: int |
| colon: : |
| statements |
| BreakStatement |
| breakKeyword: break |
| semicolon: ; |
| rightBracket: } |
| '''); |
| } |
| |
| test_rewrite_whenClause() async { |
| await assertNoErrorsInCode(r''' |
| void f(Object? x, bool Function() a) { |
| switch (x) { |
| case 0 when a(): |
| break; |
| } |
| } |
| '''); |
| |
| final node = findNode.switchStatement('switch'); |
| assertResolvedNodeText(node, r''' |
| SwitchStatement |
| switchKeyword: switch |
| leftParenthesis: ( |
| expression: SimpleIdentifier |
| token: x |
| staticElement: self::@function::f::@parameter::x |
| staticType: Object? |
| rightParenthesis: ) |
| leftBracket: { |
| members |
| SwitchPatternCase |
| keyword: case |
| guardedPattern: GuardedPattern |
| pattern: ConstantPattern |
| expression: IntegerLiteral |
| literal: 0 |
| staticType: int |
| whenClause: WhenClause |
| whenKeyword: when |
| expression: FunctionExpressionInvocation |
| function: SimpleIdentifier |
| token: a |
| staticElement: self::@function::f::@parameter::a |
| staticType: bool Function() |
| argumentList: ArgumentList |
| leftParenthesis: ( |
| rightParenthesis: ) |
| staticElement: <null> |
| staticInvokeType: bool Function() |
| staticType: bool |
| colon: : |
| statements |
| BreakStatement |
| breakKeyword: break |
| semicolon: ; |
| rightBracket: } |
| '''); |
| } |
| |
| test_whenClause() async { |
| await assertNoErrorsInCode(r''' |
| void f(Object? x) { |
| switch (x) { |
| case 0 when true: |
| break; |
| } |
| } |
| '''); |
| |
| final node = findNode.switchStatement('switch'); |
| assertResolvedNodeText(node, r''' |
| SwitchStatement |
| switchKeyword: switch |
| leftParenthesis: ( |
| expression: SimpleIdentifier |
| token: x |
| staticElement: self::@function::f::@parameter::x |
| staticType: Object? |
| rightParenthesis: ) |
| leftBracket: { |
| members |
| SwitchPatternCase |
| keyword: case |
| guardedPattern: GuardedPattern |
| pattern: ConstantPattern |
| expression: IntegerLiteral |
| literal: 0 |
| staticType: int |
| whenClause: WhenClause |
| whenKeyword: when |
| expression: BooleanLiteral |
| literal: true |
| staticType: bool |
| colon: : |
| statements |
| BreakStatement |
| breakKeyword: break |
| semicolon: ; |
| rightBracket: } |
| '''); |
| } |
| } |