summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2006-07-31 13:22:23 (EDT)
committerpelder2006-07-31 13:22:23 (EDT)
commite3771612fe41efc4fac8b720437a283ac2aca7be (patch)
treeda25673a9bd9582559635c438ae9ad547b796502
parente79f2b3ac9d49a11c928f395888148668c642702 (diff)
downloadorg.eclipse.jet-e3771612fe41efc4fac8b720437a283ac2aca7be.zip
org.eclipse.jet-e3771612fe41efc4fac8b720437a283ac2aca7be.tar.gz
org.eclipse.jet-e3771612fe41efc4fac8b720437a283ac2aca7be.tar.bz2
[150947] Add JUnits for TextTrimmingVisitor.
-rw-r--r--tests/org.eclipse.jet.tests/data/textTrimmingTests.jet70
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/AllTests.java1
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TextTrimmingTests.java226
3 files changed, 297 insertions, 0 deletions
diff --git a/tests/org.eclipse.jet.tests/data/textTrimmingTests.jet b/tests/org.eclipse.jet.tests/data/textTrimmingTests.jet
new file mode 100644
index 0000000..1570482
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/data/textTrimmingTests.jet
@@ -0,0 +1,70 @@
+<%@taglib id="org.eclipse.jet.controlTags" prefix="c"%>
+<%-- This template is used to verify text trimming rules. It is used by TextTrimmingTexts.java --%>
+
+<%-- text stripping around start/end tags that support stripping (e.g. c:iterate) --%>
+test 001
+<c:iterate select="foo" var="bar">
+ some text with four spaces after
+</c:iterate>
+test 001
+
+<%-- text stripping around start/end tags that support stripping (e.g. c:iterate)
+ with leading and trailing whitepace --%>
+test 002
+ <c:iterate select="foo" var="bar">
+ some text with four spaces after
+ </c:iterate>
+test 002
+
+<%-- text stripping around start/end tags that support stripping (e.g. c:iterate)
+ with multi-line tags --%>
+test 003
+ <c:iterate
+ select="foo"
+ var="bar"
+ >
+ some text with four spaces after
+ </c:iterate
+ >
+test 003
+
+<%-- text stripping on directives --%>
+test 004
+ <%@taglib id="org.eclipse.jet.workspaceTags" prefix="ws"%>
+test 004
+
+<%-- text stripping on scriptlet --%>
+test 005
+ <%
+ int x = 3;
+%>
+test 005
+
+<%-- text stripping on declarations --%>
+test 006
+ <%!
+ public int square(int arg) {
+ return arg * arg;
+ }
+%>
+test 006
+
+<%-- text stripping on empytags that supports stripping --%>
+test 007
+ <c:include template="templates/foo.jet"/>
+test 007
+
+<%-- text stripping on empytags that does not support stripping --%>
+test 008
+ <c:get select="templates/foo.jet"/>
+test 008
+
+<%-- text stripping on comments --%>
+test 009
+ <%-- a comment --%>
+test 009
+
+<%-- text stripping an expression --%>
+test 010
+ <%= x %>
+test 010
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/AllTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/AllTests.java
index d15dc13..be0b1b5 100644
--- a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/AllTests.java
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/AllTests.java
@@ -11,6 +11,7 @@ public class AllTests {
//$JUnit-BEGIN$
suite.addTestSuite(TestJETParser.class);
suite.addTestSuite(TestJET2Parser.class);
+ suite.addTest(TextTrimmingTests.suite());
suite.addTestSuite(LineInfoTests.class);
//$JUnit-END$
return suite;
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TextTrimmingTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TextTrimmingTests.java
new file mode 100644
index 0000000..361ab43
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/compiler/TextTrimmingTests.java
@@ -0,0 +1,226 @@
+package org.eclipse.jet.tests.compiler;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.MalformedURLException;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.eclipse.jet.compiler.DefaultJET2ASTVisitor;
+import org.eclipse.jet.compiler.JET2CompilationUnit;
+import org.eclipse.jet.compiler.TextElement;
+import org.eclipse.jet.internal.compiler.TextTrimmingVisitor;
+import org.eclipse.jet.internal.parser.LineInfo;
+import org.eclipse.jet.tests.FileUtils;
+
+public class TextTrimmingTests extends TestCase {
+
+ private static JET2CompilationUnit unstrippedCu;
+ private static JET2CompilationUnit strippedCu;
+
+ private static FileInputStream getFileContentsAsStream(String urlString) throws IOException, MalformedURLException, FileNotFoundException {
+ String fileName = FileUtils.resolveURLToLocationString(urlString);
+ FileInputStream inputStream = new FileInputStream(new File(fileName));
+ return inputStream;
+ }
+
+ /**
+ * Wrap this suite in a TestSetup so that we can do one-time setup
+ * @return a wrapped TestSetup wrapped suite.
+ */
+ public static Test suite() {
+ TestSetup setup = new TestSetup(new TestSuite(TextTrimmingTests.class)) {
+ protected void setUp() throws Exception {
+ setUpAllTests();
+ }
+
+ protected void tearDown() throws Exception {
+ tearDownAllTests();
+ }
+ };
+
+ return setup;
+ }
+
+ private static void setUpAllTests() throws Exception {
+ FileInputStream stream = getFileContentsAsStream("platform:/plugin/org.eclipse.jet.tests/data/textTrimmingTests.jet");
+
+ unstrippedCu = new JET2CompilationUnit();
+ unstrippedCu.parse(stream, "UTF-8");
+
+ stream = getFileContentsAsStream("platform:/plugin/org.eclipse.jet.tests/data/textTrimmingTests.jet");
+ strippedCu = new JET2CompilationUnit();
+ strippedCu.parse(stream, "UTF-8");
+ strippedCu.accept(new TextTrimmingVisitor());
+
+ assertFalse("Errors in test template", unstrippedCu.hasErrors());
+
+ }
+
+ private static void tearDownAllTests() {
+ // nothing to do
+ }
+
+
+ private String[] getLines(TextElement text) {
+ char[] chars = text.getText();
+ LineInfo[] lineInfos = LineInfo.calculateLines(chars);
+ String[] lines = new String[lineInfos.length];
+ for (int i = 0; i < lineInfos.length; i++) {
+ lines[i] = new String(chars, lineInfos[i].getStart(), lineInfos[i].getEnd() - lineInfos[i].getStart()) + lineInfos[i].getDelimiter();
+ }
+ return lines;
+ }
+
+ private String[] getTextLinesInRange(JET2CompilationUnit cu, final String testMarker) {
+ final StringBuffer linesBuffer = new StringBuffer();
+ cu.accept(new DefaultJET2ASTVisitor() {
+ private boolean rangeStarted = false;
+ private boolean rangeEnded = false;
+
+ public void visit(TextElement text) {
+ if(rangeEnded) return;
+
+ String lines[] = getLines(text);
+ int i = 0;
+ for (; i < lines.length && !rangeStarted; i++) {
+ if(lines[i].startsWith(testMarker)) {
+ rangeStarted = true;
+ linesBuffer.append(lines[i]);
+ }
+ }
+
+ for(; i < lines.length && rangeStarted && !rangeEnded; i++) {
+ linesBuffer.append(lines[i]);
+ if(lines[i].startsWith(testMarker)) {
+ rangeEnded = true;
+ }
+ }
+ }
+ });
+ final LineInfo[] lineInfos = LineInfo.calculateLines(linesBuffer.toString().toCharArray());
+ String[] lines = new String[lineInfos.length];
+ for (int i = 0; i < lineInfos.length; i++) {
+ lines[i] = linesBuffer.substring(lineInfos[i].getStart(), lineInfos[i].getEnd());
+ }
+ return lines;
+ }
+
+ public void testStartEndTagStripping() {
+ final String TEST_CASE = "test 001";
+ String[] unstrippedText = getTextLinesInRange(unstrippedCu, TEST_CASE);
+ String[] strippedText = getTextLinesInRange(strippedCu, TEST_CASE);
+
+ assertEquals(5, unstrippedText.length);
+
+ assertEquals(3, strippedText.length);
+ }
+
+ public void testStartEndTagStripping2() {
+ final String TEST_CASE = "test 002";
+ String[] unstrippedText = getTextLinesInRange(unstrippedCu, TEST_CASE);
+ String[] strippedText = getTextLinesInRange(strippedCu, TEST_CASE);
+
+ assertEquals(5, unstrippedText.length);
+
+ assertEquals(3, strippedText.length);
+
+ }
+
+ public void testStartEndTagStripping3() {
+ final String TEST_CASE = "test 003";
+ String[] unstrippedText = getTextLinesInRange(unstrippedCu, TEST_CASE);
+ String[] strippedText = getTextLinesInRange(strippedCu, TEST_CASE);
+
+ assertEquals(5, unstrippedText.length);
+
+ assertEquals(3, strippedText.length);
+
+ }
+
+ public void testDirectiveStripping() {
+ final String TEST_CASE = "test 004";
+ String[] unstrippedText = getTextLinesInRange(unstrippedCu, TEST_CASE);
+ String[] strippedText = getTextLinesInRange(strippedCu, TEST_CASE);
+
+ assertEquals(3, unstrippedText.length);
+
+ assertEquals(2, strippedText.length);
+
+ }
+
+ public void testScriptletStripping() {
+ final String TEST_CASE = "test 005";
+ String[] unstrippedText = getTextLinesInRange(unstrippedCu, TEST_CASE);
+ String[] strippedText = getTextLinesInRange(strippedCu, TEST_CASE);
+
+ assertEquals(3, unstrippedText.length);
+
+ assertEquals(2, strippedText.length);
+
+ }
+
+ public void testDeclarationStripping() {
+ final String TEST_CASE = "test 006";
+ String[] unstrippedText = getTextLinesInRange(unstrippedCu, TEST_CASE);
+ String[] strippedText = getTextLinesInRange(strippedCu, TEST_CASE);
+
+ assertEquals(3, unstrippedText.length);
+
+ assertEquals(2, strippedText.length);
+
+ }
+
+ public void testEmptyTagStripping_supportsRemove() {
+ final String TEST_CASE = "test 007";
+ String[] unstrippedText = getTextLinesInRange(unstrippedCu, TEST_CASE);
+ String[] strippedText = getTextLinesInRange(strippedCu, TEST_CASE);
+
+ assertEquals(3, unstrippedText.length);
+
+ assertEquals(2, strippedText.length);
+
+ }
+
+
+ public void testEmptyTagStripping_noRemove() {
+ final String TEST_CASE = "test 008";
+ String[] unstrippedText = getTextLinesInRange(unstrippedCu, TEST_CASE);
+ String[] strippedText = getTextLinesInRange(strippedCu, TEST_CASE);
+
+ assertEquals(3, unstrippedText.length);
+
+ assertEquals(3, strippedText.length);
+
+ }
+
+ public void testCommentStripping() {
+ final String TEST_CASE = "test 009";
+ String[] unstrippedText = getTextLinesInRange(unstrippedCu, TEST_CASE);
+ String[] strippedText = getTextLinesInRange(strippedCu, TEST_CASE);
+
+ assertEquals(3, unstrippedText.length);
+
+ assertEquals(2, strippedText.length);
+
+ }
+
+ public void testExpressionStripping() {
+ final String TEST_CASE = "test 010";
+ String[] unstrippedText = getTextLinesInRange(unstrippedCu, TEST_CASE);
+ String[] strippedText = getTextLinesInRange(strippedCu, TEST_CASE);
+
+ assertEquals(3, unstrippedText.length);
+
+ assertEquals(3, strippedText.length);
+
+ }
+
+
+
+}