diff --git a/core/org.eclipse.cdt.core.tests/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.core.tests/META-INF/MANIFEST.MF index 509c38b1905..c93f2c5fc0e 100644 --- a/core/org.eclipse.cdt.core.tests/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.core.tests/META-INF/MANIFEST.MF @@ -23,7 +23,8 @@ Require-Bundle: org.eclipse.core.resources, org.eclipse.ui.ide, org.eclipse.ui, org.eclipse.jface.text, - org.eclipse.core.filesystem + org.eclipse.core.filesystem, + org.eclipse.ltk.core.refactoring;bundle-version="3.4.0" Bundle-ActivationPolicy: lazy Bundle-Vendor: Eclipse.org Bundle-RequiredExecutionEnvironment: J2SE-1.5 diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CommentTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CommentTests.java index ccafed302df..b89c169fe54 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CommentTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CommentTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationMacroTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationMacroTests.java index 401fea7a0fb..04bd7cb8d2c 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationMacroTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMLocationMacroTests.java @@ -26,6 +26,7 @@ import org.eclipse.cdt.core.dom.ast.IASTPreprocessorFunctionStyleMacroDefinition import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroExpansion; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorObjectStyleMacroDefinition; +import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement; import org.eclipse.cdt.core.dom.ast.IASTPreprocessorUndefStatement; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; @@ -295,7 +296,7 @@ public class DOMLocationMacroTests extends AST2BaseTest { assertEquals( firstReferences.length, 2 ); assertEquals( firstReferences[0].getPropertyInParent(), IASTPreprocessorMacroExpansion.EXPANSION_NAME ); assertEquals( firstReferences[0].getParent().getParent(), tu ); - assertEquals( firstReferences[1].getPropertyInParent(), IASTPreprocessorUndefStatement.MACRO_NAME ); + assertEquals( firstReferences[1].getPropertyInParent(), IASTPreprocessorStatement.MACRO_NAME ); assertTrue( firstReferences[1].getParent() instanceof IASTPreprocessorUndefStatement ); assertEquals( firstDeclarations.length, 1 ); assertSame( ABC1.getName(), firstDeclarations[0] ); diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMPreprocessorInformationTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMPreprocessorInformationTest.java index 030b6ffbf7e..7e992968a13 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMPreprocessorInformationTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/DOMPreprocessorInformationTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik * Rapperswil, University of applied sciences and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/RewriteBaseTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/RewriteBaseTest.java new file mode 100644 index 00000000000..8952f7df321 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/RewriteBaseTest.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Map; +import java.util.TreeMap; +import java.util.Vector; + +import org.eclipse.cdt.core.tests.BaseTestFramework; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.ILogListener; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.jface.text.TextSelection; + +/** + * @author Guido Zgraggen IFS + * + */ +public abstract class RewriteBaseTest extends BaseTestFramework implements ILogListener{ + protected static final NullProgressMonitor NULL_PROGRESS_MONITOR = new NullProgressMonitor(); + + protected TreeMap fileMap = new TreeMap(); + protected String fileWithSelection; + protected TextSelection selection; + + protected RewriteBaseTest(String name) { + super(name); + } + + public RewriteBaseTest(String name, Vector files) { + super(name); + for (TestSourceFile file : files) { + fileMap.put(file.getName(), file); + } + } + + @Override + protected abstract void runTest() throws Throwable; + + + @Override + protected void setUp() throws Exception { + super.setUp(); + for (TestSourceFile testFile : fileMap.values()) { + if(testFile.getSource().length() > 0) { + importFile(testFile.getName(), testFile.getSource()); + } + } + } + + protected void assertEquals(TestSourceFile file, IFile file2) throws Exception { + StringBuffer code = getCodeFromIFile(file2); + assertEquals(file.getExpectedSource(), TestHelper.unifyNewLines(code.toString())); + } + + protected void compareFiles(Map testResourceFiles) throws Exception { + for (String fileName : testResourceFiles.keySet()) { + TestSourceFile file = testResourceFiles.get(fileName); + IFile iFile = project.getFile(new Path(fileName)); + StringBuffer code = getCodeFromIFile(iFile); + assertEquals(TestHelper.unifyNewLines(file.getExpectedSource()), TestHelper.unifyNewLines(code.toString())); + } + } + + protected StringBuffer getCodeFromIFile(IFile file) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(file.getContents())); + StringBuffer code = new StringBuffer(); + String line; + while((line = br.readLine()) != null) { + code.append(line); + code.append('\n'); + } + br.close(); + return code; + } + + @Override + protected void tearDown() throws Exception { + System.gc(); + fileManager.closeAllFiles(); + super.tearDown(); + } + + public void logging(IStatus status, String plugin) { + Throwable ex = status.getException(); + StringBuffer stackTrace = new StringBuffer(); + if(ex != null) { + stackTrace.append('\n'); + for(StackTraceElement ste : ex.getStackTrace()) { + stackTrace.append(ste.toString()); + } + } + fail("Log-Message: " + status.getMessage() + stackTrace.toString()); //$NON-NLS-1$ + } + + public void setFileWithSelection(String fileWithSelection) { + this.fileWithSelection = fileWithSelection; + } + + public void setSelection(TextSelection selection) { + this.selection = selection; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/RewriteTester.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/RewriteTester.java new file mode 100644 index 00000000000..3ccb632a8ba --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/RewriteTester.java @@ -0,0 +1,215 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Vector; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.cdt.core.testplugin.CTestPlugin; +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.Path; +import org.eclipse.jface.text.TextSelection; +import org.osgi.framework.Bundle; + +/** + * @author Emanuel Graf + * + */ +public class RewriteTester extends TestSuite{ + + enum MatcherState{skip, inTest, inSource, inExpectedResult} + + private static final String classRegexp = "//#(.*)\\s*(\\w*)*$"; //$NON-NLS-1$ + private static final String testRegexp = "//!(.*)\\s*(\\w*)*$"; //$NON-NLS-1$ + private static final String fileRegexp = "//@(.*)\\s*(\\w*)*$"; //$NON-NLS-1$ + private static final String resultRegexp = "//=.*$"; //$NON-NLS-1$ + + public static Test suite(String name, String file)throws Exception { + BufferedReader in = createReader(file); + + ArrayList testCases = createTests(in); + in.close(); + return createSuite(testCases, name); + } + + protected static BufferedReader createReader(String file) throws IOException { + Bundle bundle = CTestPlugin.getDefault().getBundle(); + Path path = new Path(file); + String file2 = FileLocator.toFileURL(FileLocator.find(bundle, path, null)).getFile(); + return new BufferedReader(new FileReader(file2)); + } + + private static ArrayList createTests(BufferedReader inputReader) throws Exception { + + String line; + Vector files = new Vector(); + TestSourceFile actFile = null; + MatcherState matcherState = MatcherState.skip; + ArrayList testCases = new ArrayList(); + String testName = null; + String className = null; + boolean bevorFirstTest = true; + + while ((line = inputReader.readLine()) != null){ + + if(lineMatchesBeginOfTest(line)) { + if(!bevorFirstTest) { + RewriteBaseTest test = createTestClass(className, testName, files); + testCases.add(test); + files = new Vector(); + className = null; + testName = null; + } + matcherState = MatcherState.inTest; + testName = getNameOfTest(line); + bevorFirstTest = false; + continue; + } else if (lineMatchesBeginOfResult(line)) { + matcherState = MatcherState.inExpectedResult; + continue; + }else if (lineMatchesFileName(line)) { + matcherState = MatcherState.inSource; + actFile = new TestSourceFile(getFileName(line)); + files.add(actFile); + continue; + }else if(lineMatchesClassName(line)) { + className = getNameOfClass(line); + continue; + } + + switch(matcherState) { + case inSource: + if(actFile != null) { + actFile.addLineToSource(line); + } + break; + case inExpectedResult: + if(actFile != null) { + actFile.addLineToExpectedSource(line); + } + break; + default: + break; + } + } + RewriteBaseTest test = createTestClass(className, testName, files); + testCases.add(test); + return testCases; + } + + + + private static RewriteBaseTest createTestClass(String className, String testName, Vector files) throws Exception { + + + try { + Class refClass = Class.forName(className); + Class paratypes[] = new Class[2]; + paratypes[0] = testName.getClass(); + paratypes[1] = files.getClass(); + Constructor ct = refClass.getConstructor(paratypes); + Object arglist[] = new Object[2]; + arglist[0] = testName; + arglist[1] = files; + RewriteBaseTest test = (RewriteBaseTest) ct.newInstance(arglist); + for (TestSourceFile file : files) { + TextSelection sel = file.getSelection(); + if(sel != null) { + test.setFileWithSelection(file.getName()); + test.setSelection(sel); + break; + } + } + return test; + } catch (ClassNotFoundException e) { + throw new Exception("Unknown TestClass. Make sure the test's sourcefile specifies a valid test class."); + } catch (SecurityException e) { + throw new Exception("Security Exception during Test creation", e); + } catch (NoSuchMethodException e) { + throw new Exception("Test class does not provied required constructor."); + } catch (IllegalArgumentException e) { + throw new Exception("IllegalArgumentException during Test creation", e); + } catch (InstantiationException e) { + throw new Exception("InstantiationException during Test creation", e); + } catch (IllegalAccessException e) { + throw new Exception("IllegalAccessException during Test creation", e); + } catch (InvocationTargetException e) { + throw new Exception("InvocationTargetException during Test creation", e); + } + } + + private static String getFileName(String line) { + Matcher matcherBeginOfTest = createMatcherFromString(fileRegexp, line); + if(matcherBeginOfTest.find()) + return matcherBeginOfTest.group(1); + else + return null; + } + + private static String getNameOfClass(String line) { + Matcher matcherBeginOfTest = createMatcherFromString(classRegexp, line); + if(matcherBeginOfTest.find()) + return matcherBeginOfTest.group(1); + else + return null; + } + + private static boolean lineMatchesBeginOfTest(String line) { + return createMatcherFromString(testRegexp, line).find(); + } + + private static boolean lineMatchesClassName(String line) { + return createMatcherFromString(classRegexp, line).find(); + } + + private static boolean lineMatchesFileName(String line) { + return createMatcherFromString(fileRegexp, line).find(); + } + + protected static Matcher createMatcherFromString(String pattern, String line) { + return Pattern.compile(pattern).matcher(line); + } + + private static String getNameOfTest(String line) { + Matcher matcherBeginOfTest = createMatcherFromString(testRegexp, line); + if(matcherBeginOfTest.find()) + return matcherBeginOfTest.group(1); + else + return "Not Named"; + } + + private static boolean lineMatchesBeginOfResult(String line) { + return createMatcherFromString(resultRegexp, line).find(); + } + + private static TestSuite createSuite(ArrayList testCases, String name) { + TestSuite suite = new TestSuite(name); + Iterator it = testCases.iterator(); + while(it.hasNext()) { + RewriteBaseTest subject =it.next(); + suite.addTest(subject); + } + return suite; + } +} + diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/RewriteTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/RewriteTests.java new file mode 100644 index 00000000000..fe6d7258b52 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/RewriteTests.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.cdt.core.parser.tests.rewrite.astwriter.AstWriterTestSuite; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTestSuite; +import org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTestSuite; + +public class RewriteTests extends TestSuite { + + public static Test suite() throws Exception { + TestSuite suite = new TestSuite(RewriteTests.class.getName()); + suite.addTest(AstWriterTestSuite.suite()); + suite.addTest(CommentHandlingTestSuite.suite()); + suite.addTest(ChangeGeneratorTestSuite.suite()); + return suite; + } + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/TestHelper.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/TestHelper.java new file mode 100644 index 00000000000..3bd8ac7c0a8 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/TestHelper.java @@ -0,0 +1,20 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite; + +public class TestHelper { + + public static String unifyNewLines(String code) { + String replacement = System.getProperty("line.separator"); //$NON-NLS-1$ + return code.replaceAll("(\n)|(\r\n)", replacement); //$NON-NLS-1$ + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/TestSourceFile.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/TestSourceFile.java new file mode 100644 index 00000000000..e2d3f4de406 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/TestSourceFile.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.eclipse.jface.text.TextSelection; + +/** + * @author Emanuel Graf + * + */ +public class TestSourceFile { + + private static final String REPLACEMENT = ""; //$NON-NLS-1$ + private String name; + private StringBuffer source = new StringBuffer(); + private StringBuffer expectedSource = new StringBuffer(); + private String separator = System.getProperty("line.separator"); //$NON-NLS-1$ + private int selectionStart = -1; + private int selectionEnd = -1; + + protected static final String selectionStartRegex = "//\\$"; //$NON-NLS-1$ + protected static final String selectionEndRegex = "\\$//"; //$NON-NLS-1$ + protected static final String selectionStartLineRegex = "(.*)(" + selectionStartRegex + ")(.*)"; //$NON-NLS-1$ //$NON-NLS-2$ + protected static final String selectionEndLineRegex = "(.*)("+ selectionEndRegex + ")(.*)"; //$NON-NLS-1$ //$NON-NLS-2$ + + public TestSourceFile(String name) { + super(); + this.name = name; + } + public String getExpectedSource() { + String exp = expectedSource.toString(); + if(exp.length() == 0) { + return getSource(); + }else { + return exp; + } + } + public String getName() { + return name; + } + public String getSource() { + return source.toString(); + } + + public void addLineToSource(String code) { + Matcher start = createMatcherFromString(selectionStartLineRegex, code); + if(start.matches()) { + selectionStart = start.start(2) + source.length(); + code = code.replaceAll(selectionStartRegex, REPLACEMENT); + } + Matcher end = createMatcherFromString(selectionEndLineRegex, code); + if(end.matches()) { + selectionEnd = end.start(2) + source.length(); + code = code.replaceAll(selectionEndRegex, REPLACEMENT); + } + source.append(code); + source.append(separator); + } + + public void addLineToExpectedSource(String code) { + expectedSource.append(code); + expectedSource.append(separator); + } + + public TextSelection getSelection() { + if(selectionStart < 0 || selectionEnd <0 ) { + return null; + }else { + return new TextSelection(selectionStart, selectionEnd -selectionStart); + } + } + + protected static Matcher createMatcherFromString(String pattern, String line) { + return Pattern.compile(pattern).matcher(line); + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/ASTWriterTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/ASTWriterTest.java new file mode 100644 index 00000000000..3a535119e40 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/ASTWriterTest.java @@ -0,0 +1,140 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.astwriter; + +import java.util.Map; + +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.parser.ISourceCodeParser; +import org.eclipse.cdt.core.dom.parser.c.ANSICParserExtensionConfiguration; +import org.eclipse.cdt.core.dom.parser.c.GCCParserExtensionConfiguration; +import org.eclipse.cdt.core.dom.parser.c.ICParserExtensionConfiguration; +import org.eclipse.cdt.core.dom.parser.cpp.ANSICPPParserExtensionConfiguration; +import org.eclipse.cdt.core.dom.parser.cpp.GPPParserExtensionConfiguration; +import org.eclipse.cdt.core.dom.parser.cpp.ICPPParserExtensionConfiguration; +import org.eclipse.cdt.core.parser.CodeReader; +import org.eclipse.cdt.core.parser.IParserLogService; +import org.eclipse.cdt.core.parser.IScanner; +import org.eclipse.cdt.core.parser.NullLogService; +import org.eclipse.cdt.core.parser.ParserLanguage; +import org.eclipse.cdt.core.parser.ParserMode; +import org.eclipse.cdt.core.parser.ScannerInfo; +import org.eclipse.cdt.core.parser.tests.ast2.AST2BaseTest; +import org.eclipse.cdt.core.parser.tests.rewrite.RewriteBaseTest; +import org.eclipse.cdt.core.parser.tests.rewrite.TestHelper; +import org.eclipse.cdt.core.parser.tests.rewrite.TestSourceFile; +import org.eclipse.cdt.internal.core.dom.parser.c.GNUCSourceParser; +import org.eclipse.cdt.internal.core.dom.parser.cpp.GNUCPPSourceParser; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationMap; +import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriter; +import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.ASTCommenter; +import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap; +import org.eclipse.core.resources.IFile; + +/** + * @author Guido Zgraggen + */ +public class ASTWriterTest extends RewriteBaseTest { + private static final IParserLogService NULL_LOG = new NullLogService(); + + private IFile file; + + public ASTWriterTest(String name, ASTWriterTestSourceFile file) { + super(name); + fileMap.put(file.getName(), file); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + for (TestSourceFile testFile : fileMap.values()) { + if(testFile.getSource().length() > 0) { + file = importFile(testFile.getName(), testFile.getSource()); + } + } + } + + + @Override + protected void runTest() throws Throwable { + file = project.getFile("ASTWritterTest.h"); //$NON-NLS-1$ + compareFiles(fileMap); + } + + @Override + protected void compareFiles(Map testResourceFiles) throws Exception { + for (String fileName : testResourceFiles.keySet()) { + TestSourceFile testFile = testResourceFiles.get(fileName); + String code = generateSource(testFile); + assertEquals(TestHelper.unifyNewLines(testFile.getExpectedSource()), TestHelper.unifyNewLines(code + System.getProperty("line.separator"))); //$NON-NLS-1$ + } + } + + public String generateSource(TestSourceFile testFile) throws Exception { + IASTTranslationUnit unit = getParser(testFile).parse(); + NodeCommentMap commentMap = ASTCommenter.getCommentedNodeMap(unit); + ASTModificationMap map = new ASTModificationMap(); + map.getModificationsForNode(unit.getDeclarations()[0]); + ASTWriter writer = new ASTWriter(); + return writer.write(unit, null, commentMap); + } + + protected ISourceCodeParser getParser(TestSourceFile testFile) throws Exception { + CodeReader codeReader = new CodeReader(file.getLocation().toOSString(), file.getContents()); + + ScannerInfo scannerInfo = new ScannerInfo(); + ParserLanguage language = getLanguage(testFile); + boolean useGNUExtensions = getGNUExtension(testFile); + + IScanner scanner = AST2BaseTest.createScanner(codeReader, language, ParserMode.COMPLETE_PARSE, scannerInfo, true); + + ISourceCodeParser parser2 = null; + if( language == ParserLanguage.CPP ) { + ICPPParserExtensionConfiguration config = null; + if (useGNUExtensions){ + config = new GPPParserExtensionConfiguration(); + } else{ + config = new ANSICPPParserExtensionConfiguration(); + } + parser2 = new GNUCPPSourceParser(scanner, ParserMode.COMPLETE_PARSE, NULL_LOG, config ); + } else { + ICParserExtensionConfiguration config = null; + + if (useGNUExtensions){ + config = new GCCParserExtensionConfiguration(); + } else{ + config = new ANSICParserExtensionConfiguration(); + } + + parser2 = new GNUCSourceParser(scanner, ParserMode.COMPLETE_PARSE, NULL_LOG, config ); + } + return parser2; + } + + private boolean getGNUExtension(TestSourceFile file) { + if(file instanceof ASTWriterTestSourceFile) { + return ((ASTWriterTestSourceFile)file).isUseGNUExtensions(); + } + else { + return false; + } + } + + private ParserLanguage getLanguage(TestSourceFile file) { + if(file instanceof ASTWriterTestSourceFile) { + return ((ASTWriterTestSourceFile)file).getParserLanguage(); + } + else { + return ParserLanguage.CPP; + } + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/ASTWriterTestSourceFile.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/ASTWriterTestSourceFile.java new file mode 100644 index 00000000000..82024fe01eb --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/ASTWriterTestSourceFile.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + ******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.astwriter; + +import org.eclipse.cdt.core.parser.ParserLanguage; +import org.eclipse.cdt.core.parser.tests.rewrite.TestSourceFile; + +/** + * @author Guido Zgraggen IFS + * + */ +public class ASTWriterTestSourceFile extends TestSourceFile { + private ParserLanguage parserLanguage = ParserLanguage.CPP; + private boolean useGNUExtensions = false; + + public ASTWriterTestSourceFile(String name) { + super(name); + } + + public void setParserLanguage(ParserLanguage lang) { + this.parserLanguage = lang; + } + + public ParserLanguage getParserLanguage() { + return parserLanguage; + } + + public boolean isUseGNUExtensions() { + return useGNUExtensions; + } + + public void setUseGNUExtensions(boolean useGNUExtensions) { + this.useGNUExtensions = useGNUExtensions; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/AstWriterTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/AstWriterTestSuite.java new file mode 100644 index 00000000000..65d0aef7877 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/AstWriterTestSuite.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2006, 2007 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.astwriter; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * @author Emanuel Graf + * + */ +public class AstWriterTestSuite{ + + @SuppressWarnings("nls") + public static Test suite() throws Exception { + TestSuite suite = new TestSuite("AstWriterTests"); + suite.addTest(SourceRewriteTester.suite("ExpressionTests", "resources/rewrite/ASTWriterExpressionTestSource.awts")); + + suite.addTest(SourceRewriteTester.suite("DelcSpecifierTests", "resources/rewrite/ASTWriterDeclSpecTestSource.awts")); + suite.addTest(SourceRewriteTester.suite("Commented DelcSpecifierTests", + "resources/rewrite/ASTWriterCommentedDeclSpecTestSource.awts")); + + suite.addTest(SourceRewriteTester.suite("DeclaratorTests", "resources/rewrite/ASTWriterDeclaratorTestSource.awts")); + suite.addTest(SourceRewriteTester.suite("Commented DeclaratorTests", + "resources/rewrite/ASTWriterCommentedDeclaratorTestSource.awts")); + + suite.addTest(SourceRewriteTester.suite("StatementsTests", "resources/rewrite/ASTWriterStatementTestSource.awts")); + suite.addTest(SourceRewriteTester + .suite("Commented StatementsTests", "resources/rewrite/ASTWriterCommentedStatementTestSource.awts")); + + suite.addTest(SourceRewriteTester.suite("NameTests", "resources/rewrite/ASTWriterNameTestSource.awts")); + suite.addTest(SourceRewriteTester.suite("Commented NameTests", "resources/rewrite/ASTWriterCommentedNameTestSource.awts")); + + suite.addTest(SourceRewriteTester.suite("InitializerTests", "resources/rewrite/ASTWriterInitializerTestSource.awts")); + + suite.addTest(SourceRewriteTester.suite("DeclarationTests", "resources/rewrite/ASTWriterDeclarationTestSource.awts")); + suite.addTest(SourceRewriteTester.suite("Commented DeclarationTests", + "resources/rewrite/ASTWriterCommentedDeclarationTestSource.awts")); + + suite.addTest(SourceRewriteTester.suite("TemplatesTests", "resources/rewrite/ASTWriterTemplateTestSource.awts")); + + suite.addTest(SourceRewriteTester.suite("CommentTests", "resources/rewrite/ASTWriterCommentedTestSource.awts")); + suite.addTest(SourceRewriteTester.suite("NewCommentTests", "resources/rewrite/ASTWriterCommentedTestSource2.awts")); + return suite; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/SourceRewriteTester.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/SourceRewriteTester.java new file mode 100644 index 00000000000..26ba7fd4f18 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/SourceRewriteTester.java @@ -0,0 +1,167 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.astwriter; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.cdt.core.parser.ParserLanguage; +import org.eclipse.cdt.core.parser.tests.rewrite.RewriteBaseTest; +import org.eclipse.cdt.core.testplugin.CTestPlugin; +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.Path; +import org.eclipse.jface.text.TextSelection; +import org.osgi.framework.Bundle; + +public class SourceRewriteTester extends TestSuite { + + private static final String testRegexp = "//!(.*)\\s*(\\w*)*$"; //$NON-NLS-1$ + private static final String codeTypeRegexp = "//%(C|CPP)( GNU)?$"; //$NON-NLS-1$ + private static final String resultRegexp = "//=.*$"; //$NON-NLS-1$ + + enum MatcherState{skip, inTest, inSource, inExpectedResult} + + protected static BufferedReader createReader(String file) throws IOException { + Bundle bundle = CTestPlugin.getDefault().getBundle(); + Path path = new Path(file); + String file2 = FileLocator.toFileURL(FileLocator.find(bundle, path, null)).getFile(); + return new BufferedReader(new FileReader(file2)); + } + + public static Test suite(String name, String file)throws Exception { + BufferedReader in = createReader(file); + + ArrayList testCases = createTests(in); + in.close(); + return createSuite(testCases, name); + } + + private static TestSuite createSuite(ArrayList testCases, String name) { + TestSuite suite = new TestSuite(name); + Iterator it = testCases.iterator(); + while(it.hasNext()) { + RewriteBaseTest subject =it.next(); + suite.addTest(subject); + } + return suite; + } + + protected static boolean lineMatchesBeginOfTest(String line) { + return createMatcherFromString(testRegexp, line).find(); + } + + protected static boolean lineMatchesCodeType(String line) { + return createMatcherFromString(codeTypeRegexp, line).find(); + } + + protected static Matcher createMatcherFromString(String pattern, String line) { + return Pattern.compile(pattern).matcher(line); + } + + protected static String getNameOfTest(String line) { + Matcher matcherBeginOfTest = createMatcherFromString(testRegexp, line); + if(matcherBeginOfTest.find()) + return matcherBeginOfTest.group(1); + else + return "Not Named"; + } + + protected static boolean lineMatchesBeginOfResult(String line) { + return createMatcherFromString(resultRegexp, line).find(); + } + + private static ArrayList createTests(BufferedReader inputReader) throws Exception { + String line; + ASTWriterTestSourceFile file = null; + MatcherState matcherState = MatcherState.skip; + ArrayList testCases = new ArrayList(); + + while ((line = inputReader.readLine()) != null){ + if(lineMatchesBeginOfTest(line)) { + matcherState = MatcherState.inTest; + file = new ASTWriterTestSourceFile("ASTWritterTest.h"); //$NON-NLS-1$ + testCases.add(createTestClass(getNameOfTest(line), file)); + continue; + } else if (lineMatchesBeginOfResult(line)) { + matcherState = MatcherState.inExpectedResult; + continue; + }else if (lineMatchesCodeType(line)) { + matcherState = MatcherState.inSource; + if(file != null) { + file.setParserLanguage(getParserLanguage(line)); + file.setUseGNUExtensions(useGNUExtensions(line)); + } + continue; + } + + switch(matcherState) { + case inSource: + if(file != null) { + file.addLineToSource(line); + } + break; + case inExpectedResult: + if(file != null) { + file.addLineToExpectedSource(line); + } + break; + default: + break; + } + } + return testCases; + } + + protected static boolean useGNUExtensions(String line) { + Matcher matcherBeginOfTest = createMatcherFromString(codeTypeRegexp, line); + if(matcherBeginOfTest.find()) { + String codeType = matcherBeginOfTest.group(2); + if(codeType == null) { + return false; + }else { + return true; + } + } + return false; + } + + protected static ParserLanguage getParserLanguage(String line) { + Matcher matcherBeginOfTest = createMatcherFromString(codeTypeRegexp, line); + if(matcherBeginOfTest.find()) { + String codeType = matcherBeginOfTest.group(1); + if(codeType.equalsIgnoreCase("CPP")) { //$NON-NLS-1$ + return ParserLanguage.CPP; + }else { + return ParserLanguage.C; + } + } + return ParserLanguage.C; + } + + private static RewriteBaseTest createTestClass(String testName, ASTWriterTestSourceFile file) throws Exception { + ASTWriterTest test = new ASTWriterTest(testName,file); + TextSelection sel = file.getSelection(); + if(sel != null) { + test.setFileWithSelection(file.getName()); + test.setSelection(sel); + } + return test; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/ChangeGeneratorTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/ChangeGeneratorTest.java new file mode 100644 index 00000000000..95d96c18276 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/ChangeGeneratorTest.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.dom.CDOM; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.index.IIndexManager; +import org.eclipse.cdt.core.parser.tests.rewrite.TestHelper; +import org.eclipse.cdt.core.tests.BaseTestFramework; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.CTextFileChange; +import org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.jface.text.Document; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.CompositeChange; + +public abstract class ChangeGeneratorTest extends BaseTestFramework { + + protected String source; + protected String expectedSource; + + public ChangeGeneratorTest() { + super(); + } + + @Override + protected void setUp() throws Exception { + CCorePlugin.getIndexManager().joinIndexer(IIndexManager.FOREVER, new NullProgressMonitor()); + super.setUp(); + } + + @Override + public void runTest() { + final ASTModificationStore modStore = new ASTModificationStore(); + final ChangeGenerator changegenartor = new ChangeGenerator(modStore); + try { + importFile("source.h", source); //$NON-NLS-1$ + + IASTTranslationUnit unit = CDOM.getInstance().getTranslationUnit( + project.getFile(new Path("source.h")), //$NON-NLS-1$ + CDOM.getInstance().getCodeReaderFactory( + CDOM.PARSE_SAVED_RESOURCES), + true); + CPPASTVisitor visitor = createModificator(modStore); + + unit.accept(visitor); + + + // assertEquals(expectedSource, changegenartor.write(unit)); + changegenartor.generateChange(unit); + Document doc = new Document(source); + for(Change curChange : ((CompositeChange)changegenartor.getChange()).getChildren()){ + if (curChange instanceof CTextFileChange) { + CTextFileChange textChange = (CTextFileChange) curChange; + textChange.getEdit().apply(doc); + } + } + assertEquals(TestHelper.unifyNewLines(expectedSource), TestHelper.unifyNewLines(doc.get())); + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + } + + protected abstract CPPASTVisitor createModificator(ASTModificationStore modStore); + + public ChangeGeneratorTest(String name) { + super(name); + } + + @Override + protected void tearDown() throws Exception { + System.gc(); + fileManager.closeAllFiles(); + super.tearDown(); + } + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/ChangeGeneratorTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/ChangeGeneratorTestSuite.java new file mode 100644 index 00000000000..c8c10fa2f61 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/ChangeGeneratorTestSuite.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.append.AppendTestSuite; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.insertbefore.InsertBeforeTestSuite; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove.RemoveTestSuite; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace.ReplaceTestSuite; + +/** + * @author Thomas Corbat + * + */ +public class ChangeGeneratorTestSuite{ + + @SuppressWarnings("nls") + public static Test suite() throws Exception { + TestSuite suite = new TestSuite("ChangeGeneratorTests"); + + suite.addTest(ReplaceTestSuite.suite()); + suite.addTest(RemoveTestSuite.suite()); + suite.addTest(InsertBeforeTestSuite.suite()); + suite.addTest(AppendTestSuite.suite()); + + return suite; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/AppendTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/AppendTestSuite.java new file mode 100644 index 00000000000..cf83f3b256d --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/AppendTestSuite.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.append; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * @author Thomas Corbat + * + */ +public class AppendTestSuite{ + + @SuppressWarnings("nls") + public static Test suite() throws Exception { + TestSuite suite = new TestSuite("Changegenerator Append Child Tests"); + + suite.addTest(ParameterTest.suite()); + suite.addTest(ParameterToListTest.suite()); + suite.addTest(PointerToParameterTest.suite()); + suite.addTest(PointerToPointerParameterTest.suite()); + suite.addTest(ExceptionTest.suite()); + suite.addTest(CtorChainInitializerTest.suite()); + suite.addTest(ArrayModifierTest.suite()); + suite.addTest(ExpressionTest.suite()); + suite.addTest(ArraySizeExpressionTest.suite()); + + return suite; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ArrayModifierTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ArrayModifierTest.java new file mode 100644 index 00000000000..9f43f94175b --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ArrayModifierTest.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.append; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTArrayModifier; +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTArrayModifier; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class ArrayModifierTest extends ChangeGeneratorTest { + + public ArrayModifierTest(){ + super("Replace Array Modifier"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + + + source = "int *pi[5];"; //$NON-NLS-1$ + expectedSource = "int *pi[5][3];"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof IASTArrayDeclarator) { + IASTArrayDeclarator arrayDeclarator = (IASTArrayDeclarator)declarator; + arrayDeclarator.getArrayModifiers(); + IASTArrayModifier newModifier = new CPPASTArrayModifier(); + IASTExpression expr = new CPPASTLiteralExpression(0, "3"); //$NON-NLS-1$ + newModifier.setConstantExpression(expr); + ASTModification modification = new ASTModification(ModificationKind.APPEND_CHILD, declarator, newModifier, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new ArrayModifierTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ArraySizeExpressionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ArraySizeExpressionTest.java new file mode 100644 index 00000000000..36890e2a407 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ArraySizeExpressionTest.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.append; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + +public class ArraySizeExpressionTest extends ChangeGeneratorTest { + + public ArraySizeExpressionTest(){ + super("Append Array Size Expression"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "int *values = new int[6];"; //$NON-NLS-1$ + expectedSource = "int *values = new int[6][5];"; //$NON-NLS-1$ + super.setUp(); + } + + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (expression instanceof ICPPASTNewExpression) { + ICPPASTNewExpression newExpression = (ICPPASTNewExpression) expression; + newExpression.getNewTypeIdArrayExpressions(); + ASTModification modification = new ASTModification(ASTModification.ModificationKind.APPEND_CHILD, newExpression, new CPPASTLiteralExpression(0, "5"), null); //$NON-NLS-1$ + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new ArraySizeExpressionTest(); + + } + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/CtorChainInitializerTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/CtorChainInitializerTest.java new file mode 100644 index 00000000000..0ffdf74e0f5 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/CtorChainInitializerTest.java @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.append; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTConstructorChainInitializer; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class CtorChainInitializerTest extends ChangeGeneratorTest { + + + public CtorChainInitializerTest(){ + super("Append Ctor Initializer"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + + source = "TestClass::TestClass(int a, int b):beta(b){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "TestClass::TestClass(int a, int b):beta(b), alpha(a){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + CPPASTIdExpression initExpr = new CPPASTIdExpression(new CPPASTName("a".toCharArray())); //$NON-NLS-1$ + CPPASTName initName = new CPPASTName("alpha".toCharArray()); //$NON-NLS-1$ + ICPPASTConstructorChainInitializer newInitializer = new CPPASTConstructorChainInitializer(initName, initExpr); + ASTModification modification = new ASTModification(ModificationKind.APPEND_CHILD, functionDeclarator, newInitializer, null); + modStore.storeModification(null, modification); + + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new CtorChainInitializerTest(); + + } + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ExceptionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ExceptionTest.java new file mode 100644 index 00000000000..f82e7e024c7 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ExceptionTest.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.append; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTTypeId; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTypeId; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class ExceptionTest extends ChangeGeneratorTest { + + public ExceptionTest(){ + super("Append Exception Declaration"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int parameter){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int parameter) throw (int){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + IASTTypeId exception = new CPPASTTypeId(); + CPPASTDeclarator exceptionDeclarator = new CPPASTDeclarator(); + exceptionDeclarator.setName(new CPPASTName()); + CPPASTSimpleDeclSpecifier exDeclSpec = new CPPASTSimpleDeclSpecifier(); + exDeclSpec.setType(IASTSimpleDeclSpecifier.t_int); + exception.setDeclSpecifier(exDeclSpec); + exception.setAbstractDeclarator(exceptionDeclarator); + ASTModification modification = new ASTModification(ModificationKind.APPEND_CHILD, declarator, exception, null); + modStore.storeModification(null, modification); + + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new ExceptionTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ExpressionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ExpressionTest.java new file mode 100644 index 00000000000..75f7c3cd92e --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ExpressionTest.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.append; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression; +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.IASTExpressionList; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + +public class ExpressionTest extends ChangeGeneratorTest { + + + public ExpressionTest(){ + super("Append Expression"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void main(){int s = 0, c = 0, h = 0;\ns = 3, h = 5;}"; //$NON-NLS-1$ + expectedSource = "void main(){int s = 0, c = 0, h = 0;\ns = 3, h = 5, c = 9;}"; //$NON-NLS-1$ + super.setUp(); + } + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (expression instanceof IASTExpressionList) { + IASTExpressionList expressionList = (IASTExpressionList) expression; + expressionList.getExpressions(); + CPPASTBinaryExpression binEx = new CPPASTBinaryExpression(IASTBinaryExpression.op_assign, new CPPASTIdExpression(new CPPASTName("c".toCharArray())), new CPPASTLiteralExpression(0, "9")); //$NON-NLS-1$ //$NON-NLS-2$ + ASTModification modification = new ASTModification(ASTModification.ModificationKind.APPEND_CHILD, expressionList, binEx, null); + modStore.storeModification(null, modification); + } + + return PROCESS_CONTINUE; + } + + }; + } + public static Test suite() { + return new ExpressionTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ParameterTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ParameterTest.java new file mode 100644 index 00000000000..9b8265038dd --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ParameterTest.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.append; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTParameterDeclaration; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + + + + + +public class ParameterTest extends ChangeGeneratorTest { + + public ParameterTest(){ + super("Append Parameter to List"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int existing){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int existing, int newParameter){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + + CPPASTParameterDeclaration insertedParameter = new CPPASTParameterDeclaration(); + CPPASTDeclarator parameterDeclarator = new CPPASTDeclarator(); + CPPASTName parameterName = new CPPASTName("newParameter".toCharArray()); //$NON-NLS-1$ + parameterDeclarator.setName(parameterName); + insertedParameter.setDeclarator(parameterDeclarator); + CPPASTSimpleDeclSpecifier parameterDeclSpec = new CPPASTSimpleDeclSpecifier(); + parameterDeclSpec.setType(IASTSimpleDeclSpecifier.t_int); + insertedParameter.setDeclSpecifier(parameterDeclSpec); + ASTModification modification = new ASTModification(ModificationKind.APPEND_CHILD, functionDeclarator, insertedParameter, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new ParameterTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ParameterToListTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ParameterToListTest.java new file mode 100644 index 00000000000..ddccfe2edf9 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/ParameterToListTest.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.append; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTParameterDeclaration; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + + + + + +public class ParameterToListTest extends ChangeGeneratorTest { + + public ParameterToListTest(){ + super("Append Parameter to Empty List"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int newParameter){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + + CPPASTParameterDeclaration insertedParameter = new CPPASTParameterDeclaration(); + CPPASTDeclarator parameterDeclarator = new CPPASTDeclarator(); + CPPASTName parameterName = new CPPASTName("newParameter".toCharArray()); //$NON-NLS-1$ + parameterDeclarator.setName(parameterName); + insertedParameter.setDeclarator(parameterDeclarator); + CPPASTSimpleDeclSpecifier parameterDeclSpec = new CPPASTSimpleDeclSpecifier(); + parameterDeclSpec.setType(IASTSimpleDeclSpecifier.t_int); + insertedParameter.setDeclSpecifier(parameterDeclSpec); + ASTModification modification = new ASTModification(ModificationKind.APPEND_CHILD, functionDeclarator, insertedParameter, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new ParameterToListTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/PointerToParameterTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/PointerToParameterTest.java new file mode 100644 index 00000000000..3f17405547b --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/PointerToParameterTest.java @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.append; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTPointer; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class PointerToParameterTest extends ChangeGeneratorTest { + + + public PointerToParameterTest(){ + super("Append Pointer to Parameter"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int parameter){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int *parameter){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTParameterDeclaration[] parameters = functionDeclarator.getParameters(); + for(IASTParameterDeclaration curParam : parameters){ + if(String.valueOf(curParam.getDeclarator().getName().toCharArray()).equals("parameter")){ //$NON-NLS-1$ + CPPASTPointer addedPointer = new CPPASTPointer(); + ASTModification modification = new ASTModification(ModificationKind.APPEND_CHILD, curParam.getDeclarator(), addedPointer, null); + modStore.storeModification(null, modification); + } + } + } + return PROCESS_CONTINUE; + } + + }; + } + + public static Test suite() { + return new PointerToParameterTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/PointerToPointerParameterTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/PointerToPointerParameterTest.java new file mode 100644 index 00000000000..5312a2df6e4 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/append/PointerToPointerParameterTest.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.append; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTPointer; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class PointerToPointerParameterTest extends ChangeGeneratorTest { + + public PointerToPointerParameterTest(){ + super("Append Pointer to Pointer Parameter"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int *parameter){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int **parameter){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTParameterDeclaration[] parameters = functionDeclarator.getParameters(); + for(IASTParameterDeclaration curParam : parameters){ + if(String.valueOf(curParam.getDeclarator().getName().toCharArray()).equals("parameter")){ //$NON-NLS-1$ + CPPASTPointer addedPointer = new CPPASTPointer(); + ASTModification modification = new ASTModification(ModificationKind.APPEND_CHILD, curParam.getDeclarator(), addedPointer, null); + modStore.storeModification(null, modification); + } + } + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new PointerToPointerParameterTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/ArrayModifierTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/ArrayModifierTest.java new file mode 100644 index 00000000000..4a16a9d08ac --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/ArrayModifierTest.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.insertbefore; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTArrayModifier; +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTArrayModifier; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class ArrayModifierTest extends ChangeGeneratorTest { + + public ArrayModifierTest(){ + super("Replace Array Modifier"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "int *pi[3];"; //$NON-NLS-1$ + expectedSource = "int *pi[5][3];"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof IASTArrayDeclarator) { + IASTArrayDeclarator arrayDeclarator = (IASTArrayDeclarator)declarator; + + IASTArrayModifier[] modifiers = arrayDeclarator.getArrayModifiers(); + IASTArrayModifier newModifier = new CPPASTArrayModifier(); + IASTExpression expr = new CPPASTLiteralExpression(0, "5"); //$NON-NLS-1$ + newModifier.setConstantExpression(expr); + ASTModification modification = new ASTModification(ModificationKind.INSERT_BEFORE, modifiers[0], newModifier, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new ArrayModifierTest(); + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/ArraySizeExpressionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/ArraySizeExpressionTest.java new file mode 100644 index 00000000000..caae9c52925 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/ArraySizeExpressionTest.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.insertbefore; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + +public class ArraySizeExpressionTest extends ChangeGeneratorTest { + + public ArraySizeExpressionTest(){ + super("Insert Array Size Expression"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "int *values = new int[5];"; //$NON-NLS-1$ + expectedSource = "int *values = new int[6][5];"; //$NON-NLS-1$ + super.setUp(); + } + + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (expression instanceof ICPPASTNewExpression) { + ICPPASTNewExpression newExpression = (ICPPASTNewExpression) expression; + IASTExpression[] arraySizeExpressions = newExpression.getNewTypeIdArrayExpressions(); + ASTModification modification = new ASTModification(ASTModification.ModificationKind.INSERT_BEFORE, arraySizeExpressions[0], new CPPASTLiteralExpression(0, "6"), null); //$NON-NLS-1$ + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new ArraySizeExpressionTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/CtorChainInitializerTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/CtorChainInitializerTest.java new file mode 100644 index 00000000000..cbb71d1e050 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/CtorChainInitializerTest.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.insertbefore; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTConstructorChainInitializer; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class CtorChainInitializerTest extends ChangeGeneratorTest { + + public CtorChainInitializerTest(){ + super("Insert Before Ctor Initializer"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "TestClass::TestClass(int a, int b):beta(b){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "TestClass::TestClass(int a, int b):alpha(a), beta(b){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + ICPPASTConstructorChainInitializer ctorInitializer = functionDeclarator.getConstructorChain()[0]; + CPPASTIdExpression initExpr = new CPPASTIdExpression(new CPPASTName("a".toCharArray())); //$NON-NLS-1$ + CPPASTName initName = new CPPASTName("alpha".toCharArray()); //$NON-NLS-1$ + ICPPASTConstructorChainInitializer newInitializer = new CPPASTConstructorChainInitializer(initName, initExpr); + ASTModification modification = new ASTModification(ModificationKind.INSERT_BEFORE, ctorInitializer, newInitializer, null); + modStore.storeModification(null, modification); + + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new CtorChainInitializerTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/ExceptionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/ExceptionTest.java new file mode 100644 index 00000000000..9192543bbfe --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/ExceptionTest.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.insertbefore; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTTypeId; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTypeId; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class ExceptionTest extends ChangeGeneratorTest { + + public ExceptionTest(){ + super("Insert Before Exception Declaration"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int parameter) throw (float){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int parameter) throw (int, float){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTTypeId existingException = functionDeclarator.getExceptionSpecification()[0]; + + IASTTypeId exception = new CPPASTTypeId(); + CPPASTDeclarator exceptionDeclarator = new CPPASTDeclarator(); + exceptionDeclarator.setName(new CPPASTName()); + CPPASTSimpleDeclSpecifier exDeclSpec = new CPPASTSimpleDeclSpecifier(); + exDeclSpec.setType(IASTSimpleDeclSpecifier.t_int); + exception.setDeclSpecifier(exDeclSpec); + exception.setAbstractDeclarator(exceptionDeclarator); + ASTModification modification = new ASTModification(ModificationKind.INSERT_BEFORE, existingException, exception, null); + modStore.storeModification(null, modification); + + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new ExceptionTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/ExpressionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/ExpressionTest.java new file mode 100644 index 00000000000..e5e811ff4e1 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/ExpressionTest.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.insertbefore; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression; +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.IASTExpressionList; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class ExpressionTest extends ChangeGeneratorTest { + + public ExpressionTest(){ + super("Insert Expression"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void main(){int s = 0, c = 0, h = 0;\ns = 3, h = 5;}"; //$NON-NLS-1$ + expectedSource = "void main(){int s = 0, c = 0, h = 0;\ns = 3, c = 9, h = 5;}"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (expression instanceof IASTExpressionList) { + IASTExpressionList expressionList = (IASTExpressionList) expression; + IASTExpression[] expressions = expressionList.getExpressions(); + CPPASTBinaryExpression binEx = new CPPASTBinaryExpression(IASTBinaryExpression.op_assign, new CPPASTIdExpression(new CPPASTName("c".toCharArray())), new CPPASTLiteralExpression(0, "9")); //$NON-NLS-1$ //$NON-NLS-2$ + ASTModification modification = new ASTModification(ASTModification.ModificationKind.INSERT_BEFORE, expressions[1], binEx, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new ExpressionTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/FirstParameterTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/FirstParameterTest.java new file mode 100644 index 00000000000..8509f695a09 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/FirstParameterTest.java @@ -0,0 +1,85 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.insertbefore; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTParameterDeclaration; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + + + + + +public class FirstParameterTest extends ChangeGeneratorTest { + + public FirstParameterTest(){ + super("Insert Before First Parameter"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int a){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int newParameter, int a){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTParameterDeclaration[] parameters = functionDeclarator.getParameters(); + for(IASTParameterDeclaration curParam : parameters){ + if(String.valueOf(curParam.getDeclarator().getName().toCharArray()).equals("a")){ //$NON-NLS-1$ + CPPASTParameterDeclaration insertedParameter = new CPPASTParameterDeclaration(); + CPPASTDeclarator parameterDeclarator = new CPPASTDeclarator(); + CPPASTName parameterName = new CPPASTName("newParameter".toCharArray()); //$NON-NLS-1$ + parameterDeclarator.setName(parameterName); + insertedParameter.setDeclarator(parameterDeclarator); + CPPASTSimpleDeclSpecifier parameterDeclSpec = new CPPASTSimpleDeclSpecifier(); + parameterDeclSpec.setType(IASTSimpleDeclSpecifier.t_int); + insertedParameter.setDeclSpecifier(parameterDeclSpec); + ASTModification modification = new ASTModification(ModificationKind.INSERT_BEFORE, curParam, insertedParameter, null); + modStore.storeModification(null, modification); + } + } + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new FirstParameterTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/InsertBeforeTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/InsertBeforeTestSuite.java new file mode 100644 index 00000000000..f4520988731 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/InsertBeforeTestSuite.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.insertbefore; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * @author Thomas Corbat + * + */ +public class InsertBeforeTestSuite{ + + @SuppressWarnings("nls") + public static Test suite() throws Exception { + TestSuite suite = new TestSuite("Changegenerator InsertBefore Tests"); + + suite.addTest(FirstParameterTest.suite()); + suite.addTest(PointerParameterTest.suite()); + suite.addTest(ExceptionTest.suite()); + suite.addTest(CtorChainInitializerTest.suite()); + suite.addTest(ArrayModifierTest.suite()); + suite.addTest(ExpressionTest.suite()); + suite.addTest(ArraySizeExpressionTest.suite()); + + return suite; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/PointerParameterTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/PointerParameterTest.java new file mode 100644 index 00000000000..c496ed40c63 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/PointerParameterTest.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.insertbefore; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTPointer; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class PointerParameterTest extends ChangeGeneratorTest { + + public PointerParameterTest(){ + super("Insert Pointer for Parameter"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int *parameter){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int **parameter){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTParameterDeclaration[] parameters = functionDeclarator.getParameters(); + for(IASTParameterDeclaration curParam : parameters){ + if(String.valueOf(curParam.getDeclarator().getName().toCharArray()).equals("parameter")){ //$NON-NLS-1$ + IASTPointerOperator pointer = curParam.getDeclarator().getPointerOperators()[0]; + CPPASTPointer insertedPointer = new CPPASTPointer(); + ASTModification modification = new ASTModification(ModificationKind.INSERT_BEFORE, pointer, insertedPointer, null); + modStore.storeModification(null, modification); + } + } + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new PointerParameterTest(); + + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/ArrayModifierTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/ArrayModifierTest.java new file mode 100644 index 00000000000..05b0d13961f --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/ArrayModifierTest.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTArrayModifier; +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class ArrayModifierTest extends ChangeGeneratorTest { + + public ArrayModifierTest(){ + super("Remove Array Modifier"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "int *pi[3];"; //$NON-NLS-1$ + expectedSource = "int *pi;"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new ArrayModifierTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof IASTArrayDeclarator) { + IASTArrayDeclarator arrayDeclarator = (IASTArrayDeclarator)declarator; + + IASTArrayModifier[] modifiers = arrayDeclarator.getArrayModifiers(); + ASTModification modification = new ASTModification(ModificationKind.REPLACE, modifiers[0], null, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/ArraySizeExpressionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/ArraySizeExpressionTest.java new file mode 100644 index 00000000000..a7813073d61 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/ArraySizeExpressionTest.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + +public class ArraySizeExpressionTest extends ChangeGeneratorTest { + + public ArraySizeExpressionTest(){ + super("Remove Array Size Expression"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "int *values = new int[5][6];"; //$NON-NLS-1$ + expectedSource = "int *values = new int[5];"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (expression instanceof ICPPASTNewExpression) { + ICPPASTNewExpression newExpression = (ICPPASTNewExpression) expression; + IASTExpression[] arraySizeExpressions = newExpression.getNewTypeIdArrayExpressions(); + ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, arraySizeExpressions[1], null, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new ArraySizeExpressionTest(); + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/CtorChainInitializerTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/CtorChainInitializerTest.java new file mode 100644 index 00000000000..1b6846d02f5 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/CtorChainInitializerTest.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class CtorChainInitializerTest extends ChangeGeneratorTest { + + public CtorChainInitializerTest(){ + super("Remove Ctor Initializer"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "TestClass::TestClass(int a):alpha(a){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "TestClass::TestClass(int a){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new CtorChainInitializerTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + ICPPASTConstructorChainInitializer[] ctorInitializers = functionDeclarator.getConstructorChain(); + for(ICPPASTConstructorChainInitializer curInitializer : ctorInitializers){ + ASTModification modification = new ASTModification(ModificationKind.REPLACE, curInitializer, null, null); + modStore.storeModification(null, modification); + } + } + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/DeclarationTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/DeclarationTest.java new file mode 100644 index 00000000000..c09c55305ee --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/DeclarationTest.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclaration; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class DeclarationTest extends ChangeGeneratorTest { + + public DeclarationTest(){ + super("Remove Declaration Node"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "#ifndef A_H_\n#define A_H_\n\nclass A {\n\nprivate:\n int b;\n int c;\n};\n\n#endif /*A_H_*/\n\n"; //$NON-NLS-1$ + expectedSource = "#ifndef A_H_\n#define A_H_\n\nclass A {\n\nprivate:\n int b;\n};\n\n#endif /*A_H_*/\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + + return new DeclarationTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarations = true; + } + + @Override + public int visit(IASTDeclaration declaration) { + if (declaration instanceof CPPASTSimpleDeclaration) { + CPPASTSimpleDeclaration simpleDeclaration = (CPPASTSimpleDeclaration) declaration; + if(simpleDeclaration.getDeclarators().length > 0){ + String name = String.valueOf(simpleDeclaration.getDeclarators()[0].getName().toCharArray()); + if(name.equals("c")){ //$NON-NLS-1$ + ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, declaration, null, null); + modStore.storeModification(null, modification); + } + } + } + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/ExceptionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/ExceptionTest.java new file mode 100644 index 00000000000..1167cb86ec9 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/ExceptionTest.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTTypeId; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class ExceptionTest extends ChangeGeneratorTest { + + public ExceptionTest(){ + super("Remove Exception Declaration"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int parameter) throw (int){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int parameter){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new ExceptionTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTTypeId[] exceptions = functionDeclarator.getExceptionSpecification(); + for(IASTTypeId curException : exceptions){ + ASTModification modification = new ASTModification(ModificationKind.REPLACE, curException, null, null); + modStore.storeModification(null, modification); + } + } + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/ExpressionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/ExpressionTest.java new file mode 100644 index 00000000000..9283ca3c404 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/ExpressionTest.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.IASTExpressionList; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class ExpressionTest extends ChangeGeneratorTest { + + public ExpressionTest(){ + super("Remove Expression"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void main(){int s = 0, c = 0, h = 0;\ns = 3, c = 4, h = 5;}"; //$NON-NLS-1$ + expectedSource = "void main(){int s = 0, c = 0, h = 0;\ns = 3, h = 5;}"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new ExpressionTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (expression instanceof IASTExpressionList) { + IASTExpressionList expressionList = (IASTExpressionList) expression; + IASTExpression[] expressions = expressionList.getExpressions(); + ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, expressions[1], null, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } + + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/FirstParameterTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/FirstParameterTest.java new file mode 100644 index 00000000000..ac28054ffd5 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/FirstParameterTest.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + + + + + +public class FirstParameterTest extends ChangeGeneratorTest { + + public FirstParameterTest(){ + super("Remove First Parameter Node"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int a, int b, int c){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int b, int c){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new FirstParameterTest(); + + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTParameterDeclaration[] parameters = functionDeclarator.getParameters(); + for(IASTParameterDeclaration curParam : parameters){ + if(String.valueOf(curParam.getDeclarator().getName().toCharArray()).equals("a")){ //$NON-NLS-1$ + ASTModification modification = new ASTModification(ModificationKind.REPLACE, curParam, null, null); + modStore.storeModification(null, modification); + } + } + } + return PROCESS_CONTINUE; + } + + }; + } + + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/LastParameterTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/LastParameterTest.java new file mode 100644 index 00000000000..bd900c9f84b --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/LastParameterTest.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + + + +public class LastParameterTest extends ChangeGeneratorTest { + + public LastParameterTest(){ + super("Remove Last Parameter Node"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int a, int b, int c){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int a, int b){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new LastParameterTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTParameterDeclaration[] parameters = functionDeclarator.getParameters(); + for(IASTParameterDeclaration curParam : parameters){ + if(String.valueOf(curParam.getDeclarator().getName().toCharArray()).equals("c")){ //$NON-NLS-1$ + ASTModification modification = new ASTModification(ModificationKind.REPLACE, curParam, null, null); + modStore.storeModification(null, modification); + } + } + } + + + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/MiddleParameterTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/MiddleParameterTest.java new file mode 100644 index 00000000000..80d23a31a11 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/MiddleParameterTest.java @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + + + + + +public class MiddleParameterTest extends ChangeGeneratorTest { + + public MiddleParameterTest(){ + super("Remove Middle Parameter Node"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int a, int b, int c){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int a, int c){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + + public static Test suite() { + return new MiddleParameterTest(); + } + + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTParameterDeclaration[] parameters = functionDeclarator.getParameters(); + for(IASTParameterDeclaration curParam : parameters){ + if(String.valueOf(curParam.getDeclarator().getName().toCharArray()).equals("b")){ //$NON-NLS-1$ + ASTModification modification = new ASTModification(ModificationKind.REPLACE, curParam, null, null); + modStore.storeModification(null, modification); + } + } + } + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/NewInitializerExpressionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/NewInitializerExpressionTest.java new file mode 100644 index 00000000000..5b374997a06 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/NewInitializerExpressionTest.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class NewInitializerExpressionTest extends ChangeGeneratorTest { + + public NewInitializerExpressionTest(){ + super("Remove New Initializer Expression"); //$NON-NLS-1$ + } + + @Override + public void setUp() throws Exception{ + source = "int *value = new int(5);"; //$NON-NLS-1$ + expectedSource = "int *value = new int();"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (expression instanceof ICPPASTNewExpression) { + ICPPASTNewExpression newExpression = (ICPPASTNewExpression) expression; + ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, newExpression.getNewInitializer(), null, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } + + public static Test suite() { + return new NewInitializerExpressionTest(); + + } + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/PointerInParameterTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/PointerInParameterTest.java new file mode 100644 index 00000000000..4ba51c164c9 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/PointerInParameterTest.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class PointerInParameterTest extends ChangeGeneratorTest { + + public PointerInParameterTest(){ + super("Remove Pointer in Parameter"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int *parameter){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int parameter){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + + public static Test suite() { + return new PointerInParameterTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTParameterDeclaration[] parameters = functionDeclarator.getParameters(); + for(IASTParameterDeclaration curParam : parameters){ + if(String.valueOf(curParam.getDeclarator().getName().toCharArray()).equals("parameter")){ //$NON-NLS-1$ + IASTPointerOperator pointer = curParam.getDeclarator().getPointerOperators()[0]; + ASTModification modification = new ASTModification(ModificationKind.REPLACE, pointer, null, null); + modStore.storeModification(null, modification); + } + } + } + + + return PROCESS_CONTINUE; + } + + }; + } + + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/RemoveTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/RemoveTestSuite.java new file mode 100644 index 00000000000..b124b80d030 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/RemoveTestSuite.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * @author Thomas Corbat + * + */ +public class RemoveTestSuite{ + + @SuppressWarnings("nls") + public static Test suite() throws Exception { + TestSuite suite = new TestSuite("Changegenerator Remove Tests"); + + suite.addTest(DeclarationTest.suite()); + suite.addTest(FirstParameterTest.suite()); + suite.addTest(MiddleParameterTest.suite()); + suite.addTest(LastParameterTest.suite()); + suite.addTest(SingleParameterTest.suite()); + suite.addTest(PointerInParameterTest.suite()); + suite.addTest(ExceptionTest.suite()); + suite.addTest(CtorChainInitializerTest.suite()); + suite.addTest(ArrayModifierTest.suite()); + suite.addTest(ExpressionTest.suite()); + suite.addTest(ArraySizeExpressionTest.suite()); + suite.addTest(NewInitializerExpressionTest.suite()); + suite.addTest(StatementTest.suite()); + + return suite; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/SingleParameterTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/SingleParameterTest.java new file mode 100644 index 00000000000..1cd12f3379c --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/SingleParameterTest.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class SingleParameterTest extends ChangeGeneratorTest { + + public SingleParameterTest(){ + super("Remove The Only Parameter Node"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int parameter){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + + public static Test suite() { + return new SingleParameterTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTParameterDeclaration[] parameters = functionDeclarator.getParameters(); + for(IASTParameterDeclaration curParam : parameters){ + if(String.valueOf(curParam.getDeclarator().getName().toCharArray()).equals("parameter")){ //$NON-NLS-1$ + ASTModification modification = new ASTModification(ModificationKind.REPLACE, curParam, null, null); + modStore.storeModification(null, modification); + } + } + } + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/StatementTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/StatementTest.java new file mode 100644 index 00000000000..4aeea899f1e --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/remove/StatementTest.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.remove; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTIfStatement; +import org.eclipse.cdt.core.dom.ast.IASTStatement; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class StatementTest extends ChangeGeneratorTest { + + public StatementTest(){ + super("Remove Then-Statement"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "int f()\r\n{\r\n\tint i = 0;\r\n\tif(i < 1){\r\n\t\t++i;\r\n\t}\r\n}\r\n"; //$NON-NLS-1$ + expectedSource = "int f()\r\n{\r\n\tint i = 0;\r\n}\r\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new StatementTest(); + } + + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitStatements = true; + } + + @Override + public int visit(IASTStatement statement) { + if (statement instanceof IASTIfStatement) { + IASTIfStatement ifStatement = (IASTIfStatement) statement; + + ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, ifStatement, null, null); + modStore.storeModification(null, modification); + } + + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ArrayModifierTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ArrayModifierTest.java new file mode 100644 index 00000000000..a75f7cedda8 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ArrayModifierTest.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTArrayModifier; +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTArrayModifier; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class ArrayModifierTest extends ChangeGeneratorTest { + + public ArrayModifierTest(){ + super("Replace Array Modifier"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "int *pi[3];"; //$NON-NLS-1$ + expectedSource = "int *pi[15];"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new ArrayModifierTest(); + } + + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof IASTArrayDeclarator) { + IASTArrayDeclarator arrayDeclarator = (IASTArrayDeclarator)declarator; + IASTArrayModifier[] modifiers = arrayDeclarator.getArrayModifiers(); + IASTArrayModifier newModifier = new CPPASTArrayModifier(); + IASTExpression expr = new CPPASTLiteralExpression(0, "15"); //$NON-NLS-1$ + newModifier.setConstantExpression(expr); + ASTModification modification = new ASTModification(ModificationKind.REPLACE, modifiers[0], newModifier, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ArraySizeExpressionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ArraySizeExpressionTest.java new file mode 100644 index 00000000000..215b9f9e61a --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ArraySizeExpressionTest.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class ArraySizeExpressionTest extends ChangeGeneratorTest { + + public ArraySizeExpressionTest(){ + super("Relpace Array Size Expression"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "int *values = new int[5][6];"; //$NON-NLS-1$ + expectedSource = "int *values = new int[5][7];"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new ArraySizeExpressionTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (expression instanceof ICPPASTNewExpression) { + ICPPASTNewExpression newExpression = (ICPPASTNewExpression) expression; + IASTExpression[] arraySizeExpressions = newExpression.getNewTypeIdArrayExpressions(); + ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, arraySizeExpressions[1], new CPPASTLiteralExpression(0, "7"), null); //$NON-NLS-1$ + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/CtorChainInitializerTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/CtorChainInitializerTest.java new file mode 100644 index 00000000000..8db4323f9dc --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/CtorChainInitializerTest.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTConstructorChainInitializer; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class CtorChainInitializerTest extends ChangeGeneratorTest { + + public CtorChainInitializerTest(){ + super("Replace Ctor Initializer"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "TestClass::TestClass(int a):beta(b){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "TestClass::TestClass(int a): alpha(a){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new CtorChainInitializerTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + ICPPASTConstructorChainInitializer[] ctorInitializers = functionDeclarator.getConstructorChain(); + for(ICPPASTConstructorChainInitializer curInitializer : ctorInitializers){ + CPPASTIdExpression initExpr = new CPPASTIdExpression(new CPPASTName("a".toCharArray())); //$NON-NLS-1$ + CPPASTName initName = new CPPASTName("alpha".toCharArray()); //$NON-NLS-1$ + ICPPASTConstructorChainInitializer newInitializer = new CPPASTConstructorChainInitializer(initName, initExpr); + ASTModification modification = new ASTModification(ModificationKind.REPLACE, curInitializer, newInitializer, null); + modStore.storeModification(null, modification); + } + } + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ExceptionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ExceptionTest.java new file mode 100644 index 00000000000..131fd1afeb7 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ExceptionTest.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTTypeId; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTypeId; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class ExceptionTest extends ChangeGeneratorTest { + + + public ExceptionTest(){ + super("Remove Exception Declaration"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int parameter) throw (float){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int parameter) throw (int){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new ExceptionTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTTypeId existingException = functionDeclarator.getExceptionSpecification()[0]; + + IASTTypeId exception = new CPPASTTypeId(); + CPPASTDeclarator exceptionDeclarator = new CPPASTDeclarator(); + exceptionDeclarator.setName(new CPPASTName()); + CPPASTSimpleDeclSpecifier exDeclSpec = new CPPASTSimpleDeclSpecifier(); + exDeclSpec.setType(IASTSimpleDeclSpecifier.t_int); + exception.setDeclSpecifier(exDeclSpec); + exception.setAbstractDeclarator(exceptionDeclarator); + ASTModification modification = new ASTModification(ModificationKind.REPLACE, existingException, exception, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } + + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ExpressionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ExpressionTest.java new file mode 100644 index 00000000000..ddf4f69070d --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ExpressionTest.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression; +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.IASTExpressionList; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class ExpressionTest extends ChangeGeneratorTest { + + public ExpressionTest(){ + super("Replace Expression"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void main(){int s = 0, c = 0, h = 0;\ns = 3, c = 4, h = 5;}"; //$NON-NLS-1$ + expectedSource = "void main(){int s = 0, c = 0, h = 0;\ns = 3, c = 9, h = 5;}"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new ExpressionTest(); + } + + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (expression instanceof IASTExpressionList) { + IASTExpressionList expressionList = (IASTExpressionList) expression; + IASTExpression[] expressions = expressionList.getExpressions(); + CPPASTBinaryExpression binEx = new CPPASTBinaryExpression(IASTBinaryExpression.op_assign, new CPPASTIdExpression(new CPPASTName("c".toCharArray())), new CPPASTLiteralExpression(0, "9")); //$NON-NLS-1$ //$NON-NLS-2$ + ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, expressions[1], binEx, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/IdenticalTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/IdenticalTest.java new file mode 100644 index 00000000000..6b60c8d6dc8 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/IdenticalTest.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class IdenticalTest extends ChangeGeneratorTest { + + public IdenticalTest(){ + super("Replace Node Same Node"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "#ifndef A_H_\n#define A_H_\n\nclass A {\n\nprivate:\n int c;\n};\n\n#endif /*A_H_*/\n\n"; //$NON-NLS-1$ + expectedSource = "#ifndef A_H_\n#define A_H_\n\nclass A {\n\nprivate:\n int c;\n};\n\n#endif /*A_H_*/\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new IdenticalTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, declarator.getName(), declarator.getName(), null); + modStore.storeModification(null, modification); + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/InitializerTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/InitializerTest.java new file mode 100644 index 00000000000..b7257f95e6b --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/InitializerTest.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTInitializer; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTInitializerExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class InitializerTest extends ChangeGeneratorTest { + + public InitializerTest(){ + super("Replace Initializer"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "int hs = 5;"; //$NON-NLS-1$ + expectedSource = "int hs = 999;"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new InitializerTest(); + } + + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTDeclarator) { + CPPASTDeclarator fieldDeclarator = (CPPASTDeclarator)declarator; + IASTInitializer initializer = fieldDeclarator.getInitializer(); + + CPPASTLiteralExpression litEx = new CPPASTLiteralExpression(0, "999"); //$NON-NLS-1$ + CPPASTInitializerExpression initExpr = new CPPASTInitializerExpression(litEx); + + ASTModification modification = new ASTModification(ModificationKind.REPLACE, initializer, initExpr, null); + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/MoveRenameTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/MoveRenameTest.java new file mode 100644 index 00000000000..44f497bc6fe --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/MoveRenameTest.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTDeclaration; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTCompositeTypeSpecifier; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclaration; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class MoveRenameTest extends ChangeGeneratorTest { + + public MoveRenameTest(){ + super("Swap Rename Declarations"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + + source = "#ifndef A_H_\r\n#define A_H_\r\n\r\nclass A {\r\n\r\nprivate:\r\n int b;\r\n int a;\r\n};\r\n\r\n#endif /*A_H_*/\r\n\r\n"; //$NON-NLS-1$ + expectedSource = "#ifndef A_H_\r\n#define A_H_\r\n\r\nclass A {\r\n\r\nprivate:\r\n int d;\r\n int b;\r\n};\r\n\r\n#endif /*A_H_*/\r\n\r\n"; //$NON-NLS-1$ + super.setUp(); + } + + + public static Test suite() { + return new MoveRenameTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclSpecifiers = true; + } + + @Override + public int visit(IASTDeclSpecifier declSpec) { + if (declSpec instanceof ICPPASTCompositeTypeSpecifier) { + CPPASTCompositeTypeSpecifier classSpecifier = (CPPASTCompositeTypeSpecifier) declSpec; + IASTDeclaration[] members = classSpecifier.getMembers(); + ASTModification swap1 = new ASTModification(ASTModification.ModificationKind.REPLACE, members[1], members[2], null); + ASTModification swap2 = new ASTModification(ASTModification.ModificationKind.REPLACE, members[2], members[1], null); + IASTName name =((CPPASTSimpleDeclaration)members[2]).getDeclarators()[0].getName(); + modStore.storeModification(null, swap1); + modStore.storeModification(null, swap2); + modStore.storeModification(swap1, new ASTModification(ASTModification.ModificationKind.REPLACE, name, new CPPASTName("d".toCharArray()), null)); //$NON-NLS-1$ + } + return super.visit(declSpec); + } + + }; + } + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/MoveTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/MoveTest.java new file mode 100644 index 00000000000..71104ae5302 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/MoveTest.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTDeclaration; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTCompositeTypeSpecifier; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class MoveTest extends ChangeGeneratorTest { + + public MoveTest(){ + super("Swap Declarations"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "#ifndef A_H_\r\n#define A_H_\r\n\r\nclass A {\r\n\r\nprivate:\r\n int b;\r\n int a;\r\n};\r\n\r\n#endif /*A_H_*/\r\n\r\n"; //$NON-NLS-1$ + expectedSource = "#ifndef A_H_\r\n#define A_H_\r\n\r\nclass A {\r\n\r\nprivate:\r\n int a;\r\n int b;\r\n};\r\n\r\n#endif /*A_H_*/\r\n\r\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new MoveTest(); + + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclSpecifiers = true; + } + + @Override + public int visit(IASTDeclSpecifier declSpec) { + if (declSpec instanceof ICPPASTCompositeTypeSpecifier) { + CPPASTCompositeTypeSpecifier classSpecifier = (CPPASTCompositeTypeSpecifier) declSpec; + IASTDeclaration[] members = classSpecifier.getMembers(); + ASTModification swap1 = new ASTModification(ASTModification.ModificationKind.REPLACE, members[1], members[2], null); + ASTModification swap2 = new ASTModification(ASTModification.ModificationKind.REPLACE, members[2], members[1], null); + modStore.storeModification(null, swap1); + modStore.storeModification(null, swap2); + } + return super.visit(declSpec); + } + + }; + } + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/NameTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/NameTest.java new file mode 100644 index 00000000000..501e0980df1 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/NameTest.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class NameTest extends ChangeGeneratorTest { + + + public NameTest(){ + super("Replace Name Node"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "#ifndef A_H_\r\n#define A_H_\r\n\r\nclass A {\r\n\r\nprivate:\r\n int c;\r\n};\r\n\r\n#endif /*A_H_*/\r\n\r\n"; //$NON-NLS-1$ + expectedSource = "#ifndef A_H_\r\n#define A_H_\r\n\r\nclass A {\r\n\r\nprivate:\r\n int b;\r\n};\r\n\r\n#endif /*A_H_*/\r\n\r\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new NameTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + char[] newName = new char[]{'b'}; + IASTName name = new CPPASTName(newName); + ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, declarator.getName(), name, null); + modStore.storeModification(null, modification); + return PROCESS_CONTINUE; + } + + }; + } + + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/NewInitializerExpressionTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/NewInitializerExpressionTest.java new file mode 100644 index 00000000000..323a588d7e8 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/NewInitializerExpressionTest.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTExpression; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class NewInitializerExpressionTest extends ChangeGeneratorTest { + + public NewInitializerExpressionTest(){ + super("Replace New Initializer Expression"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "int *value = new int(5);"; //$NON-NLS-1$ + expectedSource = "int *value = new int(6);"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new NewInitializerExpressionTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitExpressions = true; + } + + @Override + public int visit(IASTExpression expression) { + if (expression instanceof ICPPASTNewExpression) { + ICPPASTNewExpression newExpression = (ICPPASTNewExpression) expression; + ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, newExpression.getNewInitializer(), new CPPASTLiteralExpression(0, "6"), null); //$NON-NLS-1$ + modStore.storeModification(null, modification); + } + return PROCESS_CONTINUE; + } + }; + } + + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/PointerInParameterTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/PointerInParameterTest.java new file mode 100644 index 00000000000..a11276a4828 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/PointerInParameterTest.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTPointer; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +public class PointerInParameterTest extends ChangeGeneratorTest { + + public PointerInParameterTest(){ + super("Replace Pointer for Parameter"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "void foo(int ¶meter){\n}\n\n"; //$NON-NLS-1$ + expectedSource = "void foo(int *parameter){\n}\n\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new PointerInParameterTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + if (declarator instanceof CPPASTFunctionDeclarator) { + CPPASTFunctionDeclarator functionDeclarator = (CPPASTFunctionDeclarator)declarator; + IASTParameterDeclaration[] parameters = functionDeclarator.getParameters(); + for(IASTParameterDeclaration curParam : parameters){ + if(String.valueOf(curParam.getDeclarator().getName().toCharArray()).equals("parameter")){ //$NON-NLS-1$ + IASTPointerOperator pointer = curParam.getDeclarator().getPointerOperators()[0]; + CPPASTPointer newPointer = new CPPASTPointer(); + ASTModification modification = new ASTModification(ModificationKind.REPLACE, pointer, newPointer, null); + modStore.storeModification(null, modification); + } + } + } + return PROCESS_CONTINUE; + } + }; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceTestSuite.java new file mode 100644 index 00000000000..08fb3b5c4c2 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/ReplaceTestSuite.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * @author Thomas Corbat + * + */ +public class ReplaceTestSuite{ + + @SuppressWarnings("nls") + public static Test suite() throws Exception { + TestSuite suite = new TestSuite("ChangegeneratorReplaceTests"); + + suite.addTest(NameTest.suite()); + suite.addTest(MoveTest.suite()); + suite.addTest(MoveRenameTest.suite()); + suite.addTest(SameNameTest.suite()); + suite.addTest(IdenticalTest.suite()); + suite.addTest(PointerInParameterTest.suite()); + suite.addTest(ExceptionTest.suite()); + suite.addTest(ArrayModifierTest.suite()); + suite.addTest(InitializerTest.suite()); + suite.addTest(ExpressionTest.suite()); + suite.addTest(ArraySizeExpressionTest.suite()); + suite.addTest(NewInitializerExpressionTest.suite()); + suite.addTest(StatementTest.suite()); + return suite; + } +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/SameNameTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/SameNameTest.java new file mode 100644 index 00000000000..3d19b40f4dd --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/SameNameTest.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class SameNameTest extends ChangeGeneratorTest { + + public SameNameTest(){ + super("Replace Name Node Same Name"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + + source = "#ifndef A_H_\r\n#define A_H_\r\n\r\nclass A {\r\n\r\nprivate:\r\n int c;\r\n};\r\n\r\n#endif /*A_H_*/\r\n\r\n"; //$NON-NLS-1$ + expectedSource = "#ifndef A_H_\r\n#define A_H_\r\n\r\nclass A {\r\n\r\nprivate:\r\n int c;\r\n};\r\n\r\n#endif /*A_H_*/\r\n\r\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new SameNameTest(); + } + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclarators = true; + } + + @Override + public int visit(IASTDeclarator declarator) { + char[] newName = new char[]{'c'}; + IASTName name = new CPPASTName(newName); + ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, declarator.getName(), name, null); + modStore.storeModification(null, modification); + return PROCESS_CONTINUE; + } + + }; + } + + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/StatementTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/StatementTest.java new file mode 100644 index 00000000000..4b552cb78ab --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/replace/StatementTest.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTIfStatement; +import org.eclipse.cdt.core.dom.ast.IASTStatement; +import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTCompoundStatement; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTExpressionStatement; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTUnaryExpression; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; + + + + + +public class StatementTest extends ChangeGeneratorTest { + + public StatementTest(){ + super("Replace Then-Statement"); //$NON-NLS-1$ + } + + @Override + protected void setUp() throws Exception { + source = "int f()\r\n{\r\n\tint i = 0;\r\n\tif(i < 1){\r\n\t\t++i;\r\n\t}\r\n}\r\n"; //$NON-NLS-1$ + expectedSource = "int f()\r\n{\r\n\tint i = 0;\r\n\tif(i < 1){\r\n\t i++;\r\n\t}\r\n}\r\n"; //$NON-NLS-1$ + super.setUp(); + } + + public static Test suite() { + return new StatementTest(); + } + + + @Override + protected CPPASTVisitor createModificator( + final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitStatements = true; + } + + @Override + public int visit(IASTStatement statement) { + if (statement instanceof IASTIfStatement) { + IASTIfStatement ifStatement = (IASTIfStatement) statement; + CPPASTCompoundStatement compound = new CPPASTCompoundStatement(); + + CPPASTExpressionStatement expr = new CPPASTExpressionStatement(new CPPASTUnaryExpression(IASTUnaryExpression.op_postFixIncr, new CPPASTIdExpression(new CPPASTName("i".toCharArray())))); //$NON-NLS-1$ + compound.addStatement(expr); + ASTModification modification = new ASTModification(ASTModification.ModificationKind.REPLACE, ifStatement.getThenClause(), compound, null); + modStore.storeModification(null, modification); + } + + return PROCESS_CONTINUE; + } + }; + } +} 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 new file mode 100644 index 00000000000..42337313641 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTest.java @@ -0,0 +1,118 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + ******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.comenthandler; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.TreeSet; +import java.util.Vector; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.dom.ast.IASTComment; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.core.parser.tests.rewrite.RewriteBaseTest; +import org.eclipse.cdt.core.parser.tests.rewrite.TestSourceFile; +import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.ASTCommenter; +import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap; +import org.eclipse.core.runtime.CoreException; + +/** + * @author Guido Zgraggen 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$ + + public CommentHandlingTest(String name, Vector files) { + super(name, files); + } + + @Override + protected void runTest() throws Throwable { + + if (fileMap.size() > 1) { + throw new Exception("To many files for CommentHandlingTest"); //$NON-NLS-1$ + } else if (fileMap.size() == 0) { + throw new Exception("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()); + + 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$ + } + String leadingResult = matcher.group(1); + String trailingResult = matcher.group(2); + String freestandingResult = matcher.group(3); + + 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 void testMap(HashMap> map, String expectedResult, String err) { + TreeSet keyTree = new TreeSet(new NodeOffsetComparator()); + keyTree.addAll(map.keySet()); + StringBuilder output = new StringBuilder(); + for (IASTNode actNode : keyTree) { + ArrayList comments = map.get(actNode); + + output.append(actNode.getRawSignature() + " = "); //$NON-NLS-1$ + boolean first = true; + for (IASTComment actComment : comments) { + if (!first) { + output.append(" , "); //$NON-NLS-1$ + } + output.append(actComment.getRawSignature()); + first = false; + } + output.append(separator); + } + assertEquals(err, expectedResult.trim(), 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 final class NodeOffsetComparator implements Comparator { + public int compare(IASTNode o1, IASTNode o2) { + int offDif = o1.getFileLocation().getNodeOffset() - o2.getFileLocation().getNodeOffset(); + if (offDif == 0) { + return o1.getFileLocation().getNodeLength() - o2.getFileLocation().getNodeLength(); + } + return offDif; + } + } + + + private IASTTranslationUnit getUnit() throws CoreException { + ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(project.getFile(fileMap.keySet().iterator().next())); + return tu.getAST(); + } + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTestSuite.java new file mode 100644 index 00000000000..0aa0ead6105 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTestSuite.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + ******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.comenthandler; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.cdt.core.parser.tests.rewrite.RewriteTester; + +/** + * @author Guido Zgraggen IFS + * + */ +public class CommentHandlingTestSuite extends TestSuite { + + public static Test suite() throws Exception { + TestSuite suite = new TestSuite(CommentHandlingTestSuite.class.getName()); + + suite.addTest(RewriteTester.suite("CommentTests", "resources/rewrite/CommentHandlingTestSource.rts")); //$NON-NLS-1$ //$NON-NLS-2$ + + suite.addTestSuite(NodeCommentMapTest.class); + + return suite; + } + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/NodeCommentMapTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/NodeCommentMapTest.java new file mode 100644 index 00000000000..2d4f334dced --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/NodeCommentMapTest.java @@ -0,0 +1,102 @@ +/******************************************************************************* + * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik + * Rapperswil, University of applied sciences and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + ******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.comenthandler; + +import junit.framework.TestCase; + +import org.eclipse.cdt.core.dom.ast.IASTComment; +import org.eclipse.cdt.internal.core.dom.parser.ASTNode; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap; + +/** + * @author Guido Zgraggen IFS + * + */ +public class NodeCommentMapTest extends TestCase { + + private NodeCommentMap map; + + @Override + protected void setUp() throws Exception { + map = new NodeCommentMap(); + } + + @Override + protected void tearDown() throws Exception { + map = null; + } + + public void testNoComment(){ + ASTNode node = new CPPASTName(); + + assertEquals(0, map.getCommentsForNode(node).size()); + } + + public void testOneComment(){ + ASTNode node = new CPPASTName(); + IASTComment comm = new Comment(); + + map.addCommentToNode(node, comm); + + assertEquals(1, map.getCommentsForNode(node).size()); + assertEquals(comm, map.getCommentsForNode(node).get(0)); + } + + public void testTwoComment(){ + ASTNode node = new CPPASTName(); + IASTComment com1 = new Comment(); + IASTComment com2 = new Comment(); + + map.addCommentToNode(node, com1); + map.addCommentToNode(node, com2); + + assertEquals(2, map.getCommentsForNode(node).size()); + assertEquals(com1, map.getCommentsForNode(node).get(0)); + assertEquals(com2, map.getCommentsForNode(node).get(1)); + } + + + public void testCommentOnDifferentNodes(){ + ASTNode node1 = new CPPASTName(); + ASTNode node2 = new CPPASTName(); + IASTComment com1 = new Comment(); + IASTComment com2 = new Comment(); + IASTComment com3 = new Comment(); + + + map.addCommentToNode(node1, com1); + map.addCommentToNode(node2, com2); + map.addCommentToNode(node1, com3); + + assertEquals(2, map.getCommentsForNode(node1).size()); + assertEquals(1, map.getCommentsForNode(node2).size()); + assertEquals(com1, map.getCommentsForNode(node1).get(0)); + assertEquals(com2, map.getCommentsForNode(node2).get(0)); + assertEquals(com3, map.getCommentsForNode(node1).get(1)); + } + + + //=== InternalComment class for testing + private class Comment extends ASTNode implements IASTComment{ + private char[] comment; + + public char[] getComment() { + return comment; + } + public void setComment(char[] comment) { + this.comment = comment; + } + //not used + public boolean isBlockComment() {return false;} + } +} diff --git a/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterCommentedDeclSpecTestSource.awts b/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterCommentedDeclSpecTestSource.awts new file mode 100644 index 00000000000..9acaeb840f5 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterCommentedDeclSpecTestSource.awts @@ -0,0 +1,222 @@ +//!Commented DeclSpecifierTest +//%CPP +//Test1 +inline int foo() +{ + return 1; +} + +//Test2 +const int a = 1; +//Test3 +volatile int b = 3; +//Test4 +typedef int *intp; +//Test5 +extern int b; +//Test6 +static int c; +//Test7 +int foo() +{ + auto int i = 1; + return i; +} + +//Test8 +int bar() +{ + register int i = c; + return i; +} + + +//!Commented CompositeTypeSpecifier +//%CPP +//Test1 +struct B //Test1 +{ + int c; //Test3 + char b; //Test4 + //Test5 +}; +//Test6 +union D //Test7 +{ + int i; //Test8 + int y; //Test9 + //Test10 +}; +//Test11 +class E //Test12 +{ + //Test13 +public: + int c; //Test14 + //Test15 +}; + +//!Commented ElaboratedTypeSpecifier 1 +//%CPP +//Test1 +class A *A; //Test2 +//Test3 +enum Status{ good, bad}; //Test4 +//Test5 +enum Status stat; //Test6 +//Test7 +union D //Test8 +{ + int i; //Test9 + int y; //Test10 + //Test11 +}; +//Test12 +union D d; //Test13 + +//!Commented ElaboratedTypeSpecifier 2 +//%CPP +enum Status{ good, bad}; +enum /*Test*/ +Status stat; + +//!Commented EnumeratioSpecifier +//%CPP +enum /*Test*/ +Status{ good, bad}; + +//!NamedTypeSpecifier +//%CPP +typedef int INT; +typedef INT (FOO)(INT); + +//!SimpleDeclSpecifier +//%CPP +signed short int i; +unsigned long int y; +float f; +void x(); +char c; +double d; + +//!Commented CPPCompositeTypeSpecifier 1 +//%CPP +//TEST 1 +class TestClass //TEST 2 +{ + //TEST 3 +}; //TEST 4 + +//!Commented CPPCompositeTypeSpecifier 2 +//%CPP +/*TEST 1*/ +class TestClass /*TEST 2*/ +{ + /*TEST 3*/ +}; /*TEST 4*/ + +//!Commented CPPCompositeTypeSpecifier 3 +//%CPP +//TEST 1 +class TestClass2 //TEST 2 + : public TestClass //TEST 3 +{ + //TEST 4 +}; //TEST 5 + +//!Commented CPPCompositeTypeSpecifier 4 +//%CPP +/*TEST 1*/ +class TestClass2 /*TEST 2*/ + : public TestClass +{ + /*TEST 3*/ +}; /*TEST 4*/ + +//!Commented CPPCompositeTypeSpecifier 5 +//%CPP +class TestClass2 /*TEST 1*/ + : public TestClass +{ +}; + +//!Commented CPPCompositeTypeSpecifier 6 +//%CPP + +class TestClass2 + : public TestClass /*TEST 1*/ +{ +}; + +//= +class TestClass2 : public TestClass /*TEST 1*/ +{ +}; + +//!Commented CPPCompositeTypeSpecifier 7 +//%CPP +class TestClass2 : public TestClass +{ //TEST 1 +}; + +//= +class TestClass2 : public TestClass +{ + //TEST 1 +}; + +//!Commented CPPCompositeTypeSpecifier 8 +//%CPP +class TestClass2 + : public TestClass +{ /*TEST 1*/ +}; + +//= +class TestClass2 : public TestClass +{ + /*TEST 1*/ +}; + +//!Commented CPPNamedTypeSpecifier 1 +//%CPP +template class A +{ + //TEST 1 + typedef char C; //TEST 2 +}; + +//!Commented CPPNamedTypeSpecifier 2 +//%CPP +template class A +{ + /*TEST 1*/ + typedef char C; /*TEST 2*/ +}; + +//!Commented SimpleDeclSpecifier 1 +//%CPP +//TEST 1 +bool b; //TEST 2 + +//!Commented SimpleDeclSpecifier 2 +//%CPP +/*TEST 1*/ +wchar_t wc; /*TEST 2*/ + +//!Commented DeclSpecifier in Definition +//%CPP +//comment a +void tree(int a){ //comment b + a = 1; //comment c +} //comment d + +//= +//comment a +void tree(int a) +{ + //comment b + a = 1; //comment c +} //comment d + + diff --git a/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterCommentedDeclarationTestSource.awts b/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterCommentedDeclarationTestSource.awts new file mode 100644 index 00000000000..e814662df5b --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/resources/rewrite/ASTWriterCommentedDeclarationTestSource.awts @@ -0,0 +1,168 @@ +//!Commented ASMDeclarationTest 1 +//%CPP +//TEST 1 +asm("movl %1, %%eax;"); //TEST 2 + +//!Commented ASMDeclarationTest 2 +//%CPP +/*TEST 1*/ +asm("movl %1, %%eax;"); /*TEST 2*/ + +//!Commented FunctionDefinitionTest 1 +//%CPP +//TEST 1 +int foo() +{ + //inner comment + return 1; +} //TEST 2 + + +//!Commented FunctionDefinitionTest 2 +//%CPP +/*TEST 1*/ +int foo() +{ + //inner comment + return 1; +} /*TEST 1*/ + + +//!Commented SimpleDeclarationTest 1 +//%CPP +//TEST 1 +int i = 2, y = 3; //TEST 2 +//TEST 3 +int b = 0; //TEST 4 + +//!Commented NamespaceAlias 1 +//%CPP +//TEST 1 +namespace kurz = ziemlichlangernamespace; //TEST 2 + +//!Commented NamespaceAlias 2 +//%CPP +/*TEST 1*/ +namespace kurz = ziemlichlangernamespace; /*TEST 2*/ + +//!Commented TemplateDeclaration 1 +//%CPP +//TEST 1 +template class vector //TEST 2 +{ + //TEST 3 +}; //TEST 4 + +//!Commented TemplateDeclaration 2 +//%CPP +/*TEST 1*/ +template class vector /*TEST 2*/ +{ + /*TEST 3*/ +}; /*TEST 4*/ + +//!Commented NestedTemplateDeclaration 1 +//%CPP +//TEST 1 +template