|
24 | 24 | */ |
25 | 25 | package com.oracle.truffle.tools.chromeinspector.test; |
26 | 26 |
|
| 27 | +import static org.junit.Assert.assertFalse; |
27 | 28 | import static org.junit.Assert.assertTrue; |
28 | 29 |
|
29 | 30 | import com.oracle.truffle.api.interop.InteropException; |
30 | 31 | import com.oracle.truffle.api.interop.InteropLibrary; |
31 | 32 | import com.oracle.truffle.tools.chromeinspector.objects.JSONTruffleArray; |
| 33 | +import com.oracle.truffle.tools.chromeinspector.objects.JSONTruffleObject; |
32 | 34 | import org.graalvm.shadowed.org.json.JSONArray; |
| 35 | +import org.graalvm.shadowed.org.json.JSONObject; |
33 | 36 |
|
34 | 37 | import org.junit.Test; |
35 | 38 |
|
@@ -61,4 +64,43 @@ public void testJSONTruffleArraySort() throws InteropException { |
61 | 64 | } |
62 | 65 | } |
63 | 66 |
|
| 67 | + private static void testIdentity(Object theObject, Object sameObject, Object differentObject) { |
| 68 | + InteropLibrary interop = InteropLibrary.getUncached(theObject); |
| 69 | + assertTrue(interop.hasIdentity(theObject)); |
| 70 | + assertTrue(interop.isIdentical(theObject, sameObject, interop)); |
| 71 | + assertFalse(interop.isIdentical(theObject, differentObject, interop)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testJSONTruffleArrayIdentity() { |
| 76 | + String json = "[42, 211]"; |
| 77 | + JSONArray jsonArray = new JSONArray(json); |
| 78 | + Object array = new JSONTruffleArray(jsonArray); |
| 79 | + Object sameArray = new JSONTruffleArray(jsonArray); |
| 80 | + Object anotherArray = new JSONTruffleArray(new JSONArray(json)); |
| 81 | + testIdentity(array, sameArray, anotherArray); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testJSONTruffleObjectIdentity() { |
| 86 | + String json = "{ answer: 42, question: '?' }"; |
| 87 | + JSONObject jsonObject = new JSONObject(json); |
| 88 | + JSONTruffleObject object = new JSONTruffleObject(jsonObject); |
| 89 | + Object sameObject = new JSONTruffleObject(jsonObject); |
| 90 | + Object anotherObject = new JSONTruffleObject(new JSONObject(json)); |
| 91 | + testIdentity(object, sameObject, anotherObject); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testJSONKeysIdentity() throws InteropException { |
| 96 | + String json = "{ answer: 42, question: '?' }"; |
| 97 | + JSONObject jsonObject = new JSONObject(json); |
| 98 | + JSONTruffleObject truffleObject = new JSONTruffleObject(jsonObject); |
| 99 | + InteropLibrary interop = InteropLibrary.getUncached(); |
| 100 | + Object keys = interop.getMembers(truffleObject); |
| 101 | + Object sameKeys = interop.getMembers(truffleObject); |
| 102 | + Object otherKeys = interop.getMembers(new JSONTruffleObject(jsonObject)); |
| 103 | + testIdentity(keys, sameKeys, otherKeys); |
| 104 | + } |
| 105 | + |
64 | 106 | } |
0 commit comments