mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-30 20:35:38 +02:00
Documentation for rewrite tests, by Emanuel Graf, bug 226502.
This commit is contained in:
parent
147b628385
commit
fed2e66552
1 changed files with 88 additions and 24 deletions
|
@ -31,18 +31,55 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
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 {
|
public class CommentHandlingTest extends RewriteBaseTest {
|
||||||
|
|
||||||
private static final String ANY_CHAR_REGEXP = "(.*)"; //$NON-NLS-1$
|
private static final String ANY_CHAR_REGEXP = "(.*)"; //$NON-NLS-1$
|
||||||
private static String separator = System.getProperty("line.separator"); //$NON-NLS-1$
|
private static final 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 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) {
|
public CommentHandlingTest(String name, Vector<TestSourceFile> files) {
|
||||||
super(name, files);
|
super(name, files);
|
||||||
}
|
}
|
||||||
|
@ -51,29 +88,54 @@ public class CommentHandlingTest extends RewriteBaseTest {
|
||||||
protected void runTest() throws Throwable {
|
protected void runTest() throws Throwable {
|
||||||
|
|
||||||
if (fileMap.size() > 1) {
|
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) {
|
} 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();
|
TestSourceFile file = fileMap.values().iterator().next();
|
||||||
|
|
||||||
NodeCommentMap nodeMap = ASTCommenter.getCommentedNodeMap(getUnit());
|
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()) {
|
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$
|
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 leadingResult = matcher.group(1);
|
||||||
String trailingResult = matcher.group(2);
|
String trailingResult = matcher.group(2);
|
||||||
String freestandingResult = matcher.group(3);
|
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$
|
return expectedResultBuilder;
|
||||||
testMap(nodeMap.getTrailingMap(), trailingResult, "Trailing test failed."); //$NON-NLS-1$
|
|
||||||
testMap(nodeMap.getFreestandingMap(), freestandingResult, "Freestanding test failed."); //$NON-NLS-1$
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testMap(HashMap<IASTNode, ArrayList<IASTComment>> 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<IASTNode, ArrayList<IASTComment>> map) {
|
||||||
TreeSet<IASTNode> keyTree = new TreeSet<IASTNode>(new NodeOffsetComparator());
|
TreeSet<IASTNode> keyTree = new TreeSet<IASTNode>(new NodeOffsetComparator());
|
||||||
keyTree.addAll(map.keySet());
|
keyTree.addAll(map.keySet());
|
||||||
StringBuilder output = new StringBuilder();
|
StringBuilder output = new StringBuilder();
|
||||||
|
@ -89,16 +151,20 @@ public class CommentHandlingTest extends RewriteBaseTest {
|
||||||
output.append(actComment.getRawSignature());
|
output.append(actComment.getRawSignature());
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
output.append(separator);
|
output.append(SEPARATOR);
|
||||||
}
|
}
|
||||||
assertEquals(err, expectedResult.trim(), output.toString().trim());
|
return output.toString().trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getSeparatingRegexp() {
|
private static String getSeparatingRegexp() {
|
||||||
return LEADING_COMMENT_SEPARATOR + ANY_CHAR_REGEXP + TRAILING_COMMENT_SEPARATOR + ANY_CHAR_REGEXP + FREESTANDING_COMMENT_SEPARATOR + ANY_CHAR_REGEXP;
|
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> {
|
private final class NodeOffsetComparator implements Comparator<IASTNode> {
|
||||||
public int compare(IASTNode o1, IASTNode o2) {
|
public int compare(IASTNode o1, IASTNode o2) {
|
||||||
int offDif = o1.getFileLocation().getNodeOffset() - o2.getFileLocation().getNodeOffset();
|
int offDif = o1.getFileLocation().getNodeOffset() - o2.getFileLocation().getNodeOffset();
|
||||||
|
@ -108,11 +174,9 @@ public class CommentHandlingTest extends RewriteBaseTest {
|
||||||
return offDif;
|
return offDif;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void appendLineTrimmed(StringBuilder builderToAppendTo, String line) {
|
||||||
private IASTTranslationUnit getUnit() throws CoreException {
|
builderToAppendTo.append(line.trim());
|
||||||
ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(project.getFile(fileMap.keySet().iterator().next()));
|
builderToAppendTo.append(SEPARATOR);
|
||||||
return tu.getAST();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue