├── README.txt ├── org.cpputest.plugin.SWTBotTest ├── .classpath ├── .gitignore ├── .project ├── META-INF │ └── MANIFEST.MF ├── build.properties ├── pom.xml └── src │ └── org │ └── cpputest │ └── plugin │ └── SWTBotTest │ ├── ContextMenuTest.java │ ├── CppProjectTestBase.java │ ├── DefaultMockTest.java │ ├── EmptyCStubTest.java │ └── IncompleteSelectionTest.java ├── org.cpputest.plugin.feature ├── .gitignore ├── .project ├── build.properties ├── feature.xml └── pom.xml ├── org.cpputest.plugin.unittest ├── .classpath ├── .gitignore ├── .project ├── META-INF │ └── MANIFEST.MF ├── build.properties ├── lib │ ├── bsh-core-2.0b4.jar │ ├── cglib-2.1_3-src.jar │ ├── cglib-nodep-2.1_3.jar │ ├── hamcrest-core-1.1.jar │ ├── hamcrest-library-1.1.jar │ ├── jmock-2.5.1.jar │ ├── jmock-junit3-2.5.1.jar │ ├── jmock-junit4-2.5.1.jar │ ├── jmock-legacy-2.5.1.jar │ ├── jmock-script-2.5.1.jar │ └── objenesis-1.0.jar ├── pom.xml └── test │ └── org │ └── cpputest │ ├── codeGenerator │ └── test │ │ ├── CppDefautlMockStubberTest.java │ │ ├── CppEmptyStubberTest.java │ │ └── CppUTestCodeGeneratorTest.java │ ├── parser │ ├── impl │ │ └── test │ │ │ ├── CppFunctionSignatureReadingTest.java │ │ │ ├── CppLikeCodeTokenSplitterTest.java │ │ │ ├── PotentialLanguagePartsTest.java │ │ │ └── SignatureBuilderTest.java │ └── test │ │ └── CppSourceCodeReaderEndToEndTest.java │ └── plugin │ ├── actions │ └── test │ │ ├── CppUTestActionCopyDefaultMockToClipboardTest.java │ │ ├── CppUTestActionCopyEmptyStubToClipboardTest.java │ │ └── CppUTestActionTest.java │ └── test │ ├── CppUTestFactoryTest.java │ ├── CppUTestSourceCodeStubberForEditorTest.java │ └── CppUTestStubCodeUITest.java ├── org.cpputest.plugin ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── META-INF │ └── MANIFEST.MF ├── build.properties ├── icons │ └── sample.gif ├── plugin.xml ├── pom.xml └── src │ └── org │ └── cpputest │ ├── codeGenerator │ ├── CUTPlatformAdaptor.java │ ├── CompactCppCodeFormater.java │ ├── CppCode.java │ ├── CppCodeFormater.java │ ├── CppDefaultMockStubber.java │ ├── CppEmptyStubber.java │ ├── CppUTestPlatform.java │ ├── CppUTestStubCreator.java │ ├── SourceCodeProvider.java │ ├── SourceCodeResource.java │ ├── Stubber.java │ └── WhoCreateStubFromSourceCode.java │ ├── parser │ ├── CppSourceCodeReader.java │ ├── SourceCodeReader.java │ ├── impl │ │ ├── CppLikeCodeTokenSplitter.java │ │ ├── CppPotentialLanguagePartsToMeaningfulLanguageUnitsTranslator.java │ │ ├── CppTokensToPotentialLanguagePartsTranslator.java │ │ └── Token.java │ ├── langunit │ │ ├── CppLangFunctionImplementation.java │ │ ├── CppLangFunctionSignature.java │ │ └── LanguageUnit.java │ └── parts │ │ └── impl │ │ └── parts │ │ └── CppPart.java │ └── plugin │ ├── Activator.java │ ├── CppUTestEclipsePlatform.java │ ├── CppUTestFactory.java │ ├── CppUTestSourceCodeStubberForEditor.java │ ├── CppUTestStubCodeUI.java │ ├── ICppUTestFactory.java │ ├── SourceCodeStubberForEditor.java │ ├── StubCodeUI.java │ └── actions │ ├── CppUTestAction.java │ ├── CppUTestActionCopyDefaultMockToClipboard.java │ └── CppUTestActionCopyEmptyStubToClipboard.java ├── pom.xml └── update-site ├── .gitignore ├── .project ├── pom.xml ├── site.xml └── target ├── site.zip └── site ├── artifacts.jar ├── content.jar └── site.xml /README.txt: -------------------------------------------------------------------------------- 1 | CppUTest Eclipse Plugin 2 | ======================== 3 | CppUTest is a general purposed unit test framework for C/C++. This plugin is made to make unit testing esier to work with CppUTest & Eclipse. 4 | The goal of this plugin is roughly: 5 | 1. Act like JUnit plugin (red/green bars!) 6 | 2. Support Test-Driven Developent 7 | 3. Make unit testing for legacy code easier 8 | 9 | 10 | How To Build 11 | ------------------ 12 | mvn install 13 | 14 | This will run the unit test & integration test and then generate the update package/site. 15 | The update site is at update-site/target 16 | 17 | How To Work On It 18 | ------------------- 19 | 1.clone the project 20 | 2.import projects from the folder 21 | 22 | Each folder is a eclipse project, they are: 23 | org.cpputest.plugin The general plug-in implementation. 24 | This part will do stub/mock code generation. 25 | It does not depend on CDT 26 | org.cpputest.runtools(planning) The cpputest run tools. 27 | It will depend on CDT 28 | org.cpputest.feature Feature description of the plug-in 29 | org.cpputest.plugin.unittest Unit tests 30 | SWTBotTest Integration test 31 | 32 | In order to edit SWTBotTest project, you might need to install SWTBot eclipse plug-in. 33 | -------------------------------------------------------------------------------- /org.cpputest.plugin.SWTBotTest/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /org.cpputest.plugin.SWTBotTest/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /screenshots 3 | -------------------------------------------------------------------------------- /org.cpputest.plugin.SWTBotTest/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.cpputest.plugin.SWTBotTest 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.pde.ManifestBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.pde.SchemaBuilder 20 | 21 | 22 | 23 | 24 | 25 | org.eclipse.pde.PluginNature 26 | org.eclipse.jdt.core.javanature 27 | 28 | 29 | -------------------------------------------------------------------------------- /org.cpputest.plugin.SWTBotTest/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Bundle-ManifestVersion: 2 3 | Bundle-Name: SWTBotTest 4 | Bundle-SymbolicName: org.cpputest.plugin.SWTBotTest;singleton:=true 5 | Bundle-Version: 1.0.0.qualifier 6 | Bundle-ActivationPolicy: lazy 7 | Fragment-Host: org.cpputest.plugin.general;bundle-version="1.0.0" 8 | Require-Bundle: org.eclipse.swtbot.go, 9 | org.eclipse.swtbot.swt.finder;bundle-version="2.0.5", 10 | org.junit;bundle-version="4.8.1", 11 | org.eclipse.swtbot.junit4_x;bundle-version="2.0.5" 12 | -------------------------------------------------------------------------------- /org.cpputest.plugin.SWTBotTest/build.properties: -------------------------------------------------------------------------------- 1 | source.. = src/ 2 | output.. = bin/ 3 | bin.includes = META-INF/,\ 4 | . 5 | -------------------------------------------------------------------------------- /org.cpputest.plugin.SWTBotTest/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | org.cpputest.plugin.parent 5 | org.cpputest.plugin 6 | 1.0.0-SNAPSHOT 7 | .. 8 | 9 | org.cpputest.plugin.SWTBotTest 10 | org.cpputest.plugin.SWTBotTest 11 | eclipse-test-plugin 12 | 13 | 14 | 15 | 16 | 17 | 18 | org.eclipse.tycho 19 | tycho-surefire-plugin 20 | ${tycho.version} 21 | 22 | true 23 | false 24 | ${os-jvm-flags} 25 | 26 | 27 | 30 | p2-installable-unit 31 | org.hamcrest 32 | 0.0.0 33 | 34 | 35 | p2-installable-unit 36 | org.eclipse.cdt.feature.group 37 | 7.0.2 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | macosx-jvm-flags 47 | 48 | mac 49 | 50 | 51 | -XstartOnFirstThread 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /org.cpputest.plugin.SWTBotTest/src/org/cpputest/plugin/SWTBotTest/ContextMenuTest.java: -------------------------------------------------------------------------------- 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 | 8 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor; 9 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; 10 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; 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 ContextMenuTest extends CppProjectTestBase { 21 | SWTBotEclipseEditor editor; 22 | private static final String GENERAL_PROJECT_FOR_TESTING = "GeneralProjectForTesting"; 23 | 24 | @Before 25 | public void setupProject() throws CoreException { 26 | 27 | createCppProject(GENERAL_PROJECT_FOR_TESTING); 28 | editor = createNewCppFile(GENERAL_PROJECT_FOR_TESTING, "example.h", ""); 29 | } 30 | @After 31 | public void cleanProject() throws CoreException{ 32 | deleteProject(GENERAL_PROJECT_FOR_TESTING); 33 | } 34 | @BeforeClass 35 | static public void increaseTimeOut() { 36 | SWTBotPreferences.TIMEOUT = 20000; 37 | } 38 | @AfterClass 39 | static public void waitForAWhile(){ 40 | SWTWorkbenchBot bot = new SWTWorkbenchBot(); 41 | bot.sleep(2000); 42 | } 43 | @Ignore("Cannot run due to a bug in SWTBot, where a popup menu added by plugin.xml cannot be found.") 44 | @Test 45 | public void testCopyEmptyStubToClipboard() { 46 | editor.setText("void bar();"); 47 | editor.save(); 48 | editor.selectRange(0,0,1000); 49 | clickContextMenu(editor, "Copy Empty Stub To Clipboard"); 50 | assertEquals("void bar(){}\n", getClipboardContent()); 51 | } 52 | } -------------------------------------------------------------------------------- /org.cpputest.plugin.SWTBotTest/src/org/cpputest/plugin/SWTBotTest/CppProjectTestBase.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin.SWTBotTest; 2 | 3 | import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec; 4 | 5 | import java.io.ByteArrayInputStream; 6 | 7 | import org.eclipse.core.resources.IFile; 8 | import org.eclipse.core.resources.IProject; 9 | import org.eclipse.core.resources.IResource; 10 | import org.eclipse.core.resources.IWorkspace; 11 | import org.eclipse.core.resources.IWorkspaceRoot; 12 | import org.eclipse.core.resources.ResourcesPlugin; 13 | import org.eclipse.core.runtime.CoreException; 14 | import org.eclipse.core.runtime.NullProgressMonitor; 15 | import org.eclipse.swt.dnd.Clipboard; 16 | import org.eclipse.swt.dnd.TextTransfer; 17 | import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot; 18 | import org.eclipse.swtbot.eclipse.finder.waits.Conditions; 19 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEclipseEditor; 20 | import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor; 21 | import org.eclipse.swtbot.swt.finder.results.VoidResult; 22 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; 23 | import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; 24 | import org.eclipse.ui.IEditorReference; 25 | import org.eclipse.ui.PlatformUI; 26 | import org.hamcrest.Matcher; 27 | import org.hamcrest.Matchers; 28 | import org.hamcrest.core.IsInstanceOf; 29 | import static org.eclipse.swtbot.eclipse.finder.matchers.WidgetMatcherFactory.withPartName; 30 | import static org.junit.Assert.assertTrue; 31 | 32 | public class CppProjectTestBase { 33 | SWTWorkbenchBot bot = new SWTWorkbenchBot(); 34 | IWorkspace workspace = ResourcesPlugin.getWorkspace(); 35 | IWorkspaceRoot root = workspace.getRoot(); 36 | 37 | public CppProjectTestBase() { 38 | super(); 39 | } 40 | 41 | protected void createCppProject(String ProjectName) throws CoreException { 42 | IProject project = root.getProject(ProjectName); 43 | project.create(new NullProgressMonitor()); 44 | project.open(new NullProgressMonitor()); 45 | } 46 | 47 | protected void deleteProject(String projectName) throws CoreException { 48 | root.getProject(projectName).delete(true, true, new NullProgressMonitor()); 49 | } 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 | } 60 | 61 | protected String getClipboardContent() { 62 | final StringBuilder content = new StringBuilder(); 63 | 64 | syncExec(new VoidResult() { 65 | public void run() { 66 | Clipboard clipboard = new Clipboard(PlatformUI.getWorkbench() 67 | .getActiveWorkbenchWindow().getShell().getDisplay()); 68 | TextTransfer textTransfer = TextTransfer.getInstance(); 69 | content.append((String) clipboard.getContents(textTransfer)); 70 | } 71 | }); 72 | return content.toString(); 73 | } 74 | 75 | protected void clickContextMenu(final SWTBotEclipseEditor editor, String string) { 76 | // this doesn't work so far. 77 | syncExec(new VoidResult() { 78 | public void run() { 79 | editor.toTextEditor().contextMenu("Copy Empty Stub To Clipboard").click(); 80 | } 81 | }); 82 | 83 | } 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 | } 90 | 91 | protected void fireTheCopyEmptyStubToClipboardMenuItem() { 92 | bot.menu("CppUTest").menu("Copy Empty Stub To Clipboard").click(); 93 | } 94 | @SuppressWarnings("unchecked") 95 | protected SWTBotEclipseEditor createNewCppFile(String projectName, String fileName, String content) throws CoreException { 96 | IProject project = root.getProject(projectName); 97 | IFile file = project.getFile(fileName); 98 | file.create(new ByteArrayInputStream(content.getBytes()), true, new NullProgressMonitor()); 99 | project.refreshLocal(IResource.DEPTH_INFINITE, null); 100 | bot.viewByTitle("Project Explorer").bot().tree().expandNode(projectName).getNode(fileName).doubleClick(); 101 | long oldTimeout = SWTBotPreferences.TIMEOUT; 102 | SWTBotPreferences.TIMEOUT = 5000; 103 | Matcher editorMatcher = Matchers.allOf( 104 | IsInstanceOf.instanceOf(IEditorReference.class), 105 | withPartName(fileName) 106 | ); 107 | bot.waitUntil(Conditions.waitForEditor((Matcher) editorMatcher)); 108 | SWTBotPreferences.TIMEOUT = oldTimeout; 109 | SWTBotEditor swtBoteditor = bot.activeEditor(); 110 | return swtBoteditor.toTextEditor(); 111 | } 112 | 113 | } -------------------------------------------------------------------------------- /org.cpputest.plugin.SWTBotTest/src/org/cpputest/plugin/SWTBotTest/DefaultMockTest.java: -------------------------------------------------------------------------------- 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.widgets.SWTBotEclipseEditor; 7 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; 8 | import org.junit.After; 9 | import org.junit.Before; 10 | import org.junit.Ignore; 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | 14 | @RunWith(SWTBotJunit4ClassRunner.class) 15 | public class DefaultMockTest extends CppProjectTestBase { 16 | private static final String GENERAL_PROJECT_FOR_TESTING = "GeneralProjectForTesting"; 17 | @Before 18 | public void setupProjectAndClearClipboard() throws CoreException { 19 | createCppProject(GENERAL_PROJECT_FOR_TESTING); 20 | clearClipboard(); 21 | } 22 | @After 23 | public void cleanProject() throws CoreException{ 24 | deleteProject(GENERAL_PROJECT_FOR_TESTING); 25 | } 26 | 27 | @Test 28 | public void generateDefaultMockForVoidFunctionWithNoParameter() throws CoreException { 29 | copyDefaultMockToClipboard("void fun()"); 30 | assertEquals("void fun(){mock().actualCall(\"fun\");}\n", getClipboardContent()); 31 | } 32 | 33 | @Ignore 34 | @Test 35 | public void generateDefaultMockForVoidFunctionWithVoidParameter() throws CoreException { 36 | copyDefaultMockToClipboard("void fun(void)"); 37 | assertEquals("void fun(void){mock().actualCall(\"fun\");}\n", getClipboardContent()); 38 | } 39 | 40 | protected void copyDefaultMockToClipboard(String signature) throws CoreException { 41 | SWTBotEclipseEditor editor = createNewCppFile(GENERAL_PROJECT_FOR_TESTING, "example.h", signature); 42 | editor.selectRange(0,0,1000); 43 | fireTheCopyDefaultMockToClipboardMenuItem(); 44 | } 45 | 46 | private void fireTheCopyDefaultMockToClipboardMenuItem() { 47 | bot.menu("CppUTest").menu("Copy Default Mock To Clipboard").click(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /org.cpputest.plugin.SWTBotTest/src/org/cpputest/plugin/SWTBotTest/EmptyCStubTest.java: -------------------------------------------------------------------------------- 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.junit.After; 11 | import org.junit.AfterClass; 12 | import org.junit.Before; 13 | import org.junit.BeforeClass; 14 | import org.junit.Test; 15 | import org.junit.runner.RunWith; 16 | 17 | @RunWith(SWTBotJunit4ClassRunner.class) 18 | public class EmptyCStubTest extends CppProjectTestBase { 19 | 20 | private static final String GENERAL_PROJECT_FOR_TESTING = "GeneralProjectForTesting"; 21 | 22 | @Before 23 | public void setupProjectAndClearClipboard() throws CoreException { 24 | createCppProject(GENERAL_PROJECT_FOR_TESTING); 25 | clearClipboard(); 26 | } 27 | @After 28 | public void cleanProject() throws CoreException{ 29 | deleteProject(GENERAL_PROJECT_FOR_TESTING); 30 | } 31 | @BeforeClass 32 | static public void increaseTimeOut() { 33 | SWTBotPreferences.TIMEOUT = 20000; 34 | } 35 | @AfterClass 36 | static public void waitForAWhile(){ 37 | SWTWorkbenchBot bot = new SWTWorkbenchBot(); 38 | bot.sleep(2000); 39 | } 40 | @Test 41 | public void testCopyEmptyStubToClipboard() throws CoreException { 42 | copyEmptyStubOfCodeToClipboard("void fun1();\n"); 43 | assertEquals("void fun1(){}\n", getClipboardContent()); 44 | } 45 | @Test 46 | public void testCopyEmptyStubToClipboardWithReturnType() throws CoreException { 47 | copyEmptyStubOfCodeToClipboard("int fun2(void);\n"); 48 | assertEquals("int fun2(void){return 0;}\n", getClipboardContent()); 49 | } 50 | @Test 51 | public void testCopyEmptyStubToClipboardWithIncompleteCode() throws CoreException { 52 | copyEmptyStubOfCodeToClipboard(" "); 53 | shouldSeeUnableToGenerateStubMessagebox("No function is selected."); 54 | } 55 | @Test 56 | public void testCopyEmptyStubCanIgnoreCComment() throws CoreException { 57 | copyEmptyStubOfCodeToClipboard("/* /* */ void /*\"*/fun3(void/*\"*/)/**/;/**/\n"); 58 | assertEquals("void fun3(void){}\n", getClipboardContent()); 59 | } 60 | @Test 61 | public void testCopyEmptyStubCanIgnoreCppComment() throws CoreException { 62 | copyEmptyStubOfCodeToClipboard("// /* \n void //\"\nfun4(void//xxx\n)//\n;//\n"); 63 | assertEquals("void fun4(void){}\n", getClipboardContent()); 64 | } 65 | 66 | protected void copyEmptyStubOfCodeToClipboard(String signature) throws CoreException { 67 | SWTBotEclipseEditor editor = createNewCppFile(GENERAL_PROJECT_FOR_TESTING, "example.h", signature); 68 | editor.selectRange(0,0,1000); 69 | fireTheCopyEmptyStubToClipboardMenuItem(); 70 | } 71 | } -------------------------------------------------------------------------------- /org.cpputest.plugin.SWTBotTest/src/org/cpputest/plugin/SWTBotTest/IncompleteSelectionTest.java: -------------------------------------------------------------------------------- 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.feature/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /org.cpputest.plugin.feature/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.cpputest.plugin.feature 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.pde.FeatureBuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.pde.FeatureNature 16 | 17 | 18 | -------------------------------------------------------------------------------- /org.cpputest.plugin.feature/build.properties: -------------------------------------------------------------------------------- 1 | bin.includes = feature.xml 2 | -------------------------------------------------------------------------------- /org.cpputest.plugin.feature/feature.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | The stub/mock generating feature. 10 | 11 | 12 | 13 | [Enter Copyright Description here.] 14 | 15 | 16 | 17 | [Enter License Description here.] 18 | 19 | 20 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /org.cpputest.plugin.feature/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.cpputest.plugin 8 | org.cpputest.plugin.parent 9 | 1.0.0-SNAPSHOT 10 | 11 | org.cpputest.plugin 12 | org.cpputest.stubber.feature 13 | 1.0.0-SNAPSHOT 14 | eclipse-feature 15 | 16 | 17 | 18 | org.apache.maven.plugins 19 | maven-jar-plugin 20 | 21 | 22 | org.eclipse.tycho 23 | tycho-packaging-plugin 24 | ${tycho.version} 25 | 26 | 27 | false 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.cpputest.plugin.unittest 4 | 5 | 6 | org.cpputest.plugin 7 | 8 | 9 | 10 | org.eclipse.jdt.core.javabuilder 11 | 12 | 13 | 14 | 15 | 16 | org.eclipse.jdt.core.javanature 17 | 18 | 19 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Bundle-ManifestVersion: 2 3 | Bundle-Name: CppUTest Eclipse Plugin: unittest 4 | Bundle-SymbolicName: org.cpputest.plugin.unittest;singleton:=true 5 | Bundle-Version: 1.0.0.qualifier 6 | Bundle-Vendor: odd-e.com 7 | Fragment-Host: org.cpputest.plugin.general;bundle-version="1.0.0" 8 | Require-Bundle: org.eclipse.ui, 9 | org.eclipse.core.runtime, 10 | org.junit, 11 | org.eclipse.cdt;bundle-version="7.0.2" 12 | Bundle-RequiredExecutionEnvironment: JavaSE-1.6 13 | Bundle-ActivationPolicy: lazy 14 | Import-Package: org.eclipse.jface.text, 15 | org.junit.runner 16 | Bundle-ClassPath: ., lib/cglib-2.1_3-src.jar, lib/cglib-nodep-2.1_3.jar, lib/hamcrest-core-1.1.jar, lib/hamcrest-library-1.1.jar, lib/jmock-2.5.1.jar, lib/jmock-junit4-2.5.1.jar, lib/jmock-legacy-2.5.1.jar, lib/objenesis-1.0.jar 17 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/build.properties: -------------------------------------------------------------------------------- 1 | source.. = test/ 2 | output.. = bin/ 3 | bin.includes = test/ 4 | src.includes = test/ 5 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/lib/bsh-core-2.0b4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/org.cpputest.plugin.unittest/lib/bsh-core-2.0b4.jar -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/lib/cglib-2.1_3-src.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/org.cpputest.plugin.unittest/lib/cglib-2.1_3-src.jar -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/lib/cglib-nodep-2.1_3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/org.cpputest.plugin.unittest/lib/cglib-nodep-2.1_3.jar -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/lib/hamcrest-core-1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/org.cpputest.plugin.unittest/lib/hamcrest-core-1.1.jar -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/lib/hamcrest-library-1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/org.cpputest.plugin.unittest/lib/hamcrest-library-1.1.jar -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/lib/jmock-2.5.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/org.cpputest.plugin.unittest/lib/jmock-2.5.1.jar -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/lib/jmock-junit3-2.5.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/org.cpputest.plugin.unittest/lib/jmock-junit3-2.5.1.jar -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/lib/jmock-junit4-2.5.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/org.cpputest.plugin.unittest/lib/jmock-junit4-2.5.1.jar -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/lib/jmock-legacy-2.5.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/org.cpputest.plugin.unittest/lib/jmock-legacy-2.5.1.jar -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/lib/jmock-script-2.5.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/org.cpputest.plugin.unittest/lib/jmock-script-2.5.1.jar -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/lib/objenesis-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/org.cpputest.plugin.unittest/lib/objenesis-1.0.jar -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | org.cpputest.plugin.parent 5 | org.cpputest.plugin 6 | 1.0.0-SNAPSHOT 7 | .. 8 | 9 | org.cpputest.plugin.unittest 10 | org.cpputest.plugin.unittest 11 | eclipse-test-plugin 12 | 13 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/codeGenerator/test/CppDefautlMockStubberTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator.test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.cpputest.codeGenerator.CppCode; 6 | import org.cpputest.codeGenerator.Stubber; 7 | import org.cpputest.parser.langunit.CppLangFunctionSignature; 8 | import org.cpputest.plugin.CppUTestFactory; 9 | import org.junit.Test; 10 | 11 | public class CppDefautlMockStubberTest { 12 | Stubber stubber = new CppUTestFactory().createDefaultMockStubber(); 13 | 14 | @Test 15 | public void testVoidFunctionWithoutParameter() { 16 | CppLangFunctionSignature signature = CppLangFunctionSignature.builderStartWith("void") 17 | .addToFunctionDeclaration("fun") 18 | .build(); 19 | 20 | CppCode stub = stubber.getStubOfSignature(signature); 21 | 22 | assertEquals("void fun(){mock().actualCall(\"fun\");}\n", stub.toString()); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/codeGenerator/test/CppEmptyStubberTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator.test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.cpputest.codeGenerator.CppCode; 6 | import org.cpputest.codeGenerator.Stubber; 7 | import org.cpputest.parser.langunit.CppLangFunctionSignature; 8 | import org.cpputest.plugin.CppUTestFactory; 9 | import org.junit.Test; 10 | 11 | public class CppEmptyStubberTest { 12 | Stubber stubber = new CppUTestFactory().createEmptyStubber(); 13 | 14 | @Test 15 | public void testVoidFunctionWithoutParameter() { 16 | CppLangFunctionSignature signature = CppLangFunctionSignature.builderStartWith("void") 17 | .addToFunctionDeclaration("fun") 18 | .build(); 19 | 20 | CppCode stub = stubber.getStubOfSignature(signature); 21 | 22 | assertEquals("void fun(){}\n", stub.toString()); 23 | } 24 | 25 | @Test 26 | public void testFunctionWithoutParameterReturnsInt() { 27 | CppLangFunctionSignature signature = CppLangFunctionSignature.builderStartWith("int") 28 | .addToFunctionDeclaration("foo") 29 | .build(); 30 | 31 | CppCode stub = stubber.getStubOfSignature(signature); 32 | 33 | assertEquals("int foo(){return 0;}\n", stub.toString()); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/codeGenerator/test/CppUTestCodeGeneratorTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator.test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.cpputest.codeGenerator.CppCode; 6 | import org.cpputest.codeGenerator.CppUTestStubCreator; 7 | import org.cpputest.codeGenerator.Stubber; 8 | import org.cpputest.parser.SourceCodeReader; 9 | import org.cpputest.parser.impl.Token; 10 | import org.cpputest.parser.langunit.CppLangFunctionSignature; 11 | import org.jmock.Expectations; 12 | import org.jmock.Mockery; 13 | import org.jmock.integration.junit4.JMock; 14 | import org.jmock.integration.junit4.JUnit4Mockery; 15 | import org.junit.Test; 16 | import org.junit.runner.RunWith; 17 | 18 | @RunWith(JMock.class) 19 | public class CppUTestCodeGeneratorTest { 20 | final String SOURCE_CODE = "code"; 21 | final CppCode code1 = new CppCode("expected "); 22 | final CppCode code2 = new CppCode("stub code"); 23 | final CppCode expected_code = new CppCode("expected stub code"); 24 | Mockery context = new JUnit4Mockery(); 25 | 26 | @Test 27 | public void testGenerateEmpty() { 28 | final SourceCodeReader reader = context.mock(SourceCodeReader.class); 29 | final Stubber stubber = context.mock(Stubber.class); 30 | final Iterable units = context.mock(Iterable.class); 31 | final CppLangFunctionSignature s1 = CppLangFunctionSignature.builderStartWith(null).build(); 32 | final CppLangFunctionSignature s2 = CppLangFunctionSignature.builderStartWith(null).build(); 33 | 34 | context.checking(new Expectations() {{ 35 | oneOf(reader).signatures(SOURCE_CODE); will(returnValue(units)); 36 | oneOf(units).iterator(); will(returnIterator(s1,s2)); 37 | oneOf(stubber).getStubOfSignature(s1); will(returnValue(code1)); 38 | oneOf(stubber).getStubOfSignature(s2); will(returnValue(code2)); 39 | }}); 40 | 41 | CppUTestStubCreator cpputest = new CppUTestStubCreator(reader); 42 | assertEquals(expected_code, cpputest.getStubOfCode(SOURCE_CODE,stubber)); 43 | } 44 | @Test 45 | public void testGenerateEmptyAtPosition() { 46 | final int OFFSET = 10; 47 | final SourceCodeReader reader = context.mock(SourceCodeReader.class); 48 | final Stubber stubber = context.mock(Stubber.class); 49 | final Iterable units = context.mock(Iterable.class); 50 | final CppLangFunctionSignature s1 = CppLangFunctionSignature.builderStartWith("void") 51 | .withBeginOffset(OFFSET-4) 52 | .addToFunctionDeclaration(Token.token("foo")) 53 | .withEndOffset(OFFSET - 2) 54 | .build(); 55 | final CppLangFunctionSignature s2 = CppLangFunctionSignature.builderStartWith("void") 56 | .withBeginOffset(OFFSET-1) 57 | .addToFunctionDeclaration(Token.token("bar")) 58 | .withEndOffset(OFFSET + 1) 59 | .build(); 60 | context.checking(new Expectations() {{ 61 | oneOf(reader).signatures(SOURCE_CODE); will(returnValue(units)); 62 | oneOf(units).iterator(); will(returnIterator(s1,s2)); 63 | oneOf(stubber).getStubOfSignature(s2); will(returnValue(code2)); 64 | }}); 65 | 66 | CppUTestStubCreator cpputest = new CppUTestStubCreator(reader); 67 | assertEquals(code2, cpputest.getStubOfCodeAtPosition(SOURCE_CODE, OFFSET, stubber)); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/parser/impl/test/CppFunctionSignatureReadingTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser.impl.test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | import org.cpputest.parser.impl.CppPotentialLanguagePartsToMeaningfulLanguageUnitsTranslator; 9 | import org.cpputest.parser.impl.Token; 10 | import org.cpputest.parser.langunit.LanguageUnit; 11 | import org.cpputest.parser.parts.impl.parts.CppPart; 12 | import org.junit.Test; 13 | 14 | public class CppFunctionSignatureReadingTest { 15 | private static final int BEGIN_OFFSET = 11; 16 | private static final int END_OFFSET = 18; 17 | List units; 18 | List parts = new ArrayList(); 19 | @Test 20 | public void testParseEmptyString() { 21 | units = getLanguageUnits(parts); 22 | assertEquals(0, units.size()); 23 | } 24 | @Test 25 | public void testParseIncompleteFun() { 26 | parts.add(CppPart.StartNewFunction(Token.token("int"))); 27 | parts.add(CppPart.AddToFunctionName(Token.token("fun"))); 28 | parts.add(CppPart.PartOfLongFunctionName(Token.token("("))); 29 | units = getLanguageUnits(parts); 30 | assertEquals(0, units.size()); 31 | } 32 | @Test 33 | public void testParseFunctionSignature() { 34 | parts.add(CppPart.StartNewFunction(Token.token("int"))); 35 | parts.add(CppPart.AddToFunctionName(Token.token("fun"))); 36 | parts.add(CppPart.PartOfLongFunctionName(Token.token("("))); 37 | parts.add(CppPart.EndOfFunctionSignature(Token.token(")"))); 38 | units = getLanguageUnits(parts); 39 | assertEquals(1, units.size()); 40 | assertEquals("int fun()", units.get(0).getCode().toString()); 41 | } 42 | @Test 43 | public void testParseFunctionSignatureWithPointerReturnType() { 44 | parts.add(CppPart.StartNewFunction(Token.token("int"))); 45 | parts.add(CppPart.AddToFunctionName(Token.token("*"))); 46 | parts.add(CppPart.AddToFunctionName(Token.token("fun"))); 47 | parts.add(CppPart.PartOfLongFunctionName(Token.token("("))); 48 | parts.add(CppPart.EndOfFunctionSignature(Token.token(")"))); 49 | units = getLanguageUnits(parts); 50 | assertEquals(1, units.size()); 51 | assertEquals("int * fun()", units.get(0).getCode().toString()); 52 | } 53 | @Test 54 | public void testParseFunctionParameter() { 55 | parts.add(CppPart.StartNewFunction(Token.token("void"))); 56 | parts.add(CppPart.AddToFunctionName(Token.token("foo"))); 57 | parts.add(CppPart.PartOfLongFunctionName(Token.token("("))); 58 | parts.add(CppPart.Parameter(Token.token("int"))); 59 | parts.add(CppPart.Parameter(Token.token("a"))); 60 | parts.add(CppPart.EndOfFunctionSignature(Token.token(")"))); 61 | units = getLanguageUnits(parts); 62 | assertEquals(1, units.size()); 63 | assertEquals("void foo(int a)", units.get(0).getCode().toString()); 64 | } 65 | @Test 66 | public void testParseFunctionWithPriorMacroOnPreviousLine() { 67 | parts.add(CppPart.StartNewFunction(Token.token("EXTRA"))); 68 | parts.add(CppPart.AddToFunctionName(Token.token("void"))); 69 | parts.add(CppPart.AddToFunctionName(Token.token("foo"))); 70 | parts.add(CppPart.EndOfFunctionSignature(Token.token(")"))); 71 | units = getLanguageUnits(parts); 72 | assertEquals(1, units.size()); 73 | assertEquals("EXTRA void foo()", units.get(0).getCode().toString()); 74 | } 75 | 76 | @Test 77 | public void testParseFunctionWithPriorMacroOnPreviousLineWhenReturnTypeIsAPointer() { 78 | parts.add(CppPart.StartNewFunction(Token.token("EXTRA"))); 79 | parts.add(CppPart.AddToFunctionName(Token.token("CHAR"))); 80 | parts.add(CppPart.AddToFunctionName(Token.token("*"))); 81 | parts.add(CppPart.AddToFunctionName(Token.token("foo"))); 82 | parts.add(CppPart.EndOfFunctionSignature(Token.token(")"))); 83 | units = getLanguageUnits(parts); 84 | assertEquals("EXTRA CHAR * foo()", units.get(0).getCode().toString()); 85 | } 86 | @Test 87 | public void testParseFunctionWithPriorMacroOnSameLine() { 88 | parts.add(CppPart.StartNewFunction(Token.token("const"))); 89 | parts.add(CppPart.AddToFunctionName(Token.token("int"))); 90 | parts.add(CppPart.AddToFunctionName(Token.token("foo"))); 91 | parts.add(CppPart.EndOfFunctionSignature(Token.token(")"))); 92 | units = getLanguageUnits(parts); 93 | assertEquals(1, units.size()); 94 | assertEquals("const int foo()", units.get(0).getCode().toString()); 95 | } 96 | @Test 97 | public void testFunctionPointerIsNotASignature() { 98 | parts.add(CppPart.StartNewFunction(Token.token("int"))); 99 | parts.add(CppPart.Parameter(Token.token("*"))); 100 | parts.add(CppPart.Parameter(Token.token("foo"))); 101 | parts.add(CppPart.EndOfFunctionSignature(Token.token(")"))); 102 | units = getLanguageUnits(parts); 103 | assertEquals(0, units.size()); 104 | } 105 | @Test 106 | public void testBeginAndEndOffsetOfFunctionSignature() { 107 | parts.add(CppPart.StartNewFunction(Token.token("int", BEGIN_OFFSET))); 108 | parts.add(CppPart.AddToFunctionName(Token.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 | } 116 | 117 | private List getLanguageUnits(Iterable parts) { 118 | final ArrayList list = new ArrayList(); 119 | CppPotentialLanguagePartsToMeaningfulLanguageUnitsTranslator reader = new CppPotentialLanguagePartsToMeaningfulLanguageUnitsTranslator(); 120 | for (LanguageUnit unit : reader.languageUnits(parts)) 121 | list.add(unit); 122 | return list; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/parser/impl/test/CppLikeCodeTokenSplitterTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser.impl.test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | import org.cpputest.parser.impl.CppLikeCodeTokenSplitter; 9 | import org.cpputest.parser.impl.Token; 10 | import org.junit.Test; 11 | 12 | public class CppLikeCodeTokenSplitterTest { 13 | 14 | @Test 15 | public void testEmpty() { 16 | assertEquals(0, tokenize("").size()); 17 | } 18 | @Test 19 | public void testSpaceOnly() { 20 | List tokens = tokenize(" \n \t "); 21 | assertEquals(0, tokens.size()); 22 | } 23 | @Test 24 | public void testKeyword() { 25 | List tokens = tokenize("int"); 26 | assertEquals(1, tokens.size()); 27 | assertEquals("int", tokens.get(0).toString()); 28 | } 29 | @Test 30 | public void firstTokenOffsetShouldBe0() { 31 | List tokens = tokenize("int"); 32 | assertEquals(0, tokens.get(0).getBeginOffset()); 33 | } 34 | @Test 35 | public void firstTokenOffsetShouldBe1IfThereIsASpaceBeforeIt() { 36 | List tokens = tokenize(" int"); 37 | assertEquals(1, tokens.get(0).getBeginOffset()); 38 | } 39 | @Test 40 | public void testFunction() { 41 | List tokens = tokenize("int fun(){}"); 42 | assertEquals(6, tokens.size()); 43 | assertEquals("fun", tokens.get(1).toString()); 44 | } 45 | @Test 46 | public void testComment() { 47 | List tokens = tokenize("/*/* \n int*/"); 48 | assertEquals(1, tokens.size()); 49 | assertEquals("/*/* \n int*/", tokens.get(0).toString()); 50 | } 51 | @Test 52 | public void testCppComment() { 53 | List tokens = tokenize("int //comment \n fun"); 54 | assertEquals(3, tokens.size()); 55 | } 56 | @Test 57 | public void testPreprocess() { 58 | List tokens = tokenize("#preprocess line\nint"); 59 | assertEquals(1, tokens.size()); 60 | assertEquals("int", tokens.get(0).toString()); 61 | } 62 | @Test 63 | public void testMultipleLinePreprocess() { 64 | List tokens = tokenize("#preprocess line1\\\nline2\nint"); 65 | assertEquals(1, tokens.size()); 66 | assertEquals("int", tokens.get(0).toString()); 67 | } 68 | @Test 69 | public void testMultipleLinePreprocess2() { 70 | List tokens = tokenize("#endif\n\n#endif\n"); 71 | assertEquals(0, tokens.size()); 72 | } 73 | @Test 74 | public void testVarArgs() { 75 | List tokens = tokenize("..."); 76 | assertEquals(1, tokens.size()); 77 | } 78 | 79 | private List tokenize(String string) { 80 | final ArrayList list = new ArrayList(); 81 | 82 | CppLikeCodeTokenSplitter tokenizer = new CppLikeCodeTokenSplitter(); 83 | for (Token token: tokenizer.tokens(string)) 84 | list.add(token); 85 | 86 | return list; 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/parser/impl/test/PotentialLanguagePartsTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser.impl.test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | import org.cpputest.parser.impl.CppTokensToPotentialLanguagePartsTranslator; 9 | import org.cpputest.parser.impl.Token; 10 | import org.cpputest.parser.parts.impl.parts.CppPart; 11 | import org.junit.Test; 12 | 13 | public class PotentialLanguagePartsTest { 14 | 15 | @Test 16 | public void testEmptyCode() { 17 | List parts = getPotentialParts(new ArrayList()); 18 | assertEquals(0, parts.size()); 19 | } 20 | @Test 21 | public void testPotentialReturnType() { 22 | String[] strs = {"int"}; 23 | List parts = getPotentialParts(getTokens(strs)); 24 | assertEquals(1, parts.size()); 25 | assertEquals(CppPart.StartNewFunction(Token.token("int")), parts.get(0)); 26 | } 27 | @Test 28 | public void testSemicolonInGlobal() { 29 | String[] strs = {";","int"}; 30 | List parts = getPotentialParts(getTokens(strs)); 31 | assertEquals(2, parts.size()); 32 | assertEquals(CppPart.EndOfGlobalStatement(), parts.get(0)); 33 | assertEquals(CppPart.StartNewFunction(Token.token("int")), parts.get(1)); 34 | } 35 | @Test 36 | public void testFunction() { 37 | String[] strs = {"int", "fun", "(", ")", "{", "}"}; 38 | List parts = getPotentialParts(getTokens(strs)); 39 | assertEquals(5, parts.size()); 40 | assertEquals(CppPart.StartNewFunction(Token.token("int")), parts.get(0)); 41 | assertEquals(CppPart.AddToFunctionName(Token.token("fun")), parts.get(1)); 42 | assertEquals(CppPart.PartOfLongFunctionName(Token.token("(")), parts.get(2)); 43 | assertEquals(CppPart.EndOfFunctionSignature(Token.token(")")), parts.get(3)); 44 | assertEquals(CppPart.EndOfFunction(), parts.get(4)); 45 | } 46 | @Test 47 | public void testParameter() { 48 | String[] strs = {"int", "fun", "(", "PARAMETER1", ")"}; 49 | List parts = getPotentialParts(getTokens(strs)); 50 | assertEquals(CppPart.Parameter(Token.token("PARAMETER1")), parts.get(3)); 51 | } 52 | @Test 53 | public void testMultipleParameter() { 54 | String[] strs = {"int", "fun", "(", "TYPE1","ARG1", ",", "TYPE2","ARG2",")"}; 55 | List parts = getPotentialParts(getTokens(strs)); 56 | assertEquals(CppPart.Parameter(Token.token("TYPE1")), parts.get(3)); 57 | assertEquals(CppPart.Parameter(Token.token("ARG1")), parts.get(4)); 58 | assertEquals(CppPart.Parameter(Token.token(",")), parts.get(5)); 59 | assertEquals(CppPart.Parameter(Token.token("TYPE2")), parts.get(6)); 60 | assertEquals(CppPart.Parameter(Token.token("ARG2")), parts.get(7)); 61 | } 62 | @Test 63 | public void testPointerParameter() { 64 | String[] strs = {"int", "fun", "(", "TYPE1","*", "ARG1",")"}; 65 | List parts = getPotentialParts(getTokens(strs)); 66 | assertEquals(CppPart.Parameter(Token.token("TYPE1")), parts.get(3)); 67 | assertEquals(CppPart.Parameter(Token.token("*")), parts.get(4)); 68 | assertEquals(CppPart.Parameter(Token.token("ARG1")), parts.get(5)); 69 | } 70 | @Test 71 | public void testPointerType() { 72 | String[] strs = {"int", "*"}; 73 | List parts = getPotentialParts(getTokens(strs)); 74 | assertEquals(2, parts.size()); 75 | assertEquals(CppPart.StartNewFunction(Token.token("int")), parts.get(0)); 76 | assertEquals(CppPart.AddToFunctionName(Token.token("*")), parts.get(1)); 77 | } 78 | @Test 79 | public void testFunctionPointer() { 80 | String[] strs = {"int", "(","*", "fp",")","(",")"}; 81 | List parts = getPotentialParts(getTokens(strs)); 82 | assertEquals(6, parts.size()); 83 | assertEquals(CppPart.StartNewFunction(Token.token("int")), parts.get(0)); 84 | assertEquals(CppPart.PartOfLongFunctionName(Token.token("(")), parts.get(1)); 85 | assertEquals(CppPart.Parameter(Token.token("*")), parts.get(2)); 86 | } 87 | @Test 88 | public void testTypeDef() { 89 | String[] strs = {"typedef"}; 90 | List parts = getPotentialParts(getTokens(strs)); 91 | assertEquals(1, parts.size()); 92 | assertEquals(CppPart.Typedef(), parts.get(0)); 93 | } 94 | @Test 95 | public void testTypeDefContentShouldBeIgnored() { 96 | String[] strs = {"typedef","SHOULD_BE_IGNORED", "(","*","UNTIL_SEMICOLON",")","(",")",";","SHOULD_BE_ABILE_TO_SEE"}; 97 | List parts = getPotentialParts(getTokens(strs)); 98 | assertEquals(2, parts.size()); 99 | assertEquals(CppPart.StartNewFunction(Token.token("SHOULD_BE_ABILE_TO_SEE")), parts.get(1)); 100 | } 101 | 102 | @Test 103 | public void testGlobalAssignment() { 104 | String[] strs = {"int", "a","=","b",";"}; 105 | List parts = getPotentialParts(getTokens(strs)); 106 | assertEquals(CppPart.StartNewFunction(Token.token("int")), parts.get(0)); 107 | assertEquals(CppPart.AddToFunctionName(Token.token("a")), parts.get(1)); 108 | assertEquals(CppPart.Assignment(), parts.get(2)); 109 | assertEquals(CppPart.EndOfGlobalStatement(), parts.get(3)); 110 | } 111 | 112 | private Iterable getTokens(String[] strs) { 113 | List tokens = new ArrayList(); 114 | for (String s:strs) 115 | tokens.add(Token.token(s,0)); 116 | return tokens; 117 | } 118 | private List getPotentialParts(Iterable tokens) { 119 | final ArrayList list = new ArrayList(); 120 | CppTokensToPotentialLanguagePartsTranslator trans = new CppTokensToPotentialLanguagePartsTranslator(); 121 | for (CppPart part: trans.getPotentialLanguageParts(tokens)) 122 | list.add(part); 123 | return list; 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/parser/impl/test/SignatureBuilderTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser.impl.test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.cpputest.parser.langunit.CppLangFunctionSignature; 6 | import org.junit.Test; 7 | 8 | public class SignatureBuilderTest { 9 | 10 | private static final int END_OFFSET = 15; 11 | private static final int BEGIN_OFFSET = 10; 12 | @Test 13 | public void testBuildFunctionWithoutParameterAndVoidReturnType() { 14 | CppLangFunctionSignature signature = CppLangFunctionSignature.builderStartWith("void") 15 | .addToFunctionDeclaration("fun").build(); 16 | assertEquals("void fun()", signature.getCode().toString()); 17 | } 18 | @Test 19 | public void testBuildFunctionWithoutParameterAndPointerReturnType() { 20 | CppLangFunctionSignature signature = CppLangFunctionSignature.builderStartWith("int") 21 | .addToFunctionDeclaration("*") 22 | .addToFunctionDeclaration("fun").build(); 23 | assertEquals("int * fun()", signature.getCode().toString()); 24 | } 25 | @Test 26 | public void testMoveFunctionNameToReturnTypeWhenAddToFunctionNameMoreThanOnce() { 27 | CppLangFunctionSignature signature = CppLangFunctionSignature.builderStartWith("MACRO") 28 | .addToFunctionDeclaration("int") 29 | .addToFunctionDeclaration("fun") 30 | .build(); 31 | assertEquals("MACRO int fun()", signature.getCode().toString()); 32 | } 33 | @Test 34 | public void testConstReturnType() { 35 | CppLangFunctionSignature signature = CppLangFunctionSignature.builderStartWith("const") 36 | .addToFunctionDeclaration("int") 37 | .addToFunctionDeclaration("fun") 38 | .build(); 39 | assertEquals("fun", signature.getFunctionName()); 40 | assertEquals("const int fun()", signature.getCode().toString()); 41 | } 42 | @Test 43 | public void testExtraStuffBeforePointerReturnType() { 44 | CppLangFunctionSignature signature = CppLangFunctionSignature.builderStartWith("EXTRA") 45 | .addToFunctionDeclaration("int") 46 | .addToFunctionDeclaration("*") 47 | .addToFunctionDeclaration("fun") 48 | .build(); 49 | assertEquals("fun", signature.getFunctionName()); 50 | assertEquals("EXTRA int * fun()", signature.getCode().toString()); 51 | } 52 | @Test 53 | public void testBuildWithOffsetInforamtion() { 54 | CppLangFunctionSignature signature = CppLangFunctionSignature.builderStartWith("void") 55 | .withBeginOffset(BEGIN_OFFSET) 56 | .addToFunctionDeclaration("foo") 57 | .withEndOffset(END_OFFSET) 58 | .build(); 59 | assertFalse(signature.isOffsetInclusive(BEGIN_OFFSET - 1)); 60 | assertTrue(signature.isOffsetInclusive(BEGIN_OFFSET)); 61 | assertTrue(signature.isOffsetInclusive(END_OFFSET)); 62 | assertFalse(signature.isOffsetInclusive(END_OFFSET + 1)); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/parser/test/CppSourceCodeReaderEndToEndTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser.test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.Iterator; 6 | 7 | import org.cpputest.parser.CppSourceCodeReader; 8 | import org.cpputest.parser.langunit.CppLangFunctionSignature; 9 | import org.junit.Test; 10 | 11 | public class CppSourceCodeReaderEndToEndTest { 12 | private static final String EMPTY_SOURCE_CODE = ""; 13 | @Test 14 | public void testShouldReturnEmptyIteratorReadSignatureFromEmptyString() { 15 | CppSourceCodeReader reader = new CppSourceCodeReader(); 16 | Iterable iterable = reader.signatures(EMPTY_SOURCE_CODE); 17 | Iterator it = iterable.iterator(); 18 | assertFalse(it.hasNext()); 19 | } 20 | @Test 21 | public void testShouldReturnOneIteratorReadSignatureFromStringWithOneFunction() { 22 | CppSourceCodeReader reader = new CppSourceCodeReader(); 23 | Iterable iterable = reader.signatures("void fun();"); 24 | Iterator it = iterable.iterator(); 25 | assertTrue(it.hasNext()); 26 | assertEquals("void fun()", it.next().getCode().toString()); 27 | } 28 | @Test 29 | public void testSizeofIsNotAFunction() { 30 | CppSourceCodeReader reader = new CppSourceCodeReader(); 31 | Iterable iterable = reader.signatures("int a = sizeof(b);"); 32 | Iterator it = iterable.iterator(); 33 | assertFalse(it.hasNext()); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/plugin/actions/test/CppUTestActionCopyDefaultMockToClipboardTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin.actions.test; 2 | 3 | import org.cpputest.codeGenerator.Stubber; 4 | import org.cpputest.plugin.ICppUTestFactory; 5 | import org.cpputest.plugin.StubCodeUI; 6 | import org.cpputest.plugin.actions.CppUTestActionCopyEmptyStubToClipboard; 7 | import org.jmock.Expectations; 8 | import org.jmock.Mockery; 9 | import org.jmock.integration.junit4.JMock; 10 | import org.jmock.integration.junit4.JUnit4Mockery; 11 | import org.junit.Test; 12 | import org.junit.runner.RunWith; 13 | 14 | @RunWith(JMock.class) 15 | public class CppUTestActionCopyDefaultMockToClipboardTest { 16 | 17 | Mockery context = new JUnit4Mockery(); 18 | final Stubber emptyStubber = context.mock(Stubber.class); 19 | final StubCodeUI stubCodeUI = context.mock(StubCodeUI.class); 20 | final ICppUTestFactory factory = context.mock(ICppUTestFactory.class); 21 | CppUTestActionCopyEmptyStubToClipboard action = new CppUTestActionCopyEmptyStubToClipboard(factory); 22 | 23 | @Test 24 | public void testCopyEmptyStubOfSelectedCodeToClipboard() { 25 | context.checking(new Expectations() {{ 26 | allowing(factory).createEmptyStubber(); will(returnValue(emptyStubber)); 27 | allowing(factory).createStubCodeUI(); will(returnValue(stubCodeUI)); 28 | oneOf(stubCodeUI).copyStubCodeInEditorToClipboard(emptyStubber);; 29 | }}); 30 | 31 | action.run(null); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/plugin/actions/test/CppUTestActionCopyEmptyStubToClipboardTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin.actions.test; 2 | 3 | import org.cpputest.codeGenerator.Stubber; 4 | import org.cpputest.plugin.ICppUTestFactory; 5 | import org.cpputest.plugin.StubCodeUI; 6 | import org.cpputest.plugin.actions.CppUTestActionCopyDefaultMockToClipboard; 7 | import org.cpputest.plugin.actions.CppUTestActionCopyEmptyStubToClipboard; 8 | import org.jmock.Expectations; 9 | import org.jmock.Mockery; 10 | import org.jmock.integration.junit4.JMock; 11 | import org.jmock.integration.junit4.JUnit4Mockery; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | 15 | @RunWith(JMock.class) 16 | public class CppUTestActionCopyEmptyStubToClipboardTest { 17 | 18 | Mockery context = new JUnit4Mockery(); 19 | final Stubber stubber = context.mock(Stubber.class); 20 | final StubCodeUI stubCodeUI = context.mock(StubCodeUI.class); 21 | final ICppUTestFactory factory = context.mock(ICppUTestFactory.class); 22 | CppUTestActionCopyDefaultMockToClipboard action = new CppUTestActionCopyDefaultMockToClipboard(factory); 23 | 24 | @Test 25 | public void testCopyEmptyStubOfSelectedCodeToClipboard() { 26 | context.checking(new Expectations() {{ 27 | allowing(factory).createDefaultMockStubber(); will(returnValue(stubber)); 28 | allowing(factory).createStubCodeUI(); will(returnValue(stubCodeUI)); 29 | oneOf(stubCodeUI).copyStubCodeInEditorToClipboard(stubber);; 30 | }}); 31 | 32 | action.run(null); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/plugin/actions/test/CppUTestActionTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin.actions.test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.cpputest.codeGenerator.CUTPlatformAdaptor; 6 | import org.cpputest.plugin.ICppUTestFactory; 7 | import org.cpputest.plugin.actions.*; 8 | import org.eclipse.jface.action.IAction; 9 | import org.eclipse.ui.IObjectActionDelegate; 10 | import org.eclipse.ui.IWorkbenchPart; 11 | import org.eclipse.ui.IWorkbenchPartSite; 12 | import org.eclipse.ui.IWorkbenchWindow; 13 | import org.eclipse.ui.IWorkbenchWindowActionDelegate; 14 | import org.jmock.Expectations; 15 | import org.jmock.Mockery; 16 | import org.jmock.integration.junit4.JMock; 17 | import org.jmock.integration.junit4.JUnit4Mockery; 18 | 19 | import org.junit.Test; 20 | import org.junit.runner.RunWith; 21 | 22 | @RunWith(JMock.class) 23 | public class CppUTestActionTest { 24 | Mockery context = new JUnit4Mockery(); 25 | final ICppUTestFactory factory = context.mock(ICppUTestFactory.class); 26 | final CUTPlatformAdaptor platform = context.mock(CUTPlatformAdaptor.class); 27 | final IWorkbenchWindow window = context.mock(IWorkbenchWindow.class); 28 | 29 | @Test 30 | public void testCppUTestActionAbstractClassWillCreatePlatformForFutureUse() { 31 | context.checking(new Expectations() {{ 32 | oneOf(factory).createPlatformAdaptor(window); will(returnValue(platform)); 33 | }}); 34 | 35 | IWorkbenchWindowActionDelegate action = new CppUTestAction(factory){ 36 | @Override 37 | public void run(IAction arg0) { 38 | assertEquals(platform, this.getPlatform()); 39 | } 40 | 41 | }; 42 | action.init(window); 43 | action.run(null); 44 | } 45 | 46 | @Test 47 | public void testCppUTestActionAbstractClassWillCreatePlatformForObjectActionToo() { 48 | final IWorkbenchPart targetPart = context.mock(IWorkbenchPart.class); 49 | final IWorkbenchPartSite site = context.mock(IWorkbenchPartSite.class); 50 | 51 | context.checking(new Expectations() {{ 52 | allowing(targetPart).getSite();will(returnValue(site)); 53 | allowing(site).getWorkbenchWindow();will(returnValue(window)); 54 | oneOf(factory).createPlatformAdaptor(window); will(returnValue(platform)); 55 | }}); 56 | 57 | IObjectActionDelegate action = new CppUTestAction(factory){ 58 | @Override 59 | public void run(IAction arg0) { 60 | assertEquals(platform, this.getPlatform()); 61 | } 62 | }; 63 | action.setActivePart(null, targetPart ); 64 | action.run(null); 65 | } 66 | } 67 | 68 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/plugin/test/CppUTestFactoryTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin.test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.cpputest.codeGenerator.CompactCppCodeFormater; 6 | import org.cpputest.codeGenerator.CppCodeFormater; 7 | import org.cpputest.codeGenerator.CppDefaultMockStubber; 8 | import org.cpputest.codeGenerator.CppEmptyStubber; 9 | import org.cpputest.codeGenerator.CUTPlatformAdaptor; 10 | import org.cpputest.codeGenerator.Stubber; 11 | import org.cpputest.plugin.CppUTestEclipsePlatform; 12 | import org.cpputest.plugin.CppUTestFactory; 13 | import org.cpputest.plugin.CppUTestSourceCodeStubberForEditor; 14 | import org.cpputest.plugin.CppUTestStubCodeUI; 15 | import org.cpputest.plugin.SourceCodeStubberForEditor; 16 | import org.cpputest.plugin.StubCodeUI; 17 | import org.junit.Test; 18 | 19 | public class CppUTestFactoryTest { 20 | final CppUTestFactory factory = new CppUTestFactory(); 21 | 22 | @Test 23 | public void testCreatePlatform() { 24 | CUTPlatformAdaptor platform = factory.createPlatformAdaptor(null); 25 | assertTrue(CppUTestEclipsePlatform.class.isInstance(platform)); 26 | } 27 | 28 | @Test 29 | public void testCreateSourceCodeStubber() { 30 | SourceCodeStubberForEditor obj = factory.createSourceCodeStubber(); 31 | assertTrue(CppUTestSourceCodeStubberForEditor.class.isInstance(obj)); 32 | } 33 | 34 | @Test 35 | public void testCreateCodeFormater() { 36 | CppCodeFormater obj = factory.createCodeFormater(); 37 | assertTrue(CompactCppCodeFormater.class.isInstance(obj)); 38 | } 39 | 40 | @Test 41 | public void testCreateEmptyStubber() { 42 | Stubber obj = factory.createEmptyStubber(); 43 | assertTrue(CppEmptyStubber.class.isInstance(obj)); 44 | } 45 | 46 | @Test 47 | public void testCreateStubCodeUI() { 48 | StubCodeUI obj = factory.createStubCodeUI(); 49 | assertTrue(CppUTestStubCodeUI.class.isInstance(obj)); 50 | } 51 | 52 | @Test 53 | public void testCreateDefaultMock() { 54 | Stubber obj = factory.createDefaultMockStubber(); 55 | assertTrue(CppDefaultMockStubber.class.isInstance(obj)); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/plugin/test/CppUTestSourceCodeStubberForEditorTest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin.test; 2 | 3 | import org.cpputest.codeGenerator.CppCode; 4 | import org.cpputest.codeGenerator.SourceCodeProvider; 5 | import org.cpputest.codeGenerator.Stubber; 6 | import org.cpputest.codeGenerator.WhoCreateStubFromSourceCode; 7 | import org.cpputest.plugin.CppUTestSourceCodeStubberForEditor; 8 | import org.jmock.Expectations; 9 | import org.jmock.Mockery; 10 | import org.jmock.integration.junit4.JMock; 11 | import org.jmock.integration.junit4.JUnit4Mockery; 12 | import org.junit.Test; 13 | import org.junit.runner.RunWith; 14 | import static org.junit.Assert.*; 15 | 16 | @RunWith(JMock.class) 17 | public class CppUTestSourceCodeStubberForEditorTest { 18 | private static final int CURSOR_OFFSET = 1; 19 | Mockery context = new JUnit4Mockery(); 20 | final String EXPECTED_STUB = "stub"; 21 | final String SOURCE_CODE = "code"; 22 | final String ALL_CODE = "all code"; 23 | final SourceCodeProvider resource = context.mock(SourceCodeProvider.class); 24 | final WhoCreateStubFromSourceCode codeGenerator = context.mock(WhoCreateStubFromSourceCode.class); 25 | final Stubber stubber = context.mock(Stubber.class); 26 | @Test 27 | public void testCopyEmptyStubOfSelectedCodeToClipboard() { 28 | final CppCode code = new CppCode("not empty"); 29 | context.checking(new Expectations() {{ 30 | allowing(resource).getSelectedText(); 31 | will(returnValue(SOURCE_CODE)); 32 | oneOf(codeGenerator).getStubOfCode(SOURCE_CODE, stubber); 33 | will(returnValue(code)); 34 | }}); 35 | CppUTestSourceCodeStubberForEditor cpputest = new CppUTestSourceCodeStubberForEditor(resource, codeGenerator); 36 | cpputest.getStubOfCodeInEditor(stubber); 37 | } 38 | @Test 39 | public void testShouldTryAgainWithFullTextParsingWhenNoFunctionIsSelected() { 40 | final CppCode emptyCode = new CppCode(); 41 | final CppCode code = new CppCode("not empty"); 42 | context.checking(new Expectations() {{ 43 | allowing(resource).getSelectedText(); 44 | will(returnValue(SOURCE_CODE)); 45 | oneOf(codeGenerator).getStubOfCode(SOURCE_CODE, stubber); 46 | will(returnValue(emptyCode)); 47 | allowing(resource).getFullText(); 48 | will(returnValue(ALL_CODE)); 49 | allowing(resource).getCursorPosition(); 50 | will(returnValue(CURSOR_OFFSET)); 51 | oneOf(codeGenerator).getStubOfCodeAtPosition(ALL_CODE, CURSOR_OFFSET, stubber); 52 | will(returnValue(code)); 53 | }}); 54 | CppUTestSourceCodeStubberForEditor cpputest = new CppUTestSourceCodeStubberForEditor(resource, codeGenerator); 55 | assertEquals(code, cpputest.getStubOfCodeInEditor(stubber)); 56 | } 57 | @Test 58 | public void testShouldAlertWhenNoFunctionSelect() { 59 | final CppCode emptyCode = new CppCode(); 60 | context.checking(new Expectations() {{ 61 | allowing(resource).getSelectedText(); 62 | will(returnValue(SOURCE_CODE)); 63 | oneOf(codeGenerator).getStubOfCode(SOURCE_CODE, stubber); 64 | will(returnValue(emptyCode)); 65 | allowing(resource).getFullText(); 66 | will(returnValue(ALL_CODE)); 67 | allowing(resource).getCursorPosition(); 68 | will(returnValue(CURSOR_OFFSET)); 69 | oneOf(codeGenerator).getStubOfCodeAtPosition(ALL_CODE, CURSOR_OFFSET, stubber); 70 | will(returnValue(emptyCode)); 71 | }}); 72 | CppUTestSourceCodeStubberForEditor cpputest = new CppUTestSourceCodeStubberForEditor(resource, codeGenerator); 73 | assertEquals(emptyCode, cpputest.getStubOfCodeInEditor(stubber)); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /org.cpputest.plugin.unittest/test/org/cpputest/plugin/test/CppUTestStubCodeUITest.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin.test; 2 | 3 | import org.cpputest.codeGenerator.CppCode; 4 | import org.cpputest.codeGenerator.CppCodeFormater; 5 | import org.cpputest.codeGenerator.CUTPlatformAdaptor; 6 | import org.cpputest.codeGenerator.Stubber; 7 | import org.cpputest.plugin.CppUTestStubCodeUI; 8 | import org.cpputest.plugin.SourceCodeStubberForEditor; 9 | import org.jmock.Expectations; 10 | import org.jmock.Mockery; 11 | import org.jmock.integration.junit4.JMock; 12 | import org.jmock.integration.junit4.JUnit4Mockery; 13 | import org.junit.Test; 14 | import org.junit.runner.RunWith; 15 | 16 | 17 | @RunWith(JMock.class) 18 | public class CppUTestStubCodeUITest { 19 | Mockery context = new JUnit4Mockery(); 20 | final String EXPECTED_STUB = "stub"; 21 | final String SOURCE_CODE = "code"; 22 | final String ALL_CODE = "all code"; 23 | final CUTPlatformAdaptor platform = context.mock(CUTPlatformAdaptor.class); 24 | final SourceCodeStubberForEditor stubbers = context.mock(SourceCodeStubberForEditor.class); 25 | final CppCodeFormater formater = context.mock(CppCodeFormater.class); 26 | final Stubber stubber = context.mock(Stubber.class); 27 | CppUTestStubCodeUI cpputest = new CppUTestStubCodeUI(platform, stubbers, formater); 28 | @Test 29 | public void testCopyStubOfSelectedCodeToClipboard() { 30 | final CppCode code = new CppCode("not empty"); 31 | context.checking(new Expectations() {{ 32 | allowing(stubbers).getStubOfCodeInEditor(stubber); will(returnValue(code)); 33 | oneOf(formater).format(code); will(returnValue(EXPECTED_STUB)); 34 | oneOf(platform).copyToClipboard(EXPECTED_STUB); 35 | }}); 36 | cpputest.copyStubCodeInEditorToClipboard(stubber); 37 | } 38 | 39 | @Test 40 | public void testShouldAlertWhenNoFunctionSelect() { 41 | final CppCode emptyCode = new CppCode(); 42 | context.checking(new Expectations() {{ 43 | oneOf(stubbers).getStubOfCodeInEditor(stubber); will(returnValue(emptyCode)); 44 | oneOf(platform).messageBox("No function is selected."); 45 | }}); 46 | cpputest.copyStubCodeInEditorToClipboard(stubber); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /org.cpputest.plugin/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /org.cpputest.plugin/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /org.cpputest.plugin/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.cpputest.plugin 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.pde.ManifestBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.pde.SchemaBuilder 20 | 21 | 22 | 23 | 24 | 25 | org.eclipse.pde.PluginNature 26 | org.eclipse.jdt.core.javanature 27 | 28 | 29 | -------------------------------------------------------------------------------- /org.cpputest.plugin/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 4 | org.eclipse.jdt.core.compiler.compliance=1.6 5 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 6 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 7 | org.eclipse.jdt.core.compiler.source=1.6 8 | -------------------------------------------------------------------------------- /org.cpputest.plugin/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Bundle-ManifestVersion: 2 3 | Bundle-Name: CppUTest Eclipse Plugin: general 4 | Bundle-SymbolicName: org.cpputest.plugin.general;singleton:=true 5 | Bundle-Version: 1.0.0.qualifier 6 | Bundle-Activator: org.cpputest.plugin.Activator 7 | Bundle-Vendor: odd-e.com 8 | Require-Bundle: org.eclipse.ui, 9 | org.eclipse.core.runtime, 10 | org.eclipse.core.resources, 11 | org.eclipse.ui.workbench.texteditor 12 | Bundle-RequiredExecutionEnvironment: JavaSE-1.6 13 | Bundle-ActivationPolicy: lazy 14 | Import-Package: org.eclipse.jface.text 15 | -------------------------------------------------------------------------------- /org.cpputest.plugin/build.properties: -------------------------------------------------------------------------------- 1 | source.. = src/ 2 | output.. = bin/ 3 | bin.includes = plugin.xml,\ 4 | META-INF/,\ 5 | .,\ 6 | icons/ 7 | -------------------------------------------------------------------------------- /org.cpputest.plugin/icons/sample.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/org.cpputest.plugin/icons/sample.gif -------------------------------------------------------------------------------- /org.cpputest.plugin/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 11 | 14 | 16 | 17 | 18 | 24 | 25 | 30 | 31 | 32 | 33 | 35 | 39 | 43 | 45 | 46 | 47 | 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /org.cpputest.plugin/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.cpputest.plugin 8 | org.cpputest.plugin.parent 9 | 1.0.0-SNAPSHOT 10 | 11 | org.cpputest.plugin 12 | org.cpputest.plugin.general 13 | 1.0.0-SNAPSHOT 14 | eclipse-plugin 15 | 16 | 17 | 18 | org.apache.maven.plugins 19 | maven-jar-plugin 20 | 21 | 22 | org.eclipse.tycho 23 | tycho-surefire-plugin 24 | ${tycho.version} 25 | 26 | org.eclipse.equinox.p2.tests.AutomatedTests 27 | 28 | 29 | 30 | 31 | org.eclipse.tycho 32 | tycho-packaging-plugin 33 | ${tycho.version} 34 | 35 | 36 | false 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | junit 46 | junit 47 | 4.13.1 48 | test 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/codeGenerator/CUTPlatformAdaptor.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator; 2 | 3 | public interface CUTPlatformAdaptor { 4 | 5 | void copyToClipboard(String string); 6 | 7 | void messageBox(String string); 8 | } 9 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/codeGenerator/CompactCppCodeFormater.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator; 2 | 3 | 4 | 5 | public class CompactCppCodeFormater implements CppCodeFormater { 6 | 7 | public String format(CppCode code) { 8 | return code.toString(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/codeGenerator/CppCode.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator; 2 | 3 | public class CppCode { 4 | private String code; 5 | public CppCode(){ 6 | code = ""; 7 | } 8 | public CppCode(String str){ 9 | code = str; 10 | } 11 | public CppCode append(CppCode emptyStub) { 12 | code += emptyStub.toString(); 13 | return this; 14 | } 15 | public void append(String string) { 16 | code += string; 17 | } 18 | @Override 19 | public String toString() { 20 | return code; 21 | } 22 | @Override 23 | public int hashCode() { 24 | final int prime = 31; 25 | int result = 1; 26 | result = prime * result + ((code == null) ? 0 : code.hashCode()); 27 | return result; 28 | } 29 | @Override 30 | public boolean equals(Object obj) { 31 | if (this == obj) 32 | return true; 33 | if (obj == null) 34 | return false; 35 | if (getClass() != obj.getClass()) 36 | return false; 37 | CppCode other = (CppCode) obj; 38 | if (code == null) { 39 | if (other.code != null) 40 | return false; 41 | } else if (!code.equals(other.code)) 42 | return false; 43 | return true; 44 | } 45 | public boolean isEmpty() { 46 | return code.isEmpty(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/codeGenerator/CppCodeFormater.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator; 2 | 3 | 4 | public interface CppCodeFormater { 5 | 6 | public abstract String format(CppCode code); 7 | 8 | } -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/codeGenerator/CppDefaultMockStubber.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator; 2 | 3 | import org.cpputest.parser.langunit.CppLangFunctionSignature; 4 | 5 | public class CppDefaultMockStubber implements Stubber { 6 | 7 | @Override 8 | public CppCode getStubOfSignature(CppLangFunctionSignature signature) { 9 | CppCode stub = signature.getCode(); 10 | stub.append("{"); 11 | stub.append("mock().actualCall(\""); 12 | stub.append(signature.getFunctionName()); 13 | stub.append("\");"); 14 | stub.append("}\n"); 15 | return stub; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/codeGenerator/CppEmptyStubber.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator; 2 | 3 | import org.cpputest.parser.langunit.CppLangFunctionSignature; 4 | 5 | public class CppEmptyStubber implements Stubber { 6 | 7 | @Override 8 | public CppCode getStubOfSignature(CppLangFunctionSignature signature) { 9 | CppCode stub = signature.getCode(); 10 | if (signature.isVoid()) 11 | stub.append("{}\n"); 12 | else 13 | stub.append("{return 0;}\n"); 14 | return stub; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/codeGenerator/CppUTestPlatform.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator; 2 | 3 | public interface CppUTestPlatform { 4 | 5 | void copyToClipboard(String string); 6 | 7 | void messageBox(String string); 8 | } 9 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/codeGenerator/CppUTestStubCreator.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator; 2 | 3 | import org.cpputest.parser.SourceCodeReader; 4 | import org.cpputest.parser.langunit.CppLangFunctionSignature; 5 | 6 | public class CppUTestStubCreator implements WhoCreateStubFromSourceCode { 7 | private final SourceCodeReader signatures; 8 | 9 | public CppUTestStubCreator(SourceCodeReader reader) { 10 | this.signatures = reader; 11 | } 12 | 13 | @Override 14 | public CppCode getStubOfCode(String sourceCode, Stubber stubber) { 15 | CppCode stubCode = new CppCode(); 16 | for(CppLangFunctionSignature sig: signatures.signatures(sourceCode)) 17 | stubCode.append(stubber.getStubOfSignature(sig)); 18 | 19 | return stubCode; 20 | } 21 | 22 | @Override 23 | public CppCode getStubOfCodeAtPosition(String allCode, int offset, 24 | Stubber stubber) { 25 | for(CppLangFunctionSignature sig: signatures.signatures(allCode)) 26 | if (sig.isOffsetInclusive(offset)) 27 | return stubber.getStubOfSignature(sig); 28 | 29 | return new CppCode(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/codeGenerator/SourceCodeProvider.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator; 2 | 3 | public interface SourceCodeProvider { 4 | 5 | public abstract String getFullText(); 6 | 7 | public abstract int getCursorPosition(); 8 | 9 | public abstract String getSelectedText(); 10 | 11 | } -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/codeGenerator/SourceCodeResource.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator; 2 | 3 | public interface SourceCodeResource { 4 | 5 | public abstract String getFullText(); 6 | 7 | public abstract int getCursorPosition(); 8 | 9 | public abstract String getSelectedText(); 10 | 11 | } -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/codeGenerator/Stubber.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator; 2 | 3 | import org.cpputest.parser.langunit.CppLangFunctionSignature; 4 | 5 | public interface Stubber { 6 | 7 | CppCode getStubOfSignature(CppLangFunctionSignature signature); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/codeGenerator/WhoCreateStubFromSourceCode.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.codeGenerator; 2 | 3 | public interface WhoCreateStubFromSourceCode { 4 | CppCode getStubOfCode(String string, Stubber stubber); 5 | 6 | CppCode getStubOfCodeAtPosition(String aLL_CODE, int oFFSET, Stubber stubber); 7 | } 8 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/parser/CppSourceCodeReader.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser; 2 | 3 | import java.util.Iterator; 4 | import org.cpputest.parser.impl.CppLikeCodeTokenSplitter; 5 | import org.cpputest.parser.impl.CppPotentialLanguagePartsToMeaningfulLanguageUnitsTranslator; 6 | import org.cpputest.parser.impl.CppTokensToPotentialLanguagePartsTranslator; 7 | import org.cpputest.parser.impl.Token; 8 | import org.cpputest.parser.langunit.CppLangFunctionSignature; 9 | import org.cpputest.parser.langunit.LanguageUnit; 10 | import org.cpputest.parser.parts.impl.parts.CppPart; 11 | 12 | public class CppSourceCodeReader implements SourceCodeReader { 13 | public class SignatureIterable implements Iterable { 14 | 15 | private final Iterator units; 16 | 17 | public SignatureIterable(Iterable units) { 18 | this.units = units.iterator(); 19 | } 20 | 21 | @Override 22 | public Iterator iterator() { 23 | return new Iterator() { 24 | 25 | @Override 26 | public boolean hasNext() { 27 | return units.hasNext(); 28 | } 29 | 30 | @Override 31 | public CppLangFunctionSignature next() { 32 | return (CppLangFunctionSignature)units.next(); 33 | } 34 | 35 | @Override 36 | public void remove() { 37 | } 38 | 39 | }; 40 | } 41 | 42 | } 43 | @Override 44 | public Iterable signatures(String sourceCode) { 45 | Iterable tokens = new CppLikeCodeTokenSplitter().tokens(sourceCode); 46 | Iterable parts = new CppTokensToPotentialLanguagePartsTranslator().getPotentialLanguageParts(tokens); 47 | Iterable units = new CppPotentialLanguagePartsToMeaningfulLanguageUnitsTranslator().languageUnits(parts); 48 | return new SignatureIterable(units); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/parser/SourceCodeReader.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser; 2 | 3 | import org.cpputest.parser.langunit.CppLangFunctionSignature; 4 | 5 | public interface SourceCodeReader { 6 | 7 | Iterable signatures(String sourceCode); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/parser/impl/CppLikeCodeTokenSplitter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * This code is ported from a Python project "hfcca" by me. -- terryyin 3 | * 4 | * */ 5 | package org.cpputest.parser.impl; 6 | 7 | import java.util.Iterator; 8 | import java.util.regex.Matcher; 9 | import java.util.regex.Pattern; 10 | 11 | 12 | public class CppLikeCodeTokenSplitter { 13 | private Pattern token_pattern = Pattern.compile("(\\n|\\w+|/\\*|//|:=|::|>=|\\*=|>|#\\s*define|#\\s*if|#\\s*else|#\\s*endif|#\\s*\\w+|[!%^&\\*\\-=+\\|\\\\<>/\\]\\+]+|\\.{3}|.)"); 14 | 15 | public class TokenIterable implements Iterable { 16 | private final Iterator iterator; 17 | public TokenIterable(String string) { 18 | iterator = new TokenIterator(string); 19 | } 20 | 21 | @Override 22 | public Iterator iterator() { 23 | return iterator; 24 | } 25 | } 26 | public class TokenIterator implements Iterator { 27 | 28 | private Token nextValue; 29 | private final String sourceCode; 30 | private boolean in_middle_of_empty_lines = false; 31 | private int currentIndexInCode = 0; 32 | private final Matcher m; 33 | public TokenIterator(String string) { 34 | this.sourceCode = string; 35 | m = token_pattern.matcher(sourceCode); 36 | } 37 | 38 | @Override 39 | public boolean hasNext() { 40 | if (nextValue == null) 41 | nextValue = generateTokensFromSourceCode(); 42 | return nextValue != null; 43 | } 44 | 45 | @Override 46 | public Token next() { 47 | Token ret = nextValue; 48 | nextValue = null; 49 | return ret; 50 | } 51 | 52 | @Override 53 | public void remove() { 54 | } 55 | 56 | private Token generateTokensFromSourceCode(){ 57 | Token token; 58 | while (true){ 59 | token = generateTokensFromCodeWithMultipleNewlines(); 60 | if (token == null) break; 61 | if (!token.isNewLine() || ! in_middle_of_empty_lines) 62 | return token; 63 | in_middle_of_empty_lines = token.isNewLine(); 64 | 65 | } 66 | return null; 67 | } 68 | private Token generateTokensFromCodeWithMultipleNewlines(){ 69 | while (true) { 70 | int currentTokenOffset = currentIndexInCode; 71 | if(!m.find(currentIndexInCode)) 72 | break; 73 | String token = m.group(0); 74 | if (token.equals("\n")) { 75 | currentIndexInCode = currentIndexInCode + 1; 76 | currentTokenOffset += 1; 77 | } 78 | else if (token.startsWith("#")){ 79 | while(true){ 80 | int bindex = currentIndexInCode + 1; 81 | currentIndexInCode = sourceCode.indexOf("\n", bindex);; 82 | if (currentIndexInCode == -1) 83 | break; 84 | if (!sourceCode.substring(bindex,currentIndexInCode).trim().endsWith("\\")) 85 | break; 86 | } 87 | if(currentIndexInCode == -1) 88 | break; 89 | token = sourceCode.substring(m.start(0),currentIndexInCode); 90 | } 91 | else if (token.equals("/*")) { 92 | currentIndexInCode = sourceCode.indexOf("*/", currentIndexInCode+2); 93 | if (currentIndexInCode == -1) 94 | break; 95 | currentIndexInCode += 2; 96 | token = sourceCode.substring(m.start(0),currentIndexInCode); 97 | } 98 | else if (token.equals("//") || token.equals("#if") || token.equals("#endif")) { 99 | currentIndexInCode = sourceCode.indexOf("\n", currentIndexInCode); 100 | if (currentIndexInCode == -1) 101 | break; 102 | } 103 | else if(token.equals("\"") || token.equals("\'")) { 104 | while(true){ 105 | currentIndexInCode += 1; 106 | currentIndexInCode = sourceCode.indexOf(token, currentIndexInCode); 107 | if (currentIndexInCode == -1) 108 | break; 109 | if (sourceCode.charAt(currentIndexInCode - 1) == '\\' && sourceCode.charAt(currentIndexInCode - 2) != '\\') 110 | continue; 111 | break; 112 | } 113 | if (currentIndexInCode == -1) 114 | break; 115 | token = sourceCode.substring(m.start(0),currentIndexInCode+1); 116 | currentIndexInCode = currentIndexInCode + 1; 117 | } 118 | else 119 | currentIndexInCode = m.end(0); 120 | if (!Character.isWhitespace(token.charAt(0)) && token.charAt(0) != '#') 121 | return new Token(token, currentTokenOffset); 122 | 123 | } 124 | return null; 125 | } 126 | } 127 | public TokenIterable tokens(String string) { 128 | return new TokenIterable(string); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/parser/impl/CppPotentialLanguagePartsToMeaningfulLanguageUnitsTranslator.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser.impl; 2 | 3 | import java.util.Iterator; 4 | 5 | import org.cpputest.parser.langunit.CppLangFunctionSignature; 6 | import org.cpputest.parser.langunit.LanguageUnit; 7 | import org.cpputest.parser.langunit.CppLangFunctionSignature.SignatureBuilder; 8 | import org.cpputest.parser.parts.impl.parts.CppPart; 9 | 10 | public class CppPotentialLanguagePartsToMeaningfulLanguageUnitsTranslator{ 11 | public CppPotentialLanguagePartsToMeaningfulLanguageUnitsTranslator() { 12 | } 13 | 14 | public class LanguageUnitIterator implements Iterator { 15 | private SignatureBuilder signatureBuilder = CppLangFunctionSignature.builderStartWith(""); 16 | private LanguageUnit nextValue = null; 17 | private final Iterator parts; 18 | 19 | public LanguageUnitIterator(Iterable parts) { 20 | this.parts = parts.iterator(); 21 | } 22 | 23 | @Override 24 | public boolean hasNext() { 25 | if (nextValue == null) 26 | nextValue = getNextLanguageUnit(); 27 | return nextValue != null; 28 | } 29 | 30 | @Override 31 | public LanguageUnit next() { 32 | LanguageUnit ret = nextValue; 33 | nextValue = null; 34 | return ret; 35 | } 36 | 37 | @Override 38 | public void remove() { 39 | 40 | } 41 | 42 | private LanguageUnit getNextLanguageUnit() { 43 | while (parts.hasNext()) { 44 | CppPart part = parts.next(); 45 | switch (part.getType()) { 46 | case END_OF_FUNCTION_SIGNATURE: 47 | signatureBuilder.withEndOffset(part.getEndOffset()); 48 | if (signatureBuilder.isComplete()) 49 | return signatureBuilder.build(); 50 | case MAYBE_NEW_FUNCTION: 51 | signatureBuilder = CppLangFunctionSignature.builderStartWith(part.codeString()); 52 | signatureBuilder.withBeginOffset(part.getBeginOffset()); 53 | break; 54 | case MAYBE_PART_OF_FUNCTION_NAME: 55 | signatureBuilder 56 | .addToFunctionDeclaration(part.codeString()); 57 | break; 58 | case PARAMETER: 59 | signatureBuilder.addToParameter(part.codeString()); 60 | break; 61 | case END_OF_FUNCTION: 62 | case PART_OF_LONG_FUNCTION_NAME: 63 | case TOKEN: 64 | break; 65 | } 66 | } 67 | return null; 68 | } 69 | } 70 | 71 | public Iterable languageUnits(final Iterable parts) { 72 | return new Iterable() { 73 | 74 | @Override 75 | public Iterator iterator() { 76 | return new LanguageUnitIterator(parts); 77 | } 78 | 79 | }; 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/parser/impl/CppTokensToPotentialLanguagePartsTranslator.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser.impl; 2 | 3 | import java.util.Iterator; 4 | 5 | import org.cpputest.parser.parts.impl.parts.CppPart; 6 | 7 | public class CppTokensToPotentialLanguagePartsTranslator{ 8 | private enum State { 9 | GLOBAL, DEC, NAMESPACE, DEC_TO_IMP, IMP, FUNCTION_NAME, TYPEDEF, GLOBAL_ASSIGNMENT 10 | } 11 | public CppTokensToPotentialLanguagePartsTranslator() { 12 | } 13 | public class PotentialLanguagePartsIterator implements Iterator { 14 | private State _state=State.GLOBAL; 15 | 16 | private int pa_count = 0; 17 | private int br_count = 0; 18 | 19 | private CppPart nextValue = null; 20 | private final Iterator tokens; 21 | 22 | public PotentialLanguagePartsIterator(Iterable tokens) { 23 | this.tokens = tokens.iterator(); 24 | } 25 | 26 | @Override 27 | public boolean hasNext() { 28 | if (nextValue == null) 29 | nextValue = getNextPotentialLanguagePart(); 30 | return nextValue != null; 31 | } 32 | 33 | @Override 34 | public CppPart next() { 35 | CppPart ret = nextValue; 36 | nextValue = null; 37 | return ret; 38 | } 39 | 40 | @Override 41 | public void remove() { 42 | } 43 | 44 | private CppPart getNextPotentialLanguagePart() { 45 | while (tokens.hasNext()){ 46 | Token token = tokens.next(); 47 | CppPart fun = read_token(token); 48 | if (fun != null) 49 | return fun; 50 | } 51 | return null; 52 | } 53 | private CppPart read_token(Token token){ 54 | if (token.isWhiteSpace()) 55 | return null;//sUniversalCodeCounter.NEW_LINE, None 56 | else 57 | return input(token); 58 | } 59 | protected CppPart input(Token token) { 60 | if (token.isPreprocessor() || 61 | token.isComment()) 62 | return null; 63 | 64 | switch(get_state()){ 65 | case GLOBAL: 66 | return _GLOBAL(token); 67 | case FUNCTION_NAME: 68 | return _GLOBAL(token); 69 | case GLOBAL_ASSIGNMENT: 70 | return _GLOBAL_ASSIGNMENT(token); 71 | case DEC: 72 | return _DEC(token); 73 | case NAMESPACE: 74 | return _NAMESPACE(token); 75 | case DEC_TO_IMP: 76 | return _DEC_TO_IMP(token); 77 | case IMP: 78 | return _IMP(token); 79 | case TYPEDEF: 80 | return _TYPEDEF(token); 81 | } 82 | return null; 83 | } 84 | 85 | private CppPart _GLOBAL(Token token){ 86 | if (token.isAssignment()) { 87 | set_state(State.GLOBAL_ASSIGNMENT); 88 | return CppPart.Assignment(); 89 | } 90 | else if (token.isTypeDef()) { 91 | set_state(State.TYPEDEF); 92 | return CppPart.Typedef(); 93 | } 94 | else if (token.isLeftParentheses()) { 95 | pa_count += 1; 96 | set_state(State.DEC); 97 | return CppPart.PartOfLongFunctionName(token); 98 | } 99 | else if (token.equals("::")) 100 | set_state(State.NAMESPACE); 101 | else if (token.isSemicolon()) { 102 | set_state(State.GLOBAL); 103 | return CppPart.EndOfGlobalStatement(); 104 | } 105 | else { 106 | if (get_state() == State.FUNCTION_NAME) 107 | return CppPart.AddToFunctionName(token); 108 | set_state(State.FUNCTION_NAME); 109 | return CppPart.StartNewFunction(token); 110 | } 111 | return null; 112 | } 113 | private CppPart _GLOBAL_ASSIGNMENT(Token token) { 114 | if (token.isSemicolon()) { 115 | set_state(State.GLOBAL); 116 | return CppPart.EndOfGlobalStatement(); 117 | } 118 | return null; 119 | } 120 | private CppPart _TYPEDEF(Token token) { 121 | if (token.isSemicolon()) 122 | set_state(State.GLOBAL); 123 | return null; 124 | } 125 | private CppPart _NAMESPACE(Token token){ 126 | set_state(State.GLOBAL); 127 | return CppPart.AddToFunctionName(new Token("::"+token.toString(), 0)); 128 | } 129 | private CppPart _DEC(Token token){ 130 | if (token.isLeftBracket()) 131 | pa_count += 1; 132 | else if(token.isRightBracket()){ 133 | pa_count -= 1; 134 | if (pa_count == 0) { 135 | set_state(State.DEC_TO_IMP); 136 | return CppPart.EndOfFunctionSignature(token); 137 | } 138 | } 139 | else if (pa_count == 1) 140 | return CppPart.Parameter(token); 141 | return CppPart.PartOfLongFunctionName(new Token(" " + token.toString(), 0)); 142 | } 143 | private CppPart _DEC_TO_IMP(Token token){ 144 | if (token.equals("const")) 145 | return CppPart.PartOfLongFunctionName(new Token(" " + token.toString(), 0)); 146 | else if (token.isLeftBrace()){ 147 | br_count += 1; 148 | set_state(State.IMP); 149 | } 150 | else 151 | set_state(State.GLOBAL); 152 | return null; 153 | } 154 | private CppPart _IMP(Token token){ 155 | if (token.isLeftBrace()) 156 | br_count += 1; 157 | else if(token.isRightBrace()){ 158 | br_count -= 1; 159 | if (br_count == 0){ 160 | set_state(State.GLOBAL); 161 | return CppPart.EndOfFunction(); 162 | } 163 | } 164 | return CppPart.Token(token); 165 | } 166 | protected State get_state() { 167 | return _state; 168 | } 169 | protected void set_state(State _state) { 170 | this._state = _state; 171 | } 172 | } 173 | public Iterable getPotentialLanguageParts(final Iterable tokens) { 174 | return new Iterable (){ 175 | 176 | @Override 177 | public Iterator iterator() { 178 | return new PotentialLanguagePartsIterator(tokens); 179 | } 180 | 181 | }; 182 | } 183 | 184 | } 185 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/parser/impl/Token.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser.impl; 2 | 3 | public final class Token { 4 | public static final int UNDEFINED_OFFSET = -1; 5 | private final String token; 6 | private final int offset; 7 | 8 | protected Token(String token, int offset) { 9 | this.token = token; 10 | this.offset = offset; 11 | } 12 | 13 | @Override 14 | public String toString() { 15 | return token; 16 | } 17 | 18 | public boolean isNewLine() { 19 | return token.equals("\n"); 20 | } 21 | 22 | public boolean isWhiteSpace() { 23 | return Character.isWhitespace(token.charAt(0)); 24 | } 25 | 26 | public boolean isPreprocessor() { 27 | return token.startsWith("#"); 28 | } 29 | 30 | public boolean isComment() { 31 | return token.startsWith("/*") || 32 | token.startsWith("//"); 33 | } 34 | 35 | public boolean isSemicolon() { 36 | return token.equals(";"); 37 | } 38 | 39 | public boolean isLeftBracket() { 40 | return token.equals("(") || token.equals("<"); 41 | } 42 | 43 | public boolean isRightBracket() { 44 | return token.equals(")") || token.equals(">") || token.equals("*>"); 45 | } 46 | 47 | public boolean isLeftParentheses() { 48 | return token.equals("("); 49 | } 50 | 51 | public boolean isLeftBrace() { 52 | return token.equals("{"); 53 | } 54 | 55 | public boolean isRightBrace() { 56 | return token.equals("}"); 57 | } 58 | 59 | public boolean isTypeDef() { 60 | return token.equals("typedef"); 61 | } 62 | 63 | public boolean isAssignment() { 64 | return token.equals("="); 65 | } 66 | 67 | public static Token token(String string, int offset) { 68 | return new Token(string, offset); 69 | } 70 | 71 | public static Token token(String string) { 72 | return new Token(string, UNDEFINED_OFFSET); 73 | } 74 | public int getBeginOffset() { 75 | return offset; 76 | } 77 | 78 | public int getEndOffset() { 79 | return offset; 80 | } 81 | @Override 82 | public int hashCode() { 83 | final int prime = 31; 84 | int result = 1; 85 | result = prime * result + ((token == null) ? 0 : token.hashCode()); 86 | return result; 87 | } 88 | 89 | @Override 90 | public boolean equals(Object obj) { 91 | if (this == obj) 92 | return true; 93 | if (obj == null) 94 | return false; 95 | if (getClass() != obj.getClass()) 96 | return false; 97 | Token other = (Token) obj; 98 | if (token == null) { 99 | if (other.token != null) 100 | return false; 101 | } else if (!token.equals(other.token)) 102 | return false; 103 | return true; 104 | } 105 | 106 | 107 | } 108 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/parser/langunit/CppLangFunctionImplementation.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser.langunit; 2 | 3 | import org.cpputest.codeGenerator.CppCode; 4 | 5 | public class CppLangFunctionImplementation implements LanguageUnit { 6 | 7 | public CppLangFunctionImplementation(String code) { 8 | } 9 | 10 | @Override 11 | public CppCode getCode() { 12 | return null; 13 | } 14 | 15 | @Override 16 | public boolean isOffsetInclusive(int offset) { 17 | return false; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/parser/langunit/CppLangFunctionSignature.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser.langunit; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | import java.util.Queue; 7 | 8 | import org.cpputest.codeGenerator.CppCode; 9 | import org.cpputest.parser.impl.Token; 10 | 11 | public final class CppLangFunctionSignature implements LanguageUnit { 12 | 13 | private String functionName; 14 | private String returnType; 15 | private List parameters = new ArrayList(); 16 | private int beginOffset; 17 | private int endOffset; 18 | 19 | protected void setFunctionName(String functionName) { 20 | this.functionName = functionName; 21 | 22 | } 23 | public String getFunctionName() { 24 | return functionName; 25 | } 26 | protected void setReturnType(String returnType) { 27 | this.returnType = returnType; 28 | 29 | } 30 | 31 | @Override 32 | public CppCode getCode() { 33 | return new CppCode(returnType + " " + functionName + "(" + 34 | join(parameters, " ") + ")"); 35 | } 36 | 37 | public boolean isVoid() { 38 | return returnType.equals("void"); 39 | } 40 | 41 | protected void addToParameter(String parameter) { 42 | this.parameters.add(parameter); 43 | 44 | } 45 | static public String join(List list, String conjunction) 46 | { 47 | StringBuilder sb = new StringBuilder(); 48 | boolean first = true; 49 | for (String item : list) 50 | { 51 | if (first) 52 | first = false; 53 | else 54 | sb.append(conjunction); 55 | sb.append(item); 56 | } 57 | return sb.toString(); 58 | } 59 | 60 | @Override 61 | public boolean isOffsetInclusive(int offset) { 62 | return beginOffset <= offset && endOffset >= offset; 63 | } 64 | 65 | protected void setBeginAndEndOffsets(int beginOffset, int endOffset) { 66 | this.beginOffset = beginOffset; 67 | this.endOffset = endOffset; 68 | 69 | } 70 | public static class SignatureBuilder { 71 | CppLangFunctionSignature signature; 72 | private String currentDecidedReturnType = ""; 73 | private boolean hasReturnType = false; 74 | private boolean hasFunctionName = false; 75 | private Queue tokensBeforeFunctionNameDecided = new LinkedList(); 76 | private int beginOffset; 77 | private int endOffset; 78 | 79 | private SignatureBuilder(String possibleReturnType) { 80 | signature = new CppLangFunctionSignature(); 81 | addToFunctionDeclaration(possibleReturnType); 82 | } 83 | public CppLangFunctionSignature build() { 84 | signature.setBeginAndEndOffsets(beginOffset, endOffset); 85 | return signature; 86 | } 87 | public SignatureBuilder withBeginOffset(int beginOffset) { 88 | this.beginOffset = beginOffset; 89 | return this; 90 | } 91 | public SignatureBuilder withEndOffset(int endOffset){ 92 | this.endOffset = endOffset; 93 | return this; 94 | } 95 | public SignatureBuilder addToFunctionDeclaration(String partOfFunctionDeclaration) { 96 | tokensBeforeFunctionNameDecided.add(partOfFunctionDeclaration); 97 | addToReturnTypeAndFunctionName(); 98 | return this; 99 | } 100 | public SignatureBuilder addToFunctionDeclaration(Token token2) { 101 | return this; 102 | } 103 | private void addToReturnTypeAndFunctionName() { 104 | while(!tryToFitTokensInTheQueueIntoReturnTypeAndFunctionName()) 105 | tokensBeforeFunctionNameDecided.remove(); 106 | } 107 | private void reset() { 108 | hasReturnType = false; 109 | hasFunctionName = false; 110 | } 111 | private boolean tryToFitTokensInTheQueueIntoReturnTypeAndFunctionName() { 112 | reset(); 113 | for (String token: tokensBeforeFunctionNameDecided) 114 | if (!addTokenToCurrentTryOfReturnTyeAndFunctionName(token)) 115 | return false; 116 | return true; 117 | } 118 | private boolean addTokenToCurrentTryOfReturnTyeAndFunctionName( 119 | String partOfFunctionDeclaration) { 120 | if (!hasReturnType) { 121 | this.currentDecidedReturnType = partOfFunctionDeclaration; 122 | signature.setReturnType(currentDecidedReturnType); 123 | hasReturnType = true; 124 | } 125 | else if (currentDecidedReturnType.equals("const")) 126 | addToReturnType(partOfFunctionDeclaration); 127 | else if (hasFunctionName) { 128 | addToReturnType(signature.getFunctionName()); 129 | signature.setFunctionName(partOfFunctionDeclaration); 130 | } 131 | else if (partOfFunctionDeclaration.equals("*")) 132 | addToReturnType(partOfFunctionDeclaration); 133 | else { 134 | signature.setFunctionName(partOfFunctionDeclaration); 135 | hasFunctionName = true; 136 | } 137 | return true; 138 | } 139 | public SignatureBuilder addToParameter(String parameter) { 140 | signature.addToParameter(parameter); 141 | return this; 142 | } 143 | private void addToReturnType(String string) { 144 | currentDecidedReturnType += " " + string; 145 | signature.setReturnType(currentDecidedReturnType); 146 | } 147 | public boolean isComplete() { 148 | return hasFunctionName ; 149 | } 150 | } 151 | public static SignatureBuilder builderStartWith(String string) { 152 | return new SignatureBuilder(string); 153 | } 154 | } 155 | 156 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/parser/langunit/LanguageUnit.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser.langunit; 2 | 3 | import org.cpputest.codeGenerator.CppCode; 4 | 5 | public interface LanguageUnit { 6 | 7 | CppCode getCode(); 8 | 9 | boolean isOffsetInclusive(int offset); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/parser/parts/impl/parts/CppPart.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.parser.parts.impl.parts; 2 | 3 | import org.cpputest.parser.impl.Token; 4 | 5 | public class CppPart { 6 | public enum Type { 7 | MAYBE_NEW_FUNCTION, END_OF_FUNCTION_SIGNATURE, PART_OF_LONG_FUNCTION_NAME 8 | , MAYBE_PART_OF_FUNCTION_NAME, PARAMETER, END_OF_FUNCTION, TOKEN, END_OF_GLOBAL_STATEMENT, PREPROCESSOR, TYPEDEF, ASSIGNMENT} 9 | private Type type; 10 | private final Token token; 11 | private CppPart(Type type, Token token){ 12 | this.type = type; 13 | this.token = token; 14 | } 15 | public Type getType(){ 16 | return type; 17 | } 18 | public static CppPart StartNewFunction(Token token2) { 19 | return new CppPart(Type.MAYBE_NEW_FUNCTION, token2); 20 | } 21 | static public CppPart EndOfFunctionSignature(org.cpputest.parser.impl.Token token2){ 22 | return new CppPart(Type.END_OF_FUNCTION_SIGNATURE, token2); 23 | } 24 | public static CppPart PartOfLongFunctionName(Token token) { 25 | return new CppPart(Type.PART_OF_LONG_FUNCTION_NAME, token); 26 | } 27 | public static CppPart AddToFunctionName(Token token2) { 28 | return new CppPart(Type.MAYBE_PART_OF_FUNCTION_NAME, token2); 29 | } 30 | public static CppPart Parameter(org.cpputest.parser.impl.Token token2) { 31 | return new CppPart(Type.PARAMETER, token2); 32 | } 33 | public static CppPart EndOfFunction() { 34 | return new CppPart(Type.END_OF_FUNCTION, null); 35 | } 36 | public static CppPart Token(org.cpputest.parser.impl.Token token2) { 37 | return new CppPart(Type.TOKEN, token2); 38 | } 39 | public static CppPart EndOfGlobalStatement() { 40 | return new CppPart(Type.END_OF_GLOBAL_STATEMENT, null); 41 | } 42 | 43 | public static CppPart Typedef() { 44 | return new CppPart(Type.TYPEDEF, null); 45 | } 46 | public static CppPart Assignment() { 47 | return new CppPart(Type.ASSIGNMENT, null); 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return "CppPart [type=" + type + ", token=" + token + "]"; 53 | } 54 | @Override 55 | public int hashCode() { 56 | final int prime = 31; 57 | int result = 1; 58 | result = prime * result + ((token == null) ? 0 : token.hashCode()); 59 | result = prime * result + ((type == null) ? 0 : type.hashCode()); 60 | return result; 61 | } 62 | @Override 63 | public boolean equals(Object obj) { 64 | if (this == obj) 65 | return true; 66 | if (obj == null) 67 | return false; 68 | if (getClass() != obj.getClass()) 69 | return false; 70 | CppPart other = (CppPart) obj; 71 | if (token == null) { 72 | if (other.token != null) 73 | return false; 74 | } else if (!token.equals(other.token)) 75 | return false; 76 | if (type != other.type) 77 | return false; 78 | return true; 79 | } 80 | public String codeString() { 81 | return token.toString(); 82 | } 83 | public int getEndOffset() { 84 | return this.token.getEndOffset(); 85 | } 86 | public int getBeginOffset() { 87 | return this.token.getBeginOffset(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/plugin/Activator.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin; 2 | 3 | import org.eclipse.jface.resource.ImageDescriptor; 4 | import org.eclipse.ui.plugin.AbstractUIPlugin; 5 | import org.osgi.framework.BundleContext; 6 | 7 | public class Activator extends AbstractUIPlugin { 8 | 9 | // The plug-in ID 10 | public static final String PLUGIN_ID = "org.cpputest.plugin"; //$NON-NLS-1$ 11 | 12 | // The shared instance 13 | private static Activator plugin; 14 | 15 | public Activator() { 16 | } 17 | 18 | /* 19 | * (non-Javadoc) 20 | * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) 21 | */ 22 | public void start(BundleContext context) throws Exception { 23 | super.start(context); 24 | plugin = this; 25 | } 26 | 27 | /* 28 | * (non-Javadoc) 29 | * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) 30 | */ 31 | public void stop(BundleContext context) throws Exception { 32 | plugin = null; 33 | super.stop(context); 34 | } 35 | 36 | /** 37 | * Returns the shared instance 38 | * 39 | * @return the shared instance 40 | */ 41 | public static Activator getDefault() { 42 | return plugin; 43 | } 44 | 45 | /** 46 | * Returns an image descriptor for the image file at the given 47 | * plug-in relative path 48 | * 49 | * @param path the path 50 | * @return the image descriptor 51 | */ 52 | public static ImageDescriptor getImageDescriptor(String path) { 53 | return imageDescriptorFromPlugin(PLUGIN_ID, path); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/plugin/CppUTestEclipsePlatform.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin; 2 | 3 | import org.cpputest.codeGenerator.CUTPlatformAdaptor; 4 | import org.cpputest.codeGenerator.SourceCodeProvider; 5 | import org.eclipse.jface.viewers.ISelection; 6 | import org.eclipse.jface.dialogs.MessageDialog; 7 | import org.eclipse.jface.text.IDocument; 8 | import org.eclipse.jface.text.ITextSelection; 9 | import org.eclipse.swt.dnd.Clipboard; 10 | import org.eclipse.swt.dnd.TextTransfer; 11 | import org.eclipse.swt.dnd.Transfer; 12 | import org.eclipse.swt.widgets.Display; 13 | import org.eclipse.ui.IEditorPart; 14 | import org.eclipse.ui.ISelectionService; 15 | import org.eclipse.ui.IWorkbenchWindow; 16 | import org.eclipse.ui.texteditor.AbstractTextEditor; 17 | 18 | public class CppUTestEclipsePlatform implements CUTPlatformAdaptor, SourceCodeProvider { 19 | private static final String PLUGIN_NAME = "CppUTest"; 20 | IWorkbenchWindow workbenchWindow; 21 | 22 | public CppUTestEclipsePlatform(IWorkbenchWindow window) { 23 | workbenchWindow = window; 24 | } 25 | 26 | @Override 27 | public String getSelectedText() { 28 | ISelectionService ss = workbenchWindow.getSelectionService(); 29 | ISelection selection = ss.getSelection(); 30 | ITextSelection txt = (ITextSelection) selection; 31 | if (txt != null) 32 | return txt.getText(); 33 | return selection.toString(); 34 | } 35 | 36 | @Override 37 | public void copyToClipboard(String string) { 38 | Clipboard clipboard = new Clipboard(Display.getDefault()); 39 | clipboard.setContents(new Object[] { string }, 40 | new Transfer[] { TextTransfer.getInstance() }); 41 | clipboard.dispose(); 42 | } 43 | 44 | @Override 45 | public void messageBox(String string) { 46 | MessageDialog.openInformation(workbenchWindow.getShell(), PLUGIN_NAME, 47 | string); 48 | 49 | } 50 | 51 | @Override 52 | public String getFullText() { 53 | final IEditorPart activeEditor = workbenchWindow.getActivePage() 54 | .getActiveEditor(); 55 | if (activeEditor == null) 56 | return null; 57 | final AbstractTextEditor textEditor = (AbstractTextEditor) activeEditor 58 | .getAdapter(AbstractTextEditor.class); 59 | if (textEditor == null) 60 | return ""; 61 | 62 | IDocument document = textEditor.getDocumentProvider().getDocument( 63 | textEditor.getEditorInput()); 64 | 65 | return document.get(); 66 | } 67 | 68 | @Override 69 | public int getCursorPosition() { 70 | ISelectionService ss = workbenchWindow.getSelectionService(); 71 | ISelection selection = ss.getSelection(); 72 | ITextSelection txt = (ITextSelection) selection; 73 | return txt.getOffset(); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/plugin/CppUTestFactory.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin; 2 | 3 | import org.cpputest.codeGenerator.CompactCppCodeFormater; 4 | import org.cpputest.codeGenerator.CppCodeFormater; 5 | import org.cpputest.codeGenerator.CppDefaultMockStubber; 6 | import org.cpputest.codeGenerator.CppEmptyStubber; 7 | import org.cpputest.codeGenerator.CUTPlatformAdaptor; 8 | import org.cpputest.codeGenerator.CppUTestStubCreator; 9 | import org.cpputest.codeGenerator.Stubber; 10 | import org.cpputest.parser.CppSourceCodeReader; 11 | import org.eclipse.ui.IWorkbenchWindow; 12 | 13 | public class CppUTestFactory implements ICppUTestFactory { 14 | private CppUTestEclipsePlatform platform = null; 15 | // static public CppUTestActionRunnerForEclipseActions createCppUTestCodeGeneratorActions(IWorkbenchWindow window) { 16 | // CppSourceCodeReader reader = new CppSourceCodeReader(); 17 | // CppStubber stubber = new CppStubber(); 18 | // CppUTestEclipsePlatform eclipse = new CppUTestEclipsePlatform(window); 19 | // CppUTestStubCreator codeGenerator = new CppUTestStubCreator(reader, stubber); 20 | // SourceCodeStubber sourceCodeStubber = new CppUTestSourceCodeStubber(eclipse, codeGenerator); 21 | // CompactCppCodeFormater formater = new CompactCppCodeFormater(); 22 | // return new CppUTestActionRunnerForEclipseActions(new CppUTestStubbingActions(eclipse, sourceCodeStubber, formater)); 23 | // } 24 | 25 | @Override 26 | public CUTPlatformAdaptor createPlatformAdaptor(IWorkbenchWindow window) { 27 | platform = new CppUTestEclipsePlatform(window); 28 | return platform; 29 | } 30 | 31 | @Override 32 | public SourceCodeStubberForEditor createSourceCodeStubber() { 33 | CppSourceCodeReader reader = new CppSourceCodeReader(); 34 | CppUTestStubCreator codeGenerator = new CppUTestStubCreator(reader); 35 | return new CppUTestSourceCodeStubberForEditor(platform, codeGenerator); 36 | } 37 | 38 | @Override 39 | public CppCodeFormater createCodeFormater() { 40 | return new CompactCppCodeFormater(); 41 | } 42 | 43 | @Override 44 | public Stubber createEmptyStubber() { 45 | return new CppEmptyStubber(); 46 | } 47 | 48 | @Override 49 | public StubCodeUI createStubCodeUI() { 50 | return new CppUTestStubCodeUI(platform, createSourceCodeStubber(), createCodeFormater()); 51 | } 52 | 53 | @Override 54 | public Stubber createDefaultMockStubber() { 55 | return new CppDefaultMockStubber(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/plugin/CppUTestSourceCodeStubberForEditor.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin; 2 | 3 | import org.cpputest.codeGenerator.CppCode; 4 | import org.cpputest.codeGenerator.SourceCodeProvider; 5 | import org.cpputest.codeGenerator.Stubber; 6 | import org.cpputest.codeGenerator.WhoCreateStubFromSourceCode; 7 | 8 | public class CppUTestSourceCodeStubberForEditor implements SourceCodeStubberForEditor { 9 | private final SourceCodeProvider resource; 10 | private final WhoCreateStubFromSourceCode codeGenerator; 11 | 12 | public CppUTestSourceCodeStubberForEditor(SourceCodeProvider resource, 13 | WhoCreateStubFromSourceCode codeGenerator) { 14 | this.resource = resource; 15 | this.codeGenerator = codeGenerator; 16 | } 17 | protected CppCode getStubOfCodeAtPosition(Stubber stubber) { 18 | CppCode stubCode; 19 | String allCode = resource.getFullText(); 20 | int pos = resource.getCursorPosition(); 21 | stubCode = codeGenerator.getStubOfCodeAtPosition(allCode, pos, stubber); 22 | return stubCode; 23 | } 24 | protected CppCode getStubOfSelectedCode(Stubber stubber) { 25 | String code = resource.getSelectedText(); 26 | CppCode stubCode = codeGenerator.getStubOfCode(code, stubber); 27 | return stubCode; 28 | } 29 | @Override 30 | public CppCode getStubOfCodeInEditor(Stubber stubber) { 31 | CppCode stubCode = getStubOfSelectedCode(stubber); 32 | if (stubCode.isEmpty()) { 33 | stubCode = getStubOfCodeAtPosition(stubber); 34 | } 35 | return stubCode; 36 | } 37 | } -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/plugin/CppUTestStubCodeUI.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin; 2 | 3 | import org.cpputest.codeGenerator.CppCode; 4 | import org.cpputest.codeGenerator.CppCodeFormater; 5 | import org.cpputest.codeGenerator.CUTPlatformAdaptor; 6 | import org.cpputest.codeGenerator.Stubber; 7 | 8 | public class CppUTestStubCodeUI implements StubCodeUI { 9 | public final CUTPlatformAdaptor platform; 10 | public SourceCodeStubberForEditor sourceCodeStubber; 11 | public final CppCodeFormater formater; 12 | 13 | public CppUTestStubCodeUI(CUTPlatformAdaptor platform, SourceCodeStubberForEditor sourceCodeStubber, CppCodeFormater formater) { 14 | this.platform = platform; 15 | this.sourceCodeStubber = sourceCodeStubber; 16 | this.formater = formater; 17 | } 18 | protected void copyFormatedCodeToClipboard(CppCode stubCode) { 19 | if (!stubCode.isEmpty()) { 20 | String stub = formater.format(stubCode); 21 | platform.copyToClipboard(stub); 22 | } 23 | else 24 | platform.messageBox("No function is selected."); 25 | } 26 | @Override 27 | public void copyStubCodeInEditorToClipboard(Stubber stubber) { 28 | CppCode stubCode = sourceCodeStubber.getStubOfCodeInEditor(stubber); 29 | copyFormatedCodeToClipboard(stubCode); 30 | } 31 | } -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/plugin/ICppUTestFactory.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin; 2 | 3 | import org.cpputest.codeGenerator.CppCodeFormater; 4 | import org.cpputest.codeGenerator.CUTPlatformAdaptor; 5 | import org.cpputest.codeGenerator.Stubber; 6 | import org.eclipse.ui.IWorkbenchWindow; 7 | 8 | public interface ICppUTestFactory { 9 | 10 | CUTPlatformAdaptor createPlatformAdaptor(IWorkbenchWindow window); 11 | 12 | SourceCodeStubberForEditor createSourceCodeStubber(); 13 | 14 | CppCodeFormater createCodeFormater(); 15 | 16 | Stubber createEmptyStubber(); 17 | 18 | StubCodeUI createStubCodeUI(); 19 | 20 | Stubber createDefaultMockStubber(); 21 | 22 | } 23 | -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/plugin/SourceCodeStubberForEditor.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin; 2 | 3 | import org.cpputest.codeGenerator.CppCode; 4 | import org.cpputest.codeGenerator.Stubber; 5 | 6 | public interface SourceCodeStubberForEditor { 7 | CppCode getStubOfCodeInEditor(Stubber stubber); 8 | } -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/plugin/StubCodeUI.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin; 2 | 3 | import org.cpputest.codeGenerator.Stubber; 4 | 5 | public interface StubCodeUI { 6 | public abstract void copyStubCodeInEditorToClipboard(Stubber emptyStubber); 7 | 8 | } -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/plugin/actions/CppUTestAction.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin.actions; 2 | 3 | import org.cpputest.codeGenerator.CUTPlatformAdaptor; 4 | import org.cpputest.plugin.CppUTestFactory; 5 | import org.cpputest.plugin.ICppUTestFactory; 6 | import org.eclipse.jface.action.IAction; 7 | import org.eclipse.jface.viewers.ISelection; 8 | import org.eclipse.ui.IObjectActionDelegate; 9 | import org.eclipse.ui.IWorkbenchPart; 10 | import org.eclipse.ui.IWorkbenchWindow; 11 | import org.eclipse.ui.IWorkbenchWindowActionDelegate; 12 | 13 | public abstract class CppUTestAction implements IWorkbenchWindowActionDelegate, IObjectActionDelegate { 14 | protected ICppUTestFactory factory = new CppUTestFactory(); 15 | private CUTPlatformAdaptor platform; 16 | public CppUTestAction() { 17 | this.factory = new CppUTestFactory(); 18 | } 19 | 20 | public CppUTestAction(ICppUTestFactory factory) { 21 | this.factory = factory; 22 | } 23 | 24 | public void setActivePart(IAction action, IWorkbenchPart targetPart) { 25 | initializePlatform(targetPart.getSite().getWorkbenchWindow()); 26 | } 27 | 28 | /** 29 | * Selection in the workbench has been changed. We can change the state of 30 | * the 'real' action here if we want, but this can only happen after the 31 | * delegate has been created. 32 | * 33 | * @see IWorkbenchWindowActionDelegate#selectionChanged 34 | */ 35 | public void selectionChanged(IAction action, ISelection selection) { 36 | } 37 | 38 | /** 39 | * We can use this method to dispose of any system resources we previously 40 | * allocated. 41 | * 42 | * @see IWorkbenchWindowActionDelegate#dispose 43 | */ 44 | public void dispose() { 45 | } 46 | 47 | public void init(IWorkbenchWindow window) { 48 | initializePlatform(window); 49 | } 50 | 51 | private void initializePlatform(IWorkbenchWindow window) { 52 | this.platform = factory.createPlatformAdaptor(window); 53 | } 54 | protected CUTPlatformAdaptor getPlatform() { 55 | return this.platform; 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/plugin/actions/CppUTestActionCopyDefaultMockToClipboard.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin.actions; 2 | 3 | import org.cpputest.plugin.ICppUTestFactory; 4 | import org.eclipse.jface.action.IAction; 5 | 6 | public class CppUTestActionCopyDefaultMockToClipboard extends CppUTestAction { 7 | public CppUTestActionCopyDefaultMockToClipboard() { 8 | super(); 9 | } 10 | public CppUTestActionCopyDefaultMockToClipboard(ICppUTestFactory factory) { 11 | super(factory); 12 | } 13 | 14 | public void run(IAction action) { 15 | factory.createStubCodeUI().copyStubCodeInEditorToClipboard( 16 | factory.createDefaultMockStubber()); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /org.cpputest.plugin/src/org/cpputest/plugin/actions/CppUTestActionCopyEmptyStubToClipboard.java: -------------------------------------------------------------------------------- 1 | package org.cpputest.plugin.actions; 2 | 3 | import org.cpputest.plugin.ICppUTestFactory; 4 | import org.eclipse.jface.action.IAction; 5 | 6 | public class CppUTestActionCopyEmptyStubToClipboard extends CppUTestAction { 7 | public CppUTestActionCopyEmptyStubToClipboard() { 8 | super(); 9 | } 10 | public CppUTestActionCopyEmptyStubToClipboard(ICppUTestFactory factory) { 11 | super(factory); 12 | } 13 | 14 | public void run(IAction action) { 15 | factory.createStubCodeUI().copyStubCodeInEditorToClipboard( 16 | factory.createEmptyStubber()); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | org.cpputest.plugin 5 | org.cpputest.plugin.parent 6 | 1.0.0-SNAPSHOT 7 | pom 8 | CppUTest Plugin for Eclipse 9 | 10 | 0.15.0 11 | 12 | 13 | org.cpputest.plugin 14 | org.cpputest.plugin.feature 15 | update-site 16 | org.cpputest.plugin.unittest 17 | org.cpputest.plugin.SWTBotTest 18 | 19 | 20 | 21 | eclipse-helios 22 | p2 23 | http://download.eclipse.org/releases/helios 24 | 25 | 26 | swtbot 27 | p2 28 | http://download.eclipse.org/technology/swtbot/helios/dev-build/update-site/ 29 | 30 | 31 | 32 | 33 | sign 34 | 36 | 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-jar-plugin 42 | 2.3.1 43 | 44 | 45 | 46 | sign 47 | 48 | 49 | 50 | 51 | true 52 | ${project.build.directory}/${project.build.finalName}.jar 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | org.eclipse.tycho 64 | tycho-maven-plugin 65 | ${tycho.version} 66 | true 67 | 68 | 69 | org.eclipse.tycho 70 | target-platform-configuration 71 | ${tycho.version} 72 | 73 | p2 74 | 75 | 76 | 77 | eclipse-plugin 78 | org.eclipse.jface.text 79 | 0.0.0 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | org.apache.maven.plugins 90 | maven-jar-plugin 91 | 2.3.1 92 | 93 | 94 | org.codehaus.tycho 95 | maven-osgi-packaging-plugin 96 | ${tycho.version} 97 | 98 | 99 | timestamp 100 | validate 101 | 102 | timestamp 103 | 104 | 105 | 106 | 108 | 109 | 110 | false 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | junit 120 | junit 121 | 4.13.1 122 | test 123 | 124 | 125 | -------------------------------------------------------------------------------- /update-site/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/update-site/.gitignore -------------------------------------------------------------------------------- /update-site/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | update-site 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.pde.UpdateSiteBuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.pde.UpdateSiteNature 16 | 17 | 18 | -------------------------------------------------------------------------------- /update-site/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.cpputest.plugin 8 | org.cpputest.plugin.parent 9 | 1.0.0-SNAPSHOT 10 | 11 | org.cpputest.plugin 12 | org.cpputest.plugin.update.site 13 | 1.0.0-SNAPSHOT 14 | eclipse-update-site 15 | 16 | -------------------------------------------------------------------------------- /update-site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /update-site/target/site.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/update-site/target/site.zip -------------------------------------------------------------------------------- /update-site/target/site/artifacts.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/update-site/target/site/artifacts.jar -------------------------------------------------------------------------------- /update-site/target/site/content.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpputest/CppUTestEclipsePlugin/7d6f28c5ac6c218413804c56df5b17bb4c1b9f24/update-site/target/site/content.jar -------------------------------------------------------------------------------- /update-site/target/site/site.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | --------------------------------------------------------------------------------