package com.oracle.truffle.js.test.interop;
import static com.oracle.truffle.js.lang.JavaScriptLanguage.ID;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import org.graalvm.polyglot.Context;
import org.junit.Test;
import com.oracle.truffle.js.test.JSTest;
import com.oracle.truffle.js.test.polyglot.ForeignBoxedObject;
import com.oracle.truffle.js.test.polyglot.ForeignNull;
public class ForeignBoxedObjectTest {
@Test
public void testForeignNull() {
try (Context context = JSTest.newContextBuilder().build()) {
context.getBindings("js").putMember("obj", new ForeignNull());
assertTrue(context.eval(ID, "Object.getPrototypeOf(Object(obj)) === Object.prototype").asBoolean());
assertTrue(context.eval(ID, "Object.getPrototypeOf(new Object(obj)) === Object.prototype").asBoolean());
assertTrue(context.eval(ID, "Object.getPrototypeOf(Object.create(obj)) === null").asBoolean());
assertTrue(context.eval(ID, "try { obj.foo; false; } catch (e) { e instanceof TypeError }").asBoolean());
assertTrue(context.eval(ID, "try { obj.foo(); false; } catch (e) { e instanceof TypeError }").asBoolean());
}
}
@Test
public void testForeignBoxedString() {
try (Context context = JSTest.newContextBuilder().build()) {
context.getBindings("js").putMember("obj", ForeignBoxedObject.createNew("foo"));
assertTrue(context.eval(ID, "typeof Object(obj) === 'object'").asBoolean());
assertTrue(context.eval(ID, "typeof obj.includes === 'function'").asBoolean());
assertEquals("foo", context.eval(ID, "obj.toString()").asString());
assertEquals("foo", context.eval(ID, "obj.valueOf()").asString());
assertTrue(context.eval(ID, "obj.includes('o')").asBoolean());
}
}
@Test
public void testForeignBoxedNumber() {
try (Context context = JSTest.newContextBuilder().build()) {
context.getBindings("js").putMember("obj", ForeignBoxedObject.createNew(42));
assertTrue(context.eval(ID, "typeof Object(obj) === 'object'").asBoolean());
assertTrue(context.eval(ID, "typeof obj.valueOf === 'function'").asBoolean());
assertEquals("4.2e+1", context.eval(ID, "obj.toExponential()").asString());
assertEquals("4e+1", context.eval(ID, "obj.toExponential(0)").asString());
assertEquals("42.00", context.eval(ID, "obj.toFixed(2)").asString());
assertEquals("42", context.eval(ID, "obj.toLocaleString()").asString());
assertEquals("42", context.eval(ID, "obj.toPrecision()").asString());
assertEquals("42.0", context.eval(ID, "obj.toPrecision(3)").asString());
assertEquals("42", context.eval(ID, "obj.toString()").asString());
assertEquals(42, context.eval(ID, "obj.valueOf()").asInt());
}
}
@Test
public void testForeignBoxedBoolean() {
try (Context context = JSTest.newContextBuilder().build()) {
context.getBindings("js").putMember("obj", ForeignBoxedObject.createNew(true));
assertTrue(context.eval(ID, "typeof Object(obj) === 'object'").asBoolean());
assertTrue(context.eval(ID, "typeof obj.valueOf === 'function'").asBoolean());
assertTrue(context.eval(ID, "obj.valueOf()").asBoolean());
assertEquals("true", context.eval(ID, "obj.toString()").asString());
}
}
}