Skip to content

Commit 765523a

Browse files
committed
Add parsing related unit tests for JSONArray
1 parent 4206874 commit 765523a

File tree

1 file changed

+381
-0
lines changed

1 file changed

+381
-0
lines changed
Lines changed: 381 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
1+
package processing.data;
2+
3+
import org.junit.Test;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.PrintWriter;
7+
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertThrows;
10+
11+
12+
public class JSONArrayTest {
13+
@Test
14+
public void testDefaultConstructor() {
15+
JSONArray jsonArray = new JSONArray();
16+
assertEquals(0, jsonArray.size());
17+
}
18+
19+
@Test
20+
public void testEmptyTokenerConstructor() {
21+
JSONArray jsonArray = new JSONArray(new JSONTokener("[]"));
22+
assertEquals(0, jsonArray.size());
23+
}
24+
25+
@Test
26+
public void testNullTokenerConstructor() {
27+
JSONArray jsonArray = new JSONArray(new JSONTokener("[null]"));
28+
assertEquals(1, jsonArray.size());
29+
assertEquals(JSONObject.NULL, jsonArray.get(0));
30+
}
31+
32+
@Test
33+
public void testIntTokenerConstructor() {
34+
JSONArray jsonArray = new JSONArray(new JSONTokener("[1]"));
35+
assertEquals(1, jsonArray.size());
36+
assertEquals(1, jsonArray.get(0));
37+
}
38+
39+
@Test
40+
public void testDoubleTokenerConstructor() {
41+
JSONArray jsonArray = new JSONArray(new JSONTokener("[0.1]"));
42+
assertEquals(1, jsonArray.size());
43+
assertEquals(0.1, jsonArray.get(0));
44+
}
45+
46+
@Test
47+
public void testStringTokenerConstructor() {
48+
JSONArray jsonArray = new JSONArray(new JSONTokener("[\"a\"]"));
49+
assertEquals(1, jsonArray.size());
50+
assertEquals("a", jsonArray.get(0));
51+
}
52+
53+
@Test
54+
public void testArrayTokenerConstructor() {
55+
JSONArray jsonArray = new JSONArray(new JSONTokener("[[1]]"));
56+
assertEquals(1, jsonArray.size());
57+
assertEquals(JSONArray.class, jsonArray.get(0).getClass());
58+
JSONArray innerJsonArray = ((JSONArray) jsonArray.get(0));
59+
assertEquals(1, innerJsonArray.size());
60+
assertEquals(1, innerJsonArray.get(0));
61+
}
62+
63+
@Test
64+
public void testObjectTokenerConstructor() {
65+
JSONArray jsonArray = new JSONArray(new JSONTokener("[{\"a\":\"b\"}]"));
66+
assertEquals(1, jsonArray.size());
67+
assertEquals(JSONObject.class, jsonArray.get(0).getClass());
68+
JSONObject innerJsonObject = ((JSONObject) jsonArray.get(0));
69+
assertEquals(1, innerJsonObject.size());
70+
assertEquals("b", innerJsonObject.get("a"));
71+
}
72+
73+
@Test
74+
public void testMixedTokenerConstructor() {
75+
JSONArray jsonArray = new JSONArray(new JSONTokener("[null, 1, 2147483648, 0.1, \"a\", [1], {\"a\": \"b\"}]"));
76+
assertEquals(7, jsonArray.size());
77+
78+
assertEquals(JSONObject.NULL, jsonArray.get(0));
79+
80+
assertEquals(1, jsonArray.get(1));
81+
82+
assertEquals(2147483648L, jsonArray.get(2));
83+
84+
assertEquals(0.1, jsonArray.get(3));
85+
86+
assertEquals("a", jsonArray.get(4));
87+
88+
assertEquals(JSONArray.class, jsonArray.get(5).getClass());
89+
JSONArray innerJsonArray = (JSONArray) jsonArray.get(5);
90+
assertEquals(1, innerJsonArray.size());
91+
assertEquals(1, innerJsonArray.get(0));
92+
93+
assertEquals(JSONObject.class, jsonArray.get(6).getClass());
94+
JSONObject innerJsonObject = (JSONObject) jsonArray.get(6);
95+
assertEquals(1, innerJsonObject.size());
96+
assertEquals("b", innerJsonObject.get("a"));
97+
}
98+
99+
@Test
100+
public void testTrailingCommaTokenerConstructor() {
101+
JSONArray jsonArray = new JSONArray(new JSONTokener("[1, 2,]"));
102+
assertEquals(2, jsonArray.size());
103+
assertEquals(1, jsonArray.get(0));
104+
assertEquals(2, jsonArray.get(1));
105+
}
106+
107+
@Test
108+
public void testWhitespaceTokenerConstructor() {
109+
JSONArray jsonArray = new JSONArray(new JSONTokener(" [ 1 , 2 , ] "));
110+
assertEquals(2, jsonArray.size());
111+
assertEquals(1, jsonArray.get(0));
112+
assertEquals(2, jsonArray.get(1));
113+
}
114+
115+
@Test
116+
public void testNoSpacesTokenerConstructor() {
117+
JSONArray jsonArray = new JSONArray(new JSONTokener("[1,2]"));
118+
assertEquals(2, jsonArray.size());
119+
assertEquals(1, jsonArray.get(0));
120+
assertEquals(2, jsonArray.get(1));
121+
}
122+
123+
@Test
124+
public void testNoStartTokenerConstructorThrowsException() {
125+
RuntimeException exception = assertThrows(RuntimeException.class, () -> new JSONArray(new JSONTokener("1, 2]")));
126+
assertEquals("A JSONArray text must start with '['", exception.getMessage());
127+
}
128+
129+
@Test
130+
public void testNoEndTokenerConstructorThrowsException() {
131+
RuntimeException exception = assertThrows(RuntimeException.class, () -> new JSONArray(new JSONTokener("[1, 2")));
132+
assertEquals("Expected a ',' or ']'", exception.getMessage());
133+
}
134+
135+
@Test
136+
public void testEmptyIntListConstructor() {
137+
JSONArray jsonArray = new JSONArray(new IntList(new int[]{}));
138+
assertEquals(0, jsonArray.size());
139+
}
140+
141+
@Test
142+
public void testFilledIntListConstructor() {
143+
JSONArray jsonArray = new JSONArray(new IntList(new int[]{1, 2}));
144+
assertEquals(2, jsonArray.size());
145+
assertEquals(1, jsonArray.get(0));
146+
assertEquals(2, jsonArray.get(1));
147+
}
148+
149+
@Test
150+
public void testEmptyFloatListConstructor() {
151+
JSONArray jsonArray = new JSONArray(new FloatList(new float[]{}));
152+
assertEquals(0, jsonArray.size());
153+
}
154+
155+
@Test
156+
public void testFilledFloatListConstructor() {
157+
JSONArray jsonArray = new JSONArray(new FloatList(new float[]{0.1f, 0.2f}));
158+
assertEquals(2, jsonArray.size());
159+
assertEquals(0.1f, jsonArray.get(0));
160+
assertEquals(0.2f, jsonArray.get(1));
161+
}
162+
163+
@Test
164+
public void testEmptyStringListConstructor() {
165+
JSONArray jsonArray = new JSONArray(new StringList(new String[]{}));
166+
assertEquals(0, jsonArray.size());
167+
}
168+
169+
@Test
170+
public void testFilledStringListConstructor() {
171+
JSONArray jsonArray = new JSONArray(new StringList(new String[]{"a", "b"}));
172+
assertEquals(2, jsonArray.size());
173+
assertEquals("a", jsonArray.get(0));
174+
assertEquals("b", jsonArray.get(1));
175+
}
176+
177+
@Test
178+
public void testEmptyArrayConstructor() {
179+
JSONArray jsonArray = new JSONArray(new int[]{});
180+
assertEquals(0, jsonArray.size());
181+
}
182+
183+
@Test
184+
public void testFilledArrayConstructor() {
185+
JSONArray jsonArray = new JSONArray(new int[]{1, 2});
186+
assertEquals(2, jsonArray.size());
187+
assertEquals(1, jsonArray.get(0));
188+
assertEquals(2, jsonArray.get(1));
189+
}
190+
191+
@Test
192+
public void testNullFormat() {
193+
String json = """
194+
[
195+
null,
196+
null
197+
]""";
198+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
199+
String output = jsonArray.format(0);
200+
assertEquals(json, output);
201+
}
202+
203+
@Test
204+
public void testIntFormat() {
205+
String json = """
206+
[
207+
1,
208+
2
209+
]""";
210+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
211+
String output = jsonArray.format(0);
212+
assertEquals(json, output);
213+
}
214+
215+
@Test
216+
public void testLongFormat() {
217+
String json = """
218+
[
219+
2147483648,
220+
2147483649
221+
]""";
222+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
223+
String output = jsonArray.format(0);
224+
assertEquals(json, output);
225+
}
226+
227+
@Test
228+
public void testDoubleFormat() {
229+
String json = """
230+
[
231+
0.1,
232+
0.2
233+
]""";
234+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
235+
String output = jsonArray.format(0);
236+
assertEquals(json, output);
237+
}
238+
239+
@Test
240+
public void testJSONArrayFormat() {
241+
String json = """
242+
[
243+
[],
244+
[
245+
1,
246+
2
247+
]
248+
]""";
249+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
250+
String output = jsonArray.format(0);
251+
assertEquals(json, output);
252+
}
253+
254+
@Test
255+
public void testJSONObjectFormat() {
256+
String json = """
257+
[
258+
null,
259+
1,
260+
2147483648,
261+
0.1,
262+
[
263+
1,
264+
2
265+
],
266+
{
267+
"a":"b",
268+
"c":"d"
269+
}
270+
]""";
271+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
272+
String output = jsonArray.format(0);
273+
assertEquals(json, output);
274+
}
275+
276+
@Test
277+
public void testMixedFormat() {
278+
String json = """
279+
[
280+
null,
281+
1,
282+
2147483648,
283+
0.1,
284+
[
285+
1,
286+
2
287+
],
288+
{
289+
"a":"b",
290+
"c":"d"
291+
}
292+
]""";
293+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
294+
String output = jsonArray.format(0);
295+
assertEquals(json, output);
296+
}
297+
298+
@Test
299+
public void testIndentFormat() {
300+
String json = """
301+
[
302+
1,
303+
2,
304+
[
305+
1,
306+
2
307+
]
308+
]""";
309+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
310+
String output = jsonArray.format(4);
311+
assertEquals(json, output);
312+
}
313+
314+
@Test
315+
public void testNegativeIndentFormat() {
316+
String json = "[1,2,[1,2]]";
317+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
318+
String output = jsonArray.format(-1);
319+
assertEquals(json, output);
320+
}
321+
322+
@Test
323+
public void testIndentWrite() {
324+
String json = """
325+
[
326+
1,
327+
2,
328+
[
329+
1,
330+
2
331+
]
332+
]""";
333+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
334+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
335+
jsonArray.write(new PrintWriter(byteArrayOutputStream));
336+
assertEquals(json, byteArrayOutputStream.toString());
337+
}
338+
339+
@Test
340+
public void testIndentOptionsWrite() {
341+
String json = """
342+
[
343+
1,
344+
2,
345+
[
346+
1,
347+
2
348+
]
349+
]""";
350+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
351+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
352+
jsonArray.write(new PrintWriter(byteArrayOutputStream), "indent=4");
353+
assertEquals(json, byteArrayOutputStream.toString());
354+
}
355+
356+
@Test
357+
public void testCompactOptionsWrite() {
358+
String json = "[1,2,[1,2]]";
359+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
360+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
361+
jsonArray.write(new PrintWriter(byteArrayOutputStream), "compact");
362+
assertEquals(json, byteArrayOutputStream.toString());
363+
}
364+
365+
@Test
366+
public void testMultiOptionsWrite() {
367+
String json = """
368+
[
369+
1,
370+
2,
371+
[
372+
1,
373+
2
374+
]
375+
]""";
376+
JSONArray jsonArray = new JSONArray(new JSONTokener(json));
377+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
378+
jsonArray.write(new PrintWriter(byteArrayOutputStream), "indent=2,indent=4");
379+
assertEquals(json, byteArrayOutputStream.toString());
380+
}
381+
}

0 commit comments

Comments
 (0)