diff --git a/Sources/XMLCoder/Auxiliaries/XMLStackParser.swift b/Sources/XMLCoder/Auxiliaries/XMLStackParser.swift index 4ccd7ba2..b520bd69 100644 --- a/Sources/XMLCoder/Auxiliaries/XMLStackParser.swift +++ b/Sources/XMLCoder/Auxiliaries/XMLStackParser.swift @@ -51,8 +51,19 @@ class _XMLStackParser: NSObject { errorPosition += lines[i].count } errorPosition += xmlParser.columnNumber - let lowerBound = String.Index(encodedOffset: errorPosition - offset) - let upperBound = String.Index(encodedOffset: errorPosition + offset) + + var lowerBoundIndex = 0 + if errorPosition - offset > 0 { + lowerBoundIndex = errorPosition - offset + } + + var upperBoundIndex = string.count + if errorPosition + offset < string.count { + upperBoundIndex = errorPosition + offset + } + + let lowerBound = String.Index(encodedOffset: lowerBoundIndex) + let upperBound = String.Index(encodedOffset: upperBoundIndex) let context = string[lowerBound.. + test1 + + + test2 + + """ + let xmlData = xmlString.data(using: .utf8)! + + XCTAssertThrowsError(try decoder.decode(Container.self, + from: xmlData)) { error in + guard case let DecodingError.dataCorrupted(ctx) = error, + let underlying = ctx.underlyingError else { + XCTAssert(false, "wrong error type thrown") + return + } + + XCTAssertEqual(ctx.debugDescription, """ + \(underlying.localizedDescription) \ + at line 1, column 1: + `contai` + """) + } + } + static var allTests = [ ("testErrorContext", testErrorContext), ]