-
Notifications
You must be signed in to change notification settings - Fork 104
/
ErrorContextTest.swift
152 lines (135 loc) · 4.54 KB
/
ErrorContextTest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2018-2020 XMLCoder contributors
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
//
// Created by Matvii Hodovaniuk on 12/27/18.
//
import Foundation
import XCTest
@testable import XMLCoder
final class ErrorContextTest: XCTestCase {
struct Container: Codable {
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
}
let column2 = """
\(underlying.localizedDescription) \
at line 1, column 2:
`<blah `
"""
let column7 = """
\(underlying.localizedDescription) \
at line 1, column 7:
`ah //>`
"""
let column10 = """
\(underlying.localizedDescription) \
at line 1, column 10:
`//>`
"""
#if os(Linux) && swift(<5.4)
// XML Parser returns a different column on Linux and iOS 16+
// https://bugs.swift.org/browse/SR-11192
XCTAssertEqual(ctx.debugDescription, column7)
#elseif os(Windows) || os(Linux)
XCTAssertEqual(ctx.debugDescription, column10)
#else
if #available(iOS 16.0, tvOS 16.0, macOS 13.0, *) {
XCTAssertEqual(ctx.debugDescription, column7)
} else {
XCTAssertEqual(ctx.debugDescription, column2)
}
#endif
}
}
// FIXME: not sure why this isn't passing with SwiftFoundation since Swift 5.4
#if canImport(Darwin) || swift(<5.4)
func testErrorContext() {
let decoder = XMLDecoder()
decoder.errorContextLength = 8
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
}
let line4column1 = """
\(underlying.localizedDescription) \
at line 4, column 1:
`blah>
<c`
"""
let line3column8 = """
\(underlying.localizedDescription) \
at line 3, column 8:
`blah>
<c`
"""
#if os(Linux)
// XML Parser returns a different column on Linux
// https://bugs.swift.org/browse/SR-11192
XCTAssertEqual(ctx.debugDescription, line4column1)
#else
if #available(iOS 16.0, tvOS 16.0, macOS 13.0, *) {
XCTAssertEqual(ctx.debugDescription, line4column1)
} else {
XCTAssertEqual(ctx.debugDescription, line3column8)
}
#endif
}
}
#endif
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`
""")
}
}
}