package com.oracle.truffle.js.test.runtime;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.js.runtime.JSContext;
import com.oracle.truffle.js.runtime.builtins.JSOrdinary;
import com.oracle.truffle.js.runtime.objects.JSObject;
import com.oracle.truffle.js.test.JSTest;
public class JSObjectTest extends JSTest {
@Override
public void setup() {
super.setup();
testHelper.enterContext();
}
@Override
public void close() {
testHelper.leaveContext();
super.close();
}
@Test
public void testSetGet() {
JSContext context = testHelper.getJSContext();
DynamicObject obj = JSOrdinary.create(context);
JSObject.set(obj, "x", 10);
assertEquals(10, JSObject.get(obj, "x"));
JSObject.set(obj, "y", 20);
assertEquals(10, JSObject.get(obj, "x"));
assertEquals(20, JSObject.get(obj, "y"));
}
@Test
public void testRemove() {
JSContext context = testHelper.getJSContext();
DynamicObject obj = JSOrdinary.create(context);
JSObject.set(obj, "x", 10);
JSObject.set(obj, "y", 20);
assertEquals(10, JSObject.get(obj, "x"));
assertEquals(20, JSObject.get(obj, "y"));
assertEquals(2, JSObject.ownPropertyKeys(obj).size());
JSObject.delete(obj, "x");
assertEquals(20, JSObject.get(obj, "y"));
assertEquals(1, JSObject.ownPropertyKeys(obj).size());
assertEquals("y", JSObject.ownPropertyKeys(obj).get(0));
assertEquals(false, JSObject.hasProperty(obj, "x"));
}
@Test
public void testRemove2() {
JSContext context = testHelper.getJSContext();
DynamicObject obj = JSOrdinary.create(context);
JSObject.set(obj, "x", 10);
JSObject.set(obj, "y", 20);
assertEquals(10, JSObject.get(obj, "x"));
assertEquals(20, JSObject.get(obj, "y"));
assertEquals(2, JSObject.ownPropertyKeys(obj).size());
JSObject.delete(obj, "x");
assertEquals(20, JSObject.get(obj, "y"));
assertEquals(1, JSObject.ownPropertyKeys(obj).size());
assertEquals("y", JSObject.ownPropertyKeys(obj).get(0));
assertEquals(false, JSObject.hasProperty(obj, "x"));
JSObject.set(obj, "x", 11);
assertEquals(11, JSObject.get(obj, "x"));
assertEquals(20, JSObject.get(obj, "y"));
assertEquals(2, JSObject.ownPropertyKeys(obj).size());
assertTrue(JSObject.ownPropertyKeys(obj).contains("x") && JSObject.ownPropertyKeys(obj).contains("y"));
assertEquals(true, JSObject.hasProperty(obj, "x"));
assertEquals(true, JSObject.hasProperty(obj, "y"));
JSObject.delete(obj, "x");
JSObject.delete(obj, "x");
JSObject.set(obj, "x", 12);
JSObject.delete(obj, "y");
JSObject.set(obj, "z", 13);
JSObject.set(obj, "y", 21);
assertEquals(3, JSObject.ownPropertyKeys(obj).size());
}
@Test
public void propertyTest() {
JSContext context = testHelper.getJSContext();
DynamicObject po = JSOrdinary.create(context);
for (int i = 0; i < 10000; i++) {
JSObject.set(po, String.valueOf(i), i);
}
boolean ok = true;
for (int i = 0; i < 10000; i++) {
ok = ok && (i == (Integer) JSObject.get(po, String.valueOf(i)));
}
assertTrue(ok);
}
}