Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlena Laskavaia2015-02-24 08:56:02 -0500
committerGerrit Code Review @ Eclipse.org2015-02-25 20:19:39 -0500
commit396b94601567223f60881dbafded22991b4f2228 (patch)
tree62d99118cc35a46aeb94052bfa242262a1c99097 /codan/org.eclipse.cdt.codan.core.test
parent0ba2118dc9feb7f5fffdfa2d3c1641af4bdf7768 (diff)
downloadorg.eclipse.cdt-396b94601567223f60881dbafded22991b4f2228.tar.gz
org.eclipse.cdt-396b94601567223f60881dbafded22991b4f2228.tar.xz
org.eclipse.cdt-396b94601567223f60881dbafded22991b4f2228.zip
codan - checker for comments
implement few code style rules for comments - no line comments (sometimes enforced) - no nested comments (no /* in block comment) Change-Id: Ib4042a8ffab0dc1c5effd7b77ab6d2f51a1e6cae
Diffstat (limited to 'codan/org.eclipse.cdt.codan.core.test')
-rw-r--r--codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/CommentCheckerLineTests.java81
-rw-r--r--codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/CommentCheckerNestedTests.java51
-rw-r--r--codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java4
-rw-r--r--codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java4
-rw-r--r--codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanTestCase.java38
5 files changed, 162 insertions, 16 deletions
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/CommentCheckerLineTests.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/CommentCheckerLineTests.java
new file mode 100644
index 0000000000..b2fe155796
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/CommentCheckerLineTests.java
@@ -0,0 +1,81 @@
+/*******************************************************************************
+ * Copyright (c) 2015 QNX Software System and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Elena Laskavaia (QNX Software System) - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.codan.core.internal.checkers;
+
+import java.io.File;
+
+import org.eclipse.cdt.codan.core.test.CheckerTestCase;
+import org.eclipse.cdt.codan.internal.checkers.CommentChecker;
+import org.junit.Test;
+
+/**
+ * Tests for CommentChecker
+ */
+public class CommentCheckerLineTests extends CheckerTestCase {
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ enableProblems(CommentChecker.COMMENT_NO_LINE);
+ }
+
+ // void foo() {
+ // return; // error
+ // }
+ @Test
+ public void testLineComment() {
+ checkSampleAbove();
+ }
+
+ // void foo() {
+ // return;
+ // }
+ @Test
+ public void testNoLineComment() {
+ checkSampleAbove();
+ }
+
+ // char * foo() {
+ // return "// this is a string";
+ // }
+ @Test
+ public void testNoLineCommentInString() {
+ checkSampleAbove();
+ }
+
+ // void foo() {
+ // return; // not an error in c++
+ // }
+ @Test
+ public void testLineCommentCpp() {
+ checkSampleAboveCpp();
+ }
+
+ // #define AAA // error even in prepro
+ @Test
+ public void testLineCommentInPrepro() {
+ checkSampleAbove();
+ }
+
+ // @file:test.h
+ // int foo();// error too
+
+
+ // @file:test.c
+ // #include "test.h"
+ // int bar() {
+ // foo();
+ // }
+ public void testHeader() throws Exception {
+ loadcode(getContents(2));
+ runOnProject();
+ checkErrorLine(new File("test.h"), 1); //$NON-NLS-1$
+ }
+}
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/CommentCheckerNestedTests.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/CommentCheckerNestedTests.java
new file mode 100644
index 0000000000..a79f59542f
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/CommentCheckerNestedTests.java
@@ -0,0 +1,51 @@
+/*******************************************************************************
+ * Copyright (c) 2015 QNX Software System and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Elena Laskavaia (QNX Software System) - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.codan.core.internal.checkers;
+
+import org.eclipse.cdt.codan.core.test.CheckerTestCase;
+import org.eclipse.cdt.codan.internal.checkers.CommentChecker;
+import org.junit.Test;
+
+/**
+ * Tests for CommentChecker
+ */
+public class CommentCheckerNestedTests extends CheckerTestCase {
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ enableProblems(CommentChecker.COMMENT_NO_START);
+ }
+
+ // void foo() {
+ // return; /* /* */ // error
+ // }
+ @Test
+ public void testLineComment() {
+ checkSampleAbove();
+ }
+ // void foo() {
+ // return; /*
+ // /* // error
+ // */
+ // }
+ @Test
+ public void testLineComment2() {
+ checkSampleAbove();
+ }
+
+ // void foo() {
+ // return; /* */
+ // }
+ @Test
+ public void testNoLineComment() {
+ checkSampleAbove();
+ }
+}
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java
index 560042a5be..f81759ec48 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/AutomatedIntegrationSuite.java
@@ -21,6 +21,8 @@ import org.eclipse.cdt.codan.core.internal.checkers.AssignmentToItselfCheckerTes
import org.eclipse.cdt.codan.core.internal.checkers.CaseBreakCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.CatchByReferenceTest;
import org.eclipse.cdt.codan.core.internal.checkers.ClassMembersInitializationCheckerTest;
+import org.eclipse.cdt.codan.core.internal.checkers.CommentCheckerLineTests;
+import org.eclipse.cdt.codan.core.internal.checkers.CommentCheckerNestedTests;
import org.eclipse.cdt.codan.core.internal.checkers.FormatStringCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.NonVirtualDestructorCheckerTest;
import org.eclipse.cdt.codan.core.internal.checkers.ProblemBindingCheckerTest;
@@ -70,6 +72,8 @@ public class AutomatedIntegrationSuite extends TestSuite {
suite.addTestSuite(SuggestedParenthesisCheckerTest.class);
suite.addTestSuite(SuspiciousSemicolonCheckerTest.class);
suite.addTestSuite(UnusedSymbolInFileScopeCheckerTest.class);
+ suite.addTestSuite(CommentCheckerLineTests.class);
+ suite.addTestSuite(CommentCheckerNestedTests.class);
// framework
suite.addTest(CodanFastTestSuite.suite());
// quick fixes
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java
index 8930712e00..dcec3a34fc 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CheckerTestCase.java
@@ -220,6 +220,10 @@ public class CheckerTestCase extends CodanTestCase {
}
}
+ /**
+ * Enable given problems and disable the rest
+ * @param ids
+ */
protected void enableProblems(String... ids) {
IProblemProfile profile = CodanRuntime.getInstance().getCheckersRegistry().getWorkspaceProfile();
IProblem[] problems = profile.getProblems();
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanTestCase.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanTestCase.java
index 22107d096e..6ed1ac28ef 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanTestCase.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/CodanTestCase.java
@@ -48,7 +48,7 @@ public class CodanTestCase extends BaseTestCase {
protected File currentFile;
protected ICElement currentCElem;
protected IFile currentIFile;
- protected ArrayList<Integer> errLines= new ArrayList<Integer>();
+ protected ArrayList<Integer> errLines = new ArrayList<Integer>();
/**
*
@@ -99,13 +99,17 @@ public class CodanTestCase extends BaseTestCase {
* @throws CoreException
*/
private void removeLeftOverProjects() throws CoreException {
- final IWorkspace workspace = ResourcesPlugin.getWorkspace();
- IProject[] projects = workspace.getRoot().getProjects();
- for (int i = 0; i < projects.length; i++) {
- IProject p = projects[i];
- if (p.getName().startsWith("Codan")) {
- p.delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT, new NullProgressMonitor());
+ try {
+ final IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ IProject[] projects = workspace.getRoot().getProjects();
+ for (int i = 0; i < projects.length; i++) {
+ IProject p = projects[i];
+ if (p.getName().startsWith("Codan")) {
+ p.delete(IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT, new NullProgressMonitor());
+ }
}
+ } catch (Throwable e) {
+ // moving on...
}
}
@@ -175,8 +179,7 @@ public class CodanTestCase extends BaseTestCase {
protected StringBuilder[] getContents(int sections) {
try {
- return TestSourceReader.getContentsForTest(getPlugin().getBundle(), getSourcePrefix(),
- getClass(), getName(), sections);
+ return TestSourceReader.getContentsForTest(getPlugin().getBundle(), getSourcePrefix(), getClass(), getName(), sections);
} catch (IOException e) {
fail(e.getMessage());
return null;
@@ -215,11 +218,6 @@ public class CodanTestCase extends BaseTestCase {
return loadcode(code, testFile);
}
- public File loadcode(String code, String filename) {
- File testFile = new File(tmpDir, filename);
- return loadcode(code, testFile);
- }
-
private File loadcode(String code, File testFile) {
try {
tempFiles.add(testFile);
@@ -246,14 +244,14 @@ public class CodanTestCase extends BaseTestCase {
return null;
}
}
-
private static Pattern COMMENT_TAG_PATTERN = Pattern.compile("//\\s*(err|ERR|ERROR|error)\\b");
+
private void loadErrorComments(String trim) {
String[] lines = trim.split("\n");
for (int i = 0; i < lines.length; i++) {
String string = lines[i];
if (COMMENT_TAG_PATTERN.matcher(string).find()) {
- errLines.add(i+1);
+ errLines.add(i + 1);
}
}
}
@@ -269,4 +267,12 @@ public class CodanTestCase extends BaseTestCase {
public File loadcode(String code) {
return loadcode(code, isCpp());
}
+
+ public File loadcode(CharSequence... more) {
+ File file = null;
+ for (CharSequence cseq : more) {
+ file = loadcode(cseq.toString(), isCpp());
+ }
+ return file;
+ }
} \ No newline at end of file

Back to the top