Skip to content

Commit dc8c9e3

Browse files
committed
Merge pull request stleary#35 from stleary/more-todo-cleanup
clean up a few more todos
2 parents 4df6984 + 871a3e4 commit dc8c9e3

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

JSONStringerTest.java

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212

1313
/**
14-
* Tests for JSON-Java JSONStringer.
15-
* TODO: Could use a lot more testing. For example, cascade-style productions.
14+
* Tests for JSON-Java JSONStringer and JSONWriter.
1615
*/
1716
public class JSONStringerTest {
1817

@@ -234,43 +233,44 @@ public void simpleArrayString() {
234233
}
235234

236235
/**
237-
* Build a nested JSON doc using JSONString API calls,
238-
* then convert to JSONObject
236+
* Build a nested JSON doc using JSONString API calls, then convert to
237+
* JSONObject. Will create a long cascade of output by reusing the
238+
* returned values..
239239
*/
240240
@Test
241241
public void complexObjectString() {
242242
JSONStringer jsonStringer = new JSONStringer();
243-
jsonStringer.object();
244-
jsonStringer.key("trueValue").value(true);
245-
jsonStringer.key("falseValue").value(false);
246-
jsonStringer.key("nullValue").value(null);
247-
jsonStringer.key("stringValue").value("hello world!");
248-
jsonStringer.key("object2").object();
249-
jsonStringer.key("k1").value("v1");
250-
jsonStringer.key("k2").value("v2");
251-
jsonStringer.key("k3").value("v3");
252-
jsonStringer.key("array1").array();
253-
jsonStringer.value(1);
254-
jsonStringer.value(2);
255-
jsonStringer.object();
256-
jsonStringer.key("k4").value("v4");
257-
jsonStringer.key("k5").value("v5");
258-
jsonStringer.key("k6").value("v6");
259-
jsonStringer.key("array2").array();
260-
jsonStringer.value(5);
261-
jsonStringer.value(6);
262-
jsonStringer.value(7);
263-
jsonStringer.value(8);
264-
jsonStringer.endArray();
265-
jsonStringer.endObject();
266-
jsonStringer.value(3);
267-
jsonStringer.value(4);
268-
jsonStringer.endArray();
269-
jsonStringer.endObject();
270-
jsonStringer.key("complexStringValue").value("h\be\tllo w\u1234orld!");
271-
jsonStringer.key("intValue").value(42);
272-
jsonStringer.key("doubleValue").value(-23.45e67);
273-
jsonStringer.endObject();
243+
jsonStringer.object().
244+
key("trueValue").value(true).
245+
key("falseValue").value(false).
246+
key("nullValue").value(null).
247+
key("stringValue").value("hello world!").
248+
key("object2").object().
249+
key("k1").value("v1").
250+
key("k2").value("v2").
251+
key("k3").value("v3").
252+
key("array1").array().
253+
value(1).
254+
value(2).
255+
object().
256+
key("k4").value("v4").
257+
key("k5").value("v5").
258+
key("k6").value("v6").
259+
key("array2").array().
260+
value(5).
261+
value(6).
262+
value(7).
263+
value(8).
264+
endArray().
265+
endObject().
266+
value(3).
267+
value(4).
268+
endArray().
269+
endObject().
270+
key("complexStringValue").value("h\be\tllo w\u1234orld!").
271+
key("intValue").value(42).
272+
key("doubleValue").value(-23.45e67).
273+
endObject();
274274
String str = jsonStringer.toString();
275275
JSONObject jsonObject = new JSONObject(str);
276276

Util.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,26 @@ public static void compareActualVsExpectedJsonObjects(
5959
private static void compareActualVsExpectedObjects(Object value,
6060
Object expectedValue) {
6161
if (value instanceof JSONObject && expectedValue instanceof JSONObject) {
62+
// Compare JSONObjects
6263
JSONObject jsonObject = (JSONObject)value;
6364
JSONObject expectedJsonObject = (JSONObject)expectedValue;
6465
compareActualVsExpectedJsonObjects(
6566
jsonObject, expectedJsonObject);
6667
} else if (value instanceof JSONArray && expectedValue instanceof JSONArray) {
68+
// Compare JSONArrays
6769
JSONArray jsonArray = (JSONArray)value;
6870
JSONArray expectedJsonArray = (JSONArray)expectedValue;
6971
compareActualVsExpectedJsonArrays(
7072
jsonArray, expectedJsonArray);
7173
} else {
7274
/**
73-
* Certain helper classes (e.g. XML) may create Long instead of
74-
* Integer for small int values. As long as both are Numbers,
75-
* just compare the toString() values.
76-
* TODO: this may not work in the case where the underlying types
77-
* do not have the same precision.
75+
* Compare all other types using toString(). First, the types must
76+
* also be equal, unless both are Number type. Certain helper
77+
* classes (e.g. XML) may create Long instead of Integer for small
78+
* int values.
7879
*/
7980
if (!(value instanceof Number && expectedValue instanceof Number)) {
81+
// Non-Number and non-matching types
8082
assertTrue("object types should be equal for actual: "+
8183
value.toString()+" ("+
8284
value.getClass().toString()+") expected: "+
@@ -86,8 +88,7 @@ private static void compareActualVsExpectedObjects(Object value,
8688
expectedValue.getClass().toString()));
8789
}
8890
/**
89-
* When in doubt, compare by string
90-
* TODO: should not this be an else to the previous condition?
91+
* Same types or both Numbers, compare by toString()
9192
*/
9293
assertTrue("string values should be equal for actual: "+
9394
value.toString()+" expected: "+expectedValue.toString(),

0 commit comments

Comments
 (0)