Search in sources :

Example 1 with CeyloncTaskImpl

use of org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl 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 CeyloncTaskImpl

use of org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl in project ceylon by eclipse.

the class CompilerTests method compareWithJavaSource.

protected void compareWithJavaSource(List<String> options, final int index, String java, String... ceylon) {
    // make a compiler task
    // FIXME: runFileManager.setSourcePath(dir);
    ErrorCollector collector = new ErrorCollector();
    CeyloncTaskImpl task = getCompilerTask(options, collector, ceylon);
    // grab the CU after we've completed it
    class Listener implements TaskListener {

        JCCompilationUnit compilationUnit;

        private String compilerSrc;

        int count = 0;

        @Override
        public void started(TaskEvent e) {
        }

        @Override
        public void finished(TaskEvent e) {
            if (e.getKind() == Kind.ENTER) {
                if (compilationUnit == null && index == count++) {
                    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?
                    compilerSrc = normalizeLineEndings(compilationUnit.toString());
                }
            }
        }
    }
    Listener listener = new Listener();
    task.setTaskListener(listener);
    // now compile it all the way
    assertCompilesOk(collector, task.call2());
    // now look at what we expected
    File expectedSrcFile = new File(getPackagePath(), java);
    String expectedSrc = normalizeLineEndings(readFile(expectedSrcFile)).trim();
    String compiledSrc = listener.compilerSrc.trim();
    // THIS IS FOR INTERNAL USE ONLY!!!
    // Can be used to do batch updating of known correct tests
    // Uncomment only when you know what you're doing!
    // if (expectedSrc != null && compiledSrc != null && !expectedSrc.equals(compiledSrc)) {
    // writeFile(expectedSrcFile, compiledSrc);
    // expectedSrc = compiledSrc;
    // }
    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) TaskEvent(org.eclipse.ceylon.langtools.source.util.TaskEvent) TaskListener(org.eclipse.ceylon.langtools.source.util.TaskListener) CeyloncTaskImpl(org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 3 with CeyloncTaskImpl

use of org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl 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 4 with CeyloncTaskImpl

use of org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl 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 5 with CeyloncTaskImpl

use of org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl in project ceylon by eclipse.

the class CarGenerationTests method testCarResourceFilesSub.

private void testCarResourceFilesSub(boolean alternative) throws IOException {
    List<String> options = new LinkedList<String>();
    options.add("-src");
    options.add(getPackagePath() + "resmodules/files/source");
    options.add("-res");
    options.add(getPackagePath() + "resmodules/files/resource");
    options.addAll(defaultOptions);
    CeyloncTaskImpl task;
    if (alternative) {
        task = getCompilerTask(options, // "resmodules/files/source/test/files/module.ceylon",
        "resmodules/files/resource/test/files/extrafile");
    } else {
        task = getCompilerTask(options, "resmodules/files/source/test/files/module.ceylon", "resmodules/files/resource/test/files/README.txt");
    }
    Boolean ret = task.call();
    assertTrue(ret);
    File carFile = getModuleArchive("test.files", "1.0");
    assertTrue(carFile.exists());
    JarFile car = new JarFile(carFile);
    ZipEntry moduleClass = car.getEntry("test/files/README.txt");
    assertNotNull(moduleClass);
    moduleClass = car.getEntry("test/files/extrafile");
    if (alternative) {
        assertNotNull(moduleClass);
    } else {
        assertNull(moduleClass);
    }
    moduleClass = car.getEntry("test/files/$module_.class");
    assertNotNull(moduleClass);
    car.close();
}
Also used : ZipEntry(java.util.zip.ZipEntry) CeyloncTaskImpl(org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) LinkedList(java.util.LinkedList)

Aggregations

CeyloncTaskImpl (org.eclipse.ceylon.compiler.java.tools.CeyloncTaskImpl)62 Test (org.junit.Test)51 File (java.io.File)34 JarFile (java.util.jar.JarFile)26 LinkedList (java.util.LinkedList)23 ErrorCollector (org.eclipse.ceylon.compiler.java.test.ErrorCollector)23 ZipEntry (java.util.zip.ZipEntry)13 ZipFile (java.util.zip.ZipFile)13 CompilerError (org.eclipse.ceylon.compiler.java.test.CompilerError)12 ExitState (org.eclipse.ceylon.compiler.java.launcher.Main.ExitState)7 CeyloncTool (org.eclipse.ceylon.compiler.java.tools.CeyloncTool)6 ArrayList (java.util.ArrayList)5 CeyloncFileManager (org.eclipse.ceylon.compiler.java.tools.CeyloncFileManager)5 TaskEvent (org.eclipse.ceylon.langtools.source.util.TaskEvent)4 TaskListener (org.eclipse.ceylon.langtools.source.util.TaskListener)4 Set (java.util.Set)3 DiagnosticListener (org.eclipse.ceylon.javax.tools.DiagnosticListener)3 JCCompilationUnit (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit)3 Module (org.eclipse.ceylon.model.typechecker.model.Module)3 Ignore (org.junit.Ignore)3