diff options
author | mazab | 2015-06-23 13:51:48 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2015-12-03 00:14:42 +0000 |
commit | 241a60fe8a522a0b39586ec5ec93c67a77f2898d (patch) | |
tree | acb9b35a4782e6c293dd7d23ac52638a7c447e7e /core/org.eclipse.cdt.ui.tests/ui | |
parent | e636b977a94cd9b00d5f5437578a08e0945d8ff5 (diff) | |
download | org.eclipse.cdt-241a60fe8a522a0b39586ec5ec93c67a77f2898d.tar.gz org.eclipse.cdt-241a60fe8a522a0b39586ec5ec93c67a77f2898d.tar.xz org.eclipse.cdt-241a60fe8a522a0b39586ec5ec93c67a77f2898d.zip |
Bug 438549 Add mechanism for parameter guessing.
Change-Id: Ib348e401932a9119185dbab8ecacaf80fd3e17ff
Signed-off-by: mazab <mohamed_azab@mentor.com>
Diffstat (limited to 'core/org.eclipse.cdt.ui.tests/ui')
5 files changed, 331 insertions, 11 deletions
diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java index 99b9b83cbcb..e6c78ba6d80 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/AbstractContentAssistTest.java @@ -12,12 +12,15 @@ * Markus Schorn (Wind River Systems) * Thomas Corbat (IFS) * Sergey Prigogin (Google) + * Mohamed Azab (Mentor Graphics) *******************************************************************************/ package org.eclipse.cdt.ui.tests.text.contentassist2; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; @@ -49,6 +52,7 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNameBase; import org.eclipse.cdt.internal.ui.text.contentassist.CCompletionProposal; import org.eclipse.cdt.internal.ui.text.contentassist.CContentAssistProcessor; import org.eclipse.cdt.internal.ui.text.contentassist.ContentAssistPreference; +import org.eclipse.cdt.internal.ui.text.contentassist.ParameterGuessingProposal; import org.eclipse.cdt.internal.ui.text.contentassist.RelevanceConstants; public abstract class AbstractContentAssistTest extends BaseUITestCase { @@ -56,6 +60,18 @@ public abstract class AbstractContentAssistTest extends BaseUITestCase { ID, DISPLAY, REPLACEMENT, CONTEXT, INFORMATION } + private class ContentAssistResult { + long startTime; + long endTime; + Object[] results; + + public ContentAssistResult(long startTime, long endTime, Object[] results) { + this.startTime = startTime; + this.endTime = endTime; + this.results = results; + } + } + protected ICProject fCProject; private IFile fCFile; protected ITextEditor fEditor; @@ -107,8 +123,9 @@ public abstract class AbstractContentAssistTest extends BaseUITestCase { return CUIPlugin.getDefault().getPreferenceStore(); } - protected void assertContentAssistResults(int offset, int length, String[] expected, - boolean isCompletion, boolean isTemplate, boolean filterResults, CompareType compareType) throws Exception { + private ContentAssistResult invokeContentAssist(int offset, int length, + boolean isCompletion, boolean isTemplate, boolean filterResults) + throws Exception { if (CTestPlugin.getDefault().isDebugging()) { System.out.println("\n\n\n\n\nTesting " + this.getClass().getName()); } @@ -138,12 +155,20 @@ public abstract class AbstractContentAssistTest extends BaseUITestCase { results= filterResults(results, isCode); } } - String[] resultStrings= toStringArray(results, compareType); + return new ContentAssistResult(startTime, endTime, results); + } + + protected void assertContentAssistResults(int offset, int length, + String[] expected, boolean isCompletion, boolean isTemplate, + boolean filterResults, CompareType compareType) throws Exception { + ContentAssistResult r = invokeContentAssist(offset, length, isCompletion, isTemplate, filterResults); + + String[] resultStrings= toStringArray(r.results, compareType); Arrays.sort(expected); Arrays.sort(resultStrings); if (CTestPlugin.getDefault().isDebugging()) { - System.out.println("Time: " + (endTime - startTime) + " ms"); + System.out.println("Time: " + (r.endTime - r.startTime) + " ms"); for (String proposal : resultStrings) { System.out.println("Result: " + proposal); } @@ -177,6 +202,77 @@ public abstract class AbstractContentAssistTest extends BaseUITestCase { } } + protected void assertContentAssistResults(int offset, int length, Map<String, String[][]> expected, + boolean isCompletion, boolean isTemplate, boolean filterResults, CompareType compareType) + throws Exception { + ContentAssistResult r = invokeContentAssist(offset, length, isCompletion, isTemplate, filterResults); + Map<String, String[][]> resultMap = toMap(r.results, compareType); + + if (CTestPlugin.getDefault().isDebugging()) { + System.out.println("Time : " + (r.endTime - r.startTime) + " ms"); + for (String proposal : resultMap.keySet()) { + System.out.println("Result: " + proposal); + String[][] result = resultMap.get(proposal); + for (String[] row : result) { + for (String s : row) { + System.out.print(s + " "); + } + System.out.println(); + } + } + System.out.println(); + } + + for (String proposal : expected.keySet()) { + String[][] result = resultMap.get(proposal); + assertNotNull(result); + String[][] expectedGuesses = expected.get(proposal); + String exp = ""; + String guess = ""; + int minLength = expectedGuesses.length < result.length ? expectedGuesses.length : result.length; + for (int i = 0; i < minLength; i++) { + String[] tmp = expectedGuesses[i]; + Arrays.sort(tmp); + exp += toString(tmp) + "\n"; + tmp = result[i]; + Arrays.sort(tmp); + guess += toString(tmp) + "\n"; + } + assertEquals(exp, guess); + } + } + + private Map<String, String[][]> toMap(Object[] results, + CompareType compareType) { + Map<String, String[][]> resultsMap = new HashMap<>(); + for (Object result : results) { + switch (compareType) { + case REPLACEMENT: + if (result instanceof ParameterGuessingProposal) { + ParameterGuessingProposal proposal = (ParameterGuessingProposal) result; + String pName = proposal.getReplacementString(); + ICompletionProposal[][] pProposals = proposal + .getParametersGuesses(); + String[][] p; + if (pProposals != null) { + p = new String[pProposals.length][]; + for (int i = 0; i < pProposals.length; i++) { + p[i] = new String[pProposals[i].length]; + for (int j = 0; j < pProposals[i].length; j++) { + p[i][j] = pProposals[i][j].getDisplayString(); + } + } + } else { + p = new String[0][]; + } + resultsMap.put(pName, p); + } + break; + } + } + return resultsMap; + } + protected void assertContentAssistResults(int offset, int length, String[] expected, boolean isCompletion, boolean isTemplate, CompareType compareType) throws Exception { assertContentAssistResults(offset, length, expected, isCompletion, isTemplate, true, compareType); } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CPPParameterGuessingTests.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CPPParameterGuessingTests.java new file mode 100644 index 00000000000..f6f8883afa6 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CPPParameterGuessingTests.java @@ -0,0 +1,141 @@ +/******************************************************************************* + * Copyright (c) 2015 Mentor Graphics Corporation. + * 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: + * Mohamed Azab (Mentor Graphics) - Initial implementation. + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.text.contentassist2; + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.Test; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; + +import org.eclipse.cdt.core.testplugin.util.BaseTestCase; + +public class CPPParameterGuessingTests extends AbstractContentAssistTest { + private static final String HEADER_FILE_NAME = "PGTest.h"; + private static final String SOURCE_FILE_NAME = "PGTest.cpp"; + + // {PGTest.h} + // class aClass { + // public: + // int aField; + // void aMethod(char c); + // void aMethod(char c, int x); + // }; + // + // class bClass : aClass { + // }; + // + // void overload(int x, aClass a); + // void overload(int x, aClass* aPtr); + // int piab(aClass a, bClass b); + // template<class T>void tFunc(T x, T y); + + public CPPParameterGuessingTests(String name) { + super(name, true); + } + + public static Test suite() { + return BaseTestCase.suite(CPPParameterGuessingTests.class, "_"); + } + + @Override + protected IFile setUpProjectContent(IProject project) throws Exception { + String headerContent = readTaggedComment(HEADER_FILE_NAME); + StringBuilder sourceContent = getContentsForTest(1)[0]; + sourceContent.insert(0, "#include \"" + HEADER_FILE_NAME + "\"\n"); + assertNotNull(createFile(project, HEADER_FILE_NAME, headerContent)); + return createFile(project, SOURCE_FILE_NAME, sourceContent.toString()); + } + + protected void assertParametersGuesses(Map<String, String[][]> expected) + throws Exception { + assertContentAssistResults(getBuffer().length() - 1, 0, expected, true, + false, false, CompareType.REPLACEMENT); + } + + // void foo(){ + // aClass* aTypePtr; + // bClass bTypeObj; + // piab( + public void testIndirectTypes() throws Exception { + Map<String, String[][]> resultsMap = new HashMap<>(); + resultsMap.put("piab(a, b)", new String[][] { { "*aTypePtr", "bTypeObj" }, + { "bTypeObj" } }); + assertParametersGuesses(resultsMap); + } + + // void foo(){ + // int intVal; + // aClass aTypeObj; + // overload( + public void testOverloadedFunction() throws Exception { + Map<String, String[][]> resultsMap = new HashMap<>(); + resultsMap.put("overload(x, a)", new String[][] { { "intVal" }, { "aTypeObj" } }); + resultsMap.put("overload(x, aPtr)", + new String[][] { { "intVal" }, { "&aTypeObj" } }); + assertParametersGuesses(resultsMap); + } + + // void foo(){ + // aClass aTypeObj; + // tFunc<aClass> ( + public void testTemplateFunction() throws Exception { + Map<String, String[][]> resultsMap = new HashMap<>(); + resultsMap.put("tFunc<aClass> (x, y)", new String[][] { { "x" }, + { "y" } }); + assertParametersGuesses(resultsMap); + } + + // struct container { + // aClass* aTypePtr; + // }; + // + // void foo(){ + // char charX, charY, charZ; + // container containerObj; + // containerObj.aTypePtr = new aClass(); + // containerObj.aTypePtr-> + public void testOverloadedMethod() throws Exception { + Map<String, String[][]> resultsMap = new HashMap<>(); + resultsMap.put("aMethod(c)", new String[][] { { "charX", "charY", "charZ" } }); + resultsMap.put("aMethod(c, x)", new String[][] { { "charX", "charY", "charZ" }, + { "charX", "charY", "charZ" } }); + assertParametersGuesses(resultsMap); + } + + // void testParameterNameMatching(int lngName, int shrt); + // + // void foo() { + // int lng; + // int shrtNameMatch; + // testParameter + public void testParameterNameMatching() throws Exception { + Map<String, String[][]> resultsMap = new HashMap<>(); + resultsMap.put("testParameterNameMatching(lngName, shrt)", new String[][] { + { "lng", "shrtNameMatch" }, { "lng", "shrtNameMatch" } }); + assertParametersGuesses(resultsMap); + } + + // class cClass : bClass { + // public: + // cClass(int inCall) { + // char charX, charY; + // aClass:: + public void testInsideConstructor() throws Exception { + Map<String, String[][]> resultsMap = new HashMap<>(); + resultsMap.put("aMethod(c)", new String[][] { { "charX", "charY", "inCall" } }); + resultsMap.put("aMethod(c, x)", new String[][] { + { "charX", "charY"}, { "inCall" } }); + assertParametersGuesses(resultsMap); + } +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CParameterGuessingTests.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CParameterGuessingTests.java new file mode 100644 index 00000000000..5c9b94d6345 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CParameterGuessingTests.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2015 Mentor Graphics Corporation. + * 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: + * Mohamed Azab (Mentor Graphics) - Initial implementation. + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.text.contentassist2; + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.Test; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; + +import org.eclipse.cdt.core.testplugin.util.BaseTestCase; + +public class CParameterGuessingTests extends AbstractContentAssistTest { + private static final String HEADER_FILE_NAME = "PGTest_C.h"; + private static final String SOURCE_FILE_NAME = "PGTest_C.c"; + + // {PGTest_C.h} + // typedef struct aStruct { + // int a; + // int b; + // } aStruct; + // + // void ov1(int x, aStruct a); + // void ov2(int x, aStruct* aPtr); + // int funWith2ATypeObjectParams(aStruct a, aStruct b); + + public CParameterGuessingTests(String name) { + super(name, false); + } + + public static Test suite() { + return BaseTestCase.suite(CParameterGuessingTests.class, "_"); + } + + @Override + protected IFile setUpProjectContent(IProject project) throws Exception { + String headerContent = readTaggedComment(HEADER_FILE_NAME); + StringBuilder sourceContent = getContentsForTest(1)[0]; + sourceContent.insert(0, "#include \"" + HEADER_FILE_NAME + "\"\n"); + assertNotNull(createFile(project, HEADER_FILE_NAME, headerContent)); + return createFile(project, SOURCE_FILE_NAME, sourceContent.toString()); + } + + protected void assertParametersGuesses(Map<String, String[][]> expected) throws Exception { + assertContentAssistResults(getBuffer().length() - 1, 0, expected, true, + false, false, CompareType.REPLACEMENT); + } + + // void foo(){ + // aStruct* axPtr; + // aStruct ax; + // funWith2ATypeObjectParams( + public void testIndirectTypes() throws Exception { + Map<String, String[][]> resultsMap = new HashMap<>(); + resultsMap.put("funWith2ATypeObjectParams(a, b)", new String[][] { { "ax", "*axPtr" }, + { "ax", "*axPtr" } }); + assertParametersGuesses(resultsMap); + } + + // void foo(){ + // aStruct ax; + // ov + public void testMultipleFunctions() throws Exception { + Map<String, String[][]> resultsMap = new HashMap<>(); + resultsMap.put("ov1(x, a)", new String[][] { { "x" }, { "ax" } }); + resultsMap.put("ov2(x, aPtr)", new String[][] { { "x" }, { "&ax" } }); + assertParametersGuesses(resultsMap); + } +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTests.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTests.java index 85bcbc33ecd..a3325c6fcdd 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTests.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2014 Wind River Systems, Inc. and others. + * Copyright (c) 2006, 2015 Wind River Systems, Inc. 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 @@ -15,6 +15,7 @@ * Nathan Ridge * Thomas Corbat (IFS) * Michael Woski + * Mohamed Azab (Mentor Graphics) - Bug 438549. Add mechanism for parameter guessing. *******************************************************************************/ package org.eclipse.cdt.ui.tests.text.contentassist2; @@ -855,7 +856,7 @@ public class CompletionTests extends AbstractContentAssistTest { // Printer::/*cursor*/ public void testPrivateStaticMember_109480() throws Exception { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=109480 - final String[] expected= { "InitPrinter()", "port" }; + final String[] expected= { "InitPrinter(port)", "port" }; assertCompletionResults(fCursorOffset, expected, REPLACEMENT); } @@ -1261,7 +1262,7 @@ public class CompletionTests extends AbstractContentAssistTest { // } //}; public void testContentAssistInDeferredClassInstance_194592() throws Exception { - final String[] expected= { "add()" }; + final String[] expected= { "add(tOther)" }; assertCompletionResults(fCursorOffset, expected, REPLACEMENT); } @@ -1428,7 +1429,7 @@ public class CompletionTests extends AbstractContentAssistTest { // } // using N::f/*cursor*/ public void testUsingDeclaration_379631() throws Exception { - final String[] expected= { "foo;" }; + final String[] expected= { "foo()" }; assertCompletionResults(fCursorOffset, expected, REPLACEMENT); } @@ -1575,7 +1576,7 @@ public class CompletionTests extends AbstractContentAssistTest { // } // using N::fo/*cursor*/; public void testUsingCompletionWithFollowingSemicolon() throws Exception { - final String[] expected = { "foo" }; + final String[] expected = { "foo()" }; assertContentAssistResults(fCursorOffset, expected, true, REPLACEMENT); final String[] expectedInformation = { "null" }; assertContentAssistResults(fCursorOffset, expectedInformation, true, CONTEXT); @@ -1613,7 +1614,7 @@ public class CompletionTests extends AbstractContentAssistTest { setDisplayDefaultArguments(true); final String[] expectedDisplay = { "default_argument(int i = 23) : void" }; assertContentAssistResults(fCursorOffset, expectedDisplay, true, DISPLAY); - final String[] expectedReplacement = { "default_argument()" }; + final String[] expectedReplacement = { "default_argument(i)" }; assertContentAssistResults(fCursorOffset, expectedReplacement, true, REPLACEMENT); } diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java index 7a064666c2d..354cb986d25 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/ContentAssist2TestSuite.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2007 Siemens AG and others. + * Copyright (c) 2006, 2015 Siemens AG and others. * All rights reserved. This content 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 @@ -9,6 +9,7 @@ * Norbert Ploett - Initial implementation * Bryan Wilkinson (QNX) * Andrew Ferguson (Symbian) + * Mohamed Azab (Mentor Graphics) - Bug 438549. Add mechanism for parameter guessing. *******************************************************************************/ package org.eclipse.cdt.ui.tests.text.contentassist2; @@ -71,6 +72,8 @@ public class ContentAssist2TestSuite extends TestSuite { addTest(CompletionTests.suite()); addTest(CompletionTests_PlainC.suite()); addTest(ParameterHintTests.suite()); + addTest(CPPParameterGuessingTests.suite()); + addTest(CParameterGuessingTests.suite()); addTest(ShowCamelCasePreferenceTest.suite()); |