1
- // RUN: %target-run-stdlib-swift(-enable-experimental-feature Span) -enable-experimental-feature Span %S/Inputs/
1
+ // RUN: %target-run-stdlib-swift(-enable-experimental-feature Span) -enable-experimental-feature Span %S/Inputs/
2
2
3
3
// REQUIRES: executable_test
4
4
@@ -35,7 +35,7 @@ private struct ValidationError {
35
35
36
36
37
37
init (
38
- _ error: UTF8 . EncodingError ,
38
+ _ error: UTF8 . EncodingError ,
39
39
errorStart: Bool
40
40
) {
41
41
self . error = error
@@ -88,7 +88,7 @@ private struct ValidationTestCase {
88
88
init (
89
89
_ bytes: [ UInt8 ] ,
90
90
_ errors: [ ValidationError ] ,
91
- file: String = #file,
91
+ file: String = #file,
92
92
line: UInt = #line
93
93
) {
94
94
self . bytes = bytes
@@ -109,25 +109,27 @@ private struct ValidationTestCase {
109
109
func expect< T: Equatable > (
110
110
_ lhs: T ,
111
111
_ rhs: T ,
112
- file: String = #file,
112
+ file: String = #file,
113
113
line: UInt = #line
114
114
) {
115
115
expectEqual (
116
- lhs,
117
- rhs,
116
+ lhs,
117
+ rhs,
118
118
stackTrace: loc. withCurrentLoc ( file: file, line: line) )
119
119
}
120
120
func fail(
121
121
_ message: String ,
122
- file: String = #file,
122
+ file: String = #file,
123
123
line: UInt = #line
124
124
) {
125
125
expectationFailure (
126
- message,
127
- trace: " " ,
126
+ message,
127
+ trace: " " ,
128
128
stackTrace: loc. with ( . init( file, line) ) )
129
129
}
130
130
131
+ /// Test UTF8._checkAllErrors(), which matches directly against
132
+ /// the provided expected-errors.
131
133
func testAllErrors( ) {
132
134
let caughtErrors = Array ( UTF8 . _checkAllErrors ( bytes) )
133
135
for i in 0 ..< Swift . min ( caughtErrors. count, errors. count) {
@@ -136,8 +138,10 @@ private struct ValidationTestCase {
136
138
expect ( caughtErrors. count, errors. count)
137
139
}
138
140
141
+ /// Test UTF8Span validation. Surface subsequent errors by slicing the
142
+ /// input (which will convert the error-kind to .unexpectedContinuationByte)
139
143
func testSpanSlicedErrors( ) {
140
- bytes. withSpan { span in
144
+ bytes. withSpan { span in
141
145
if errors. isEmpty {
142
146
do throws ( UTF8 . EncodingError) {
143
147
// No errors expected
@@ -148,22 +152,42 @@ private struct ValidationTestCase {
148
152
return
149
153
}
150
154
151
- // Check for each error.
152
- // NOTE: We currently do it by slicing, which will change the
153
- // error classification.
154
- for errorIdx in errors. indices {
155
- let expectedError = fetchError ( at: errorIdx, wasSliced: true )
156
- let start = expectedError. range. lowerBound
155
+ // Check every error, by slicing (which will change error classification
156
+ // of continuation bytes in multi-byte errors to .unexpectedContinuation)
157
+ var currentPos = 0
158
+ var errorIdx = 0
159
+ while true {
157
160
do throws ( UTF8 . EncodingError) {
158
- _ = try UTF8Span ( _validating: span. _extracting ( start... ) )
159
- fail ( " Expected a thrown UTF-8 encoding error " )
161
+ // print("extracting \(currentPos)")
162
+ _ = try UTF8Span ( _validating: span. _extracting ( currentPos... ) )
163
+
164
+ if errorIdx != errors. endIndex {
165
+ fail ( " Expected a thrown UTF-8 encoding error " )
166
+ }
167
+ break
160
168
} catch {
169
+ guard errorIdx < errors. endIndex else {
170
+ fail ( " Found unexpected subsequent error \( error) " )
171
+ break
172
+ }
173
+
174
+ let expectedError = fetchError ( at: errorIdx, wasSliced: true )
175
+ // print(currentPos)
176
+ // print(error)
177
+
178
+ // print(error.range._offset(by: currentPos))
179
+
180
+
161
181
let adjustedErr = UTF8 . EncodingError (
162
182
error. kind,
163
- error. range. _offset ( by: start )
183
+ error. range. _offset ( by: currentPos )
164
184
)
165
185
expect ( expectedError, adjustedErr)
186
+
187
+ currentPos = adjustedErr. range. upperBound
188
+ errorIdx += 1
166
189
}
190
+
167
191
}
168
192
169
193
// Rest of input should be error-free
@@ -186,10 +210,6 @@ private struct ValidationTestCase {
186
210
}
187
211
188
212
if #available( SwiftStdlib 6 . 1 , * ) {
189
- // suite.test("UTF8EncodingError/test") {
190
- // fatalError()
191
- // }
192
-
193
213
suite. test ( " UTF8Span/encoding errors " ) {
194
214
func test( _ t: ValidationTestCase ) {
195
215
t. run ( )
@@ -262,7 +282,7 @@ if #available(SwiftStdlib 6.1, *) {
262
282
[ . overlongEncodingByte( at: 0 ) , // E0
263
283
. overlongEncodingByte( at: 1 , errorStart: false ) , // 81
264
284
. overlongEncodingByte( at: 2 , errorStart: false ) , // 80
265
- ] ) )
285
+ ] ) )
266
286
}
267
287
}
268
288
0 commit comments