@@ -4,11 +4,11 @@ import XCTest
4
4
import _SwiftSyntaxTestSupport
5
5
6
6
public class RecoveryTests : XCTestCase {
7
- func testRecoverOneExtraLabel( ) throws {
8
- try XCTAssertHasSubstructure (
9
- " (first second third: Int) " ,
10
- parse : { withParser ( source : $0 ) { Syntax ( raw : $0 . parseFunctionSignature ( ) . raw ) } } ,
11
- FunctionParameterSyntax (
7
+ func testRecoverOneExtraLabel( ) {
8
+ AssertParse (
9
+ " (first second #^DIAG^# third: Int) " ,
10
+ { $0 . parseFunctionSignature ( ) } ,
11
+ substructure : Syntax ( FunctionParameterSyntax (
12
12
attributes: nil ,
13
13
firstName: TokenSyntax . identifier ( " first " ) ,
14
14
secondName: TokenSyntax . identifier ( " second " ) ,
@@ -18,15 +18,18 @@ public class RecoveryTests: XCTestCase {
18
18
ellipsis: nil ,
19
19
defaultArgument: nil ,
20
20
trailingComma: nil
21
- )
21
+ ) ) ,
22
+ diagnostics: [
23
+ DiagnosticSpec ( message: " Unexpected text 'third' found in function parameter " )
24
+ ]
22
25
)
23
26
}
24
27
25
- func testRecoverTwoExtraLabels( ) throws {
26
- try XCTAssertHasSubstructure (
27
- " (first second third fourth: Int) " ,
28
- parse : { withParser ( source : $0 ) { Syntax ( raw : $0 . parseFunctionSignature ( ) . raw ) } } ,
29
- FunctionParameterSyntax (
28
+ func testRecoverTwoExtraLabels( ) {
29
+ AssertParse (
30
+ " (first second #^DIAG^# third fourth: Int) " ,
31
+ { $0 . parseFunctionSignature ( ) } ,
32
+ substructure : Syntax ( FunctionParameterSyntax (
30
33
attributes: nil ,
31
34
firstName: TokenSyntax . identifier ( " first " ) ,
32
35
secondName: TokenSyntax . identifier ( " second " ) ,
@@ -36,30 +39,42 @@ public class RecoveryTests: XCTestCase {
36
39
ellipsis: nil ,
37
40
defaultArgument: nil ,
38
41
trailingComma: nil
39
- )
42
+ ) ) ,
43
+ diagnostics: [
44
+ DiagnosticSpec ( message: " Unexpected text 'third fourth' found in function parameter " )
45
+ ]
40
46
)
41
47
}
42
48
43
49
func testDontRecoverFromDeclKeyword( ) {
44
- var source = """
45
- (first second third struct: Int)
46
- """
47
- let ( _, currentToken) : ( RawFunctionSignatureSyntax , Lexer . Lexeme ) =
48
- source. withUTF8 { buffer in
49
- var parser = Parser ( buffer)
50
- return ( parser. parseFunctionSignature ( ) , parser. currentToken)
51
- }
52
-
53
- // The 'struct' keyword should be taken as an indicator that a new decl
54
- // starts here, so `parseFunctionSignature` shouldn't eat it.
55
- XCTAssertEqual ( currentToken. tokenKind, . structKeyword)
50
+ AssertParse (
51
+ " func foo(first second #^MISSING_COLON^#third #^MISSING_RPAREN^#struct#^MISSING_IDENTIFIER^##^BRACES^#: Int) {} " ,
52
+ substructure: Syntax ( FunctionParameterSyntax (
53
+ attributes: nil ,
54
+ firstName: . identifier( " first " ) ,
55
+ secondName: . identifier( " second " ) ,
56
+ colon: . colonToken( presence: . missing) ,
57
+ type: TypeSyntax ( SimpleTypeIdentifierSyntax ( name: . identifier( " third " ) , genericArgumentClause: nil ) ) ,
58
+ ellipsis: nil ,
59
+ defaultArgument: nil ,
60
+ trailingComma: nil
61
+ ) ) ,
62
+ diagnostics: [
63
+ DiagnosticSpec ( locationMarker: " MISSING_COLON " , message: " Expected ':' in function parameter " ) ,
64
+ DiagnosticSpec ( locationMarker: " MISSING_RPAREN " , message: " Expected ')' to end parameter clause " ) ,
65
+ // FIXME: We should issues something like "Expected identifier in declaration"
66
+ DiagnosticSpec ( locationMarker: " MISSING_IDENTIFIER " , message: " Expected '' in declaration " ) ,
67
+ DiagnosticSpec ( locationMarker: " BRACES " , message: " Expected '{' " ) ,
68
+ DiagnosticSpec ( locationMarker: " BRACES " , message: " Expected '}' " ) ,
69
+ ]
70
+ )
56
71
}
57
72
58
- func testRecoverFromParens( ) throws {
59
- try XCTAssertHasSubstructure (
60
- " (first second [third fourth]: Int) " ,
61
- parse : { withParser ( source : $0 ) { Syntax ( raw : $0 . parseFunctionSignature ( ) . raw ) } } ,
62
- FunctionParameterSyntax (
73
+ func testRecoverFromParens( ) {
74
+ AssertParse (
75
+ " (first second #^DIAG^# [third fourth]: Int) " ,
76
+ { $0 . parseFunctionSignature ( ) } ,
77
+ substructure : Syntax ( FunctionParameterSyntax (
63
78
attributes: nil ,
64
79
firstName: TokenSyntax . identifier ( " first " ) ,
65
80
secondName: TokenSyntax . identifier ( " second " ) ,
@@ -74,68 +89,71 @@ public class RecoveryTests: XCTestCase {
74
89
ellipsis: nil ,
75
90
defaultArgument: nil ,
76
91
trailingComma: nil
77
- )
92
+ ) ) ,
93
+ diagnostics: [
94
+ DiagnosticSpec ( message: " Unexpected text '[third fourth]' found in function parameter " )
95
+ ]
78
96
)
79
97
}
80
98
81
- func testDontRecoverFromUnbalancedParens( ) throws {
82
- let source = """
83
- (first second [third fourth: Int)
84
- """
85
- try withParser ( source: source) { parser in
86
- let signature = Syntax ( raw: parser. parseFunctionSignature ( ) . raw)
87
- let currentToken = parser. currentToken
88
- XCTAssertEqual ( currentToken. tokenKind, . identifier)
89
- XCTAssertEqual ( currentToken. tokenText, " fourth " )
90
- try XCTAssertHasSubstructure (
91
- signature,
92
- FunctionParameterSyntax (
93
- attributes: nil ,
94
- firstName: TokenSyntax . identifier ( " first " ) ,
95
- secondName: TokenSyntax . identifier ( " second " ) ,
96
- colon: TokenSyntax ( . colon, presence: . missing) ,
97
- type: TypeSyntax ( ArrayTypeSyntax (
98
- leftSquareBracket: TokenSyntax . leftSquareBracketToken ( ) ,
99
- elementType: TypeSyntax ( SimpleTypeIdentifierSyntax ( name: TokenSyntax . identifier ( " third " ) , genericArgumentClause: nil ) ) ,
100
- rightSquareBracket: TokenSyntax ( . rightSquareBracket, presence: . missing)
101
- ) ) ,
102
- ellipsis: nil ,
103
- defaultArgument: nil ,
104
- trailingComma: nil
105
- )
106
- )
107
- }
99
+ func testDontRecoverFromUnbalancedParens( ) {
100
+ AssertParse (
101
+ " func foo(first second #^COLON^#[third #^RSQUARE_COLON^#fourth: Int) {} " ,
102
+ substructure: Syntax ( FunctionParameterSyntax (
103
+ attributes: nil ,
104
+ firstName: TokenSyntax . identifier ( " first " ) ,
105
+ secondName: TokenSyntax . identifier ( " second " ) ,
106
+ colon: TokenSyntax ( . colon, presence: . missing) ,
107
+ type: TypeSyntax ( ArrayTypeSyntax (
108
+ leftSquareBracket: TokenSyntax . leftSquareBracketToken ( ) ,
109
+ elementType: TypeSyntax ( SimpleTypeIdentifierSyntax ( name: TokenSyntax . identifier ( " third " ) , genericArgumentClause: nil ) ) ,
110
+ rightSquareBracket: TokenSyntax ( . rightSquareBracket, presence: . missing)
111
+ ) ) ,
112
+ ellipsis: nil ,
113
+ defaultArgument: nil ,
114
+ trailingComma: nil
115
+ ) ) ,
116
+ diagnostics: [
117
+ DiagnosticSpec ( locationMarker: " COLON " , message: " Expected ':' in function parameter " ) ,
118
+ DiagnosticSpec ( locationMarker: " RSQUARE_COLON " , message: " Expected ']' to end type " ) ,
119
+ DiagnosticSpec ( locationMarker: " RSQUARE_COLON " , message: " Expected ')' to end parameter clause " ) ,
120
+ ]
121
+ )
108
122
}
109
123
110
124
func testDontRecoverIfNewlineIsBeforeColon( ) {
111
- var source = """
112
- (first second third
113
- : Int)
114
- """
115
- let ( _, currentToken) : ( RawFunctionSignatureSyntax , Lexer . Lexeme ) =
116
- source. withUTF8 { buffer in
117
- var parser = Parser ( buffer)
118
- return ( parser. parseFunctionSignature ( ) , parser. currentToken)
119
- }
120
-
121
- XCTAssertEqual ( currentToken. tokenKind, . colon)
125
+ AssertParse (
126
+ """
127
+ func foo(first second #^COLON^#third#^RPAREN^#
128
+ : Int) {}
129
+ """ ,
130
+ substructure: Syntax ( FunctionParameterSyntax (
131
+ attributes: nil ,
132
+ firstName: TokenSyntax . identifier ( " first " ) ,
133
+ secondName: TokenSyntax . identifier ( " second " ) ,
134
+ colon: TokenSyntax ( . colon, presence: . missing) ,
135
+ type: TypeSyntax ( SimpleTypeIdentifierSyntax ( name: TokenSyntax . identifier ( " third " ) , genericArgumentClause: nil ) ) ,
136
+ ellipsis: nil ,
137
+ defaultArgument: nil ,
138
+ trailingComma: nil
139
+ ) ) ,
140
+ diagnostics: [
141
+ DiagnosticSpec ( locationMarker: " COLON " , message: " Expected ':' in function parameter " ) ,
142
+ DiagnosticSpec ( locationMarker: " RPAREN " , message: " Expected ')' to end parameter clause " ) ,
143
+ ]
144
+ )
122
145
}
123
146
124
- public func testNoParamsForFunction( ) throws {
125
- let source = """
126
- class MyClass {
127
- func withoutParameters
128
-
129
- func withParameters() {}
130
- }
131
- """
147
+ public func testNoParamsForFunction( ) {
148
+ AssertParse (
149
+ """
150
+ class MyClass {
151
+ func withoutParameters#^DIAG^#
132
152
133
- let classDecl = withParser ( source: source) {
134
- Syntax ( raw: $0. parseDeclaration ( ) . raw)
135
- }
136
- try XCTAssertHasSubstructure (
137
- classDecl,
138
- FunctionDeclSyntax (
153
+ func withParameters() {}
154
+ }
155
+ """ ,
156
+ substructure: Syntax ( FunctionDeclSyntax (
139
157
attributes: nil ,
140
158
modifiers: nil ,
141
159
funcKeyword: . funcKeyword( ) ,
@@ -153,7 +171,10 @@ public class RecoveryTests: XCTestCase {
153
171
) ,
154
172
genericWhereClause: nil ,
155
173
body: nil
156
- )
174
+ ) ) ,
175
+ diagnostics: [
176
+ DiagnosticSpec ( message: " Expected argument list in function declaration " )
177
+ ]
157
178
)
158
179
}
159
180
}
0 commit comments