# HG changeset patch # User Jaroslav Tulach # Date 1433321775 -7200 # Node ID c8418635b575bfa46117f2299ebaacec9c90c92a # Parent ed234a3178af8422526f2efb6409632f5dc0cf90# Parent 99588c43c4b88dec50575222b7c9ab15be48bbef Verifying behavior of TruffleLanguage when it faces invalid source code. diff -r ed234a3178af -r c8418635b575 graal/com.oracle.graal.test/src/com/oracle/graal/test/GraalJUnitCore.java --- a/graal/com.oracle.graal.test/src/com/oracle/graal/test/GraalJUnitCore.java Wed Jun 03 10:17:19 2015 +0200 +++ b/graal/com.oracle.graal.test/src/com/oracle/graal/test/GraalJUnitCore.java Wed Jun 03 10:56:15 2015 +0200 @@ -23,6 +23,7 @@ package com.oracle.graal.test; import java.io.*; +import java.lang.reflect.Modifier; import java.nio.file.*; import java.util.*; @@ -104,7 +105,10 @@ } } try { - classes.add(Class.forName(each, false, GraalJUnitCore.class.getClassLoader())); + Class cls = Class.forName(each, false, GraalJUnitCore.class.getClassLoader()); + if ((cls.getModifiers() & Modifier.ABSTRACT) == 0) { + classes.add(cls); + } } catch (ClassNotFoundException e) { system.out().println("Could not find class: " + each); Description description = Description.createSuiteDescription(each); diff -r ed234a3178af -r c8418635b575 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/TruffleTCK.java --- a/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/TruffleTCK.java Wed Jun 03 10:17:19 2015 +0200 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/TruffleTCK.java Wed Jun 03 10:56:15 2015 +0200 @@ -22,22 +22,21 @@ */ package com.oracle.truffle.api.test.vm; -import java.util.*; - -import org.junit.*; - -import com.oracle.truffle.api.vm.*; +import com.oracle.truffle.api.vm.TruffleVM; +import java.io.IOException; +import java.util.Random; import static org.junit.Assert.*; +import org.junit.Test; /** * A collection of tests that can certify language implementaiton to be complient with most recent * requirements of the Truffle infrastructure and tooling. Subclass, implement abstract methods and * include in your test suite. */ -public class TruffleTCK { // abstract +public abstract class TruffleTCK { private TruffleVM tckVM; - public TruffleTCK() { // protected + protected TruffleTCK() { } /** @@ -45,15 +44,22 @@ * your language up, so it is ready for testing. * {@link TruffleVM#eval(java.lang.String, java.lang.String) Execute} any scripts you need, and * prepare global symbols with proper names. The symbols will then be looked up by the - * infastructure (using the names provided by you from methods like {@link #plusInt()}) and used - * for internal testing. + * infrastructure (using the names provided by you from methods like {@link #plusInt()}) and + * used for internal testing. * * @return initialized Truffle virtual machine * @throws java.lang.Exception thrown when the VM preparation fails */ - protected TruffleVM prepareVM() throws Exception { // abstract - return null; - } + protected abstract TruffleVM prepareVM() throws Exception; + + /** + * Mimetype associated with your language. The mimetype will be passed to + * {@link TruffleVM#eval(java.lang.String, java.lang.String)} method of the {@link #prepareVM() + * created TruffleVM}. + * + * @return mime type of the tested language + */ + protected abstract String mimeType(); /** * Name of function which will return value 42 as a number. The return value of the method @@ -62,9 +68,7 @@ * * @return name of globally exported symbol */ - protected String fourtyTwo() { // abstract - return null; - } + protected abstract String fourtyTwo(); /** * Name of a function that returns null. Truffle languages are encouraged to have @@ -74,9 +78,7 @@ * * @return name of globally exported symbol */ - protected String returnsNull() { // abstract - return null; - } + protected abstract String returnsNull(); /** * Name of function to add two integer values together. The symbol will be invoked with two @@ -85,9 +87,16 @@ * * @return name of globally exported symbol */ - protected String plusInt() { // abstract - return null; - } + protected abstract String plusInt(); + + /** + * Return a code snippet that is invalid in your language. Its + * {@link TruffleVM#eval(java.lang.String, java.lang.String) evaluation} should fail and yield + * an exception. + * + * @return code snippet invalid in the tested language + */ + protected abstract String invalidCode(); private TruffleVM vm() throws Exception { if (tckVM == null) { @@ -102,9 +111,6 @@ @Test public void testFortyTwo() throws Exception { - if (getClass() == TruffleTCK.class) { - return; - } TruffleVM.Symbol fourtyTwo = findGlobalSymbol(fourtyTwo()); Object res = fourtyTwo.invoke(null); @@ -130,9 +136,6 @@ @Test public void testPlusWithInts() throws Exception { - if (getClass() == TruffleTCK.class) { - return; - } Random r = new Random(); int a = r.nextInt(100); int b = r.nextInt(100); @@ -148,6 +151,14 @@ assert a + b == n.intValue() : "The value is correct: (" + a + " + " + b + ") = " + n.intValue(); } + @Test(expected = IOException.class) + public void testInvalidTestMethod() throws Exception { + String mime = mimeType(); + String code = invalidCode(); + Object ret = vm().eval(mime, code); + fail("Should yield IOException, but returned " + ret); + } + private TruffleVM.Symbol findGlobalSymbol(String name) throws Exception { TruffleVM.Symbol s = vm().findGlobalSymbol(name); assert s != null : "Symbol " + name + " is not found!"; diff -r ed234a3178af -r c8418635b575 graal/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java --- a/graal/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java Wed Jun 03 10:17:19 2015 +0200 +++ b/graal/com.oracle.truffle.sl.test/src/com/oracle/truffle/sl/test/SLTckTest.java Wed Jun 03 10:56:15 2015 +0200 @@ -57,6 +57,11 @@ } @Override + protected String mimeType() { + return "application/x-sl"; + } + + @Override protected String fourtyTwo() { return "fourtyTwo"; } @@ -70,4 +75,14 @@ protected String returnsNull() { return "null"; } + + @Override + protected String invalidCode() { + // @formatter:off + return + "f unction main() {\n" + + " retu rn 42;\n" + + "}\n"; + // @formatter:on + } } diff -r ed234a3178af -r c8418635b575 graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLMain.java --- a/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLMain.java Wed Jun 03 10:17:19 2015 +0200 +++ b/graal/com.oracle.truffle.sl/src/com/oracle/truffle/sl/SLMain.java Wed Jun 03 10:56:15 2015 +0200 @@ -359,7 +359,11 @@ @Override protected Object eval(Source code) throws IOException { - context.executeMain(code); + try { + context.executeMain(code); + } catch (Exception e) { + throw new IOException(e); + } return null; }