package com.oracle.truffle.js.test.builtins;
import static org.junit.Assert.fail;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.Source;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import com.oracle.truffle.js.lang.JavaScriptLanguage;
import com.oracle.truffle.js.runtime.JSContextOptions;
import com.oracle.truffle.js.test.JSTest;
@RunWith(Parameterized.class)
public class PropertyStackTraceTest {
@Parameters(name = "{0}")
public static List<Boolean> data() {
return Arrays.asList(Boolean.TRUE, Boolean.FALSE);
}
@Parameter(value = 0) public boolean propertyCaches;
private Context context;
@Before
public void before() {
Context.Builder contextBuilder = JSTest.newContextBuilder();
contextBuilder.allowHostAccess(HostAccess.ALL);
contextBuilder.option(JSContextOptions.DEBUG_BUILTIN_NAME, Boolean.toString(true));
if (!propertyCaches) {
contextBuilder.option(JSContextOptions.PROPERTY_CACHE_LIMIT_NAME, String.valueOf(0));
}
context = contextBuilder.build();
}
@After
public void dispose() {
context.close();
}
@Test
public void testGetterErrorLocation() {
try {
context.eval(Source.newBuilder(JavaScriptLanguage.ID, "var O = {get P(){ throw new Error('expected'); }};\nO.P;", "SOURCE").buildLiteral());
fail();
} catch (PolyglotException ex) {
verifyStackTrace(ex,
"<js> get P\\(SOURCE:1:.*\\)",
"<js> :program\\(SOURCE:2:.*\\)");
}
}
@Test
public void testSetterErrorLocation() {
try {
context.eval(Source.newBuilder(JavaScriptLanguage.ID, "var O = {set P(V){ throw new Error('expected'); }};\nO.P = 42;", "SOURCE").buildLiteral());
fail();
} catch (PolyglotException ex) {
verifyStackTrace(ex,
"<js> set P\\(SOURCE:1:.*\\)",
"<js> :program\\(SOURCE:2:.*\\)");
}
}
@Test
public void testProxyGetErrorLocation() {
try {
context.eval(Source.newBuilder(JavaScriptLanguage.ID, "var O = new Proxy({}, {get(){ throw new Error('expected'); }});\nO.P;", "SOURCE").buildLiteral());
fail();
} catch (PolyglotException ex) {
verifyStackTrace(ex,
"<js> get\\(SOURCE:1:.*\\)",
"<js> :program\\(SOURCE:2:.*\\)");
}
}
@Test
public void testProxySetErrorLocation() {
try {
context.eval(Source.newBuilder(JavaScriptLanguage.ID, "var O = new Proxy({}, {set(){ throw new Error('expected'); }});\nO.P = 42;", "SOURCE").buildLiteral());
fail();
} catch (PolyglotException ex) {
verifyStackTrace(ex,
"<js> set\\(SOURCE:1:.*\\)",
"<js> :program\\(SOURCE:2:.*\\)");
}
}
@Test
public void testProxyHasErrorLocation() {
try {
context.eval(Source.newBuilder(JavaScriptLanguage.ID, "var O = new Proxy({}, {has(){ throw new Error('expected'); }});\n'P' in O;", "SOURCE").buildLiteral());
fail();
} catch (PolyglotException ex) {
verifyStackTrace(ex,
"<js> has\\(SOURCE:1:.*\\)",
"<js> :program\\(SOURCE:2:.*\\)");
}
}
static void verifyStackTrace(PolyglotException ex, String... patterns) {
StringWriter buffer = new StringWriter();
try (PrintWriter out = new PrintWriter(buffer)) {
ex.printStackTrace(out);
}
String[] lines = Arrays.stream(buffer.toString().split(System.lineSeparator())).map(String::trim).filter(l -> l.startsWith("at ")).map(l -> l.substring(3)).toArray(String[]::new);
Assert.assertTrue("Not enough lines " + Arrays.toString(lines), patterns.length <= lines.length);
for (int i = 0; i < patterns.length; i++) {
String line = lines[i];
Pattern pattern = Pattern.compile(patterns[i]);
Assert.assertTrue("Expected " + patterns[i] + " but got " + line, pattern.matcher(line).matches());
}
}
}