From 90358f53746e2227005c82d5412a8fca1c83b9cd Mon Sep 17 00:00:00 2001 From: Marco Stornelli Date: Wed, 29 May 2019 19:03:21 +0200 Subject: Bug 355174 - Improved tests and code for quickfix Change-Id: I032040e6c4d3c9342ebdbf72b3ba75114810a31f Signed-off-by: Marco Stornelli --- .../META-INF/MANIFEST.MF | 2 +- codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml | 4 +- .../ui/quickfix/QuickFixAddCaseSwitch.java | 29 +++++++------ .../ui/quickfix/QuickFixAddDefaultSwitch.java | 22 ++++++---- .../checkers/ui/quickfix/QuickFixAddCaseTest.java | 50 ++++++++++++++++++++++ .../ui/quickfix/QuickFixAddDefaultTest.java | 35 +++++++++++++++ 6 files changed, 118 insertions(+), 24 deletions(-) (limited to 'codan') diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF b/codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF index ca272a0877e..9724352eaf2 100644 --- a/codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF +++ b/codan/org.eclipse.cdt.codan.checkers.ui/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %Bundle-Name Bundle-SymbolicName: org.eclipse.cdt.codan.checkers.ui;singleton:=true -Bundle-Version: 3.2.3.qualifier +Bundle-Version: 3.2.100.qualifier Bundle-Activator: org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator Require-Bundle: org.eclipse.core.resources, org.eclipse.core.runtime, diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml index e4921ba9afa..f795a728f95 100644 --- a/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml +++ b/codan/org.eclipse.cdt.codan.checkers.ui/plugin.xml @@ -5,11 +5,11 @@ point="org.eclipse.cdt.codan.ui.codanMarkerResolution"> + problemId="org.eclipse.cdt.codan.internal.checkers.MissDefaultProblem"> + problemId="org.eclipse.cdt.codan.internal.checkers.MissCaseProblem"> enumValues = new HashMap<>(); if (type instanceof IEnumeration) { IEnumerator[] enums = ((IEnumeration) type).getEnumerators(); - String prefix = ""; + String prefix = ""; //$NON-NLS-1$ if (type instanceof ICPPEnumeration) { String[] qualName = CPPVisitor.getQualifiedName((IEnumeration) type); StringBuilder builder = new StringBuilder(); for (int i = 0; i < qualName.length - 1; ++i) { builder.append(qualName[i]); - builder.append("::"); + builder.append("::"); //$NON-NLS-1$ } prefix = builder.toString(); } diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java index 468dc3eed2b..219dbe67a90 100644 --- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java +++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java @@ -17,6 +17,7 @@ import org.eclipse.cdt.core.dom.ast.IASTBreakStatement; import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement; import org.eclipse.cdt.core.dom.ast.IASTDefaultStatement; import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTNullStatement; import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.INodeFactory; @@ -58,24 +59,27 @@ public class QuickFixAddDefaultSwitch extends AbstractAstRewriteQuickFix { IASTBreakStatement breakStatement = factory.newBreakStatement(); IASTNode[] children = astNode.getChildren(); IASTCompoundStatement compound = null; + IASTNullStatement nullStatement = null; for (int i = 0; i < children.length; ++i) { if (children[i] instanceof IASTCompoundStatement) { compound = (IASTCompoundStatement) children[i]; break; - } + } else if (children[i] instanceof IASTNullStatement) + nullStatement = (IASTNullStatement) children[i]; } - if (compound == null) + if (compound == null && nullStatement != null) { + compound = factory.newCompoundStatement(); + compound.addStatement(defStatement); + compound.addStatement(breakStatement); + r.replace(nullStatement, compound, null); + } else if (compound != null) { + r.insertBefore(compound, null, defStatement, null); + r.insertBefore(compound, null, breakStatement, null); + } else return; - r.insertBefore(compound, null, defStatement, null); - r.insertBefore(compound, null, breakStatement, null); Change c = r.rewriteAST(); try { c.perform(new NullProgressMonitor()); - } catch (CoreException e) { - CheckersUiActivator.log(e); - return; - } - try { marker.delete(); } catch (CoreException e) { CheckersUiActivator.log(e); diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseTest.java index d20b41f9ede..2da277dc137 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseTest.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseTest.java @@ -50,4 +50,54 @@ public class QuickFixAddCaseTest extends QuickFixTestCase { assertContainedIn("PEAR:", result); //$NON-NLS-1$ assertContainedIn("BANANA:", result); //$NON-NLS-1$ } + + //enum FRUIT { + // APPLE, PEAR, BANANA + //}; + //void func() { + //FRUIT f = APPLE; + //switch (f) + //{ + // + //} + //} + public void testAddCase2() throws Exception { + loadcode(getAboveComment()); + String result = runQuickFixOneFile(); + assertContainedIn("PEAR:", result); //$NON-NLS-1$ + assertContainedIn("BANANA:", result); //$NON-NLS-1$ + assertContainedIn("APPLE:", result); //$NON-NLS-1$ + } + + //enum FRUIT { + // APPLE, PEAR, BANANA + //}; + //void func() { + //FRUIT f = APPLE; + //switch (f) + // ; + //} + public void testAddCase3() throws Exception { + loadcode(getAboveComment()); + String result = runQuickFixOneFile(); + assertContainedIn("PEAR:", result); //$NON-NLS-1$ + assertContainedIn("BANANA:", result); //$NON-NLS-1$ + assertContainedIn("APPLE:", result); //$NON-NLS-1$ + } + + //enum FRUIT { + // APPLE, PEAR, BANANA + //}; + //void func() { + //FRUIT f = APPLE; + //switch (f) + // case APPLE: + // break; + //} + public void testAddCase4() throws Exception { + loadcode(getAboveComment()); + String result = runQuickFixOneFile(); + assertContainedIn("PEAR:", result); //$NON-NLS-1$ + assertContainedIn("BANANA:", result); //$NON-NLS-1$ + } } diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultTest.java index 45a0732bf38..29fb17ddabe 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultTest.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultTest.java @@ -47,4 +47,39 @@ public class QuickFixAddDefaultTest extends QuickFixTestCase { assertContainedIn("default:", result); //$NON-NLS-1$ } + //void func() { + //int f = 0; + //switch (f) + //{ + // + //} + //} + public void testAddCase2() throws Exception { + loadcode(getAboveComment()); + String result = runQuickFixOneFile(); + assertContainedIn("default:", result); //$NON-NLS-1$ + } + + //void func() { + //int f = 0; + //switch (f) + // ; + //} + public void testAddCase3() throws Exception { + loadcode(getAboveComment()); + String result = runQuickFixOneFile(); + assertContainedIn("default:", result); //$NON-NLS-1$ + } + + //void func() { + //int f = 0; + //switch (f) + // case 0: + // break; + //} + public void testAddCase4() throws Exception { + loadcode(getAboveComment()); + String result = runQuickFixOneFile(); + assertContainedIn("default:", result); //$NON-NLS-1$ + } } -- cgit v1.2.3