-
Notifications
You must be signed in to change notification settings - Fork 0
/
JustJsonTests.swift
144 lines (123 loc) · 4.09 KB
/
JustJsonTests.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// JustJsonTests.swift
// JustJsonTests
//
// Created by Horst Leung on 2017-01-16.
// Copyright © 2017 VDelegate Limited. All rights reserved.
//
import XCTest
@testable import JustJson
class JustJsonTests: XCTestCase {
var dict: [String: Any] = [:]
var jsonStr: String?
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
dict = [
"language": "de",
"translator": "Erika Fuchs",
"translations": [
"characters": [
"Scrooge McDuck": "Dagobert",
"Huey": "Tick",
"Dewey": "Trick",
"Louie": "Track",
"Gyro Gearloose": "Daniel Düsentrieb",
],
"places": [
"Duckburg": "Entenhausen",
"Money Bin": "Geldspeicher",
]
],
"arrayDict" : [
"value" : ["1", "2", "3", "5", "7"]
],
"int" : "1",
"float" : Float(1.1),
"double" : 2.11
]
jsonStr = dict.toJSONStr()
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testSimpleGet() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
let val = dict[keyPath: "translations.characters.Gyro Gearloose"] as? String
XCTAssert(val == "Daniel Düsentrieb")
}
func testSimpleGetInt() {
let val = dict[int: "int"]
XCTAssert(val == Int(1))
}
func testSimpleGetFloat() {
let val = dict[float: "float"]
XCTAssert(val == Float(1.1))
}
func testSimpleGetDouble() {
let val = dict[double: "double"]
XCTAssert(val == Double(2.11))
}
func testSimpleEdit() {
let editVal = "testing 123"
dict[keyPath: "translations.characters.Gyro Gearloose"] = editVal
let val = dict[keyPath: "translations.characters.Gyro Gearloose"] as? String
XCTAssert(val == editVal)
}
func testAppendString() {
let appendVal = " foo foo foo"
dict[stringValue: "translations.characters.Gyro Gearloose"].append(appendVal)
let val = dict[string: "translations.characters.Gyro Gearloose"]
XCTAssert(val == "Daniel Düsentrieb".appending(appendVal))
}
func testAppendNode() {
dict["hello"] = "world"
XCTAssert(dict[string: "hello"] == "world")
}
func testAppendNode2() {
dict[keyPath: "hello"] = ["testing": ["123" : "yeah"]]
XCTAssert(dict[string: "hello.testing.123"] == "yeah", dict.toJSONStr() ?? "")
}
func testRemoveAll() {
dict[dict: "translations.places"]?.removeAll()
XCTAssert(dict[dict: "translations.places"]?.count == 0)
}
func testRemoveNode() {
dict[keyPath: "translations.places"] = nil
XCTAssert(dict[keyPath: "translations.places"] == nil)
}
func testLoopArray() {
let target = "12357"
var result = ""
if let array = dict[array: "arrayDict.value"] {
for num in array {
result.append(num as! String)
}
}
XCTAssert(result == target)
}
func testLoopArray2() {
let target = "12357"
var result = ""
for num in dict[arrayValue: "arrayDict.value"] {
result.append(num as! String)
}
XCTAssert(result == target)
}
func testPerformanceToJSONStr() {
self.measure {
for _ in 0...10000 {
let _ = self.dict.toJSONStr()
}
}
}
func testPerformanceToDict() {
self.measure {
for _ in 0...10000 {
let _ = self.jsonStr?.toDictionary()
}
}
}
}