Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tests and fix error context handling #84

Merged
merged 2 commits into from Mar 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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