Skip to content

Commit fd3ead7

Browse files
committed
incomplete selection done.
1 parent 42d182a commit fd3ead7

30 files changed

+334
-134
lines changed

org.cpputest.plugin.SWTBotTest/src/org/cpputest/plugin/SWTBotTest/ContextMenuTest.java

-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
import static org.junit.Assert.*;
44

5-
import org.eclipse.core.resources.IWorkspace;
6-
import org.eclipse.core.resources.IWorkspaceRoot;
7-
import org.eclipse.core.resources.ResourcesPlugin;
85
import org.eclipse.core.runtime.CoreException;
9-
import org.eclipse.core.runtime.NullProgressMonitor;
106
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
117

128
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor;

org.cpputest.plugin.SWTBotTest/src/org/cpputest/plugin/SWTBotTest/CppProjectTestBase.java

+21
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
2121
import org.eclipse.swtbot.swt.finder.results.VoidResult;
2222
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
23+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
2324
import org.eclipse.ui.IEditorReference;
2425
import org.eclipse.ui.PlatformUI;
2526
import org.hamcrest.Matcher;
2627
import org.hamcrest.Matchers;
2728
import org.hamcrest.core.IsInstanceOf;
2829
import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartName;
30+
import static org.junit.Assert.assertTrue;
2931

3032
public class CppProjectTestBase {
3133
SWTWorkbenchBot bot = new SWTWorkbenchBot();
@@ -45,6 +47,16 @@ protected void createCppProject(String ProjectName) throws CoreException {
4547
protected void deleteProject(String projectName) throws CoreException {
4648
root.getProject(projectName).delete(true, true, new NullProgressMonitor());
4749
}
50+
51+
protected void clearClipboard() {
52+
syncExec(new VoidResult() {
53+
public void run() {
54+
Clipboard clipboard = new Clipboard(PlatformUI.getWorkbench()
55+
.getActiveWorkbenchWindow().getShell().getDisplay());
56+
clipboard.clearContents();
57+
}
58+
});
59+
}
4860

4961
protected String getClipboardContent() {
5062
final StringBuilder content = new StringBuilder();
@@ -69,7 +81,16 @@ public void run() {
6981
});
7082

7183
}
84+
protected void shouldSeeUnableToGenerateStubMessagebox(String string) {
85+
SWTBotShell shell = bot.shell("CppUTest");
86+
shell.activate();
87+
assertTrue(null != bot.label(string));
88+
bot.button("OK").click();
89+
}
7290

91+
protected void fireTheCopyEmptyStubToClipboardMenuItem() {
92+
bot.menu("CppUTest").menu("Copy Empty Stub To Clipboard").click();
93+
}
7394
@SuppressWarnings("unchecked")
7495
protected SWTBotEclipseEditor createNewCppFile(String projectName, String fileName, String content) throws CoreException {
7596
IProject project = root.getProject(projectName);

org.cpputest.plugin.SWTBotTest/src/org/cpputest/plugin/SWTBotTest/EmptyCStubTest.java

+3-19
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor;
88
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
99
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
10-
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
1110
import org.junit.After;
1211
import org.junit.AfterClass;
1312
import org.junit.Before;
1413
import org.junit.BeforeClass;
15-
import org.junit.Ignore;
1614
import org.junit.Test;
1715
import org.junit.runner.RunWith;
1816

@@ -22,9 +20,9 @@ public class EmptyCStubTest extends CppProjectTestBase {
2220
private static final String GENERAL_PROJECT_FOR_TESTING = "GeneralProjectForTesting";
2321

2422
@Before
25-
public void setupProject() throws CoreException {
26-
bot.perspectiveByLabel("C/C++").activate();
23+
public void setupProjectAndClearClipboard() throws CoreException {
2724
createCppProject(GENERAL_PROJECT_FOR_TESTING);
25+
clearClipboard();
2826
}
2927
@After
3028
public void cleanProject() throws CoreException{
@@ -65,23 +63,9 @@ public void testCopyEmptyStubCanIgnoreCppComment() throws CoreException {
6563
assertEquals("void fun4(void){}\n", getClipboardContent());
6664
}
6765

68-
@Ignore("still under development")
69-
@Test
70-
public void testStillAbleToCreateStubWhenSelectedPartOfTheSignature() throws CoreException {
71-
copyEmptyStubOfCodeToClipboard("// /* \n void //\"\nfun5(void//xxx\n)//\n;//\n");
72-
assertEquals("void fun5(void){}\n", getClipboardContent());
73-
}
74-
75-
private void shouldSeeUnableToGenerateStubMessagebox(String string) {
76-
SWTBotShell shell = bot.shell("CppUTest");
77-
shell.activate();
78-
assertTrue(null != bot.label(string));
79-
bot.button("OK").click();
80-
81-
}
8266
protected void copyEmptyStubOfCodeToClipboard(String signature) throws CoreException {
8367
SWTBotEclipseEditor editor = createNewCppFile(GENERAL_PROJECT_FOR_TESTING, "example.h", signature);
8468
editor.selectRange(0,0,1000);
85-
bot.menu("CppUTest").menu("Copy Empty Stub To Clipboard").click();
69+
fireTheCopyEmptyStubToClipboardMenuItem();
8670
}
8771
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.cpputest.plugin.SWTBotTest;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.eclipse.core.runtime.CoreException;
6+
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
7+
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor;
8+
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
9+
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
10+
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
11+
import org.junit.After;
12+
import org.junit.AfterClass;
13+
import org.junit.Before;
14+
import org.junit.BeforeClass;
15+
import org.junit.Ignore;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
19+
@RunWith(SWTBotJunit4ClassRunner.class)
20+
public class IncompleteSelectionTest extends CppProjectTestBase {
21+
22+
private static final String GENERAL_PROJECT_FOR_TESTING = "GeneralProjectForTesting";
23+
24+
@Before
25+
public void setupProjectAndClearClipboard() throws CoreException {
26+
createCppProject(GENERAL_PROJECT_FOR_TESTING);
27+
clearClipboard();
28+
}
29+
@After
30+
public void cleanProject() throws CoreException{
31+
deleteProject(GENERAL_PROJECT_FOR_TESTING);
32+
}
33+
@BeforeClass
34+
static public void increaseTimeOut() {
35+
SWTBotPreferences.TIMEOUT = 20000;
36+
}
37+
@AfterClass
38+
static public void waitForAWhile(){
39+
SWTWorkbenchBot bot = new SWTWorkbenchBot();
40+
bot.sleep(2000);
41+
}
42+
@Test
43+
public void onlySelectedFunctionsShouldBeUsedWhenSelectCompleteFunctionSignature() throws CoreException {
44+
SWTBotEclipseEditor editor = openAnEditorWith3Functions();
45+
editor.selectLine(1);
46+
fireTheCopyEmptyStubToClipboardMenuItem();
47+
assertEquals("void fun2(){}\n", getClipboardContent());
48+
}
49+
@Test
50+
public void theFunctionAfterTheCurserShouldBeUsedWhenPutCurserAtTheBeginingOfTheSignature() throws CoreException {
51+
SWTBotEclipseEditor editor = openAnEditorWith3Functions();
52+
editor.selectRange(1, 0, 0);
53+
fireTheCopyEmptyStubToClipboardMenuItem();
54+
assertEquals("void fun2(){}\n", getClipboardContent());
55+
}
56+
@Test
57+
public void theFunctionOnTheCurserShouldBeUsedWhenPutCurserInTheMiddleOfTheSignature() throws CoreException {
58+
SWTBotEclipseEditor editor = openAnEditorWith3Functions();
59+
editor.selectRange(1, 7, 0);
60+
fireTheCopyEmptyStubToClipboardMenuItem();
61+
assertEquals("void fun2(){}\n", getClipboardContent());
62+
}
63+
@Test
64+
public void onlyTheCompleteFunctionsInTheSelectionShouldBeUsedWhenSelectMultipleSignature() throws CoreException {
65+
SWTBotEclipseEditor editor = openAnEditorWith3Functions();
66+
editor.selectRange(0, 7 //start from the middle of fun1
67+
, 39 // end at the middle of fun4
68+
);
69+
fireTheCopyEmptyStubToClipboardMenuItem();
70+
assertEquals("void fun2(){}\nvoid fun3(){}\n", getClipboardContent());
71+
}
72+
73+
protected SWTBotEclipseEditor openAnEditorWith3Functions()
74+
throws CoreException {
75+
SWTBotEclipseEditor editor = createNewCppFile(GENERAL_PROJECT_FOR_TESTING, "example.h",
76+
"void fun1();"+System.getProperty("line.separator")+
77+
"void fun2();"+System.getProperty("line.separator")+
78+
"void fun3();"+System.getProperty("line.separator")+
79+
"void fun4();");
80+
return editor;
81+
}
82+
}

org.cpputest.plugin.unittest/.classpath

+1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@
4141
</accessrules>
4242
</classpathentry>
4343
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
44+
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
4445
<classpathentry kind="output" path="bin"/>
4546
</classpath>

org.cpputest.plugin.unittest/src/org/cpputest/action/test/CppUTestActionsTest.java

+34-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
import org.jmock.integration.junit4.JUnit4Mockery;
1212
import org.junit.Test;
1313
import org.junit.runner.RunWith;
14+
import org.eclipse.jface.text.Position;
15+
1416

1517
@RunWith(JMock.class)
1618
public class CppUTestActionsTest {
1719
Mockery context = new JUnit4Mockery();
1820
final String EXPECTED_STUB = "stub";
1921
final String SOURCE_CODE = "code";
22+
final String ALL_CODE = "all code";
2023
final CppUTestPlatform platform = context.mock(CppUTestPlatform.class);
2124
final UnitTestCodeGenerator codeGenerator = context.mock(UnitTestCodeGenerator.class);
2225
final CppCodeFormater formater = context.mock(CppCodeFormater.class);
@@ -35,13 +38,42 @@ public void testCopyEmptyStubOfSelectedCodeToClipboard() {
3538
cpputest.copyEmptyStubOfSelectedCodeToClipboard();
3639
}
3740
@Test
38-
public void testShouldAlertWhenNoFunctionSelect() {
39-
final CppCode code = new CppCode();
41+
public void testShouldTryAgainWithFullTextParsingWhenNoFunctionIsSelected() {
42+
final CppCode emptyCode = new CppCode();
43+
final CppCode code = new CppCode("not empty");
44+
final Position pos = new Position(1);
4045
context.checking(new Expectations() {{
4146
allowing(platform).getSelectedText();
4247
will(returnValue(SOURCE_CODE));
4348
oneOf(codeGenerator).getEmptyStubOfCode(SOURCE_CODE);
49+
will(returnValue(emptyCode));
50+
allowing(platform).getFullText();
51+
will(returnValue(ALL_CODE));
52+
allowing(platform).getCursorPosition();
53+
will(returnValue(pos));
54+
oneOf(codeGenerator).getEmptyStubOfCodeAtPosition(ALL_CODE, 1);
4455
will(returnValue(code));
56+
oneOf(formater).format(code); will(returnValue(EXPECTED_STUB));
57+
oneOf(platform).copyToClipboard(EXPECTED_STUB);
58+
}});
59+
CppUTestActions cpputest = new CppUTestActions(platform, codeGenerator, formater);
60+
cpputest.copyEmptyStubOfSelectedCodeToClipboard();
61+
}
62+
@Test
63+
public void testShouldAlertWhenNoFunctionSelect() {
64+
final CppCode emptyCode = new CppCode();
65+
final Position pos = new Position(1);
66+
context.checking(new Expectations() {{
67+
allowing(platform).getSelectedText();
68+
will(returnValue(SOURCE_CODE));
69+
oneOf(codeGenerator).getEmptyStubOfCode(SOURCE_CODE);
70+
will(returnValue(emptyCode));
71+
allowing(platform).getFullText();
72+
will(returnValue(ALL_CODE));
73+
allowing(platform).getCursorPosition();
74+
will(returnValue(pos));
75+
oneOf(codeGenerator).getEmptyStubOfCodeAtPosition(ALL_CODE, 1);
76+
will(returnValue(emptyCode));
4577
oneOf(platform).messageBox("No function is selected.");
4678
}});
4779
CppUTestActions cpputest = new CppUTestActions(platform, codeGenerator, formater);

org.cpputest.plugin.unittest/src/org/cpputest/parser/impl/test/CppFunctionSignatureReadingTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import org.junit.Test;
1313

1414
public class CppFunctionSignatureReadingTest {
15+
private static final int BEGIN_OFFSET = 11;
16+
private static final int END_OFFSET = 18;
1517
List<LanguageUnit> units;
1618
List<CppPart> parts = new ArrayList<CppPart>();
1719
@Test
@@ -91,6 +93,7 @@ public void testParseFunctionWithPriorMacroOnSameLine() {
9193
assertEquals(1, units.size());
9294
assertEquals("const int foo()", units.get(0).getCode().toString());
9395
}
96+
@Test
9497
public void testFunctionPointerIsNotASignature() {
9598
parts.add(CppPart.StartNewFunction(new Token("int")));
9699
parts.add(CppPart.Parameter(new Token("*")));
@@ -99,6 +102,17 @@ public void testFunctionPointerIsNotASignature() {
99102
units = getLanguageUnits(parts);
100103
assertEquals(0, units.size());
101104
}
105+
@Test
106+
public void testBeginAndEndOffsetOfFunctionSignature() {
107+
parts.add(CppPart.StartNewFunction(Token.token("int", BEGIN_OFFSET)));
108+
parts.add(CppPart.AddToFunctionName(new Token("foo")));
109+
parts.add(CppPart.EndOfFunctionSignature(Token.token(")", END_OFFSET)));
110+
LanguageUnit unit = getLanguageUnits(parts).get(0);
111+
assertFalse(unit.isOffsetInclusive(BEGIN_OFFSET-1));
112+
assertTrue(unit.isOffsetInclusive(BEGIN_OFFSET));
113+
assertFalse(unit.isOffsetInclusive(END_OFFSET+1));
114+
115+
}
102116

103117
private List<LanguageUnit> getLanguageUnits(Iterable<CppPart> parts) {
104118
final ArrayList<LanguageUnit> list = new ArrayList<LanguageUnit>();

org.cpputest.plugin.unittest/src/org/cpputest/parser/impl/test/CppLikeCodeTokenSplitterTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public void testKeyword() {
2727
assertEquals("int", tokens.get(0).toString());
2828
}
2929
@Test
30+
public void firstTokenOffsetShouldBe0() {
31+
List<Token> tokens = tokenize("int");
32+
assertEquals(0, tokens.get(0).getBeginOffset());
33+
}
34+
@Test
35+
public void firstTokenOffsetShouldBe1IfThereIsASpaceBeforeIt() {
36+
List<Token> tokens = tokenize(" int");
37+
assertEquals(1, tokens.get(0).getBeginOffset());
38+
}
39+
@Test
3040
public void testFunction() {
3141
List<Token> tokens = tokenize("int fun(){}");
3242
assertEquals(6, tokens.size());

org.cpputest.plugin.unittest/src/org/cpputest/parser/impl/test/SignatureBuilderTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
public class SignatureBuilderTest {
1010

11+
private static final int END_OFFSET = 15;
12+
private static final int BEGIN_OFFSET = 10;
1113
@Test
1214
public void testBuildFunctionWithoutParameterAndVoidReturnType() {
1315
CppLangFunctionSignature signature = new SignatureBuilder("void")
@@ -48,5 +50,17 @@ public void testExtraStuffBeforePointerReturnType() {
4850
assertEquals("fun", signature.getFunctionName());
4951
assertEquals("EXTRA int * fun()", signature.getCode().toString());
5052
}
53+
@Test
54+
public void testBuildWithOffsetInforamtion() {
55+
CppLangFunctionSignature signature = new SignatureBuilder("void")
56+
.withBeginOffset(BEGIN_OFFSET)
57+
.addToFunctionDeclaration("foo")
58+
.withEndOffset(END_OFFSET)
59+
.build();
60+
assertFalse(signature.isOffsetInclusive(BEGIN_OFFSET - 1));
61+
assertTrue(signature.isOffsetInclusive(BEGIN_OFFSET));
62+
assertTrue(signature.isOffsetInclusive(END_OFFSET));
63+
assertFalse(signature.isOffsetInclusive(END_OFFSET + 1));
64+
}
5165

5266
}

0 commit comments

Comments
 (0)