๐Ÿ“ฆ calda / NestedKeyEncodingStrategy

๐Ÿ“„ NestedKeyEncodingStrategyTests.swift ยท 91 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91//
//  NestedKeyEncodingStrategyTests.swift
//  NestedKeyEncodingStrategyTests
//
//  Created by Cal Stephens on 2/22/20.
//  Copyright ยฉ 2020 Cal Stephens. All rights reserved.
//

import XCTest
import NestedKeyEncodingStrategy

// MARK: Codable objects

struct NestedObjectCoding: Codable {
    let rootValue: String
    let nestedObject: NestedObject
    
    struct NestedObject: Codable {
        let nestedValue: String
    }
    
    static let encoder = NestedKeyEncodingStrategy.JSONEncoder()
    static let decoder = NestedKeyEncodingStrategy.JSONDecoder()
}

struct NestedKeyCoding: Codable {
    let rootValue: String
    let nestedValue: String
    
    enum CodingKeys: String, CodingKey {
        case rootValue
        case nestedValue = "nestedObject.nestedValue"
    }
    
    static var encoder: NestedKeyEncodingStrategy.JSONEncoder {
        let encoder = NestedKeyEncodingStrategy.JSONEncoder()
        encoder.nestedKeyEncodingStrategy = .useDotNotation
        return encoder
    }
    
    static var decoder: NestedKeyEncodingStrategy.JSONDecoder {
        let decoder = NestedKeyEncodingStrategy.JSONDecoder()
        decoder.nestedKeyDecodingStrategy = .useDotNotation
        return decoder
    }
}

// MARK: Tests

class NestedKeyEncodingStrategyTests: XCTestCase {

    func test_encodeNested_decodeObjects() throws {
        let nestedKeyCodingInstance = NestedKeyCoding(
            rootValue: "root",
            nestedValue: "nested")
        
        let data = try? NestedKeyCoding.encoder.encode(nestedKeyCodingInstance)
        XCTAssertNotNil(data)
        
        XCTAssertEqual(
            data.flatMap { String(data: $0, encoding: .utf8) },
            #"{"rootValue":"root","nestedObject":{"nestedValue":"nested"}}"#)
        
        let objectCodingInstance = try data.flatMap { try NestedObjectCoding.decoder.decode(NestedObjectCoding.self, from: $0) }
        XCTAssertNotNil(objectCodingInstance)
        
        XCTAssertEqual(nestedKeyCodingInstance.rootValue, objectCodingInstance?.rootValue)
        XCTAssertEqual(nestedKeyCodingInstance.nestedValue, objectCodingInstance?.nestedObject.nestedValue)
    }
    
    func test_encodeObjects_decodeNested() throws {
        let objectCodingInstance = NestedObjectCoding(
            rootValue: "root",
            nestedObject: NestedObjectCoding.NestedObject(nestedValue: "nested"))
        
        let data = try? NestedObjectCoding.encoder.encode(objectCodingInstance)
        XCTAssertNotNil(data)
        
        XCTAssertEqual(
            data.flatMap { String(data: $0, encoding: .utf8) },
            #"{"rootValue":"root","nestedObject":{"nestedValue":"nested"}}"#)
        
        let nestedKeyCodingInstance = try data.flatMap { try NestedKeyCoding.decoder.decode(NestedKeyCoding.self, from: $0) }
        XCTAssertNotNil(nestedKeyCodingInstance)
        
        XCTAssertEqual(nestedKeyCodingInstance?.rootValue, objectCodingInstance.rootValue)
        XCTAssertEqual(nestedKeyCodingInstance?.nestedValue, objectCodingInstance.nestedObject.nestedValue)
    }

}