mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 03:15:32 -04:00
chore: write unit tests for behavior
This commit is contained in:
parent
30a70c8886
commit
669316d29e
@ -41,9 +41,10 @@ public class JSONPointerTest {
|
|||||||
|
|
||||||
private static final JSONObject document;
|
private static final JSONObject document;
|
||||||
private static final String EXPECTED_COMPLETE_DOCUMENT = "{\"\":0,\" \":7,\"g|h\":4,\"c%d\":2,\"k\\\"l\":6,\"a/b\":1,\"i\\\\j\":5," +
|
private static final String EXPECTED_COMPLETE_DOCUMENT = "{\"\":0,\" \":7,\"g|h\":4,\"c%d\":2,\"k\\\"l\":6,\"a/b\":1,\"i\\\\j\":5," +
|
||||||
"\"obj\":{\"\":{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some other value\"}," +
|
"\"obj\":{\"\":{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some other value\"}," +
|
||||||
"\"other~key\":{\"another/key\":[\"val\"]},\"key\":\"value\"},\"foo\":[\"bar\",\"baz\"],\"e^f\":3," +
|
"\"other~key\":{\"another/key\":[\"val\"]},\"key\":\"value\"},\"foo\":[\"bar\",\"baz\"],\"e^f\":3," +
|
||||||
"\"m~n\":8}";
|
"\"m~n\":8,\"four\\\\\\\\slashes\":\"slash test!\"}";
|
||||||
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
@SuppressWarnings("resource")
|
@SuppressWarnings("resource")
|
||||||
@ -125,6 +126,17 @@ public class JSONPointerTest {
|
|||||||
assertEquals(5, query("/i\\j"));
|
assertEquals(5, query("/i\\j"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When creating a jsonObject we need to parse escaped characters "\\\\"
|
||||||
|
* --> it's the string representation of "\\", so when query'ing via the JSONPointer
|
||||||
|
* we DON'T escape them
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void multipleBackslashHandling() {
|
||||||
|
assertEquals("slash test!", query("/four\\\\slashes"));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We pass quotations as-is
|
* We pass quotations as-is
|
||||||
*
|
*
|
||||||
@ -135,14 +147,6 @@ public class JSONPointerTest {
|
|||||||
assertEquals(6, query("/k\"l"));
|
assertEquals(6, query("/k\"l"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* KD Added
|
|
||||||
* */
|
|
||||||
@Test
|
|
||||||
public void quotationEscaping() {
|
|
||||||
assertEquals(document.get("k\"l"), query("/k\\\"l"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whitespaceKey() {
|
public void whitespaceKey() {
|
||||||
assertEquals(7, query("/ "));
|
assertEquals(7, query("/ "));
|
||||||
@ -399,31 +403,21 @@ public class JSONPointerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* KD added
|
* KD added - this should pass
|
||||||
* Coverage for JSONObject query(JSONPointer)
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void queryFromJSONObjectUsingPointer2() {
|
|
||||||
String str = "{"+
|
|
||||||
"\"string\\\\\\\\Key\":\"hello world!\","+
|
|
||||||
"}"+
|
|
||||||
"}";
|
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
|
||||||
Object obj = jsonObject.optQuery(new JSONPointer("/string\\\\\\\\Key"));
|
|
||||||
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* KD added - understanding behavior
|
|
||||||
* Coverage for JSONObject query(JSONPointer)
|
* Coverage for JSONObject query(JSONPointer)
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void queryFromJSONObjectUsingPointer0() {
|
public void queryFromJSONObjectUsingPointer0() {
|
||||||
String str = "{"+
|
String str = "{"+
|
||||||
"\"string\\\\Key\":\"hello world!\","+
|
"\"string\\\\\\\\Key\":\"hello world!\","+
|
||||||
"}"+
|
|
||||||
"}";
|
"\"\\\\\":\"slash test\"," +
|
||||||
JSONObject jsonObject = new JSONObject(str);
|
"}"+
|
||||||
Object obj = jsonObject.optQuery(new JSONPointer("/string\\Key"));
|
"}";
|
||||||
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
|
JSONObject jsonObject = new JSONObject(str);
|
||||||
|
//Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held
|
||||||
|
// as "\\" which makes it impossible to get back where expected
|
||||||
|
Object obj = jsonObject.optQuery(new JSONPointer("/\\"));
|
||||||
|
assertEquals("slash test", obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user