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(s) parent coding key
- Loading branch information
1 parent
9fc73b7
commit c222eb1
Showing
13 changed files
with
556 additions
and
15 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
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
41 changes: 41 additions & 0 deletions
41
...DynamicDecodingContextContainerCodingKey/DynamicDecodingCollectionDictionaryWrapper.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,41 @@ | ||
import XCTest | ||
@testable import DynamicCodableKit | ||
|
||
final class DynamicDecodingCollectionDictionaryWrapperTests: XCTestCase { | ||
func testDecoding() throws { | ||
let url = Bundle.module.url(forResource: "container-decode", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
let postPage = try decoder.decode(ThrowingKeyedPostPage.self, from: data) | ||
XCTAssertEqual(postPage.content.count, 4) | ||
XCTAssertEqual(Set(postPage.content.map(\.value.type)), Set([.text, .picture, .audio, .video])) | ||
postPage.content.forEach { XCTAssertEqual($1.type, $0) } | ||
} | ||
|
||
func testInvalidDataDecodingWithThrowConfig() throws { | ||
let url = Bundle.module.url(forResource: "container-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
XCTAssertThrowsError(try decoder.decode(ThrowingKeyedPostPage.self, from: data)) | ||
} | ||
|
||
func testInvalidDataDecodingWithLossyConfig() throws { | ||
let url = Bundle.module.url(forResource: "container-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
let postPage = try decoder.decode(LossyKeyedPostPage.self, from: data) | ||
XCTAssertEqual(postPage.content.count, 3) | ||
XCTAssertEqual(Set(postPage.content.map(\.value.type)), Set([.text, .picture, .video])) | ||
postPage.content.forEach { XCTAssertEqual($1.type, $0) } | ||
} | ||
} | ||
|
||
struct ThrowingKeyedPostPage: Decodable { | ||
let next: URL | ||
@StrictDynamicDecodingDictionaryWrapper<PostType> var content: [PostType: Post] | ||
} | ||
|
||
struct LossyKeyedPostPage: Decodable { | ||
let next: URL | ||
@LossyDynamicDecodingDictionaryWrapper<PostType> var content: [PostType: Post] | ||
} |
50 changes: 50 additions & 0 deletions
50
...eKitTests/DynamicDecodingContextContainerCodingKey/DynamicDecodingDictionaryWrapper.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,50 @@ | ||
import XCTest | ||
@testable import DynamicCodableKit | ||
|
||
final class DynamicDecodingDictionaryWrapperTests: XCTestCase { | ||
func testDecoding() throws { | ||
let url = Bundle.module.url(forResource: "container-collection-decode", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
let postPage = try decoder.decode(ThrowingKeyedPostPageCollection.self, from: data) | ||
XCTAssertEqual(postPage.content.count, 4) | ||
postPage.content.forEach { type, posts in | ||
XCTAssertEqual(posts.count, 3) | ||
posts.forEach { XCTAssertEqual($0.type, type) } | ||
} | ||
} | ||
|
||
func testInvalidDataDecodingWithThrowConfig() throws { | ||
let url = Bundle.module.url(forResource: "container-collection-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
XCTAssertThrowsError(try decoder.decode(ThrowingKeyedPostPageCollection.self, from: data)) | ||
} | ||
|
||
func testInvalidDataDecodingWithLossyConfig() throws { | ||
let url = Bundle.module.url(forResource: "container-collection-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
let postPage = try decoder.decode(LossyKeyedPostPageCollection.self, from: data) | ||
XCTAssertEqual(postPage.content.count, 4) | ||
postPage.content.forEach { type, posts in | ||
switch type { | ||
case .audio, .video: | ||
XCTAssertEqual(posts.count, 2) | ||
default: | ||
XCTAssertEqual(posts.count, 3) | ||
} | ||
posts.forEach { XCTAssertEqual($0.type, type) } | ||
} | ||
} | ||
} | ||
|
||
struct ThrowingKeyedPostPageCollection: Decodable { | ||
let next: URL | ||
@StrictDynamicDecodingArrayDictionaryWrapper<PostType> var content: [PostType: [Post]] | ||
} | ||
|
||
struct LossyKeyedPostPageCollection: Decodable { | ||
let next: URL | ||
@LossyDynamicDecodingArrayDictionaryWrapper<PostType> var content: [PostType: [Post]] | ||
} |
Oops, something went wrong.