@@ -122,6 +122,70 @@ final class SwiftDecodableTests: XCTestCase {
122122 }
123123}
124124
125+ final class SwiftMiscellaneousCodableTests : XCTestCase {
126+ private enum TestKey : CodingKey {
127+ case int( Int )
128+ case string( String )
129+
130+ init ? ( intValue: Int ) {
131+ self = . int( intValue)
132+ }
133+
134+ init ? ( stringValue: String ) {
135+ self = . string( stringValue)
136+ }
137+
138+ var intValue : Int ? {
139+ switch self {
140+ case . int( let x) : return x
141+ case . string: return nil
142+ }
143+ }
144+
145+ var stringValue : String {
146+ switch self {
147+ case . int( let x) : return String ( x)
148+ case . string( let s) : return s
149+ }
150+ }
151+ }
152+
153+ func testErrorWithEmptyPrefixedCodingPath( ) {
154+ let error = JSONError . missingOrInvalidType ( path: " x " , expected: . required( . number) , actual: nil )
155+ XCTAssertEqual ( error. withPrefixedCodingPath ( [ ] ) . path, " x " )
156+ }
157+
158+ func testErrorWithSingleStringPrefixedCodingPath( ) {
159+ let error = JSONError . outOfRangeInt64 ( path: " x " , value: 32760 , expected: Int8 . self)
160+ XCTAssertEqual ( error. withPrefixedCodingPath ( [ TestKey . string ( " foo " ) ] ) . path, " foo.x " )
161+ }
162+
163+ func testErrorWithMultipleStringPrefixedCodingPath( ) {
164+ let error = JSONError . outOfRangeDouble ( path: " [1] " , value: 32760 , expected: Int8 . self)
165+ XCTAssertEqual ( error. withPrefixedCodingPath ( [ TestKey . string ( " foo " ) , TestKey . string ( " bar " ) ] ) . path, " foo.bar[1] " )
166+ }
167+
168+ func testErrorWithSingleIntPrefixedCodingPath( ) {
169+ let error = JSONError . outOfRangeDecimal ( path: " x " , value: 32760 , expected: Int8 . self)
170+ XCTAssertEqual ( error. withPrefixedCodingPath ( [ TestKey . int ( 42 ) ] ) . path, " [42].x " )
171+ }
172+
173+ func testErrorWithMultipleIntPrefixedCodingPath( ) {
174+ let error = JSONError . missingOrInvalidType ( path: " x " , expected: . required( . number) , actual: nil )
175+ XCTAssertEqual ( error. withPrefixedCodingPath ( [ TestKey . int ( 2 ) , TestKey . int ( 42 ) ] ) . path, " [2][42].x " )
176+ }
177+
178+ func testErrorWithMixedPrefixedCodignPath( ) {
179+ let error = JSONError . missingOrInvalidType ( path: " x " , expected: . required( . number) , actual: nil )
180+ XCTAssertEqual ( error. withPrefixedCodingPath ( [ TestKey . string ( " foo " ) , TestKey . int ( 42 ) , TestKey . string ( " bar " ) ] ) . path, " foo[42].bar.x " )
181+ }
182+
183+ func testErrorWithNilPathPrefixedCodingPath( ) {
184+ let error = JSONError . missingOrInvalidType ( path: nil , expected: . required( . number) , actual: nil )
185+ XCTAssertEqual ( error. withPrefixedCodingPath ( [ TestKey . string ( " foo " ) , TestKey . string ( " bar " ) ] ) . path, " foo.bar " )
186+ }
187+ }
188+
125189/// Wrapper to make JSONEncoder/JSONDecoder happy about encoding values.
126190private struct ValueWrapper : Codable {
127191 let value : JSON
0 commit comments