Skip to main content
aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorMarkus Schorn2008-04-11 05:56:09 -0400
committerMarkus Schorn2008-04-11 05:56:09 -0400
commitfed2e665523bc6fe0f9e7bafa3edc14e4e2139a8 (patch)
treeadb9f8435980dba3513f35f0226a2ef6b05d45ed /core
parent147b628385001e610194394e138457a451f835d2 (diff)
downloadorg.eclipse.cdt-fed2e665523bc6fe0f9e7bafa3edc14e4e2139a8.tar.gz
org.eclipse.cdt-fed2e665523bc6fe0f9e7bafa3edc14e4e2139a8.tar.xz
org.eclipse.cdt-fed2e665523bc6fe0f9e7bafa3edc14e4e2139a8.zip
Documentation for rewrite tests, by Emanuel Graf, bug 226502.
Diffstat (limited to 'core')
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTest.java112
1 files changed, 88 insertions, 24 deletions
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTest.java
index 42337313641..b84f8c6e587 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTest.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTest.java
@@ -31,18 +31,55 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
import org.eclipse.core.runtime.CoreException;
/**
- * @author Guido Zgraggen IFS
+ * This test tests the behavoir of the class ASTCommenter. It checks if the ASTCommenter assigns the comments contained in an AST to the right ASTNodes.<br>
+ * The source for the CommentHandling tests is located at /resources/rewrite/CommentHandlingTestSource.rts.<br>
+ * This file contains the source code and the expected output for all the tests. Following a little example how such a test looks like:<br><br>
+ *
+ * <code><pre>
+ * //!NameOfTheTest - will be used as JUnit test name
+ * //#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
+ * //@NameOfASourceFile.h
+ * class myTestClass
+ * {
+ * //myLeadingComment
+ * void aMethod(); //myTrailingComment
+ * //myFreestandingComment
+ * //myFreestandingComment2
+ * };
+ *
+ * //=
+ * =>leading
+ * void aMethod(); = //myLeadingComment
+ *
+ * =>trailing
+ * void aMethod(); = //myTrailingComment
+ *
+ * =>freestanding
+ * void aMethod(); = //myFreestandingComment , //myFreestandingComment2
+ * </pre></code>
+ *
+ * The second line (//#org.eclipse.cdt...) indicates the test class (in this case this class).<br>
+ * The "//=" indicates the beginning of the expected test result.<br>
+ * The test result contains three sections (separated by "=>leading", "=>trailing" and "=>freestanding").<br>
+ * Each section contains the raw signature of the node to which a comment is assigned plus " = " and the comment. If there are several comments
+ * assigned to the same node they are concatenated with a " , ".
+ *
+ * @author Guido Zgraggen IFS, Lukas Felber IFS
*
*/
public class CommentHandlingTest extends RewriteBaseTest {
private static final String ANY_CHAR_REGEXP = "(.*)"; //$NON-NLS-1$
- private static String separator = System.getProperty("line.separator"); //$NON-NLS-1$
-
- private static String LEADING_COMMENT_SEPARATOR = "=>leading"; //$NON-NLS-1$
- private static String TRAILING_COMMENT_SEPARATOR = "=>trailing"; //$NON-NLS-1$
- private static String FREESTANDING_COMMENT_SEPARATOR = "=>freestanding"; //$NON-NLS-1$
+ private static final String SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$
+
+ private static final String LEADING_COMMENT_SEPARATOR = "=>leading"; //$NON-NLS-1$
+ private static final String TRAILING_COMMENT_SEPARATOR = "=>trailing"; //$NON-NLS-1$
+ private static final String FREESTANDING_COMMENT_SEPARATOR = "=>freestanding"; //$NON-NLS-1$
+ private static final String LEADING_COMMENT_TITLE = "<<<=== Leading Comment Test Section ===>>>"; //$NON-NLS-1$
+ private static final String TRAILING_COMMENT_TITLE = "<<<=== Trailing Comment Test Section ===>>>"; //$NON-NLS-1$
+ private static final String FREESTANDING_COMMENT_TITLE = "<<<=== Freestanding Comment Test Section ===>>>"; //$NON-NLS-1$
+
public CommentHandlingTest(String name, Vector<TestSourceFile> files) {
super(name, files);
}
@@ -51,29 +88,54 @@ public class CommentHandlingTest extends RewriteBaseTest {
protected void runTest() throws Throwable {
if (fileMap.size() > 1) {
- throw new Exception("To many files for CommentHandlingTest"); //$NON-NLS-1$
+ fail("To many files for CommentHandlingTest"); //$NON-NLS-1$
} else if (fileMap.size() == 0) {
- throw new Exception("No file for testing"); //$NON-NLS-1$
+ fail("No file for testing"); //$NON-NLS-1$
}
TestSourceFile file = fileMap.values().iterator().next();
-
NodeCommentMap nodeMap = ASTCommenter.getCommentedNodeMap(getUnit());
- Matcher matcher = Pattern.compile(CommentHandlingTest.getSeparatingRegexp(), Pattern.MULTILINE | Pattern.DOTALL).matcher(file.getExpectedSource());
+
+ StringBuilder expectedResultBuilder = buildExpectedResult(file);
+ StringBuilder actualResultBuilder = buildActualResult(nodeMap);
+
+ assertEquals(expectedResultBuilder.toString(), actualResultBuilder.toString());
+ }
+ private StringBuilder buildExpectedResult(TestSourceFile file) {
+
+ Matcher matcher = Pattern.compile(CommentHandlingTest.getSeparatingRegexp(), Pattern.MULTILINE | Pattern.DOTALL).matcher(file.getExpectedSource());
if (!matcher.find()) {
fail("Missing expected section. Expected result code must be of the following format:\n\"=>leading\n...\n=>trailing\n...\n=>freestanding\""); //$NON-NLS-1$
}
+ StringBuilder expectedResultBuilder = new StringBuilder();
+
String leadingResult = matcher.group(1);
String trailingResult = matcher.group(2);
String freestandingResult = matcher.group(3);
+
+ appendLineTrimmed(expectedResultBuilder, LEADING_COMMENT_TITLE);
+ appendLineTrimmed(expectedResultBuilder, leadingResult);
+ appendLineTrimmed(expectedResultBuilder, TRAILING_COMMENT_TITLE);
+ appendLineTrimmed(expectedResultBuilder, trailingResult);
+ appendLineTrimmed(expectedResultBuilder, FREESTANDING_COMMENT_TITLE);
+ appendLineTrimmed(expectedResultBuilder, freestandingResult);
+
+ return expectedResultBuilder;
+ }
- testMap(nodeMap.getLeadingMap(), leadingResult, "Leading test failed."); //$NON-NLS-1$
- testMap(nodeMap.getTrailingMap(), trailingResult, "Trailing test failed."); //$NON-NLS-1$
- testMap(nodeMap.getFreestandingMap(), freestandingResult, "Freestanding test failed."); //$NON-NLS-1$
+ private StringBuilder buildActualResult(NodeCommentMap nodeMap) {
+ StringBuilder actualResultBuilder = new StringBuilder();
+ appendLineTrimmed(actualResultBuilder, LEADING_COMMENT_TITLE);
+ appendLineTrimmed(actualResultBuilder, getCommentMapResult(nodeMap.getLeadingMap()));
+ appendLineTrimmed(actualResultBuilder, TRAILING_COMMENT_TITLE);
+ appendLineTrimmed(actualResultBuilder, getCommentMapResult(nodeMap.getTrailingMap()));
+ appendLineTrimmed(actualResultBuilder, FREESTANDING_COMMENT_TITLE);
+ appendLineTrimmed(actualResultBuilder, getCommentMapResult(nodeMap.getFreestandingMap()));
+ return actualResultBuilder;
}
- private void testMap(HashMap<IASTNode, ArrayList<IASTComment>> map, String expectedResult, String err) {
+ private String getCommentMapResult(HashMap<IASTNode, ArrayList<IASTComment>> map) {
TreeSet<IASTNode> keyTree = new TreeSet<IASTNode>(new NodeOffsetComparator());
keyTree.addAll(map.keySet());
StringBuilder output = new StringBuilder();
@@ -89,16 +151,20 @@ public class CommentHandlingTest extends RewriteBaseTest {
output.append(actComment.getRawSignature());
first = false;
}
- output.append(separator);
+ output.append(SEPARATOR);
}
- assertEquals(err, expectedResult.trim(), output.toString().trim());
+ return output.toString().trim();
}
private static String getSeparatingRegexp() {
return LEADING_COMMENT_SEPARATOR + ANY_CHAR_REGEXP + TRAILING_COMMENT_SEPARATOR + ANY_CHAR_REGEXP + FREESTANDING_COMMENT_SEPARATOR + ANY_CHAR_REGEXP;
}
-
- // === Nested classes for testing purpose
+
+ private IASTTranslationUnit getUnit() throws CoreException {
+ ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(project.getFile(fileMap.keySet().iterator().next()));
+ return tu.getAST();
+ }
+
private final class NodeOffsetComparator implements Comparator<IASTNode> {
public int compare(IASTNode o1, IASTNode o2) {
int offDif = o1.getFileLocation().getNodeOffset() - o2.getFileLocation().getNodeOffset();
@@ -108,11 +174,9 @@ public class CommentHandlingTest extends RewriteBaseTest {
return offDif;
}
}
-
-
- private IASTTranslationUnit getUnit() throws CoreException {
- ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(project.getFile(fileMap.keySet().iterator().next()));
- return tu.getAST();
+
+ private void appendLineTrimmed(StringBuilder builderToAppendTo, String line) {
+ builderToAppendTo.append(line.trim());
+ builderToAppendTo.append(SEPARATOR);
}
-
}

Back to the top