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 user context provider
- Loading branch information
1 parent
79935b4
commit 74fd0b4
Showing
13 changed files
with
235 additions
and
112 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
42 changes: 42 additions & 0 deletions
42
Tests/DynamicCodableKitTests/DynamicDecodingContextCodingKey/Models/PostPage.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,42 @@ | ||
import XCTest | ||
@testable import DynamicCodableKit | ||
|
||
struct SinglePostPage: Decodable { | ||
let next: URL | ||
@DynamicDecodingWrapper<PostCodingKey> var content: Post | ||
} | ||
|
||
struct OptionalSinglePostPage: Decodable { | ||
let next: URL | ||
@OptionalDynamicDecodingWrapper<PostCodingKey> var content: Post? | ||
} | ||
|
||
struct ThrowingPostPage: Decodable { | ||
let next: URL | ||
@StrictDynamicDecodingArrayWrapper<PostCodingKey> var content: [Post] | ||
} | ||
|
||
struct DefaultPostPage: Decodable { | ||
let next: URL | ||
@DefaultValueDynamicDecodingArrayWrapper<PostCodingKey> var content: [Post] | ||
} | ||
|
||
struct LossyPostPage: Decodable { | ||
let next: URL | ||
@LossyDynamicDecodingArrayWrapper<PostCodingKey> var content: [Post] | ||
} | ||
|
||
struct ThrowingPostPageSet: Decodable { | ||
let next: URL | ||
@StrictDynamicDecodingCollectionWrapper<PostSetCodingKey, Set<AnyPost<Post>>> var content: Set<AnyPost<Post>> | ||
} | ||
|
||
struct DefaultPostPageSet: Decodable { | ||
let next: URL | ||
@DefaultValueDynamicDecodingCollectionWrapper<PostSetCodingKey, Set<AnyPost<Post>>> var content: Set<AnyPost<Post>> | ||
} | ||
|
||
struct LossyPostPageSet: Decodable { | ||
let next: URL | ||
@LossyDynamicDecodingCollectionWrapper<PostSetCodingKey, Set<AnyPost<Post>>> var content: Set<AnyPost<Post>> | ||
} |
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
27 changes: 27 additions & 0 deletions
27
...ynamicCodableKitTests/DynamicDecodingContextContainerCodingKey/Models/PostContainer.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,27 @@ | ||
import XCTest | ||
@testable import DynamicCodableKit | ||
|
||
struct ThrowingKeyedPostPage: Decodable { | ||
let next: URL | ||
@StrictDynamicDecodingDictionaryWrapper<PostType> var content: [PostType: Post] | ||
} | ||
|
||
struct LossyKeyedPostPage: Decodable { | ||
let next: URL | ||
@LossyDynamicDecodingDictionaryWrapper<PostType> var content: [PostType: Post] | ||
} | ||
|
||
struct ThrowingKeyedPostPageCollection: Decodable { | ||
let next: URL | ||
@StrictDynamicDecodingArrayDictionaryWrapper<PostType> var content: [PostType: [Post]] | ||
} | ||
|
||
struct DefaultValueKeyedPostPageCollection: Decodable { | ||
let next: URL | ||
@DefaultValueDynamicDecodingArrayDictionaryWrapper<PostType> var content: [PostType: [Post]] | ||
} | ||
|
||
struct LossyKeyedPostPageCollection: Decodable { | ||
let next: URL | ||
@LossyDynamicDecodingArrayDictionaryWrapper<PostType> var content: [PostType: [Post]] | ||
} |
49 changes: 49 additions & 0 deletions
49
Tests/DynamicCodableKitTests/DynamicDecodingContextContainerCodingKey/Models/PostPath.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,49 @@ | ||
import XCTest | ||
@testable import DynamicCodableKit | ||
|
||
struct CommonPost: Decodable { | ||
let id: UUID | ||
let author: UUID | ||
let likes: Int | ||
let createdAt: String | ||
@PathCodingKeyWrapper var type: PostType | ||
} | ||
|
||
struct OptionalTypeCommonPost: Decodable { | ||
let id: UUID | ||
let author: UUID | ||
let likes: Int | ||
let createdAt: String | ||
@OptionalPathCodingKeyWrapper var type: PostType? | ||
} | ||
|
||
struct CommonPostPage: Decodable { | ||
let next: URL | ||
@PostData var content: [PostType: [CommonPost]] | ||
} | ||
|
||
struct ThrowingCommonPostPage: Decodable { | ||
let next: URL | ||
var content: [String: [CommonPost]] | ||
} | ||
|
||
struct OptionalTypeCommonPostPage: Decodable { | ||
let next: URL | ||
var content: [String: [OptionalTypeCommonPost]] | ||
} | ||
|
||
@propertyWrapper | ||
struct PostData: Decodable { | ||
public var wrappedValue: [PostType: [CommonPost]] | ||
|
||
public init(wrappedValue: [PostType: [CommonPost]]) { | ||
self.wrappedValue = wrappedValue | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: PostType.self) | ||
self.wrappedValue = try container.allKeys.reduce(into: [:], { values, key in | ||
values[key] = try container.decode([CommonPost].self, forKey: key) | ||
}) | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
...itTests/DynamicDecodingContextProvider/DynamicDecodingCollectionContextBasedWrapper.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,40 @@ | ||
import XCTest | ||
@testable import DynamicCodableKit | ||
|
||
final class DynamicDecodingCollectionContextBasedWrapperTests: XCTestCase { | ||
func testDecoding() throws { | ||
let url = Bundle.module.url(forResource: "identifier-collection-decode", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
decoder.userInfo[.postKey] = DynamicDecodingContext<Post>(withKey: PostCodingKey.self) | ||
let postPage = try decoder.decode(ProviderBasedThrowingPostPage.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-collection-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
XCTAssertThrowsError(try decoder.decode(ProviderBasedThrowingPostPage.self, from: data)) | ||
} | ||
|
||
func testInvalidDataDecodingWithDefaultConfig() throws { | ||
let url = Bundle.module.url(forResource: "identifier-collection-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
decoder.userInfo[.postKey] = DynamicDecodingContext<Post>(withKey: PostCodingKey.self) | ||
let postPage = try decoder.decode(ProviderBasedDefaultPostPage.self, from: data) | ||
XCTAssertEqual(postPage.content.count, 0) | ||
} | ||
|
||
func testInvalidDataDecodingWithLossyConfig() throws { | ||
let url = Bundle.module.url(forResource: "identifier-collection-decode-with-invalid-data", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
decoder.userInfo[.postKey] = DynamicDecodingContext<Post>(withKey: PostCodingKey.self) | ||
let postPage = try decoder.decode(ProviderBasedLossyPostPage.self, from: data) | ||
XCTAssertEqual(postPage.content.count, 4) | ||
XCTAssertEqual(postPage.content.map(\.type), [.text, .picture, .audio, .video]) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...icCodableKitTests/DynamicDecodingContextProvider/DynamicDecodingContextBasedWrapper.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,39 @@ | ||
import XCTest | ||
@testable import DynamicCodableKit | ||
|
||
final class DynamicDecodingContextBasedWrapperTests: XCTestCase { | ||
func testDecoding() throws { | ||
let url = Bundle.module.url(forResource: "identifier-decode", withExtension: "json")! | ||
let data = try Data(contentsOf: url) | ||
let decoder = JSONDecoder() | ||
decoder.userInfo[.postKey] = DynamicDecodingContext<Post>(decoding: VideoPost.self) | ||
let postPage = try decoder.decode(ProviderBasedSinglePostPage.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() | ||
decoder.userInfo[.postKey] = DynamicDecodingContext<Post>(decoding: VideoPost.self) | ||
let postPage = try decoder.decode(ProviderBasedOptionalSinglePostPage.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(ProviderBasedSinglePostPage.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(ProviderBasedOptionalSinglePostPage.self, from: data) | ||
XCTAssertNil(postPage.content) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Tests/DynamicCodableKitTests/DynamicDecodingContextProvider/Models/ProviderPost.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,36 @@ | ||
import XCTest | ||
@testable import DynamicCodableKit | ||
|
||
extension CodingUserInfoKey { | ||
static let postKey = CodingUserInfoKey(rawValue: "post_key")! | ||
} | ||
|
||
struct PostDecodingProvider: UserInfoDynamicDecodingContextProvider { | ||
typealias Identified = Post | ||
static var infoKey: CodingUserInfoKey { .postKey } | ||
} | ||
|
||
struct ProviderBasedSinglePostPage: Decodable { | ||
let next: URL | ||
@DynamicDecodingContextBasedWrapper<PostDecodingProvider> var content: Post | ||
} | ||
|
||
struct ProviderBasedOptionalSinglePostPage: Decodable { | ||
let next: URL | ||
@OptionalDynamicDecodingContextBasedWrapper<PostDecodingProvider> var content: Post? | ||
} | ||
|
||
struct ProviderBasedThrowingPostPage: Decodable { | ||
let next: URL | ||
@StrictDynamicDecodingArrayContextBasedWrapper<PostDecodingProvider> var content: [Post] | ||
} | ||
|
||
struct ProviderBasedDefaultPostPage: Decodable { | ||
let next: URL | ||
@DefaultValueDynamicDecodingArrayContextBasedWrapper<PostDecodingProvider> var content: [Post] | ||
} | ||
|
||
struct ProviderBasedLossyPostPage: Decodable { | ||
let next: URL | ||
@LossyDynamicDecodingArrayContextBasedWrapper<PostDecodingProvider> var content: [Post] | ||
} |
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