Skip to content

Commit

Permalink
Improve tests and fix error context handling (#84)
Browse files Browse the repository at this point in the history
Fixes #83 

* Improve tests and fix error context handling
* Rename test to testErrorContextFirstLine
  • Loading branch information
MaxDesiatov committed Mar 9, 2019
1 parent f8616cf commit 85a4e01
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions .swiftformat
Expand Up @@ -6,3 +6,4 @@
--operatorfunc nospace
--ifdef noindent
--stripunusedargs closure-only
--disable andOperator
3 changes: 2 additions & 1 deletion Sources/XMLCoder/Auxiliaries/XMLStackParser.swift
Expand Up @@ -48,7 +48,8 @@ class XMLStackParser: NSObject {
))
}

guard errorContextLength > 0 else {
// `lineNumber` isn't 0-indexed, so 0 is an invalid value for context
guard errorContextLength > 0 && xmlParser.lineNumber > 0 else {
throw error
}

Expand Down
23 changes: 23 additions & 0 deletions Tests/XMLCoderTests/ErrorContextTest.swift
Expand Up @@ -14,6 +14,29 @@ final class ErrorContextTest: XCTestCase {
let value: [String: Int]
}

func testErrorContextFirstLine() {
let decoder = XMLDecoder()
decoder.errorContextLength = 8

let xmlString = "<blah //>"
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 2:
`<blah `
""")
}
}

func testErrorContext() {
let decoder = XMLDecoder()
decoder.errorContextLength = 8
Expand Down

0 comments on commit 85a4e01

Please sign in to comment.