This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for dynamic JSON decoding with type identifiers
- Loading branch information
1 parent
96d4ba6
commit 9fc73b7
Showing
9 changed files
with
178 additions
and
106 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
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
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
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
47 changes: 47 additions & 0 deletions
47
Tests/DynamicCodableKitTests/DynamicDecodingContextCodingKey/DynamicDecodingWrapper.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,47 @@ | ||
import XCTest | ||
@testable import DynamicCodableKit | ||
|
||
final class DynamicDecodingWrapperTests: 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(SinglePostPage.self, from: data) | ||
XCTAssertEqual(postPage.content.type, .video) | ||
XCTAssertEqual(postPage.content.likes, 2345) | ||
} | ||
|
||
func testOptionalDecoding() 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(OptionalSinglePostPage.self, from: data) | ||
XCTAssertEqual(postPage.content?.type, .video) | ||
XCTAssertEqual(postPage.content?.likes, 2345) | ||
} | ||
|
||
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(SinglePostPage.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(OptionalSinglePostPage.self, from: data) | ||
XCTAssertNil(postPage.content) | ||
} | ||
} | ||
|
||
struct SinglePostPage: Decodable { | ||
let next: URL | ||
@DynamicDecodingWrapper<PostCodingKey> var content: Post | ||
} | ||
|
||
struct OptionalSinglePostPage: Decodable { | ||
let next: URL | ||
@OptionalDynamicDecodingWrapper<PostCodingKey> var content: Post? | ||
} |
58 changes: 58 additions & 0 deletions
58
...DynamicDecodingContextCodingKey/JSONs/identifier-collection-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" | ||
} |
41 changes: 41 additions & 0 deletions
41
...icCodableKitTests/DynamicDecodingContextCodingKey/JSONs/identifier-collection-decode.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,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" | ||
} |
66 changes: 10 additions & 56 deletions
66
...leKitTests/DynamicDecodingContextCodingKey/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 |
---|---|---|
@@ -1,58 +1,12 @@ | ||
{ | ||
"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" | ||
} | ||
], | ||
"content": { | ||
"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 | ||
}, | ||
"next": "https://a.url.com/to/next/page" | ||
} | ||
} |
48 changes: 10 additions & 38 deletions
48
Tests/DynamicCodableKitTests/DynamicDecodingContextCodingKey/JSONs/identifier-decode.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 |
---|---|---|
@@ -1,41 +1,13 @@ | ||
{ | ||
"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" | ||
} | ||
], | ||
"content": { | ||
"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" | ||
} |