Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2007-06-01 16:26:19 -0400
committerpelder2007-06-01 16:26:19 -0400
commitb90ede1920bca18583181e1d713ee9d832d18d7a (patch)
tree804d7c77cc63e82d68e311b3d082013ca31c08f2 /tests/org.eclipse.jet.tests
parente2923ee699865e27c4c84415cc573cf532b8560c (diff)
downloadorg.eclipse.jet-b90ede1920bca18583181e1d713ee9d832d18d7a.tar.gz
org.eclipse.jet-b90ede1920bca18583181e1d713ee9d832d18d7a.tar.xz
org.eclipse.jet-b90ede1920bca18583181e1d713ee9d832d18d7a.zip
[190568] JET AST Visitor does not always call preVisit and postVisit
Diffstat (limited to 'tests/org.eclipse.jet.tests')
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestCompilationHelper.java163
1 files changed, 163 insertions, 0 deletions
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestCompilationHelper.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestCompilationHelper.java
new file mode 100644
index 0000000..963a59b
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TestCompilationHelper.java
@@ -0,0 +1,163 @@
+/**
+ *
+ */
+package org.eclipse.jet.tests.compiler;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRunnable;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jet.JET2Platform;
+import org.eclipse.jet.core.parser.ast.JETASTVisitor;
+import org.eclipse.jet.core.parser.ast.JETCompilationUnit;
+import org.eclipse.jet.core.parser.ast.JavaDeclaration;
+import org.eclipse.jet.core.parser.ast.JavaElement;
+import org.eclipse.jet.core.parser.ast.JavaExpression;
+import org.eclipse.jet.core.parser.ast.JavaScriptlet;
+import org.eclipse.jet.internal.compiler.CompilationHelper;
+import org.eclipse.jet.tests.FileUtils;
+import org.eclipse.jface.text.Position;
+
+/**
+ * @author pelder
+ *
+ */
+public class TestCompilationHelper extends TestCase {
+
+ private static final String TEST_PLUGIN_BASE_URL = "platform:/plugin/org.eclipse.jet.tests/data/";
+ private static final String TEST_PROJECT_NAME = TestCompilationHelper.class.getName() + " Tests";
+ protected static IProject project;
+ private CompilationHelper helper;
+
+ public static Test suite() {
+ TestSetup setup = new TestSetup(new TestSuite(TestCompilationHelper.class)) {
+ protected void setUp() throws Exception {
+ setUpAllTests();
+ }
+
+ protected void tearDown() throws Exception {
+ tearDownAllTests();
+ }
+ };
+
+ return setup;
+ }
+
+ protected static void tearDownAllTests() {
+ // TODO Auto-generated method stub
+
+ }
+
+ protected static void setUpAllTests() {
+ String newProjectModel = "<newProjectModel>" +
+ "<project" +
+ " name=\"" + TEST_PROJECT_NAME + "\"" +
+ "/>" +
+ "</newProjectModel>";
+
+ IStatus status = JET2Platform.runTransformOnString("org.eclipse.jet.transforms.newproject", newProjectModel, new NullProgressMonitor());
+ assertTrue("setUpAllTests", status.isOK());
+ try {
+ ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
+
+ public void run(IProgressMonitor monitor) throws CoreException {
+ // TODO Auto-generated method stub
+
+ }}, new NullProgressMonitor());
+ } catch (CoreException e) {
+ fail(e.getMessage());
+ }
+
+ project = ResourcesPlugin.getWorkspace().getRoot().getProject(TEST_PROJECT_NAME);
+ assertTrue("Test project exists", project.exists());
+
+ }
+
+ protected void setUp() throws Exception {
+ helper = new CompilationHelper(project);
+ }
+
+ public void testGetAST() {
+ JETCompilationUnit ast = helper.getAST("templates/main.jet");
+
+ assertNotNull(ast);
+ assertFalse(ast.hasErrors() && ast.hasWarnings());
+ }
+
+ public void testGetASTFromSource() throws MalformedURLException, IOException, CoreException {
+ String source = FileUtils.getFileContents(TEST_PLUGIN_BASE_URL + "simplifiedCompiler/templates/simple.v2.input.jet", true);
+ JETCompilationUnit ast = helper.getASTFromSource(source);
+
+ assertNotNull(ast);
+ assertFalse(ast.hasErrors() && ast.hasWarnings());
+ }
+
+ public void testJavaCode() throws MalformedURLException, IOException, CoreException {
+ String source = FileUtils.getFileContents(TEST_PLUGIN_BASE_URL + "simplifiedCompiler/templates/simple.v2.input.jet", true);
+ JETCompilationUnit ast = helper.getASTFromSource(source);
+
+ assertNotNull(ast);
+ ast.setOutputJavaClassName("_jet_simplev2input");
+ assertFalse(ast.hasErrors() && ast.hasWarnings());
+
+ String code = helper.getJavaCode(ast);
+ String expectedCode = FileUtils.getFileContents(TEST_PLUGIN_BASE_URL + "simplifiedCompiler/templates/simple.v2.input.jet.expected.java", true);
+ assertEquals(expectedCode, code);
+ }
+
+ public void testJavaCode_ast_map() throws MalformedURLException, IOException, CoreException {
+ String source = FileUtils.getFileContents(TEST_PLUGIN_BASE_URL + "alljet2.jet", true);
+ JETCompilationUnit ast = helper.getASTFromSource(source);
+
+ assertNotNull(ast);
+ assertFalse(ast.hasErrors() && ast.hasWarnings());
+
+ final Map elementPositions = new HashMap();
+ final String code = helper.getJavaCode(ast, elementPositions);
+ String expectedCode = FileUtils.getFileContents(TEST_PLUGIN_BASE_URL + "alljet2.jet.expected.java", true);
+ assertEquals(expectedCode, code);
+
+ ast.accept(new JETASTVisitor() {
+ public boolean visit(JavaDeclaration declaration) {
+ checkJavaElement(declaration);
+
+ return true;
+ }
+
+ public boolean visit(JavaExpression expression) {
+ checkJavaElement(expression);
+
+ return true;
+ }
+
+ public boolean visit(JavaScriptlet scriptlet) {
+ checkJavaElement(scriptlet);
+
+ return true;
+ }
+
+ /**
+ * @param element
+ */
+ private void checkJavaElement(JavaElement element) {
+ Position position = (Position) elementPositions.get(element);
+
+ assertEquals(element.getJavaContent(), code.substring(position.offset, position.offset + position.length));
+ }
+ });
+
+ }
+}

Back to the top