diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/cfg/ControlFlowGraphTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/cfg/ControlFlowGraphTest.java
index 086dd8fb0a6..01684540742 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/cfg/ControlFlowGraphTest.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/cfg/ControlFlowGraphTest.java
@@ -148,11 +148,17 @@ public class ControlFlowGraphTest extends CodanTestCase {
return false;
}
+ protected void buildAndCheck(String code) {
+ buildAndCheck(code, false);
+ }
+ protected void buildAndCheck_cpp(String code) {
+ buildAndCheck(code, true);
+ }
/**
* @param file
*/
- protected void buildAndCheck(String file) {
- load(file);
+ protected void buildAndCheck(String code, boolean cpp) {
+ loadcode(code, cpp);
buildCfg();
checkCfg();
}
@@ -195,30 +201,27 @@ public class ControlFlowGraphTest extends CodanTestCase {
return null;
}
- /*-
-
- main() {
- int a;
- a=1;
- }
-
- */
- public void test_basic() {
- buildAndCheck("test1.c");
+ @Override
+ public boolean isCpp() {
+ return true;
}
- /*-
-
- main() {
- int a=10;
- while (a--) {
- a=a-2;
- }
- }
-
- */
+ // main() {
+ // int a;
+ // a=1;
+ // }
+ public void test_basic() {
+ buildAndCheck(getAboveComment());
+ }
+
+ // main() {
+ // int a=10;
+ // while (a--) {
+ // a=a-2;
+ // }
+ // }
public void test_while() {
- buildAndCheck("test2.c");
+ buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IConnectorNode conn = (IConnectorNode) decl.getOutgoing();
@@ -233,18 +236,14 @@ public class ControlFlowGraphTest extends CodanTestCase {
IExitNode ret = (IExitNode) ((IConnectorNode) m1).getOutgoing();
}
- /*-
-
- main() {
- int a=10;
- if (a--) {
- a=a-2;
- }
- }
-
- */
+ // main() {
+ // int a=10;
+ // if (a--) {
+ // a=a-2;
+ // }
+ // }
public void test_if() {
- buildAndCheck("test3.c");
+ buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing();
@@ -257,18 +256,14 @@ public class ControlFlowGraphTest extends CodanTestCase {
assertSame(m1, m2);
}
- /*-
-
- main() {
- int a=10;
- if (a--) {
- return;
- }
- }
-
- */
+ // main() {
+ // int a=10;
+ // if (a--) {
+ // return;
+ // }
+ // }
public void test_if_ret() {
- buildAndCheck("test4.c");
+ buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing();
@@ -277,19 +272,16 @@ public class ControlFlowGraphTest extends CodanTestCase {
IBasicBlock bElse = branchEnd(des, IBranchNode.ELSE);
IBasicBlock m1 = jumpEnd(bElse);
}
- /*-
-
- main() {
- int a=10;
- if (a--) {
- return;
- a++;
- }
- }
-
- */
+
+ // main() {
+ // int a=10;
+ // if (a--) {
+ // return;
+ // a++;
+ // }
+ // }
public void test_if_dead() {
- buildAndCheck("test5.c");
+ buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing();
@@ -297,21 +289,18 @@ public class ControlFlowGraphTest extends CodanTestCase {
IExitNode bThen = (IExitNode) branchEnd(des, IBranchNode.THEN);
IBasicBlock bElse = branchEnd(des, IBranchNode.ELSE);
IBasicBlock m1 = jumpEnd(bElse);
- assertEquals(1,graph.getUnconnectedNodeSize());
+ assertEquals(1, graph.getUnconnectedNodeSize());
}
- /*-
-
- foo() {
- int a=10, x=5;
- if (a--) {
- if (x<0)
- a++;
- }
- }
-
- */
+
+ // foo() {
+ // int a=10, x=5;
+ // if (a--) {
+ // if (x<0)
+ // a++;
+ // }
+ // }
public void test_ifif() {
- buildAndCheck("test_ifif.c");
+ buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing();
@@ -319,41 +308,33 @@ public class ControlFlowGraphTest extends CodanTestCase {
IDecisionNode bThen = (IDecisionNode) branchEnd(des, IBranchNode.THEN);
assertEquals("x<0", data(bThen));
IBasicBlock bElse = branchEnd(des, IBranchNode.ELSE);
- IBasicBlock m2 = jumpEnd(branchEnd(bThen,IBranchNode.THEN));
+ IBasicBlock m2 = jumpEnd(branchEnd(bThen, IBranchNode.THEN));
IBasicBlock m1 = jumpEnd(bElse);
IBasicBlock m3 = jumpEnd(m2);
assertSame(m1, m3);
}
- /*-
-
- int foo() {
- throw 5;
- }
-
- */
+
+ // int foo() {
+ // throw 5;
+ // }
public void test_throw() {
- buildAndCheck("test_throw.cc");
+ buildAndCheck_cpp(getAboveComment());
IStartNode startNode = graph.getStartNode();
assertEquals(1, graph.getExitNodeSize());
Iterator exitNodeIterator = graph.getExitNodeIterator();
IExitNode exit = exitNodeIterator.next();
-
assertEquals("throw 5;", data(exit));
}
- /*-
-
- int foo() {
- exit(0);
- }
-
- */
+
+ // int foo() {
+ // exit(0);
+ // }
public void test_exit() {
- buildAndCheck("test_exit.c");
+ buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode();
assertEquals(1, graph.getExitNodeSize());
Iterator exitNodeIterator = graph.getExitNodeIterator();
IExitNode exit = exitNodeIterator.next();
-
assertEquals("exit(0);", data(exit));
}
}
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ExpressionRequiredInReturnCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ExpressionRequiredInReturnCheckerTest.java
index 292313184b8..b4b875b0802 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ExpressionRequiredInReturnCheckerTest.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ExpressionRequiredInReturnCheckerTest.java
@@ -18,95 +18,71 @@ import org.eclipse.cdt.codan.core.test.CheckerTestCase;
*
*/
public class ExpressionRequiredInReturnCheckerTest extends CheckerTestCase {
- /*-
-
- dummy() {
- return; // error here on line 2
- }
-
- */
+
+
+// dummy() {
+// return; // error here on line 2
+// }
public void testDummyFunction() {
- load("test1.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkNoErrors(); // because return type if not defined, usually people don't care
}
- /*-
-
- void void_function(void) {
- return; // no error here
- }
-
- */
+
+// void void_function(void) {
+// return; // no error here
+// }
public void testVoidFunction() {
- load("test2.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkNoErrors();
}
- /*-
-
- int integer_return_function(void) {
- if (global) {
- if (global == 100) {
- return; // error here on line 4
- }
- }
- }
-
- */
+
+// int integer_return_function(void) {
+// if (global) {
+// if (global == 100) {
+// return; // error here on line 4
+// }
+// }
+// }
public void testBasicTypeFunction() {
- load("test3.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkErrorLine(4);
}
- /*-
-
- struct My_Struct {
- int a;
- };
-
- struct My_Struct struct_return_function(void) {
- return; // error here on line 6
- }
-
-
- */
+//
+// struct My_Struct {
+// int a;
+// };
+//
+// struct My_Struct struct_return_function(void) {
+// return; // error here on line 6
+// }
public void testUserDefinedFunction() {
- load("test4.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkErrorLine(6);
}
- /*-
-
- typedef unsigned int uint8_t;
-
- uint8_t return_typedef(void) {
- return; // error here on line 4
- }
-
- */
+
+// typedef unsigned int uint8_t;
+//
+// uint8_t return_typedef(void) {
+// return; // error here on line 4
+// }
public void testTypedefReturnFunction() {
- load("test5.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkErrorLine(4);
}
- /*-
-
- typedef unsigned int uint8_t;
-
- uint8_t (*return_fp_no_typedef(void))(void)
- {
- return; // error here on line 5
- }
-
- */
+
+// typedef unsigned int uint8_t;
+//
+// uint8_t (*return_fp_no_typedef(void))(void)
+// {
+// return; // error here on line 5
+// }
public void testFunctionPointerReturnFunction() {
- load("test6.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkErrorLine(5);
}
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/StatementHasNoEffectCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/StatementHasNoEffectCheckerTest.java
index 0511ee75357..cc324d050a3 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/StatementHasNoEffectCheckerTest.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/StatementHasNoEffectCheckerTest.java
@@ -10,6 +10,9 @@
*******************************************************************************/
package org.eclipse.cdt.codan.core.internal.checkers;
+import java.io.File;
+import java.io.IOException;
+
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
/**
@@ -17,84 +20,89 @@ import org.eclipse.cdt.codan.core.test.CheckerTestCase;
*
*/
public class StatementHasNoEffectCheckerTest extends CheckerTestCase {
- /*-
-
- main() {
- int a;
- +a; // error here on line 3
- }
-
- */
- public void testUnaryExpression() {
- load("test1.c");
- runOnFile();
+ // main() {
+ // int a;
+ // +a; // error here on line 3
+ // }
+ public void testUnaryExpression() throws IOException {
+ loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
-
- /*-
-
- main() {
- int a,b;
-
- b+a; // error here on line 4
- }
-
- */
+
+ // main() {
+ // int a,b;
+ //
+ // b+a; // error here on line 4
+ // }
public void testBinaryExpression() {
- load("test2.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkErrorLine(4);
}
-
- /*-
-
- main() {
- int a,b;
-
- a=b+a; // no error here
- }
-
- */
+
+ // main() {
+ // int a,b;
+ //
+ // a=b+a; // no error here
+ // }
public void testNormalAssignment() {
- load("test3.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkNoErrors();
}
- /*-
-
- main() {
- int a,b;
-
- (a=b); // no errors here
- a+=b;
- a<<=b;
- a-=b;
- a++;
- b--;
- --a;
- ++b;
- a%=2;
- a>>=2;
- }
-
- */
+
+ // main() {
+ // int a,b;
+ //
+ // (a=b); // no errors here
+ // a+=b;
+ // a<<=b;
+ // a-=b;
+ // a++;
+ // b--;
+ // --a;
+ // ++b;
+ // a%=2;
+ // a>>=2;
+ // }
public void testFalsePositives() {
- load("test4.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkNoErrors();
}
-
- /*-
-
- main() {
- int a;
- a; // error here on line 3
- }
-
- */
+
+ // main() {
+ // int a;
+ // a; // error here on line 3
+ // }
public void testIdExpression() {
- load("test5.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
+
+ // main() {
+ // int a=({foo();a;}); // no error here on line 2
+ // }
+ public void testGNUExpressionCompoundStmt() {
+ loadCodeAndRun(getAboveComment());
+ checkNoErrors();
+ }
+
+ /* first file */
+ // main() {
+ // int a;
+ // +a; // error here on line 3
+ // }
+ /* second file */
+ // foo() {
+ // int a;
+ //
+ // +a; // error here on line 4
+ // }
+ /* this test is using two files */
+ public void test2FilesUnaryExpression() throws IOException {
+ StringBuffer[] code = getContents(2);
+ File f1 = loadcode(code[0].toString());
+ File f2 = loadcode(code[1].toString());
+ runOnProject();
+ checkErrorLine(f1, 3);
+ checkErrorLine(f2, 4);
+ }
}
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/SuggestedParenthesisCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/SuggestedParenthesisCheckerTest.java
index 386ddb5e88c..2c87699c7a5 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/SuggestedParenthesisCheckerTest.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/SuggestedParenthesisCheckerTest.java
@@ -17,87 +17,68 @@ import org.eclipse.cdt.codan.core.test.CheckerTestCase;
*
*/
public class SuggestedParenthesisCheckerTest extends CheckerTestCase {
- /*-
-
- main() {
- int a=1,b=3;
- if (!a<10) b=4; // error here on line 3
- }
-
- */
+
+// main() {
+// int a=1,b=3;
+// if (!a<10) b=4; // error here on line 3
+// }
public void test1() {
- load("test1.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
- /*-
-
- main() {
- int a=1,b=3;
-
- if (b+a && a>b || b-a) b--; // error here on line 4
- }
-
- */
+
+// main() {
+// int a=1,b=3;
+//
+// if (b+a && a>b || b-a) b--; // error here on line 4
+// }
+
public void test2() {
- load("test2.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkErrorLine(4);
}
- /*-
-
- main() {
- int a=1,b=3;
- if (!(a<10)) b=4; // no error here on line 3
- }
-
- */
+
+// main() {
+// int a=1,b=3;
+// if (!(a<10)) b=4; // no error here on line 3
+// }
+
public void test3() {
- load("test3.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkNoErrors();
}
- /*-
-
- main() {
- int a=1,b=3;
- if (a && !b) b=4; // no error here on line 3
- }
-
- */
+
+ // main() {
+ // int a=1,b=3;
+ // if (a && !b) b=4; // no error here on line 3
+ // }
+
public void test_lastnot() {
- load("test4.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkNoErrors();
}
- /*-
-
- main() {
- int a=1,b=3;
- if ((!a) && 10) b=4; // no error here on line 3
- }
-
- */
+
+// main() {
+// int a=1,b=3;
+// if ((!a) && 10) b=4; // no error here on line 3
+// }
+
public void test_fixed() {
- load("test5.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkNoErrors();
}
- /*-
-
- main() {
- int a=1,b=3;
- if (a && b & a) b=4; // error here on line 3
- }
-
- */
+
+// main() {
+// int a=1,b=3;
+// if (a && b & a) b=4; // error here on line 3
+// }
+
public void test_mixedbin() {
- load("test6.c");
- runOnFile();
+ loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
}
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 c878a97eb6c..f9b7bf4173d 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
@@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.cdt.codan.core.test;
+import java.io.File;
import java.io.IOException;
import org.eclipse.cdt.codan.core.CodanRuntime;
@@ -24,12 +25,14 @@ import org.eclipse.core.runtime.CoreException;
public class CheckerTestCase extends CodanTestCase {
private IMarker[] markers;
-
+ public void checkErrorLine(int i) {
+ checkErrorLine(currentFile, i);
+ }
/**
* @param i
* - line
*/
- public void checkErrorLine(int i) {
+ public void checkErrorLine(File file, int i) {
assertTrue(markers != null);
assertTrue(markers.length > 0);
boolean found = false;
@@ -48,8 +51,13 @@ public class CheckerTestCase extends CodanTestCase {
} catch (IOException e) {
fail(e.getMessage());
}
+ String mfile = m.getResource().getName();
if (line.equals(i)) {
found = true;
+ if (file!=null && !file.getName().equals(mfile))
+ found = false;
+ else
+ break;
}
}
assertTrue("Error on line " + i + " not found ", found);
@@ -63,12 +71,27 @@ public class CheckerTestCase extends CodanTestCase {
/**
*
*/
- public void runOnFile() {
+ public void runOnProject() {
try {
- loadFiles();
+ indexFiles();
} catch (CoreException e) {
fail(e.getMessage());
}
+ runCodan();
+ }
+
+ public void loadCodeAndRun(String code) {
+ loadcode(code);
+ runOnProject();
+ }
+ public void loadCodeAndRunCpp(String code) {
+ loadcode(code, true);
+ runOnProject();
+ }
+ /**
+ *
+ */
+ protected void runCodan() {
CodanRuntime.getInstance().getBuilder().processResource(
cproject.getProject(), NPM);
try {
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 5a31a794704..491466010f1 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
@@ -10,17 +10,20 @@
*******************************************************************************/
package org.eclipse.cdt.codan.core.test;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.util.ArrayList;
+import junit.framework.AssertionFailedError;
+
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
+import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
@@ -54,7 +57,7 @@ public class CodanTestCase extends BaseTestCase {
}
/**
- * Override for c++
+ * Override for c++ (i.e. at least one c++ test)
*
* @return is c++ tests
*/
@@ -69,32 +72,6 @@ public class CodanTestCase extends BaseTestCase {
tmpDir = cproject.getProject().getLocation().makeAbsolute().toFile();
}
- public File load(String file) {
- Class clazz = getClass();
- InputStream st = null;
- try {
- st = TestUtils.getJavaFileText(clazz);
- } catch (IOException e) {
- fail("Cannot find java file: " + clazz + ": " + e.getMessage());
- }
- try {
- File testFile = new File(tmpDir, file);
- tempFiles.add(testFile);
- TestUtils.saveFile(st, testFile);
- st.close();
- currentFile = testFile;
- try {
- cproject.getProject().refreshLocal(1, null);
- } catch (CoreException e) {
- // hmm
- }
- return testFile;
- } catch (IOException e) {
- fail("Cannot save test: " + file + ": " + e.getMessage());
- return null;
- }
- }
-
public void tearDown() throws CoreException {
if (cproject != null) {
try {
@@ -102,7 +79,6 @@ public class CodanTestCase extends BaseTestCase {
IResource.FORCE
| IResource.ALWAYS_DELETE_PROJECT_CONTENT,
new NullProgressMonitor());
-
} catch (CoreException e) {
throw e;
}
@@ -156,7 +132,7 @@ public class CodanTestCase extends BaseTestCase {
return cprojects[0];
}
- protected void loadFiles() throws CoreException {
+ protected void indexFiles() throws CoreException {
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
workspace.run(new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException {
@@ -167,7 +143,7 @@ public class CodanTestCase extends BaseTestCase {
CCorePlugin.getIndexManager().setIndexerId(cproject,
IPDOMManager.ID_FAST_INDEXER);
// wait until the indexer is done
- assertTrue(CCorePlugin.getIndexManager().joinIndexer(1000*60, // 1 min
+ assertTrue(CCorePlugin.getIndexManager().joinIndexer(1000 * 60, // 1 min
new NullProgressMonitor()));
return;
}
@@ -190,10 +166,75 @@ public class CodanTestCase extends BaseTestCase {
return line;
cur++;
}
- ;
} finally {
st.close();
}
return 0;
}
+
+ protected String getAboveComment() {
+ return getContents(1)[0].toString();
+ }
+
+ protected StringBuffer[] getContents(int sections) {
+ try {
+ CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
+ if (plugin == null)
+ throw new AssertionFailedError(
+ "This test must be run as a JUnit plugin test");
+ return TestSourceReader.getContentsForTest(plugin.getBundle(),
+ "src", getClass(), getName(), sections);
+ } catch (IOException e) {
+ fail(e.getMessage());
+ return null;
+ }
+ }
+
+ public File loadcode(String code, boolean cpp) {
+ String ext = cpp ? ".cc" : ".c"; //$NON-NLS-1$ //$NON-NLS-2$
+ File testFile = null;
+ try {
+ testFile = File.createTempFile("test", ext, tmpDir); //$NON-NLS-1$
+ } catch (IOException e1) {
+ fail(e1.getMessage());
+ return null;
+ }
+ 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);
+ TestUtils.saveFile(
+ new ByteArrayInputStream(code.trim().getBytes()), testFile);
+ currentFile = testFile;
+ try {
+ cproject.getProject().refreshLocal(1, null);
+ } catch (CoreException e) {
+ // hmm
+ fail(e.getMessage());
+ }
+ return testFile;
+ } catch (IOException e) {
+ fail("Cannot save test: " + testFile + ": " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
+ return null;
+ }
+ }
+
+ public File loadcode_c(String code) {
+ return loadcode(code, true);
+ }
+
+ public File loadcode_cpp(String code) {
+ return loadcode(code, false);
+ }
+
+ public File loadcode(String code) {
+ return loadcode(code, isCpp());
+ }
}
\ No newline at end of file
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/TestUtils.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/TestUtils.java
index 98560820f5d..5085d07c7e2 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/TestUtils.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/test/TestUtils.java
@@ -7,8 +7,7 @@
*
* Contributors:
* Alena Laskavaia - initial API and implementation
- *******************************************************************************/
-
+ *******************************************************************************/
package org.eclipse.cdt.codan.core.test;
import java.io.BufferedReader;
@@ -19,42 +18,19 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
/**
* TODO: add description
*/
public class TestUtils {
- static final Pattern filePattern = Pattern.compile("file=\"(.*)\"");
- /**
- * @param st
- * @param testFile
- * @return
- * @throws FileNotFoundException
- * @throws IOException
- */
public static File saveFile(InputStream st, File testFile)
throws FileNotFoundException, IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(st));
String line;
PrintStream wr = new PrintStream(testFile);
try {
- boolean print = false;
while ((line = r.readLine()) != null) {
- if (line.contains("")) {
- print = false;
- } else if (print) {
- wr.println(line);
- }
+ wr.println(line);
}
} finally {
wr.close();
@@ -67,20 +43,19 @@ public class TestUtils {
* @return
* @throws IOException
*/
- public static InputStream getJavaFileText(Class clazz) throws IOException {
+ public static InputStream getJavaFileText(String srcRoot, Class clazz)
+ throws IOException {
CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
String classFile = clazz.getName().replaceAll("\\.", "/");
classFile += ".java";
InputStream st = null;
-
- if (plugin != null) {
- URL resource = plugin.getBundle().getResource(
- "src/" + classFile);
- st = resource.openStream();
- } else {
- st = clazz.getResourceAsStream(classFile);
- }
-
+ if (plugin != null) {
+ URL resource = plugin.getBundle().getResource(
+ srcRoot + "/" + classFile);
+ st = resource.openStream();
+ } else {
+ st = clazz.getResourceAsStream(classFile);
+ }
return st;
}
}