fix(#887): regressions, unit tests

- JSONArray now evaluates EOF accordingly for empty Array inputs.
- JSONTokener fixed indentation
- externalized two JSONMLTest cases
This commit is contained in:
rikkarth
2024-04-28 23:30:05 +01:00
parent cf00ef3e8a
commit 1ae43bdb90
8 changed files with 218 additions and 81 deletions

View File

@@ -24,16 +24,12 @@ public class CDLTest {
* String of lines where the column names are in the first row,
* and all subsequent rows are values. All keys and values should be legal.
*/
// TODO: This regression causes several unit tests to fail in strictMode.
// a single quote embedded in a valid st ring value should be allowed.
// This probably needs to be fixed in the strictMode parser.
private static final String LINES = "Col 1, Col 2, \tCol 3, Col 4, Col 5, Col 6, Col 7\n" +
"val1, val2, val3, val4, val5, val6, val7\n" +
"1, 2, 3, 4\t, 5, 6, 7\n" +
"true, false, true, true, false, false, false\n" +
"0.23, 57.42, 5e27, -234.879, 2.34e5, 0.0, 9e-3\n" +
// "\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", \"va'l6\", val7\n";
"\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", \"val6\", val7\n";
"\"va\tl1\", \"v\bal2\", \"val3\", \"val\f4\", \"val5\", \"va'l6\", val7\n";
/**
* CDL.toJSONArray() adds all values as strings, with no filtering or
@@ -46,8 +42,7 @@ public class CDLTest {
"{\"Col 1\":\"1\", \"Col 2\":\"2\", \"Col 3\":\"3\", \"Col 4\":\"4\", \"Col 5\":\"5\", \"Col 6\":\"6\", \"Col 7\":\"7\"}, " +
"{\"Col 1\":\"true\", \"Col 2\":\"false\", \"Col 3\":\"true\", \"Col 4\":\"true\", \"Col 5\":\"false\", \"Col 6\":\"false\", \"Col 7\":\"false\"}, " +
"{\"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\"}, " +
// "{\"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\"}]";
"{\"Col 1\":\"va\tl1\", \"Col 2\":\"v\bal2\", \"Col 3\":\"val3\", \"Col 4\":\"val\f4\", \"Col 5\":\"val5\", \"Col 6\":\"val6\", \"Col 7\":\"val7\"}]";
"{\"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\"}]";
/**
* Attempts to create a JSONArray from a null string.

View File

@@ -6,6 +6,11 @@ Public Domain.
import static org.junit.Assert.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.json.*;
import org.junit.Test;
@@ -648,16 +653,10 @@ public class JSONMLTest {
// create a JSON array from the original string and make sure it
// looks as expected
JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
// TODO: The next 2 test cases fail due to a strictMode regression. They should be isolated in separate
// test cases and fixed.
// JSONArray expectedJsonArray = new JSONArray(expectedJSONArrayStr);
// Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray);
// restore the XML, then make another JSONArray and make sure it
// looks as expected
String jsonArrayXmlToStr = JSONML.toString(jsonArray);
// JSONArray finalJsonArray = JSONML.toJSONArray(jsonArrayXmlToStr);
// Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
// lastly, confirm the restored JSONObject XML and JSONArray XML look
// reasonably similar
@@ -666,6 +665,31 @@ public class JSONMLTest {
Util.compareActualVsExpectedJsonObjects(jsonObjectFromObject, jsonObjectFromArray);
}
@Test
public void givenXmlStr_testToJSONArray_shouldEqualExpectedArray() throws IOException {
try (Stream<String> jsonLines = Files.lines(
Paths.get("src/test/resources/JSONArrayExpectedTestCaseForToJsonArrayTest.json"));
Stream<String> xmlLines = Files.lines(Paths.get("src/test/resources/XmlTestCaseTestToJsonArray.xml"))) {
String xmlStr = xmlLines.collect(Collectors.joining());
String expectedJSONArrayStr = jsonLines.collect(Collectors.joining());
JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
JSONArray expectedJsonArray = new JSONArray(expectedJSONArrayStr);
assertEquals(expectedJsonArray.toString(), jsonArray.toString());
//TODO Util.compareActualVsExpectedJsonArrays can be replaced with above assertEquals(expectedJsonArray.toString(), jsonArray.toString())
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray);
String jsonArrayXmlToStr = JSONML.toString(jsonArray);
JSONArray finalJsonArray = JSONML.toJSONArray(jsonArrayXmlToStr);
//TODO Util.compareActualVsExpectedJsonArrays can be replaced with assertEquals(expectedJsonArray.toString(), finalJsonArray.toString())
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
}
}
/**
* Convert an XML document which contains embedded comments into
* a JSONArray. Use JSONML.toString() to turn it into a string, then

View File

@@ -47,14 +47,15 @@ public class JSONParserConfigurationTest {
}
@Test
public void givenEmptyArray_testStrictModeTrue_shouldNotThrowJsonException(){
public void givenEmptyArray_testStrictModeTrue_shouldNotThrowJsonException() {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration()
.withStrictMode(true);
String testCase = "[]";
JSONArray jsonArray = new JSONArray(testCase, jsonParserConfiguration);
System.out.println(jsonArray);
assertEquals(testCase, jsonArray.toString());
}
@Test
@@ -215,7 +216,7 @@ public class JSONParserConfigurationTest {
() -> new JSONArray(testCaseFour, jsonParserConfiguration));
assertEquals(
"Field contains unbalanced quotes. Starts with \" but ends with single quote. at 6 [character 7 line 1]",
"Value 'test' is not surrounded by quotes at 13 [character 14 line 1]",
jeOne.getMessage());
assertEquals(
"Single quote wrap not allowed in strict mode at 2 [character 3 line 1]",

View File

@@ -94,11 +94,9 @@ public class JSONTokenerTest {
checkValid(" {} ",JSONObject.class);
checkValid("{\"a\":1}",JSONObject.class);
checkValid(" {\"a\":1} ",JSONObject.class);
// TODO: strictMode regression, needs to be fixed.
// checkValid("[]",JSONArray.class);
checkValid("[]",JSONArray.class);
checkValid(" [] ",JSONArray.class);
// TODO: strictMode regression, needs to be fixed
// checkValid("[1,2]",JSONArray.class);
checkValid("[1,2]",JSONArray.class);
checkValid("\n\n[1,2]\n\n",JSONArray.class);
// TODO: strictMode regression, needs to be fixed
// checkValid("1 2", String.class);