Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

Commit 96d4ba6

Browse files
committed
test: add DynamicDecodable unit tests
1 parent afa45fe commit 96d4ba6

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ let package = Package(
3232
name: "DynamicCodableKitTests",
3333
dependencies: ["DynamicCodableKit"],
3434
resources: [
35-
.process("JSONs"),
35+
.process("DynamicDecodingContextCodingKey/JSONs"),
3636
]
3737
),
3838
],

Tests/DynamicCodableKitTests/DynamicDecodable.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
11
import XCTest
22
@testable import DynamicCodableKit
33

4+
extension Int: DynamicDecodable {}
5+
46
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+
}
537
func testCastingToExistential() throws {
638
let textPost = TextPost(
739
id: UUID(),

0 commit comments

Comments
 (0)