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

Commit

Permalink
test: add tests for dynamic JSON decoding with type identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyamahunt committed Jun 4, 2022
1 parent 96d4ba6 commit 9fc73b7
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public struct DynamicDecodingCollectionWrapper<
}

public extension KeyedDecodingContainerProtocol
where Key: DynamicDecodingContextIdentifierCodingKey {
where Key: DynamicDecodingContextCodingKey {
/// Decodes a value of dynamic ``DynamicDecodingCollectionWrapper``
/// type for the given coding key.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public struct DynamicDecodingDefaultValueWrapper<
}

public extension KeyedDecodingContainerProtocol
where Key: DynamicDecodingContextIdentifierCodingKey {
where Key: DynamicDecodingContextCodingKey {
/// Decodes a value of dynamic ``DynamicDecodingDefaultValueWrapper``
/// type for the given coding key.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public struct DynamicDecodingWrapper<
}

public extension KeyedDecodingContainerProtocol
where Key: DynamicDecodingContextIdentifierCodingKey {
where Key: DynamicDecodingContextCodingKey {
/// Decodes a value of dynamic ``DynamicDecodingWrapper``
/// type for the given coding key.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest

final class DynamicDecodingCollectionWrapperTests: XCTestCase {
func testDecoding() throws {
let url = Bundle.module.url(forResource: "identifier-decode", withExtension: "json")!
let url = Bundle.module.url(forResource: "identifier-collection-decode", withExtension: "json")!
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
let postPage = try decoder.decode(ThrowingPostPage.self, from: data)
Expand All @@ -12,22 +12,22 @@ final class DynamicDecodingCollectionWrapperTests: XCTestCase {
}

func testInvalidDataDecodingWithThrowConfig() throws {
let url = Bundle.module.url(forResource: "identifier-decode-with-invalid-data", withExtension: "json")!
let url = Bundle.module.url(forResource: "identifier-collection-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 url = Bundle.module.url(forResource: "identifier-collection-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 url = Bundle.module.url(forResource: "identifier-collection-decode-with-invalid-data", withExtension: "json")!
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
let postPage = try decoder.decode(LossyPostPage.self, from: data)
Expand All @@ -36,7 +36,7 @@ final class DynamicDecodingCollectionWrapperTests: XCTestCase {
}

// func testLossyDecodingPerformance() throws {
// let url = Bundle.module.url(forResource: "identifier-decode-with-invalid-data", withExtension: "json")!
// let url = Bundle.module.url(forResource: "identifier-collection-decode-with-invalid-data", withExtension: "json")!
// let data = try Data(contentsOf: url)
// measure {
// let decoder = JSONDecoder()
Expand All @@ -47,7 +47,7 @@ final class DynamicDecodingCollectionWrapperTests: XCTestCase {
// }

func testDecodingWithSet() throws {
let url = Bundle.module.url(forResource: "identifier-decode", withExtension: "json")!
let url = Bundle.module.url(forResource: "identifier-collection-decode", withExtension: "json")!
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
let postPage = try decoder.decode(ThrowingPostPageSet.self, from: data)
Expand All @@ -58,22 +58,22 @@ final class DynamicDecodingCollectionWrapperTests: XCTestCase {
}

func testInvalidDataDecodingWithThrowConfigWithSet() throws {
let url = Bundle.module.url(forResource: "identifier-decode-with-invalid-data", withExtension: "json")!
let url = Bundle.module.url(forResource: "identifier-collection-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 url = Bundle.module.url(forResource: "identifier-collection-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 url = Bundle.module.url(forResource: "identifier-collection-decode-with-invalid-data", withExtension: "json")!
let data = try Data(contentsOf: url)
let decoder = JSONDecoder()
let postPage = try decoder.decode(LossyPostPageSet.self, from: data)
Expand Down
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?
}
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"
}
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"
}
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"
}
}
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"
}

0 comments on commit 9fc73b7

Please sign in to comment.