Search in sources :

Example 1 with ExitState

use of org.eclipse.ceylon.compiler.java.launcher.Main.ExitState in project ceylon by eclipse.

the class CompilerTests method compareWithJavaSourceWithLines.

protected void compareWithJavaSourceWithLines(String name) {
    // make a compiler task
    // FIXME: runFileManager.setSourcePath(dir);
    CeyloncTaskImpl task = getCompilerTask(new String[] { name + ".ceylon" });
    // grab the CU after we've completed it
    class Listener implements TaskListener {

        JCCompilationUnit compilationUnit;

        private String compilerSrc;

        private JavaPositionsRetriever javaPositionsRetriever = new JavaPositionsRetriever();

        @Override
        public void started(TaskEvent e) {
        }

        @Override
        public void finished(TaskEvent e) {
            if (e.getKind() == Kind.ENTER) {
                if (compilationUnit == null) {
                    compilationUnit = (JCCompilationUnit) e.getCompilationUnit();
                    // for some reason compilationUnit is full here in the listener, but empty as soon
                    // as the compile task is done. probably to clean up for the gc?
                    javaPositionsRetriever.retrieve(compilationUnit);
                    compilerSrc = normalizeLineEndings(javaPositionsRetriever.getJavaSourceCodeWithCeylonLines());
                    AbstractTransformer.trackNodePositions(null);
                }
            }
        }
    }
    Listener listener = new Listener();
    task.setTaskListener(listener);
    // now compile it all the way
    ExitState exitState = task.call2();
    Assert.assertEquals("Compilation failed", exitState.ceylonState, CeylonState.OK);
    // now look at what we expected
    String expectedSrc = normalizeLineEndings(readFile(new File(getPackagePath(), name + ".src"))).trim();
    String compiledSrc = listener.compilerSrc.trim();
    Assert.assertEquals("Source code differs", expectedSrc, compiledSrc);
}
Also used : JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) DiagnosticListener(org.eclipse.ceylon.javax.tools.DiagnosticListener) ExitState(org.eclipse.ceylon.compiler.java.launcher.Main.ExitState) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) CeyloncTaskImpl(org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl) JavaPositionsRetriever(org.eclipse.ceylon.compiler.java.codegen.JavaPositionsRetriever) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with ExitState

use of org.eclipse.ceylon.compiler.java.launcher.Main.ExitState in project ceylon by eclipse.

the class CompilerTests method compareWithJavaSourceWithPositions.

protected void compareWithJavaSourceWithPositions(String name) {
    // make a compiler task
    // FIXME: runFileManager.setSourcePath(dir);
    CeyloncTaskImpl task = getCompilerTask(new String[] { name + ".ceylon" });
    // grab the CU after we've completed it
    class Listener implements TaskListener {

        JCCompilationUnit compilationUnit;

        private String compilerSrc;

        private JavaPositionsRetriever javaPositionsRetriever = new JavaPositionsRetriever();

        @Override
        public void started(TaskEvent e) {
            AbstractTransformer.trackNodePositions(javaPositionsRetriever);
        }

        @Override
        public void finished(TaskEvent e) {
            if (e.getKind() == Kind.ENTER) {
                if (compilationUnit == null) {
                    compilationUnit = (JCCompilationUnit) e.getCompilationUnit();
                    // for some reason compilationUnit is full here in the listener, but empty as soon
                    // as the compile task is done. probably to clean up for the gc?
                    javaPositionsRetriever.retrieve(compilationUnit);
                    compilerSrc = normalizeLineEndings(javaPositionsRetriever.getJavaSourceCodeWithCeylonPositions());
                    AbstractTransformer.trackNodePositions(null);
                }
            }
        }
    }
    Listener listener = new Listener();
    task.setTaskListener(listener);
    // now compile it all the way
    ExitState exitState = task.call2();
    Assert.assertEquals("Compilation failed", CeylonState.OK, exitState.ceylonState);
    // now look at what we expected
    String expectedSrc = normalizeLineEndings(readFile(new File(getPackagePath(), name + ".src"))).trim();
    String compiledSrc = listener.compilerSrc.trim();
    Assert.assertEquals("Source code differs", expectedSrc, compiledSrc);
}
Also used : JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) DiagnosticListener(org.eclipse.ceylon.javax.tools.DiagnosticListener) ExitState(org.eclipse.ceylon.compiler.java.launcher.Main.ExitState) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) CeyloncTaskImpl(org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl) JavaPositionsRetriever(org.eclipse.ceylon.compiler.java.codegen.JavaPositionsRetriever) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 3 with ExitState

use of org.eclipse.ceylon.compiler.java.launcher.Main.ExitState in project ceylon by eclipse.

the class CompilerTests method compileErrorTest.

protected ErrorCollector compileErrorTest(String[] ceylonFiles, List<String> options, Throwable expectedException, boolean includeWarnings, boolean expectedToFail) {
    // make a compiler task
    // FIXME: runFileManager.setSourcePath(dir);
    ErrorCollector collector = new ErrorCollector();
    CeyloncTaskImpl task = getCompilerTask(options, collector, ceylonFiles);
    // now compile it all the way
    Throwable ex = null;
    ExitState exitState = task.call2();
    switch(exitState.ceylonState) {
        case OK:
            if (expectedToFail) {
                Assert.fail("Compilation successful (it should have failed!)");
            }
            break;
        case BUG:
            if (expectedException == null) {
                throw new AssertionError("Compiler bug", exitState.abortingException);
            }
            ex = exitState.abortingException;
            break;
        case ERROR:
            if (!expectedToFail) {
                Assert.fail("Compilation failed (it should have been successful!)");
            }
            break;
        case SYS:
            Assert.fail("System error");
            break;
        default:
            Assert.fail("Unknown exit state");
    }
    if (ex != null) {
        if (expectedException == null) {
            throw new AssertionError("Compilation terminated with unexpected error", ex);
        } else if (expectedException.getClass() != ex.getClass() || !eq(expectedException.getMessage(), ex.getMessage())) {
            throw new AssertionError("Compilation terminated with a different error than the expected " + expectedException, ex);
        }
    } else if (expectedException != null) {
        Assert.fail("Expected compiler exception " + expectedException);
    }
    return collector;
}
Also used : ExitState(org.eclipse.ceylon.compiler.java.launcher.Main.ExitState) CeyloncTaskImpl(org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl)

Example 4 with ExitState

use of org.eclipse.ceylon.compiler.java.launcher.Main.ExitState in project ceylon by eclipse.

the class CompilerTests method compileIgnoringErrors.

protected void compileIgnoringErrors(ExpectedError expectedErrors, String... ceylon) {
    ErrorCollector c = new ErrorCollector();
    ExitState exitState = getCompilerTask(c, ceylon).call2();
    assert (exitState.javacExitCode.exitCode == Main.EXIT_ERROR);
    assert (exitState.ceylonState == CeylonState.ERROR);
    TreeSet<CompilerError> actualErrors = c.get(Diagnostic.Kind.ERROR);
    compareErrors(actualErrors, expectedErrors);
}
Also used : ExitState(org.eclipse.ceylon.compiler.java.launcher.Main.ExitState)

Example 5 with ExitState

use of org.eclipse.ceylon.compiler.java.launcher.Main.ExitState in project ceylon by eclipse.

the class IssuesTests_1500_1999 method testBug1830.

@Test
public void testBug1830() {
    ErrorCollector collector = new ErrorCollector();
    CeyloncTaskImpl task = getCompilerTask(defaultOptions, collector, "bug18xx/Bug1830.ceylon");
    ExitState call2 = task.call2();
    Assert.assertEquals(CeylonState.ERROR, call2.ceylonState);
    Assert.assertEquals(Main.EXIT_ERROR, call2.javacExitCode.exitCode);
}
Also used : ExitState(org.eclipse.ceylon.compiler.java.launcher.Main.ExitState) ErrorCollector(org.eclipse.ceylon.compiler.java.test.ErrorCollector) CeyloncTaskImpl(org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl) Test(org.junit.Test)

Aggregations

ExitState (org.eclipse.ceylon.compiler.java.launcher.Main.ExitState)11 CeyloncTaskImpl (org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl)7 Test (org.junit.Test)5 ErrorCollector (org.eclipse.ceylon.compiler.java.test.ErrorCollector)4 File (java.io.File)2 ZipFile (java.util.zip.ZipFile)2 JavaPositionsRetriever (org.eclipse.ceylon.compiler.java.codegen.JavaPositionsRetriever)2 CompilerError (org.eclipse.ceylon.compiler.java.test.CompilerError)2 DiagnosticListener (org.eclipse.ceylon.javax.tools.DiagnosticListener)2 TaskEvent (org.eclipse.ceylon.langtools.source.util.TaskEvent)2 TaskListener (org.eclipse.ceylon.langtools.source.util.TaskListener)2 JCCompilationUnit (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit)2 Writer (java.io.Writer)1 TreeSet (java.util.TreeSet)1 CeyloncTool (org.eclipse.ceylon.compiler.java.tools.CeyloncTool)1 JavacFileManager (org.eclipse.ceylon.langtools.tools.javac.file.JavacFileManager)1