-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathActionCodableTests.swift
104 lines (94 loc) · 3.79 KB
/
ActionCodableTests.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* The MIT License (MIT)
*
* Copyright (C) 2019, CosmicMind, Inc. <http://cosmicmind.com>.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import XCTest
import Foundation
@testable import Graph
class ActionCodableTests: XCTestCase {
let array: [Any] = [-1, "2", [3, "4"], Decimal(12.34)]
func testEncoder() {
let action = Action("User", graph: "CodableGraph")
action.name = "Orkhan"
action.age = 20
action.url = URL(string: "http://cosmicmind.com")!
action.array = array
action.dictionary = ["key": array]
action.add(tags: ["Swift", "iOS"])
action.add(to: ["Programming", "Religion"])
guard let data = try? JSONEncoder().encode(action) else {
XCTFail("[EntityCodableTests Error: Encoder failed to encode.]")
return
}
guard let json = GraphJSON.parse(data) else {
XCTFail("[EntityCodableTests Error: Failed to create GraphJSON.]")
return
}
XCTAssertEqual(json.type.asString, "User")
XCTAssertEqual((json.tags.object as? [String]).map { Set($0) }, Set(["Swift", "iOS"]))
XCTAssertEqual((json.groups.object as? [String]).map { Set($0) }, Set(["Programming", "Religion"]))
XCTAssertEqual(json.properties.name.asString, "Orkhan")
XCTAssertEqual(json.properties.age.asInt, 20)
XCTAssertEqual(json.properties.url.asString, "http://cosmicmind.com")
XCTAssert((json.properties.array.object as? NSArray)?.isEqual(to: array) == true)
XCTAssert((json.properties.dictionary.object as? NSDictionary)?.isEqual(to: ["key": array]) == true)
}
func testDecoder() {
let json = """
{
"type" : "User",
"groups" : [
"Programming",
"Religion"
],
"tags" : [
"Swift",
"iOS"
],
"properties" : {
"age" : 20,
"name" : "Orkhan",
"array": [-1, "2", [3, "4"], 12.34],
"dictionary": { "key": [-1, "2", [3, "4"], 12.34] }
}
}
"""
guard let data = json.data(using: .utf8) else {
XCTFail("[EntityCodableTests Error: Failed to create Data from json string.]")
return
}
let decoder = JSONDecoder()
decoder.userInfo[.graph] = "CodableGraph"
guard let action = try? decoder.decode(Action.self, from: data) else {
XCTFail("[EntityCodableTests Error: Decoder failed to decode.]")
return
}
XCTAssertEqual(action.type, "User")
XCTAssertTrue(action.has(tags: ["iOS", "Swift"]))
XCTAssertTrue(action.member(of: ["Programming", "Religion"]))
XCTAssertEqual(action.name as? String, "Orkhan")
XCTAssertEqual(action.age as? Int, 20)
XCTAssert((action.array as? NSArray)?.isEqual(to: array) == true)
XCTAssert((action.dictionary as? NSDictionary)?.isEqual(to: ["key": array]) == true)
}
}