From fed2e665523bc6fe0f9e7bafa3edc14e4e2139a8 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Fri, 11 Apr 2008 09:56:09 +0000 Subject: [PATCH] Documentation for rewrite tests, by Emanuel Graf, bug 226502. --- .../comenthandler/CommentHandlingTest.java | 112 ++++++++++++++---- 1 file 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.
+ * The source for the CommentHandling tests is located at /resources/rewrite/CommentHandlingTestSource.rts.
+ * This file contains the source code and the expected output for all the tests. Following a little example how such a test looks like:

+ * + *
+ * //!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
+ * 
+ * + * The second line (//#org.eclipse.cdt...) indicates the test class (in this case this class).
+ * The "//=" indicates the beginning of the expected test result.
+ * The test result contains three sections (separated by "=>leading", "=>trailing" and "=>freestanding").
+ * 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 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); - 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$ + return expectedResultBuilder; } - private void testMap(HashMap> map, String expectedResult, String err) { + 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 String getCommentMapResult(HashMap> map) { TreeSet keyTree = new TreeSet(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 { 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); } - }