-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasyJSONTest.java
More file actions
177 lines (157 loc) · 5.89 KB
/
Copy pathEasyJSONTest.java
File metadata and controls
177 lines (157 loc) · 5.89 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import xyz.victorolaitan.easyjson.EasyJSON;
import xyz.victorolaitan.easyjson.EasyJSONException;
import xyz.victorolaitan.easyjson.JSONElement;
public class EasyJSONTest {
@Test
@DisplayName("create empty JSON")
public void createEmptyJSON() {
EasyJSON json = EasyJSON.create();
Assert.assertEquals("{}", json.toString());
}
@Test
@DisplayName("add string")
public void addString() {
EasyJSON json = EasyJSON.create();
json.putPrimitive("my", "name");
Assert.assertEquals("{\"my\":\"name\"}", json.toString());
}
@Test
@DisplayName("add number")
public void addNumber() {
EasyJSON json = EasyJSON.create();
json.putPrimitive("number", 123.456f);
Assert.assertEquals("{\"number\":123.456}", json.toString());
}
@Test
@DisplayName("add boolean")
public void addBoolean() {
EasyJSON json = EasyJSON.create();
json.putPrimitive("bool", true);
Assert.assertEquals("{\"bool\":true}", json.toString());
}
@Test
@DisplayName("add element (infer key)")
public void addElementInferKey() {
EasyJSON json = EasyJSON.create();
json.putPrimitive("bool", true);
json.putElement(EasyJSON.create().putPrimitive("key", "value"));
Assert.assertEquals("{\"bool\":true,\"key\":\"value\"}", json.toString());
}
@Test
@DisplayName("add element (explicit key)")
public void addElementExplicitKey() {
EasyJSON json = EasyJSON.create();
json.putPrimitive("bool", true);
json.putElement("name", EasyJSON.create().putPrimitive("key", "value"));
Assert.assertEquals("{\"bool\":true,\"name\":\"value\"}", json.toString());
}
@Test
@DisplayName("add primitive (null key)")
public void addPrimitiveNullKey() {
EasyJSON json = EasyJSON.create();
json.putPrimitive("__something");
Assert.assertEquals("{\"null\":\"__something\"}", json.toString());
}
@Test
@DisplayName("add structure")
public void addStructure() {
EasyJSON json = EasyJSON.create();
json.putStructure("struct").putPrimitive("ping", "pong");
Assert.assertEquals("{\"struct\":{\"ping\":\"pong\"}}", json.toString());
}
@Test
@DisplayName("add structure (from root)")
public void addStructureFromRoot() {
EasyJSON json = EasyJSON.create();
EasyJSON struct = EasyJSON.create();
struct.putPrimitive("ping", "pong");
json.putStructure("struct", struct);
Assert.assertEquals("{\"struct\":{\"ping\":\"pong\"}}", json.toString());
}
@Test
@DisplayName("add structure (from element)")
public void addStructureFromElement() {
EasyJSON json = EasyJSON.create();
EasyJSON struct = EasyJSON.create();
struct.putPrimitive("ping", "pong");
json.putStructure("struct", struct.getRootNode());
Assert.assertEquals("{\"struct\":{\"ping\":\"pong\"}}", json.toString());
}
@Test
@DisplayName("add array")
public void addArray() {
EasyJSON json = EasyJSON.create();
JSONElement arr = json.putArray("arr", 1, 2, 3);
arr.putPrimitive("ping", "pong");
Assert.assertEquals("{\"arr\":[1,2,3,\"pong\"]}", json.toString());
}
@Test
@DisplayName("add array (from element)")
public void addArrayFromElement() {
EasyJSON json = EasyJSON.create();
JSONElement arr = EasyJSON.create().putArray("arr", 1, 2, 3);
arr.putPrimitive("ping", "pong");
json.putElement(arr);
Assert.assertEquals("{\"arr\":[1,2,3,\"pong\"]}", json.toString());
}
@Test
@DisplayName("remove element")
public void removeElement() {
EasyJSON json = EasyJSON.create();
json.putStructure("ping").putPrimitive("pong", "ball");
json.removeElement("ping", "pong");
Assert.assertEquals("{\"ping\":{}}", json.toString());
}
@Test
@DisplayName("element exists")
public void elementExists() {
EasyJSON json = EasyJSON.create();
json.putArray("ping").putStructure("").putPrimitive("pong", "ball");
Assert.assertTrue(json.elementExists("ping", "0", "pong"));
}
@Test
@DisplayName("search")
public void search() {
EasyJSON json = EasyJSON.create();
JSONElement struct = json.putArray("ping").putStructure("");
JSONElement e = struct.putPrimitive("pong", "ball");
Assert.assertEquals(e, json.search("ping", "0", "pong"));
}
@Test
@DisplayName("valueOf")
public void valueOf() {
EasyJSON json = EasyJSON.create();
json.putArray("ping").putStructure("").putPrimitive("pong", "ball");
Assert.assertEquals("ball", json.valueOf("ping", "0", "pong"));
}
@Test
@DisplayName("putAll (root)")
public void putAllRoot() {
EasyJSON json1 = EasyJSON.create();
json1.putArray("ping").putPrimitive("hi");
json1.putStructure("pong").putPrimitive("ball", ":)");
EasyJSON json2 = EasyJSON.create();
json2.putAll(json1);
Assert.assertEquals("{\"ping\":[\"hi\"],\"pong\":{\"ball\":\":)\"}}", json2.toString());
}
@Test
@DisplayName("putAll (element)")
public void putAllElement() {
EasyJSON json1 = EasyJSON.create();
json1.putArray("ping").putPrimitive("hi");
json1.putStructure("pong").putPrimitive("ball", ":)");
EasyJSON json2 = EasyJSON.create();
json2.putAll(json1.getRootNode());
Assert.assertEquals("{\"ping\":[\"hi\"],\"pong\":{\"ball\":\":)\"}}", json2.toString());
}
@Test
@DisplayName("export")
public void export() throws EasyJSONException {
EasyJSON json = EasyJSON.create();
json.putPrimitive("ping", "pong");
Assert.assertEquals("pong", json.exportToJSONObject().get("ping"));
}
}