|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +@_spi(RawSyntax) import SwiftSyntax |
| 14 | +@_spi(RawSyntax) import SwiftParser |
| 15 | +import XCTest |
| 16 | + |
| 17 | +final class PatternTests: XCTestCase { |
| 18 | + private var genericArgEnumPattern: Syntax { |
| 19 | + // let E<Int>.e(y) |
| 20 | + Syntax( |
| 21 | + ValueBindingPatternSyntax( |
| 22 | + bindingKeyword: .keyword(.let), |
| 23 | + valuePattern: ExpressionPatternSyntax( |
| 24 | + expression: FunctionCallExprSyntax( |
| 25 | + calledExpression: MemberAccessExprSyntax( |
| 26 | + base: SpecializeExprSyntax( |
| 27 | + expression: IdentifierExprSyntax(identifier: .identifier("E")), |
| 28 | + genericArgumentClause: GenericArgumentClauseSyntax( |
| 29 | + arguments: .init([ |
| 30 | + .init(argumentType: SimpleTypeIdentifierSyntax(name: .identifier("Int"))) |
| 31 | + ]) |
| 32 | + ) |
| 33 | + ), |
| 34 | + name: .identifier("e") |
| 35 | + ), |
| 36 | + leftParen: .leftParenToken(), |
| 37 | + argumentList: TupleExprElementListSyntax([ |
| 38 | + .init( |
| 39 | + expression: UnresolvedPatternExprSyntax( |
| 40 | + pattern: IdentifierPatternSyntax(identifier: .identifier("y")) |
| 41 | + ) |
| 42 | + ) |
| 43 | + ]), |
| 44 | + rightParen: .rightParenToken() |
| 45 | + ) |
| 46 | + ) |
| 47 | + ) |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + func testNonBinding1() { |
| 52 | + assertParse( |
| 53 | + """ |
| 54 | + if case let E<Int>.e(y) = x {} |
| 55 | + """, |
| 56 | + substructure: genericArgEnumPattern |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + func testNonBinding2() { |
| 61 | + assertParse( |
| 62 | + """ |
| 63 | + switch e { |
| 64 | + case let E<Int>.e(y): |
| 65 | + y |
| 66 | + } |
| 67 | + """, |
| 68 | + substructure: genericArgEnumPattern |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + private var tupleWithSubscriptAndBindingPattern: Syntax { |
| 73 | + // let (y[0], z) |
| 74 | + Syntax( |
| 75 | + ValueBindingPatternSyntax( |
| 76 | + bindingKeyword: .keyword(.let), |
| 77 | + valuePattern: ExpressionPatternSyntax( |
| 78 | + expression: TupleExprSyntax( |
| 79 | + elements: .init([ |
| 80 | + .init( |
| 81 | + expression: SubscriptExprSyntax( |
| 82 | + calledExpression: IdentifierExprSyntax(identifier: .identifier("y")), |
| 83 | + leftBracket: .leftSquareToken(), |
| 84 | + argumentList: TupleExprElementListSyntax([ |
| 85 | + .init(expression: IntegerLiteralExprSyntax(digits: .integerLiteral("0"))) |
| 86 | + ]), |
| 87 | + rightBracket: .rightSquareToken() |
| 88 | + ), |
| 89 | + trailingComma: .commaToken() |
| 90 | + ), |
| 91 | + .init( |
| 92 | + expression: UnresolvedPatternExprSyntax( |
| 93 | + pattern: IdentifierPatternSyntax(identifier: .identifier("z")) |
| 94 | + ) |
| 95 | + ), |
| 96 | + ]) |
| 97 | + ) |
| 98 | + ) |
| 99 | + ) |
| 100 | + ) |
| 101 | + } |
| 102 | + |
| 103 | + func testNonBinding3() { |
| 104 | + assertParse( |
| 105 | + """ |
| 106 | + if case let (y[0], z) = x {} |
| 107 | + """, |
| 108 | + substructure: tupleWithSubscriptAndBindingPattern |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + func testNonBinding4() { |
| 113 | + assertParse( |
| 114 | + """ |
| 115 | + switch x { |
| 116 | + case let (y[0], z): |
| 117 | + z |
| 118 | + } |
| 119 | + """, |
| 120 | + substructure: tupleWithSubscriptAndBindingPattern |
| 121 | + ) |
| 122 | + } |
| 123 | + |
| 124 | + private var subscriptWithBindingPattern: Syntax { |
| 125 | + // let y[z] |
| 126 | + Syntax( |
| 127 | + ValueBindingPatternSyntax( |
| 128 | + bindingKeyword: .keyword(.let), |
| 129 | + valuePattern: ExpressionPatternSyntax( |
| 130 | + expression: SubscriptExprSyntax( |
| 131 | + calledExpression: IdentifierExprSyntax(identifier: .identifier("y")), |
| 132 | + leftBracket: .leftSquareToken(), |
| 133 | + argumentList: TupleExprElementListSyntax([ |
| 134 | + .init( |
| 135 | + expression: UnresolvedPatternExprSyntax( |
| 136 | + pattern: IdentifierPatternSyntax(identifier: .identifier("z")) |
| 137 | + ) |
| 138 | + ) |
| 139 | + ]), |
| 140 | + rightBracket: .rightSquareToken() |
| 141 | + ) |
| 142 | + ) |
| 143 | + ) |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + func testNonBinding5() { |
| 148 | + assertParse( |
| 149 | + """ |
| 150 | + if case let y[z] = x {} |
| 151 | + """, |
| 152 | + substructure: subscriptWithBindingPattern |
| 153 | + ) |
| 154 | + } |
| 155 | + |
| 156 | + func testNonBinding6() { |
| 157 | + assertParse( |
| 158 | + """ |
| 159 | + switch 0 { |
| 160 | + case let y[z]: |
| 161 | + z |
| 162 | + case y[z]: |
| 163 | + 0 |
| 164 | + default: |
| 165 | + 0 |
| 166 | + } |
| 167 | + """, |
| 168 | + substructure: subscriptWithBindingPattern |
| 169 | + ) |
| 170 | + } |
| 171 | +} |
0 commit comments