-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCodableDictionaryTests.swift
61 lines (49 loc) · 1.64 KB
/
CodableDictionaryTests.swift
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
//
// CodableDictionaryTests.swift
// MoreCodableTests
//
// Created by Tatsuya Tanaka on 20180909.
// Copyright © 2018年 tattn. All rights reserved.
//
import XCTest
import MoreCodable
class CodableDictionaryTests: XCTestCase {
let jsonEncoder = JSONEncoder()
let jsonDecoder = JSONDecoder()
override func setUp() {
super.setUp()
}
func testEnumKey() {
enum Key: String, Codable, CodingKey {
case foo
}
let originalEnumKeyedDictionary: [Key: Int] = [
.foo: 100
]
let enumKeyedDictionary: CodableDictionary<Key, Int> = [
.foo: 100
]
let json = "{\"foo\":100}"
let unexpectedJSON = "[\"foo\",100]"
do {
let encodedData = try! jsonEncoder.encode(originalEnumKeyedDictionary)
let encodedJSON = String(data: encodedData, encoding: .utf8)!
XCTAssertEqual(encodedJSON, unexpectedJSON)
}
do {
let encodedData = try! jsonEncoder.encode(enumKeyedDictionary)
let encodedJSON = String(data: encodedData, encoding: .utf8)!
XCTAssertEqual(encodedJSON, json)
}
do {
let data = unexpectedJSON.data(using: .utf8)!
let decoded = try! jsonDecoder.decode(type(of: originalEnumKeyedDictionary), from: data)
XCTAssertEqual(decoded, originalEnumKeyedDictionary)
}
do {
let data = json.data(using: .utf8)!
let decoded = try! jsonDecoder.decode(type(of: enumKeyedDictionary), from: data)
XCTAssertEqual(decoded, enumKeyedDictionary)
}
}
}