Skip to content

Commit 669316d

Browse files
committed
chore: write unit tests for behavior
1 parent 30a70c8 commit 669316d

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

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

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ public class JSONPointerTest {
4141

4242
private static final JSONObject document;
4343
private static final String EXPECTED_COMPLETE_DOCUMENT = "{\"\":0,\" \":7,\"g|h\":4,\"c%d\":2,\"k\\\"l\":6,\"a/b\":1,\"i\\\\j\":5," +
44-
"\"obj\":{\"\":{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some other value\"}," +
44+
"\"obj\":{\"\":{\"\":\"empty key of an object with an empty key\",\"subKey\":\"Some other value\"}," +
4545
"\"other~key\":{\"another/key\":[\"val\"]},\"key\":\"value\"},\"foo\":[\"bar\",\"baz\"],\"e^f\":3," +
46-
"\"m~n\":8}";
46+
"\"m~n\":8,\"four\\\\\\\\slashes\":\"slash test!\"}";
4747

48+
4849
static {
4950
@SuppressWarnings("resource")
5051
InputStream resourceAsStream = JSONPointerTest.class.getClassLoader().getResourceAsStream("jsonpointer-testdoc.json");
@@ -125,6 +126,17 @@ public void backslashHandling() {
125126
assertEquals(5, query("/i\\j"));
126127
}
127128

129+
/**
130+
* When creating a jsonObject we need to parse escaped characters "\\\\"
131+
* --> it's the string representation of "\\", so when query'ing via the JSONPointer
132+
* we DON'T escape them
133+
*
134+
*/
135+
@Test
136+
public void multipleBackslashHandling() {
137+
assertEquals("slash test!", query("/four\\\\slashes"));
138+
}
139+
128140
/**
129141
* We pass quotations as-is
130142
*
@@ -134,15 +146,7 @@ public void backslashHandling() {
134146
public void quotationHandling() {
135147
assertEquals(6, query("/k\"l"));
136148
}
137-
138-
/**
139-
* KD Added
140-
* */
141-
@Test
142-
public void quotationEscaping() {
143-
assertEquals(document.get("k\"l"), query("/k\\\"l"));
144-
}
145-
149+
146150
@Test
147151
public void whitespaceKey() {
148152
assertEquals(7, query("/ "));
@@ -399,31 +403,21 @@ public void optQueryFromJSONArrayUsingPointer() {
399403
}
400404

401405
/**
402-
* KD added
403-
* Coverage for JSONObject query(JSONPointer)
404-
*/
405-
@Test
406-
public void queryFromJSONObjectUsingPointer2() {
407-
String str = "{"+
408-
"\"string\\\\\\\\Key\":\"hello world!\","+
409-
"}"+
410-
"}";
411-
JSONObject jsonObject = new JSONObject(str);
412-
Object obj = jsonObject.optQuery(new JSONPointer("/string\\\\\\\\Key"));
413-
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
414-
}
415-
/**
416-
* KD added - understanding behavior
406+
* KD added - this should pass
417407
* Coverage for JSONObject query(JSONPointer)
418408
*/
419409
@Test
420410
public void queryFromJSONObjectUsingPointer0() {
421-
String str = "{"+
422-
"\"string\\\\Key\":\"hello world!\","+
423-
"}"+
424-
"}";
425-
JSONObject jsonObject = new JSONObject(str);
426-
Object obj = jsonObject.optQuery(new JSONPointer("/string\\Key"));
427-
assertTrue("Expected 'hello world!'", "hello world!".equals(obj));
411+
String str = "{"+
412+
"\"string\\\\\\\\Key\":\"hello world!\","+
413+
414+
"\"\\\\\":\"slash test\"," +
415+
"}"+
416+
"}";
417+
JSONObject jsonObject = new JSONObject(str);
418+
//Summary of issue: When a KEY in the jsonObject is "\\\\" --> it's held
419+
// as "\\" which makes it impossible to get back where expected
420+
Object obj = jsonObject.optQuery(new JSONPointer("/\\"));
421+
assertEquals("slash test", obj);
428422
}
429423
}

0 commit comments

Comments
 (0)