Skip to content

Commit 54e57c4

Browse files
committed
wip: fixup test case logic
1 parent 827d873 commit 54e57c4

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

test/stdlib/UTF8EncodingErrorTests.swift

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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/
22

33
// REQUIRES: executable_test
44

@@ -35,7 +35,7 @@ private struct ValidationError {
3535

3636

3737
init(
38-
_ error: UTF8.EncodingError,
38+
_ error: UTF8.EncodingError,
3939
errorStart: Bool
4040
) {
4141
self.error = error
@@ -88,7 +88,7 @@ private struct ValidationTestCase {
8888
init(
8989
_ bytes: [UInt8],
9090
_ errors: [ValidationError],
91-
file: String = #file,
91+
file: String = #file,
9292
line: UInt = #line
9393
) {
9494
self.bytes = bytes
@@ -109,25 +109,27 @@ private struct ValidationTestCase {
109109
func expect<T: Equatable>(
110110
_ lhs: T,
111111
_ rhs: T,
112-
file: String = #file,
112+
file: String = #file,
113113
line: UInt = #line
114114
) {
115115
expectEqual(
116-
lhs,
117-
rhs,
116+
lhs,
117+
rhs,
118118
stackTrace: loc.withCurrentLoc(file: file, line: line))
119119
}
120120
func fail(
121121
_ message: String,
122-
file: String = #file,
122+
file: String = #file,
123123
line: UInt = #line
124124
) {
125125
expectationFailure(
126-
message,
127-
trace: "",
126+
message,
127+
trace: "",
128128
stackTrace: loc.with(.init(file, line)))
129129
}
130130

131+
/// Test UTF8._checkAllErrors(), which matches directly against
132+
/// the provided expected-errors.
131133
func testAllErrors() {
132134
let caughtErrors = Array(UTF8._checkAllErrors(bytes))
133135
for i in 0..<Swift.min(caughtErrors.count, errors.count) {
@@ -136,8 +138,10 @@ private struct ValidationTestCase {
136138
expect(caughtErrors.count, errors.count)
137139
}
138140

141+
/// Test UTF8Span validation. Surface subsequent errors by slicing the
142+
/// input (which will convert the error-kind to .unexpectedContinuationByte)
139143
func testSpanSlicedErrors() {
140-
bytes.withSpan { span in
144+
bytes.withSpan { span in
141145
if errors.isEmpty {
142146
do throws(UTF8.EncodingError) {
143147
// No errors expected
@@ -148,22 +152,42 @@ private struct ValidationTestCase {
148152
return
149153
}
150154

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 {
157160
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
160168
} 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+
161181
let adjustedErr = UTF8.EncodingError(
162182
error.kind,
163-
error.range._offset(by: start)
183+
error.range._offset(by: currentPos)
164184
)
165185
expect(expectedError, adjustedErr)
186+
187+
currentPos = adjustedErr.range.upperBound
188+
errorIdx += 1
166189
}
190+
167191
}
168192

169193
// Rest of input should be error-free
@@ -186,10 +210,6 @@ private struct ValidationTestCase {
186210
}
187211

188212
if #available(SwiftStdlib 6.1, *) {
189-
// suite.test("UTF8EncodingError/test") {
190-
// fatalError()
191-
// }
192-
193213
suite.test("UTF8Span/encoding errors") {
194214
func test(_ t: ValidationTestCase) {
195215
t.run()
@@ -262,7 +282,7 @@ if #available(SwiftStdlib 6.1, *) {
262282
[.overlongEncodingByte(at: 0), // E0
263283
.overlongEncodingByte(at: 1, errorStart: false), // 81
264284
.overlongEncodingByte(at: 2, errorStart: false), // 80
265-
]))
285+
]))
266286
}
267287
}
268288

0 commit comments

Comments
 (0)