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

Add test to case when error context size goes outside content size #61

Merged
merged 2 commits into from Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
8 changes: 8 additions & 0 deletions Tests/XMLCoderTests/ErrorContextTests.swift
@@ -0,0 +1,8 @@
//
// ErrorContextTests.swift
// XMLCoder
//
// Created by Matvii Hodovaniuk on 1/7/19.
//

import Foundation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this mostly empty file committed by mistake?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MaxDesiatov yes, I can't see this file through Xcode. It was committed by mistake.