From d5f27d5c45f43ebb42fdcaebe2ace0136f140dfd Mon Sep 17 00:00:00 2001 From: Andrew Niefer Date: Tue, 16 Nov 2004 15:17:56 +0000 Subject: [PATCH] - patch from Tanya Wolff for open declaration tests - new Refactoring test framework (from me) --- core/org.eclipse.cdt.core.tests/ChangeLog | 3 + .../cdt/core/tests/BaseTestFramework.java | 6 +- .../cdt/core/tests/SearchRegressionTests.java | 12 +- .../core/tests/SelectionRegressionTest.java | 670 +++++++++++++++++- core/org.eclipse.cdt.ui.tests/ChangeLog | 5 + .../ContentAssistRegressionTests.java | 2 +- .../RefactoringRegressionTests.java | 186 +++++ .../regression/RegressionTestsUISuite.java | 50 ++ 8 files changed, 916 insertions(+), 18 deletions(-) rename core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/{text/contentassist => regression}/ContentAssistRegressionTests.java (99%) create mode 100644 core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/regression/RefactoringRegressionTests.java create mode 100644 core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/regression/RegressionTestsUISuite.java diff --git a/core/org.eclipse.cdt.core.tests/ChangeLog b/core/org.eclipse.cdt.core.tests/ChangeLog index b188e573d57..437bb56e1bf 100644 --- a/core/org.eclipse.cdt.core.tests/ChangeLog +++ b/core/org.eclipse.cdt.core.tests/ChangeLog @@ -1,3 +1,6 @@ +2004-11-15 Tanya Wolff + added selection search tests to regression folder + 2004-10-13 Tanya Wolff added search tests to regression folder diff --git a/core/org.eclipse.cdt.core.tests/regression/org/eclipse/cdt/core/tests/BaseTestFramework.java b/core/org.eclipse.cdt.core.tests/regression/org/eclipse/cdt/core/tests/BaseTestFramework.java index cdc2085cd37..bac330864ca 100644 --- a/core/org.eclipse.cdt.core.tests/regression/org/eclipse/cdt/core/tests/BaseTestFramework.java +++ b/core/org.eclipse.cdt.core.tests/regression/org/eclipse/cdt/core/tests/BaseTestFramework.java @@ -39,6 +39,7 @@ abstract public class BaseTestFramework extends TestCase { static protected NullProgressMonitor monitor; static protected IWorkspace workspace; static protected IProject project; + static protected ICProject cproject; static protected FileManager fileManager; { @@ -48,11 +49,10 @@ abstract public class BaseTestFramework extends TestCase { workspace = ResourcesPlugin.getWorkspace(); - ICProject cPrj; try { - cPrj = CProjectHelper.createCCProject("RegressionTestProject", "bin"); //$NON-NLS-1$ //$NON-NLS-2$ + cproject = CProjectHelper.createCCProject("RegressionTestProject", "bin"); //$NON-NLS-1$ //$NON-NLS-2$ - project = cPrj.getProject(); + project = cproject.getProject(); project.setSessionProperty(IndexManager.activationKey, Boolean.FALSE ); } catch ( CoreException e ) { /*boo*/ diff --git a/core/org.eclipse.cdt.core.tests/regression/org/eclipse/cdt/core/tests/SearchRegressionTests.java b/core/org.eclipse.cdt.core.tests/regression/org/eclipse/cdt/core/tests/SearchRegressionTests.java index 11bace3645d..cbd554d620f 100644 --- a/core/org.eclipse.cdt.core.tests/regression/org/eclipse/cdt/core/tests/SearchRegressionTests.java +++ b/core/org.eclipse.cdt.core.tests/regression/org/eclipse/cdt/core/tests/SearchRegressionTests.java @@ -24,6 +24,7 @@ import junit.framework.TestSuite; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.index.IIndexChangeListener; +import org.eclipse.cdt.core.index.IIndexDelta; import org.eclipse.cdt.core.index.IndexChangeEvent; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.search.BasicSearchResultCollector; @@ -110,20 +111,19 @@ public class SearchRegressionTests extends BaseTestFramework implements ICSearch * @see org.eclipse.cdt.core.index.IIndexChangeListener#indexChanged(org.eclipse.cdt.core.index.IndexChangeEvent) */ public void indexChanged( IndexChangeEvent event ) { - indexChanged = true; + if( event.getDelta().getDeltaType() == IIndexDelta.MERGE_DELTA ){ + indexChanged = true; + } } protected IFile importFile(String fileName, String contents ) throws Exception{ indexChanged = false; IFile file = super.importFile( fileName, contents ); - int loops = 0; - while( !indexChanged && loops++ < 40){ + while( !indexChanged ){ Thread.sleep( 100 ); } - if( loops >= 20 ) - fail("Timeout waiting for file \"" + fileName + "\" to index." ); //$NON-NLS-1$//$NON-NLS-2$ - + return file; } diff --git a/core/org.eclipse.cdt.core.tests/regression/org/eclipse/cdt/core/tests/SelectionRegressionTest.java b/core/org.eclipse.cdt.core.tests/regression/org/eclipse/cdt/core/tests/SelectionRegressionTest.java index c8fd90612cc..3cd9095facc 100644 --- a/core/org.eclipse.cdt.core.tests/regression/org/eclipse/cdt/core/tests/SelectionRegressionTest.java +++ b/core/org.eclipse.cdt.core.tests/regression/org/eclipse/cdt/core/tests/SelectionRegressionTest.java @@ -56,7 +56,33 @@ public class SelectionRegressionTest extends BaseTestFramework { public static Test suite( boolean cleanup ) { TestSuite suite = new TestSuite("SelectionRegressionTests"); //$NON-NLS-1$ suite.addTest( new SelectionRegressionTest("testSimpleOpenDeclaration") ); //$NON-NLS-1$ - + suite.addTest( new SelectionRegressionTest( "testClass" ) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testClassRHS" ) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testStruct" ) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testStructRHS" ) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testEnumeration" ) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testEnumerationArg" ) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testEnumerator" ) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testMethod" ) ); //$NON-NLS-1$ + suite.addTest( new FailingTest( new SelectionRegressionTest( "testMethodRHS78656" ), 78656 )); //$NON-NLS-1$ + suite.addTest( new FailingTest( new SelectionRegressionTest( "testMethod78114" ), 78114 )); //$NON-NLS-1$ + suite.addTest( new FailingTest( new SelectionRegressionTest( "testMethod78118" ), 78118 )); //$NON-NLS-1$ + suite.addTest( new FailingTest( new SelectionRegressionTest( "testOverloadedMethod78389" ), 78389 )); //$NON-NLS-1$ + suite.addTest( new FailingTest( new SelectionRegressionTest( "testConstructor78625" ), 78625) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testClassField" ) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testStructField" ) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testNamespace" ) ); //$NON-NLS-1$ + suite.addTest( new FailingTest(new SelectionRegressionTest( "testNamespace77989" ), 77989) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testFunction" ) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testFunctionArg" ) ); //$NON-NLS-1$ + suite.addTest( new FailingTest(new SelectionRegressionTest( "testFunctionArg78435" ), 78435 )); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testVariable" ) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testVariableStruct" ) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testVariableArg" ) ); //$NON-NLS-1$ + suite.addTest( new FailingTest (new SelectionRegressionTest( "testVariableArg77996" ), 77996 )); //$NON-NLS-1$ + suite.addTest( new FailingTest (new SelectionRegressionTest( "testVariable77996" ), 77996) ); //$NON-NLS-1$ + suite.addTest( new SelectionRegressionTest( "testUnion" ) ); //$NON-NLS-1$ + if( cleanup ) suite.addTest( new SelectionRegressionTest( "cleanupProject" ) ); //$NON-NLS-1$ @@ -98,18 +124,28 @@ public class SelectionRegressionTest extends BaseTestFramework { protected void assertNodeLocation( IASTNode result, IFile file, int offset ) throws Exception { if( result != null && result instanceof IASTOffsetableNamedElement ){ IASTOffsetableNamedElement el = (IASTOffsetableNamedElement) result; - assertEquals( new String( el.getFilename() ), file.getLocation().toOSString() ); - assertEquals( el.getNameOffset(), offset ); + assertEquals( file.getLocation().toOSString(), new String( el.getFilename() ) ); + assertEquals( offset, el.getNameOffset() ); return; } - fail("Node not found at " + file.getLocation().toOSString() + " line " + offset ); //$NON-NLS-1$//$NON-NLS-2$ + fail("Node not found in " + file.getLocation().toOSString() + " offset " + offset); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + } + protected void assertNodeNull( IASTNode node ) throws Exception { + if (node == null) { + return; + } + if (node instanceof IASTOffsetableNamedElement) { + IASTOffsetableNamedElement el = (IASTOffsetableNamedElement) node; + fail ("node found at " + new String( el.getFilename()) + " offset " + el.getNameOffset() ); //$NON-NLS-1$ //$NON-NLS-2$ + return; + } + fail("Node found when none expected."); //$NON-NLS-1$ } - public void testSimpleOpenDeclaration() throws Exception{ Writer writer = new StringWriter(); writer.write("class A{}; \n"); //$NON-NLS-1$ - String h = writer.toString(); - IFile header = importFile( "a.h", h ); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ writer = new StringWriter(); writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ @@ -121,6 +157,624 @@ public class SelectionRegressionTest extends BaseTestFramework { int start = source.indexOf( "A" ); //$NON-NLS-1$ IASTNode node = getSelection( cpp, start, start + 1 ); - assertNodeLocation( node, header, h.indexOf("A") ); //$NON-NLS-1$ + assertNodeLocation( node, h, header.indexOf("A")); //$NON-NLS-1$ + } + public void testClass() throws Exception{ + Writer writer = new StringWriter(); + writer.write("class A{}; \n"); //$NON-NLS-1$ + writer.write("namespace N { \n"); //$NON-NLS-1$ + writer.write(" class B { \n"); //$NON-NLS-1$ + writer.write(" class C{}; \n"); //$NON-NLS-1$ + writer.write(" }; \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "A/*vp1*/ a; \n"); //$NON-NLS-1$ + writer.write( "N::B/*vp2*/ b; \n"); //$NON-NLS-1$ + writer.write( "using namespace N; \n"); //$NON-NLS-1$ + writer.write( "B::C/*vp3*/ c; \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln global; Seln global + int start = source.indexOf( "A/*vp1*/" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("A")); //$NON-NLS-1$ + //vp2 Decln in namespace; Seln global, scoped + start = source.indexOf( "N::B/*vp2*/" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 4 ); + assertNodeLocation( node, h, header.indexOf("B")); //$NON-NLS-1$ + //vp3 Decln in namespace and nested class; Seln in partially scoped ref + start = source.indexOf( "C/*vp3*/" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("C")); //$NON-NLS-1$ + + } + public void testClassRHS() throws Exception{ + Writer writer = new StringWriter(); + writer.write("class A{ \n"); //$NON-NLS-1$ + writer.write(" enum{E0}; \n"); //$NON-NLS-1$ + writer.write("}; \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "int a=A::E0; \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp 1 Decln global, Seln global, on rhs of assignment + int start = source.indexOf( "A" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("A")); //$NON-NLS-1$ + } + public void testStruct() throws Exception{ + Writer writer = new StringWriter(); + writer.write("struct A{};//vp1 \n"); //$NON-NLS-1$ + writer.write("class B{ \n"); //$NON-NLS-1$ + writer.write(" public: struct C{}; \n"); //$NON-NLS-1$ + writer.write("}; \n"); //$NON-NLS-1$ + writer.write("namespace N{ \n"); //$NON-NLS-1$ + writer.write(" struct A{};//vp3 \n"); //$NON-NLS-1$ + writer.write(" struct D{}; \n"); //$NON-NLS-1$ + writer.write("}; \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " A a;//vp1 \n"); //$NON-NLS-1$ + writer.write( " B::C d;//vp2 \n"); //$NON-NLS-1$ + writer.write( " N::A e;//vp3 \n"); //$NON-NLS-1$ + writer.write( " using namespace N; \n"); //$NON-NLS-1$ + writer.write( " A/*vp4*/ f; \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln global; Seln in function + int start = source.indexOf( "A a;//vp1" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("A{};//vp1")); //$NON-NLS-1$ + //vp2 Decln in class; Seln in function, in :: scope + start = source.indexOf( "C" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("C")); //$NON-NLS-1$ + //vp3 Decln in namespace; Seln in function, fully qualified + start = source.indexOf( "N::A" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 4 ); + assertNodeLocation( node, h, header.indexOf("A{};//vp3")); //$NON-NLS-1$ //$NON-NLS-2$ + //vp4 Decln ambiguous; Seln in function, unqualified + start = source.indexOf( "A/*vp4*/" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 1 ); + assertNodeNull( node ); + } + public void testStructRHS() throws Exception{ + Writer writer = new StringWriter(); + writer.write("struct A{ \n"); //$NON-NLS-1$ + writer.write(" static const float pi=3.14; \n"); //$NON-NLS-1$ + writer.write("}; \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " float f=A::pi; \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln global; Seln in function, rhs of assignment + int start = source.indexOf( "A" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("A")); //$NON-NLS-1$ + } + public void testEnumeration() throws Exception{ + Writer writer = new StringWriter(); + writer.write("enum A{}; \n"); //$NON-NLS-1$ + writer.write("class B{ \n"); //$NON-NLS-1$ + writer.write(" public: \n"); //$NON-NLS-1$ + writer.write(" enum C {enum2}; \n"); //$NON-NLS-1$ + writer.write(" void boo(); \n"); //$NON-NLS-1$ + writer.write("}; \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " A/*vp1*/ a; \n"); //$NON-NLS-1$ + writer.write( " B::C/*vp2*/ c; \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln global; Seln in function + int start = source.indexOf( "A" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("A")); //$NON-NLS-1$ + //vp2 Decln in class; Seln in function, fully qualified + start = source.indexOf( "B::C" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 4 ); + assertNodeLocation( node, h, header.indexOf("C")); //$NON-NLS-1$ + } + public void testEnumerationArg() throws Exception{ + Writer writer = new StringWriter(); + writer.write("enum A{}; \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(A a){}; \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln global; Seln in argument list of function + int start = source.indexOf( "A" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("A")); //$NON-NLS-1$ + } + public void testEnumerator() throws Exception{ + Writer writer = new StringWriter(); + writer.write("enum A{enum1}; \n"); //$NON-NLS-1$ + writer.write("class B{ \n"); //$NON-NLS-1$ + writer.write(" public: \n"); //$NON-NLS-1$ + writer.write(" enum {enum2}; \n"); //$NON-NLS-1$ + writer.write(" enum {enum3} f1; \n"); //$NON-NLS-1$ + writer.write(" void boo(); \n"); //$NON-NLS-1$ + writer.write("}; \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " A a; \n"); //$NON-NLS-1$ + writer.write( " a=enum1;//vp1 \n"); //$NON-NLS-1$ + writer.write( " int i=B::enum2;//vp2 \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + writer.write( "void B::boo() { \n"); //$NON-NLS-1$ + writer.write( " f1=enum3;//vp3 \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln global; Seln in function, on rhs of assignment + int start = source.indexOf( "enum1" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 5 ); + assertNodeLocation( node, h, header.indexOf("enum1")); //$NON-NLS-1$ + //vp2 Decln in class, in anon enumeration; Seln in function, on rhs of assignment, in :: scope + start = source.indexOf( "enum2" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 5 ); + assertNodeLocation( node, h, header.indexOf("enum2")); //$NON-NLS-1$ + //vp3 Decln in class, in anon enumeration with field; Seln in method, on rhs of assignment + start = source.indexOf( "enum3" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 5 ); + assertNodeLocation( node, h, header.indexOf("enum3")); //$NON-NLS-1$ + } + public void testMethod() throws Exception{ + Writer writer = new StringWriter(); + writer.write("class A{ \n"); //$NON-NLS-1$ + writer.write("int method1(){} \n"); //$NON-NLS-1$ + writer.write("static const int method2(); \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " A a; \n"); //$NON-NLS-1$ + writer.write( " a.method1();//vp1 \n"); //$NON-NLS-1$ + writer.write( " A *b=new A(); \n"); //$NON-NLS-1$ + writer.write( " b->method1();//vp2 \n"); //$NON-NLS-1$ + writer.write( " A::method2();//vp3 \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln private; Seln in function, in dot reference + int start = source.indexOf( "method1();//vp1" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 7 ); + assertNodeLocation( node, h, header.indexOf("method1")); //$NON-NLS-1$ + //vp2 Decln private; Seln in function, in arrow reference + start = source.indexOf( "method1();//vp2" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 7 ); + assertNodeLocation( node, h, header.indexOf("method1")); //$NON-NLS-1$ + //vp3 Decln private; Seln in function, in scope reference + start = source.indexOf( "method2();//vp3" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 7 ); + assertNodeLocation( node, h, header.indexOf("method2")); //$NON-NLS-1$ //$NON-NLS-2$ + } + public void testMethodRHS78656() throws Exception{ + Writer writer = new StringWriter(); + writer.write("class A{ \n"); //$NON-NLS-1$ + writer.write("int method1(){} \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " A a; \n"); //$NON-NLS-1$ + writer.write( " int i=a.method1();//vp1 \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln private; Seln in dot reference, on RHS of assignment + //defect is node not found + int start = source.indexOf( "method1();//vp1" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 7 ); + assertNodeLocation( node, h, header.indexOf("method1")); //$NON-NLS-1$ + } + public void testMethod78114() throws Exception{ + Writer writer = new StringWriter(); + writer.write("class Point{ \n"); //$NON-NLS-1$ + writer.write(" public: \n"); //$NON-NLS-1$ + writer.write(" Point(): xCoord(0){} \n"); //$NON-NLS-1$ + writer.write(" private: int xCoord; \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " Point &p2 = *(new Point()); \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln public; Seln on rhs in code scope + //defect is class is found rather than constructor + int start = source.indexOf( "Point()" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 5 ); + assertNodeLocation( node, h, header.indexOf("Point()")); //$NON-NLS-1$ + } + public void testMethod78118() throws Exception{ + Writer writer = new StringWriter(); + writer.write("class Point{ \n"); //$NON-NLS-1$ + writer.write(" public: \n"); //$NON-NLS-1$ + writer.write(" Point(): xCoord(0){} \n"); //$NON-NLS-1$ + writer.write(" Point& operator=(const Point &rhs){return *this}; \n"); //$NON-NLS-1$ + writer.write("}; \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " Point a; \n"); //$NON-NLS-1$ + writer.write( " const Point zero; \n"); //$NON-NLS-1$ + writer.write( " a.operator=(zero);//vp1 \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln in class, public; Seln in function, in dot reference + //defect is npe; parser field, greaterContextDuple is null + int start = source.indexOf( "operator=" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 9 ); + assertNodeLocation( node, h, header.indexOf("operator=")); //$NON-NLS-1$ //$NON-NLS-2$ + } + public void testOverloadedMethod78389() throws Exception{ + Writer writer = new StringWriter(); + writer.write("class Point{ \n"); //$NON-NLS-1$ + writer.write(" public: \n"); //$NON-NLS-1$ + writer.write(" void method1(){} \n"); //$NON-NLS-1$ + writer.write(" void method1(int i){} \n"); //$NON-NLS-1$ + writer.write("}; \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " Point a; \n"); //$NON-NLS-1$ + writer.write( " a.method1(3); \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln public; Seln in function, in dot reference + //defect is operation unavailable on current selection + int start = source.indexOf( "method1" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 7 ); + assertNodeLocation( node, h, header.indexOf("method1(int i)")); //$NON-NLS-1$ //$NON-NLS-2$ + + } + public void testConstructor78625() throws Exception{ + Writer writer = new StringWriter(); + writer.write("class Mammal { \n" ); //$NON-NLS-1$ + writer.write(" public: \n" ); //$NON-NLS-1$ + writer.write(" Mammal(bool b): isCarnivore(b){} \n" ); //$NON-NLS-1$ + writer.write(" private: \n" ); //$NON-NLS-1$ + writer.write(" bool isCarnivore; \n" ); //$NON-NLS-1$ + writer.write("}; \n" ); //$NON-NLS-1$ + writer.write("class Bear : Mammal{ \n" ); //$NON-NLS-1$ + writer.write("public: \n" ); //$NON-NLS-1$ + writer.write(" Bear(int s): Mammal(true){}//vp1 \n" ); //$NON-NLS-1$ + writer.write("}; \n" ); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln public; Seln in initializer list of derived class cctor + int start = source.indexOf( "Mammal(true)" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 6 ); + assertNodeLocation( node, cpp, source.indexOf("Mammal(bool b)")); //$NON-NLS-1$ + + } + + public void testClassField() throws Exception{ + Writer writer = new StringWriter(); + writer.write("class A{; \n"); //$NON-NLS-1$ + writer.write("int bee; \n"); //$NON-NLS-1$ + writer.write("}; \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " A *a=new A(); \n"); //$NON-NLS-1$ + writer.write( " a->bee;//vp1 \n"); //$NON-NLS-1$ + writer.write( " A b; \n"); //$NON-NLS-1$ + writer.write( " b.bee;//vp2 \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln in class; Seln in function, in arrow reference + int start = source.indexOf( "bee;//vp1" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 3 ); + assertNodeLocation( node, h, header.indexOf("bee")); //$NON-NLS-1$ + //vp2 Decln in class; Seln in function, in dot reference + start = source.indexOf( "bee;//vp2" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 3 ); + assertNodeLocation( node, h, header.indexOf("bee")); //$NON-NLS-1$ + } + public void testStructField() throws Exception{ + Writer writer = new StringWriter(); + writer.write("namespace N{ \n"); //$NON-NLS-1$ + writer.write("struct A{ \n"); //$NON-NLS-1$ + writer.write("int bee; \n"); //$NON-NLS-1$ + writer.write("}; \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " N::A *a; \n"); //$NON-NLS-1$ + writer.write( " a->bee;//vp1 \n"); //$NON-NLS-1$ + writer.write( " using namespace N; \n"); //$NON-NLS-1$ + writer.write( " A b; \n"); //$NON-NLS-1$ + writer.write( " b.bee;//vp2 \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln in struct in namespace; Seln in function, in arrow reference + int start = source.indexOf( "bee;//vp1" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 3 ); + assertNodeLocation( node, h, header.indexOf("bee")); //$NON-NLS-1$ //$NON-NLS-2$ + //vp2 Decln in struct in namespace; Seln in function, in dot reference + start = source.indexOf( "bee;//vp2" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 3 ); + assertNodeLocation( node, h, header.indexOf("bee")); //$NON-NLS-1$ //$NON-NLS-2$ + } + + public void testNamespace() throws Exception{ + Writer writer = new StringWriter(); + writer.write("namespace N{ \n"); //$NON-NLS-1$ + writer.write("class A{}; \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " N::A a; \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln global; Seln in function + int start = source.indexOf( "N::A a;" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("N")); //$NON-NLS-1$ //$NON-NLS-2$ + + } + public void testNamespace77989() throws Exception{ + Writer writer = new StringWriter(); + writer.write("namespace N{ \n"); //$NON-NLS-1$ + writer.write("class A{}; \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " using namespace N;//vp1 \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln global; Seln in function, in using statement + //defect is decln found at the selection, not in the header + int start = source.indexOf( "N;//vp1" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("N")); //$NON-NLS-1$ + + } + public void testFunction() throws Exception{ + Writer writer = new StringWriter(); + writer.write("namespace N{ \n"); //$NON-NLS-1$ + writer.write("char *foo(){} \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " N::foo();//vp1 \n"); //$NON-NLS-1$ + writer.write( " char* x = N::foo();//vp2 \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln in namespace; Seln in function, in scope reference + int start = source.indexOf( "foo();//vp1" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 3 ); + assertNodeLocation( node, h, header.indexOf("foo")); //$NON-NLS-1$ + //vp2 Decln in namespace; Seln in function, scoped, on rhs of assignment + start = source.indexOf( "N::foo();//vp2" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 6 ); + assertNodeLocation( node, h, header.indexOf("foo")); //$NON-NLS-1$ + } + public void testFunctionArg() throws Exception{ + Writer writer = new StringWriter(); + writer.write("double f(double){return 2.0;}; \n"); //$NON-NLS-1$ + writer.write("double g(double){return 2.0;}; \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "double sum_sq(double (*foo)(double), double d){} \n"); //$NON-NLS-1$ + writer.write( "void hi() {double x = sum_sq(g,g(3.2));} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln global; Seln in function, in actual parameter list of caller + int start = source.indexOf( "g,g(3.2)" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("g")); //$NON-NLS-1$ + //vp2 Decln global; Seln in function, in actual parameter list of caller + start = source.indexOf( "g(3.2)" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("g")); //$NON-NLS-1$ + } + public void testFunctionArg78435() throws Exception{ + Writer writer = new StringWriter(); + writer.write( "double sum_sq(double (*foo)/*vp1*/(double), double d){} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln is inside formal parameter list; Seln is declaration + //defect is that operation is unavailable on current selection + int start = source.indexOf( "foo" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 3 ); + assertNodeLocation( node, cpp, source.indexOf("foo")); //$NON-NLS-1$ //$NON-NLS-2$ + } + + + public void testVariable() throws Exception{ + Writer writer = new StringWriter(); + writer.write("char* a_pc=\"hello\"; \n"); //$NON-NLS-1$ + writer.write("union B{int x; char y;} b_u; \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " int s=(sizeof(a_pc));//vp1 \n"); //$NON-NLS-1$ + writer.write( " b_u=3; \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln global, type char*; Seln in function, on rhs, in actual parameter list of caller + int start = source.indexOf( "a_pc" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 4 ); + assertNodeLocation( node, h, header.indexOf("a_pc")); //$NON-NLS-1$ + //vp2 Decln global, type union; Seln in function + start = source.indexOf( "b_u" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 3 ); + assertNodeLocation( node, h, header.indexOf("b_u")); //$NON-NLS-1$ + } + public void testVariableArg78435() throws Exception{ + + Writer writer = new StringWriter(); + writer.write( "int aa; \n"); //$NON-NLS-1$ + writer.write( "void f(int aa){} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Variable: Decln is in formal parameter list; Seln is itself + //defect is the global aa is found instead + int start = source.indexOf( "aa" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 2 ); + assertNodeLocation( node, cpp, source.indexOf("aa){}")); //$NON-NLS-1$ + } + public void testVariableStruct() throws Exception{ + Writer writer = new StringWriter(); + writer.write("struct C {int i;}; \n"); //$NON-NLS-1$ + writer.write("C c; \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " c.i/*vp1*/=3; \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln global, type struct also defined globally; Seln in function + int start = source.indexOf( "c.i/*vp1*/" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("c;")); //$NON-NLS-1$ //$NON-NLS-2$ + } + + public void testVariableArg() throws Exception{ + Writer writer = new StringWriter(); + writer.write( "int aa=3; \n"); //$NON-NLS-1$ + writer.write( "void f(int aa){//decl \n"); //$NON-NLS-1$ + writer.write( " int bb=aa;//vp1 \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln in formal argument list of function; Seln in function definition, on rhs of assignment + int start = source.indexOf( "aa;" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 2 ); + assertNodeLocation( node, cpp, source.indexOf("aa){//decl")); //$NON-NLS-1$ //$NON-NLS-2$ + } + public void testVariableClass77996() throws Exception{ + Writer writer = new StringWriter(); + writer.write("class C {public: int i;}; \n"); //$NON-NLS-1$ + String header = writer.toString(); + importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " C c; \n"); //$NON-NLS-1$ + writer.write( " c.i/*vp1*/=3; \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln in function, type declared outside function, Seln in function + int start = source.indexOf( "c.i/*vp1*/" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, cpp, source.indexOf("c;")); //$NON-NLS-1$ + } + public void testUnion() throws Exception{ + Writer writer = new StringWriter(); + writer.write("union A{}; \n"); //$NON-NLS-1$ + writer.write("class B{ \n"); //$NON-NLS-1$ + writer.write(" union C{} c; \n"); //$NON-NLS-1$ + writer.write("} \n"); //$NON-NLS-1$ + String header = writer.toString(); + IFile h = importFile( "a.h", header ); //$NON-NLS-1$ + + writer = new StringWriter(); + writer.write( "#include \"a.h\" \n"); //$NON-NLS-1$ + writer.write( "void f(){ \n"); //$NON-NLS-1$ + writer.write( " A a; //vp1 \n"); //$NON-NLS-1$ + writer.write( " B::C c; //vp2 \n"); //$NON-NLS-1$ + writer.write( "} \n"); //$NON-NLS-1$ + String source = writer.toString(); + IFile cpp = importFile( "a.cpp", source ); //$NON-NLS-1$ + //vp1 Decln global; Seln in function + int start = source.indexOf( "A" ); //$NON-NLS-1$ + IASTNode node = getSelection( cpp, start, start + 1 ); + assertNodeLocation( node, h, header.indexOf("A")); //$NON-NLS-1$ + //vp2 Decln in class; Seln in function, scoped + start = source.indexOf( "B::C" ); //$NON-NLS-1$ + node = getSelection( cpp, start, start + 4 ); + assertNodeLocation( node, h, header.indexOf("C")); //$NON-NLS-1$ } } diff --git a/core/org.eclipse.cdt.ui.tests/ChangeLog b/core/org.eclipse.cdt.ui.tests/ChangeLog index 3975942c7ba..491641180d4 100644 --- a/core/org.eclipse.cdt.ui.tests/ChangeLog +++ b/core/org.eclipse.cdt.ui.tests/ChangeLog @@ -1,3 +1,8 @@ +2004-11-16 Andrew Niefer + - created package ui/org.eclipse.ui.tests.regression + - moved ContentAssistRegressionTests to it and created a RefactoringRegressionTests + - created a RegressionTestUISuite + 2004-11-08 Mikhail Voronin New tests for help extension. * ui/org/eclipse/cdt/ui/tests/chelp/CHelpProviderTester.java diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist/ContentAssistRegressionTests.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/regression/ContentAssistRegressionTests.java similarity index 99% rename from core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist/ContentAssistRegressionTests.java rename to core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/regression/ContentAssistRegressionTests.java index 9f0591288d1..4bb64db0b2c 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist/ContentAssistRegressionTests.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/regression/ContentAssistRegressionTests.java @@ -12,7 +12,7 @@ /* * Created on Oct 4, 2004 */ -package org.eclipse.cdt.ui.tests.text.contentassist; +package org.eclipse.cdt.ui.tests.regression; import java.io.StringWriter; diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/regression/RefactoringRegressionTests.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/regression/RefactoringRegressionTests.java new file mode 100644 index 00000000000..4ed857597bf --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/regression/RefactoringRegressionTests.java @@ -0,0 +1,186 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +/* + * Created on Nov 10, 2004 + */ +package org.eclipse.cdt.ui.tests.regression; + +import java.io.StringWriter; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.cdt.core.model.CModelException; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.ISourceReference; +import org.eclipse.cdt.core.tests.SearchRegressionTests; +import org.eclipse.cdt.internal.core.model.CModelManager; +import org.eclipse.cdt.internal.core.model.TranslationUnit; +import org.eclipse.cdt.internal.corext.refactoring.RenameRefactoring; +import org.eclipse.cdt.internal.corext.refactoring.base.IChange; +import org.eclipse.cdt.internal.corext.refactoring.base.ICompositeChange; +import org.eclipse.cdt.internal.corext.refactoring.changes.TranslationUnitChange; +import org.eclipse.cdt.internal.corext.refactoring.rename.RenameElementProcessor; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.text.edits.MultiTextEdit; +import org.eclipse.text.edits.ReplaceEdit; +import org.eclipse.text.edits.TextEdit; + +/** + * @author aniefer + */ +public class RefactoringRegressionTests extends SearchRegressionTests { + + public RefactoringRegressionTests() + { + super(); + } + public RefactoringRegressionTests(String name) + { + super(name); + } + + /** + * + * @param element The CElement to rename + * @param newName The new name for the element + * @return + * @throws Exception + */ + public IChange getRefactorChanges( ISourceReference element, String newName ) throws Exception { + RenameRefactoring fRefactoring = new RenameRefactoring(element); + RenameElementProcessor processor = (RenameElementProcessor) fRefactoring.getProcessor(); + processor.setNewElementName( newName ); + + fRefactoring.checkInput( new NullProgressMonitor() ); + IChange change = fRefactoring.createChange( new NullProgressMonitor() ); + return change; + } + + protected ISourceReference findElementAtOffset( IFile file, int offset ) throws CModelException{ + CModelManager manager = CModelManager.getDefault(); + TranslationUnit tu = (TranslationUnit) manager.create( file, cproject ); + ICElement el = tu.getElementAtOffset( offset ); + return (ISourceReference) ( ( el instanceof ISourceReference ) ? el : null ); + } + + public static Test suite(){ + return suite( true ); + } + public static Test suite( boolean cleanup ) { + TestSuite suite = new TestSuite("RefactoringRegressionTests"); //$NON-NLS-1$ + suite.addTest( new RefactoringRegressionTests("testSimpleRename") ); //$NON-NLS-1$ + + if( cleanup ) + suite.addTest( new RefactoringRegressionTests("cleanupProject") ); //$NON-NLS-1$ + + return suite; + } + + protected void assertTotalChanges( IChange changes, int numChanges ) throws Exception { + int count = countChanges( changes ); + assertEquals( count, numChanges ); + } + + private int countChanges( IChange change ){ + int count = 0; + if( change instanceof ICompositeChange ){ + IChange [] children = ((ICompositeChange) change).getChildren(); + for( int i = 0; i < children.length; i++ ){ + count += countChanges( children[i] ); + } + } else if( change instanceof TranslationUnitChange ){ + count += countEdits( ((TranslationUnitChange) change).getEdit() ); + } + return count; + } + + private int countEdits( TextEdit edit ){ + if( edit instanceof MultiTextEdit ){ + return ((MultiTextEdit) edit).getChildrenSize(); + } + return 1; + } + + /** + * + * @param changes The IChange returned from getRefactorChanges + * @param file The file we expect to see the change in + * @param startOffset The offset of the name that will be changed + * @param numChars The length of the original name that is changing + * @param newText The new text that the element will be renamed to + * @throws Exception + */ + protected void assertChange( IChange changes, IFile file, int startOffset, int numChars, String newText ) throws Exception { + boolean found = false; + if( changes instanceof ICompositeChange ){ + found = checkCompositeChange( (ICompositeChange) changes, file, startOffset, numChars, newText ); + } + + if( !found ) + assertFalse( true ); + } + private boolean checkCompositeChange( ICompositeChange composite, IFile file, int startOffset, int numChars, String newText ){ + boolean found = false; + IChange [] children = composite.getChildren(); + for( int i = 0; i < children.length; i++ ){ + if( children[i] instanceof ICompositeChange ) + found = checkCompositeChange( (ICompositeChange) children[i], file, startOffset, numChars, newText ); + else if( children[i] instanceof TranslationUnitChange ){ + TranslationUnitChange tuChange = (TranslationUnitChange) children[i]; + if( tuChange.getFile().toString().equals( file.toString() ) ){ + found = checkTranslationUnitChange( tuChange, startOffset, numChars, newText ); + } + } + if( found ) + return found; + } + return found; + } + + private boolean checkTranslationUnitChange( TranslationUnitChange change, int startOffset, int numChars, String newText ){ + TextEdit textEdit = change.getEdit(); + if( textEdit instanceof MultiTextEdit ){ + MultiTextEdit multiEdit = (MultiTextEdit) textEdit; + TextEdit [] edits = multiEdit.getChildren(); + for( int i = 0; i < edits.length; i++ ){ + if( edits[i] instanceof ReplaceEdit && checkReplaceEdit( (ReplaceEdit) edits[i], startOffset, numChars, newText ) ) + return true; + } + } else if( textEdit instanceof ReplaceEdit ){ + return checkReplaceEdit( (ReplaceEdit) textEdit, startOffset, numChars, newText ); + } + return false; + } + + private boolean checkReplaceEdit( ReplaceEdit edit, int startOffset, int numChars, String newText ){ + return ( edit.getOffset() == startOffset && edit.getLength() == numChars && edit.getText().equals( newText ) ); + } + + public void testSimpleRename() throws Exception { + StringWriter writer = new StringWriter(); + writer.write( "int boo; \n" ); //$NON-NLS-1$ + writer.write( "void f() { \n" ); //$NON-NLS-1$ + writer.write( " boo++; \n" ); //$NON-NLS-1$ + writer.write( "} \n" ); //$NON-NLS-1$ + + String contents = writer.toString(); + IFile file = importFile( "t.cpp", contents ); //$NON-NLS-1$ + ISourceReference element = findElementAtOffset( file, contents.indexOf( "boo" ) ); //$NON-NLS-1$ + IChange changes = getRefactorChanges( element, "ooga" ); //$NON-NLS-1$ + + assertTotalChanges( changes, 2 ); + assertChange( changes, file, contents.indexOf("boo"), 3, "ooga" ); //$NON-NLS-1$//$NON-NLS-2$ + assertChange( changes, file, contents.indexOf("boo++"), 3, "ooga" ); //$NON-NLS-1$//$NON-NLS-2$ + } +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/regression/RegressionTestsUISuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/regression/RegressionTestsUISuite.java new file mode 100644 index 00000000000..645265d0458 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/regression/RegressionTestsUISuite.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ + +/* + * Created on Nov 16, 2004 + */ +package org.eclipse.cdt.ui.tests.regression; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * @author aniefer + */ +public class RegressionTestsUISuite extends TestSuite { + public RegressionTestsUISuite() { + super(); + } + + public RegressionTestsUISuite(Class theClass, String name) { + super(theClass, name); + } + + public RegressionTestsUISuite(Class theClass) { + super(theClass); + } + + public RegressionTestsUISuite(String name) { + super(name); + } + + public static Test suite() { + final RegressionTestsUISuite suite = new RegressionTestsUISuite(); + + suite.addTest( ContentAssistRegressionTests.suite( false ) ); + suite.addTest( RefactoringRegressionTests.suite( false ) ); + + suite.addTest( new RefactoringRegressionTests("cleanupProject") ); //$NON-NLS-1$ + return suite; + } + +}