|
1 | 1 | import XCTest |
2 | 2 | @testable import DynamicCodableKit |
3 | 3 |
|
| 4 | +extension Int: DynamicDecodable {} |
| 5 | + |
4 | 6 | final class DynamicDecodableTests: XCTestCase { |
| 7 | + func testDefaultDownCasting() throws { |
| 8 | + let value: Decodable = try 5.castAs(type: Decodable.self, codingPath: []) |
| 9 | + XCTAssertEqual(value as? Int, 5) |
| 10 | + } |
| 11 | + func testDefaultDownCastingFailure() throws { |
| 12 | + XCTAssertThrowsError(try 5.castAs(type: String.self, codingPath: [])) |
| 13 | + } |
| 14 | + func testDefaultOptionalDownCasting() throws { |
| 15 | + let value: Decodable? = 5.castAs(type: Decodable?.self, codingPath: []) |
| 16 | + XCTAssertEqual(value as? Int, 5) |
| 17 | + } |
| 18 | + func testDefaultOptionalDownCastingFailure() throws { |
| 19 | + let value: String? = 5.castAs(type: String?.self, codingPath: []) |
| 20 | + XCTAssertNil(value) |
| 21 | + } |
| 22 | + func testDefaultCollectionDownCasting() throws { |
| 23 | + let value: [Decodable] = try [5, 6, 7].castAs(type: [Decodable].self, codingPath: []) |
| 24 | + XCTAssertEqual(value as! Array<Int>, [5, 6, 7]) |
| 25 | + let set: Set<AnyHashable> = try ([5, 6, 7] as Set).castAs(type: Set<AnyHashable>.self, codingPath: []) |
| 26 | + XCTAssertEqual(set, [5, 6, 7] as Set) |
| 27 | + } |
| 28 | + func testDefaultCollectionCastingForSingleValue() throws { |
| 29 | + let value: [Decodable] = try 5.castAs(type: [Decodable].self, codingPath: []) |
| 30 | + XCTAssertEqual(value as! Array<Int>, [5]) |
| 31 | + let set: Set<AnyHashable> = try 5.castAs(type: Set<AnyHashable>.self, codingPath: []) |
| 32 | + XCTAssertEqual(set, [5] as Set) |
| 33 | + } |
| 34 | + func testDefaultCollectionDownCastingFailure() throws { |
| 35 | + XCTAssertThrowsError(try [5, 6, 7].castAs(type: [String].self, codingPath: [])) |
| 36 | + } |
5 | 37 | func testCastingToExistential() throws { |
6 | 38 | let textPost = TextPost( |
7 | 39 | id: UUID(), |
|
0 commit comments