Skip to content

Commit

Permalink
Add test to case when error context size goes outside content size (#61)
Browse files Browse the repository at this point in the history
* Add test to case when error context size goes outside content size

* Remove unnecessary file
  • Loading branch information
hodovani committed Jan 8, 2019
1 parent e04d921 commit 48d2c2c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
15 changes: 13 additions & 2 deletions Sources/XMLCoder/Auxiliaries/XMLStackParser.swift
Expand Up @@ -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..<upperBound]

Expand Down
31 changes: 31 additions & 0 deletions Tests/XMLCoderTests/ErrorContextTest.swift
Expand Up @@ -46,6 +46,37 @@ final class ErrorContextTest: XCTestCase {
}
}

func testErrorContextSizeOutsizeContent() {
let decoder = XMLDecoder()
decoder.errorContextLength = 10

let xmlString =
"""
container>
test1
</blah>
<container>
test2
</container>
"""
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),
]
Expand Down

0 comments on commit 48d2c2c

Please sign in to comment.