This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add type identifier decoding tests
- Loading branch information
1 parent
8a61ceb
commit 3ae17b3
Showing
8 changed files
with
523 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "swift-docc-plugin", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-docc-plugin", | ||
"state" : { | ||
"revision" : "3303b164430d9a7055ba484c8ead67a52f7b74f6", | ||
"version" : "1.0.0" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// swift-tools-version: 5.6 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "DynamicCodableKit", | ||
platforms: [ | ||
.iOS(.v8), | ||
.macOS(.v10_10), | ||
.tvOS(.v9), | ||
.watchOS(.v2), | ||
.macCatalyst(.v13) | ||
], | ||
products: [ | ||
.library( | ||
name: "DynamicCodableKit", | ||
targets: ["DynamicCodableKit"] | ||
), | ||
], | ||
dependencies: [ | ||
.package( | ||
url: "https://github.com/apple/swift-docc-plugin", | ||
from: "1.0.0" | ||
), | ||
], | ||
targets: [ | ||
.target( | ||
name: "DynamicCodableKit", | ||
dependencies: [] | ||
), | ||
.testTarget( | ||
name: "DynamicCodableKitTests", | ||
dependencies: ["DynamicCodableKit"], | ||
resources: [ | ||
.process("JSONs"), | ||
] | ||
), | ||
], | ||
swiftLanguageVersions: [.v5] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import XCTest | ||
@testable import DynamicCodableKit | ||
|
||
final class DynamicDecodableTests: XCTestCase { | ||
func testCastingToExistential() throws { | ||
let textPost = TextPost( | ||
id: UUID(), | ||
type: .text, | ||
author: UUID(), | ||
likes: 78, | ||
createdAt: "2021-07-23T07:36:43Z", | ||
text: "Lorem Ipsium" | ||
) | ||
let post = try textPost.castAs(type: Post.self, codingPath: []) | ||
XCTAssertEqual(post.type, .text) | ||
} | ||
func testCastingToBoxType() throws { | ||
let textPost = TextPost( | ||
id: UUID(), | ||
type: .text, | ||
author: UUID(), | ||
likes: 78, | ||
createdAt: "2021-07-23T07:36:43Z", | ||
text: "Lorem Ipsium" | ||
) | ||
let post = try textPost.castAs(type: AnyPost<Post>.self, codingPath: []) | ||
XCTAssertEqual(post.type, .text) | ||
} | ||
func testOptionalCastingToExistential() throws { | ||
let textPost = TextPost( | ||
id: UUID(), | ||
type: .text, | ||
author: UUID(), | ||
likes: 78, | ||
createdAt: "2021-07-23T07:36:43Z", | ||
text: "Lorem Ipsium" | ||
) | ||
let post = textPost.castAs(type: Post?.self, codingPath: []) | ||
XCTAssertEqual(post?.type, .text) | ||
} | ||
func testOptionalCastingToBoxType() throws { | ||
let textPost = TextPost( | ||
id: UUID(), | ||
type: .text, | ||
author: UUID(), | ||
likes: 78, | ||
createdAt: "2021-07-23T07:36:43Z", | ||
text: "Lorem Ipsium" | ||
) | ||
let post = textPost.castAs(type: AnyPost<Post>?.self, codingPath: []) | ||
XCTAssertEqual(post?.type, .text) | ||
} | ||
func testArrayCastingToExistentialArray() throws { | ||
let textPosts = Array( | ||
repeating: TextPost( | ||
id: UUID(), | ||
type: .text, | ||
author: UUID(), | ||
likes: 78, | ||
createdAt: "2021-07-23T07:36:43Z", | ||
text: "Lorem Ipsium" | ||
), | ||
count: 10 | ||
) | ||
let posts = try textPosts.castAs(type: [Post].self, codingPath: []) | ||
posts.forEach { XCTAssertEqual($0.type, .text) } | ||
} | ||
func testArrayCastingToBoxTypeArray() throws { | ||
let textPosts = Array( | ||
repeating: TextPost( | ||
id: UUID(), | ||
type: .text, | ||
author: UUID(), | ||
likes: 78, | ||
createdAt: "2021-07-23T07:36:43Z", | ||
text: "Lorem Ipsium" | ||
), | ||
count: 10 | ||
) | ||
let posts = try textPosts.castAs(type: [AnyPost<Post>].self, codingPath: []) | ||
posts.forEach { XCTAssertEqual($0.type, .text) } | ||
} | ||
func testSetCastingToBoxTypeSet() throws { | ||
let textPosts: Set<TextPost> = [ | ||
TextPost( | ||
id: UUID(), | ||
type: .text, | ||
author: UUID(), | ||
likes: 78, | ||
createdAt: "2021-07-23T07:36:43Z", | ||
text: "Lorem Ipsium" | ||
), | ||
TextPost( | ||
id: UUID(), | ||
type: .text, | ||
author: UUID(), | ||
likes: 88, | ||
createdAt: "2021-06-23T07:36:43Z", | ||
text: "Lorem Ipsium" | ||
), | ||
TextPost( | ||
id: UUID(), | ||
type: .text, | ||
author: UUID(), | ||
likes: 887, | ||
createdAt: "2021-06-28T07:36:43Z", | ||
text: "Lorem Ipsium" | ||
) | ||
] | ||
let posts = try textPosts.castAs(type: [AnyPost<Post>].self, codingPath: []) | ||
posts.forEach { XCTAssertEqual($0.type, .text) } | ||
let postSet = try textPosts.castAs(type: Set<AnyPost<Post>>.self, codingPath: []) | ||
postSet.forEach { XCTAssertEqual($0.type, .text) } | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
Tests/DynamicCodableKitTests/DynamicDecodingCollectionWrapper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import XCTest | ||
@testable import DynamicCodableKit | ||
|
||
final class DynamicDecodingCollectionWrapperTests: XCTestCase { | ||
func testDecoding() throws { | ||
let url = Bundle.module.url(forResource: "identifier-decode", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
let postPage = try decoder.decode(ThrowingPostPage.self, from: data) | ||
XCTAssertEqual(postPage.content.count, 4) | ||
XCTAssertEqual(postPage.content.map(\.type), [.text, .picture, .audio, .video]) | ||
} | ||
|
||
func testInvalidDataDecodingWithThrowConfig() throws { | ||
let url = Bundle.module.url(forResource: "identifier-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
XCTAssertThrowsError(try decoder.decode(ThrowingPostPage.self, from: data)) | ||
} | ||
|
||
func testInvalidDataDecodingWithDefaultConfig() throws { | ||
let url = Bundle.module.url(forResource: "identifier-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
let postPage = try decoder.decode(DefaultPostPage.self, from: data) | ||
XCTAssertEqual(postPage.content.count, 0) | ||
} | ||
|
||
func testInvalidDataDecodingWithLossyConfig() throws { | ||
let url = Bundle.module.url(forResource: "identifier-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
let postPage = try decoder.decode(LossyPostPage.self, from: data) | ||
XCTAssertEqual(postPage.content.count, 4) | ||
XCTAssertEqual(postPage.content.map(\.type), [.text, .picture, .audio, .video]) | ||
} | ||
|
||
func testLossyDecodingPerformance() throws { | ||
let url = Bundle.module.url(forResource: "identifier-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
measure { | ||
let decoder = JSONDecoder() | ||
for _ in 0..<100 { | ||
_ = try? decoder.decode(LossyPostPage.self, from: data) | ||
} | ||
} | ||
} | ||
|
||
func testDecodingWithSet() throws { | ||
let url = Bundle.module.url(forResource: "identifier-decode", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
let postPage = try decoder.decode(ThrowingPostPageSet.self, from: data) | ||
XCTAssertEqual(postPage.content.count, 4) | ||
let decodedPostTypes = Set(postPage.content.map(\.type)) | ||
let expectedPostTypes: Set<PostType> = [.text, .picture, .audio, .video] | ||
XCTAssertEqual(decodedPostTypes, expectedPostTypes) | ||
} | ||
|
||
func testInvalidDataDecodingWithThrowConfigWithSet() throws { | ||
let url = Bundle.module.url(forResource: "identifier-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
XCTAssertThrowsError(try decoder.decode(ThrowingPostPageSet.self, from: data)) | ||
} | ||
|
||
func testInvalidDataDecodingWithDefaultConfigWithSet() throws { | ||
let url = Bundle.module.url(forResource: "identifier-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
let postPage = try decoder.decode(DefaultPostPageSet.self, from: data) | ||
XCTAssertEqual(postPage.content.count, 0) | ||
} | ||
|
||
func testInvalidDataDecodingWithLossyConfigWithSet() throws { | ||
let url = Bundle.module.url(forResource: "identifier-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
let postPage = try decoder.decode(LossyPostPageSet.self, from: data) | ||
XCTAssertEqual(postPage.content.count, 4) | ||
let decodedPostTypes = Set(postPage.content.map(\.type)) | ||
let expectedPostTypes: Set<PostType> = [.text, .picture, .audio, .video] | ||
XCTAssertEqual(decodedPostTypes, expectedPostTypes) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
Tests/DynamicCodableKitTests/JSONs/identifier-decode-with-invalid-data.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"content": [ | ||
{ | ||
"id": "00005678-abcd-efab-0123-456789abcdef", | ||
"type": "text", | ||
"author": "12345678-abcd-efab-0123-456789abcdef", | ||
"likes": 145, | ||
"createdAt": "2021-07-23T07:36:43Z", | ||
"text": "Lorem Ipsium" | ||
}, | ||
{ | ||
"id": "43215678-abcd-efab-0123-456789abcdef", | ||
"type": "picture", | ||
"author": "abcd5678-abcd-efab-0123-456789abcdef", | ||
"likes": 370, | ||
"createdAt": "2021-07-23T09:32:13Z", | ||
"url": "https://a.url.com/to/a/picture.png", | ||
"caption": "Lorem Ipsium" | ||
}, | ||
{ | ||
"id": "4c76f901-3c4f-482c-8663-600a73416773", | ||
"type": "invalid", | ||
"author": "026d7a8a-12b1-4193-8a0d-415bc8f80c1a", | ||
"likes": 25, | ||
"createdAt": "2021-07-23T09:33:48Z", | ||
"url": "https://a.url.com/to/a/audio.aac", | ||
"duration": 60 | ||
}, | ||
{ | ||
"id": "64475bcb-caff-48c1-bb53-8376628b350b", | ||
"type": "audio", | ||
"author": "4c17c269-1c56-45ab-8863-d8924ece1d0b", | ||
"likes": 25, | ||
"createdAt": "2021-07-23T09:33:48Z", | ||
"url": "https://a.url.com/to/a/audio.aac", | ||
"duration": 60 | ||
}, | ||
{ | ||
"type": "video", | ||
"likes": 2345, | ||
"createdAt": "2021-07-23T09:36:38Z", | ||
"url": "https://a.url.com/to/a/video.mp4", | ||
"duration": 460, | ||
"thumbnail": "https://a.url.com/to/a/thmbnail.png" | ||
}, | ||
{ | ||
"id": "98765432-abcd-efab-0123-456789abcdef", | ||
"type": "video", | ||
"author": "04355678-abcd-efab-0123-456789abcdef", | ||
"likes": 2345, | ||
"createdAt": "2021-07-23T09:36:38Z", | ||
"url": "https://a.url.com/to/a/video.mp4", | ||
"duration": 460, | ||
"thumbnail": "https://a.url.com/to/a/thmbnail.png" | ||
} | ||
], | ||
"next": "https://a.url.com/to/next/page" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"content": [ | ||
{ | ||
"id": "00005678-abcd-efab-0123-456789abcdef", | ||
"type": "text", | ||
"author": "12345678-abcd-efab-0123-456789abcdef", | ||
"likes": 145, | ||
"createdAt": "2021-07-23T07:36:43Z", | ||
"text": "Lorem Ipsium" | ||
}, | ||
{ | ||
"id": "43215678-abcd-efab-0123-456789abcdef", | ||
"type": "picture", | ||
"author": "abcd5678-abcd-efab-0123-456789abcdef", | ||
"likes": 370, | ||
"createdAt": "2021-07-23T09:32:13Z", | ||
"url": "https://a.url.com/to/a/picture.png", | ||
"caption": "Lorem Ipsium" | ||
}, | ||
{ | ||
"id": "64475bcb-caff-48c1-bb53-8376628b350b", | ||
"type": "audio", | ||
"author": "4c17c269-1c56-45ab-8863-d8924ece1d0b", | ||
"likes": 25, | ||
"createdAt": "2021-07-23T09:33:48Z", | ||
"url": "https://a.url.com/to/a/audio.aac", | ||
"duration": 60 | ||
}, | ||
{ | ||
"id": "98765432-abcd-efab-0123-456789abcdef", | ||
"type": "video", | ||
"author": "04355678-abcd-efab-0123-456789abcdef", | ||
"likes": 2345, | ||
"createdAt": "2021-07-23T09:36:38Z", | ||
"url": "https://a.url.com/to/a/video.mp4", | ||
"duration": 460, | ||
"thumbnail": "https://a.url.com/to/a/thmbnail.png" | ||
} | ||
], | ||
"next": "https://a.url.com/to/next/page" | ||
} |
Oops, something went wrong.