Skip to content

Commit d3c7eaf

Browse files
Sean LearySean Leary
authored andcommitted
restore-jsonparserconfiguration: fix unit tests to work when strictMode default is true
1 parent 09536cd commit d3c7eaf

File tree

8 files changed

+461
-399
lines changed

8 files changed

+461
-399
lines changed

src/test/java/org/json/junit/CDLTest.java

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class CDLTest {
2929
"1, 2, 3, 4\t, 5, 6, 7\n" +
3030
"true, false, true, true, false, false, false\n" +
3131
"0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" +
32-
"\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", va'l6, val7\n";
32+
"\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", \"va'l6\", val7\n";
3333

3434

3535
/**
@@ -38,11 +38,54 @@ public class CDLTest {
3838
* values all must be quoted in the cases where the JSONObject parsing
3939
* might normally convert the value into a non-string.
4040
*/
41-
private static final String EXPECTED_LINES = "[{Col 1:val1, Col 2:val2, Col 3:val3, Col 4:val4, Col 5:val5, Col 6:val6, Col 7:val7}, " +
42-
"{Col 1:\"1\", Col 2:\"2\", Col 3:\"3\", Col 4:\"4\", Col 5:\"5\", Col 6:\"6\", Col 7:\"7\"}, " +
43-
"{Col 1:\"true\", Col 2:\"false\", Col 3:\"true\", Col 4:\"true\", Col 5:\"false\", Col 6:\"false\", Col 7:\"false\"}, " +
44-
"{Col 1:\"0.23\", Col 2:\"57.42\", Col 3:\"5e27\", Col 4:\"-234.879\", Col 5:\"2.34e5\", Col 6:\"0.0\", Col 7:\"9e-3\"}, " +
45-
"{Col 1:\"va\tl1\", Col 2:\"v\bal2\", Col 3:val3, Col 4:\"val\f4\", Col 5:val5, Col 6:va'l6, Col 7:val7}]";
41+
private static final String EXPECTED_LINES =
42+
"[ " +
43+
"{" +
44+
"\"Col 1\":\"val1\", " +
45+
"\"Col 2\":\"val2\", " +
46+
"\"Col 3\":\"val3\", " +
47+
"\"Col 4\":\"val4\", " +
48+
"\"Col 5\":\"val5\", " +
49+
"\"Col 6\":\"val6\", " +
50+
"\"Col 7\":\"val7\"" +
51+
"}, " +
52+
" {" +
53+
"\"Col 1\":\"1\", " +
54+
"\"Col 2\":\"2\", " +
55+
"\"Col 3\":\"3\", " +
56+
"\"Col 4\":\"4\", " +
57+
"\"Col 5\":\"5\", " +
58+
"\"Col 6\":\"6\", " +
59+
"\"Col 7\":\"7\"" +
60+
"}, " +
61+
" {" +
62+
"\"Col 1\":\"true\", " +
63+
"\"Col 2\":\"false\", " +
64+
"\"Col 3\":\"true\", " +
65+
"\"Col 4\":\"true\", " +
66+
"\"Col 5\":\"false\", " +
67+
"\"Col 6\":\"false\", " +
68+
"\"Col 7\":\"false\"" +
69+
"}, " +
70+
"{" +
71+
"\"Col 1\":\"0.23\", " +
72+
"\"Col 2\":\"57.42\", " +
73+
"\"Col 3\":\"5e27\", " +
74+
"\"Col 4\":\"-234.879\", " +
75+
"\"Col 5\":\"2.34e5\", " +
76+
"\"Col 6\":\"0.0\", " +
77+
"\"Col 7\":\"9e-3\"" +
78+
"}, " +
79+
"{" +
80+
"\"Col 1\":\"va\tl1\", " +
81+
"\"Col 2\":\"v\bal2\", " +
82+
"\"Col 3\":\"val3\", " +
83+
"\"Col 4\":\"val\f4\", " +
84+
"\"Col 5\":\"val5\", " +
85+
"\"Col 6\":\"va'l6\", " +
86+
"\"Col 7\":\"val7\"" +
87+
"}" +
88+
"]";
4689

4790
/**
4891
* Attempts to create a JSONArray from a null string.
@@ -283,11 +326,11 @@ public void textToJSONArrayPipeDelimited() {
283326
*/
284327
@Test
285328
public void jsonArrayToJSONArray() {
286-
String nameArrayStr = "[Col1, Col2]";
329+
String nameArrayStr = "[\"Col1\", \"Col2\"]";
287330
String values = "V1, V2";
288331
JSONArray nameJSONArray = new JSONArray(nameArrayStr);
289332
JSONArray jsonArray = CDL.toJSONArray(nameJSONArray, values);
290-
JSONArray expectedJsonArray = new JSONArray("[{Col1:V1,Col2:V2}]");
333+
JSONArray expectedJsonArray = new JSONArray("[{\"Col1\":\"V1\",\"Col2\":\"V2\"}]");
291334
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
292335
}
293336

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,15 @@ public void failedGetArrayValues() {
476476
*/
477477
@Test
478478
public void unquotedText() {
479-
String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]";
480-
JSONArray jsonArray = new JSONArray(str);
481-
List<Object> expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45");
482-
assertEquals(expected, jsonArray.toList());
479+
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
480+
if (jsonParserConfiguration.isStrictMode()) {
481+
System.out.println("Skipping JSONArrayTest unquotedText() when strictMode default is true");
482+
} else {
483+
String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]";
484+
JSONArray jsonArray = new JSONArray(str);
485+
List<Object> expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45");
486+
assertEquals(expected, jsonArray.toList());
487+
}
483488
}
484489

485490
/**
@@ -690,8 +695,8 @@ public void put() {
690695

691696
String jsonArrayStr =
692697
"["+
693-
"hello,"+
694-
"world"+
698+
"\"hello\","+
699+
"\"world\""+
695700
"]";
696701
// 2
697702
jsonArray.put(new JSONArray(jsonArrayStr));
@@ -768,8 +773,8 @@ public void putIndex() {
768773

769774
String jsonArrayStr =
770775
"["+
771-
"hello,"+
772-
"world"+
776+
"\"hello\","+
777+
"\"world\""+
773778
"]";
774779
// 2
775780
jsonArray.put(2, new JSONArray(jsonArrayStr));

src/test/java/org/json/junit/JSONMLTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ public void toJSONObjectToJSONArray() {
625625
"\"subValue\","+
626626
"{\"svAttr\":\"svValue\"},"+
627627
"\"abc\""+
628-
"],"+
628+
"]"+
629629
"],"+
630630
"[\"value\",3],"+
631631
"[\"value\",4.1],"+

src/test/java/org/json/junit/JSONObjectNumberTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ public class JSONObjectNumberTest {
2323
@Parameters(name = "{index}: {0}")
2424
public static Collection<Object[]> data() {
2525
return Arrays.asList(new Object[][]{
26-
{"{value:50}", 1},
27-
{"{value:50.0}", 1},
28-
{"{value:5e1}", 1},
29-
{"{value:5E1}", 1},
30-
{"{value:5e1}", 1},
31-
{"{value:'50'}", 1},
32-
{"{value:-50}", -1},
33-
{"{value:-50.0}", -1},
34-
{"{value:-5e1}", -1},
35-
{"{value:-5E1}", -1},
36-
{"{value:-5e1}", -1},
37-
{"{value:'-50'}", -1}
26+
{"{\"value\":50}", 1},
27+
{"{\"value\":50.0}", 1},
28+
{"{\"value\":5e1}", 1},
29+
{"{\"value\":5E1}", 1},
30+
{"{\"value\":5e1}", 1},
31+
{"{\"value\":\"50\"}", 1},
32+
{"{\"value\":-50}", -1},
33+
{"{\"value\":-50.0}", -1},
34+
{"{\"value\":-5e1}", -1},
35+
{"{\"value\":-5E1}", -1},
36+
{"{\"value\":-5e1}", -1},
37+
{"{\"value\":\"-50\"}", -1}
3838
// JSON does not support octal or hex numbers;
3939
// see https://stackoverflow.com/a/52671839/6323312
4040
// "{value:062}", // octal 50

0 commit comments

Comments
 (0)