1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-13 19:25:38 +02:00

Fix 137821, header files were returning true as valid source unit names. This causes certain builders to try and build them.

This commit is contained in:
Doug Schaefer 2006-04-20 22:57:14 +00:00
parent 4a3202744a
commit cd284ca6ec
2 changed files with 177 additions and 151 deletions

View file

@ -17,6 +17,7 @@ import java.util.Stack;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IInclude; import org.eclipse.cdt.core.model.IInclude;
import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.model.ITranslationUnit;
@ -24,24 +25,28 @@ import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.util.ExpectedStrings; import org.eclipse.cdt.core.testplugin.util.ExpectedStrings;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
/** /**
* @author Peter Graves * @author Peter Graves
* *
* This file contains a set of generic tests for the core C model's TranslationUnit * This file contains a set of generic tests for the core C model's
* class. There is nothing exotic here, mostly just sanity type tests * TranslationUnit class. There is nothing exotic here, mostly just sanity type
* tests
* *
*/ */
public class TranslationUnitTests extends TranslationUnitBaseTest { public class TranslationUnitTests extends TranslationUnitBaseTest {
/* This is a list of elements in the test .c file. It will be used /*
* in a number of places in the tests * This is a list of elements in the test .c file. It will be used in a
* number of places in the tests
*/ */
String[] expectedStringList = { "stdio.h", "unistd.h", "func2p", String[] expectedStringList = { "stdio.h", "unistd.h", "func2p",
"globalvar", "myenum", "mystruct", "mystruct_t", "myunion", "mytype", "globalvar", "myenum", "mystruct", "mystruct_t", "myunion",
"func1", "func2", "main", "func3" }; "mytype", "func1", "func2", "main", "func3" };
int[] expectedLines={ 12,14,17,20,23,28,32,35,42,47,53,58,65,70};
/* This is a list of that the types of the above list of elements is int[] expectedLines = { 12, 14, 17, 20, 23, 28, 32, 35, 42, 47, 53, 58, 65,
70 };
/*
* This is a list of that the types of the above list of elements is
* expected to be. * expected to be.
*/ */
int[] expectedTypes = { ICElement.C_INCLUDE, ICElement.C_INCLUDE, int[] expectedTypes = { ICElement.C_INCLUDE, ICElement.C_INCLUDE,
@ -51,9 +56,9 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
ICElement.C_FUNCTION, ICElement.C_FUNCTION, ICElement.C_FUNCTION, ICElement.C_FUNCTION, ICElement.C_FUNCTION, ICElement.C_FUNCTION,
ICElement.C_FUNCTION }; ICElement.C_FUNCTION };
/** /**
* Constructor for TranslationUnitTests * Constructor for TranslationUnitTests
*
* @param name * @param name
*/ */
public TranslationUnitTests(String name) { public TranslationUnitTests(String name) {
@ -67,7 +72,9 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
suite.addTest(new TranslationUnitTests("testGetElement")); suite.addTest(new TranslationUnitTests("testGetElement"));
suite.addTest(new TranslationUnitTests("testBug23478A")); suite.addTest(new TranslationUnitTests("testBug23478A"));
suite.addTest(new TranslationUnitTests("testBug23478B")); suite.addTest(new TranslationUnitTests("testBug23478B"));
// TODO: suite.addTest(new TranslationUnitTests("testGetElementAtLine")); suite.addTest(new TranslationUnitTests("testIsValidSourceUnitName"));
// TODO: suite.addTest(new
// TranslationUnitTests("testGetElementAtLine"));
return suite; return suite;
} }
@ -75,22 +82,22 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
junit.textui.TestRunner.run(suite()); junit.textui.TestRunner.run(suite());
} }
/***************************************************************************
* Simple sanity test to make sure TranslationUnit.isTranslationUnit returns
* true
/***
* Simple sanity test to make sure TranslationUnit.isTranslationUnit returns true
* *
*/ */
public void testIsTranslationUnit() throws CoreException,FileNotFoundException { public void testIsTranslationUnit() throws CoreException,
FileNotFoundException {
ITranslationUnit myTranslationUnit; ITranslationUnit myTranslationUnit;
myTranslationUnit=CProjectHelper.findTranslationUnit(testProject,"exetest.c"); myTranslationUnit = CProjectHelper.findTranslationUnit(testProject,
"exetest.c");
assertTrue("A TranslationUnit", myTranslationUnit != null); assertTrue("A TranslationUnit", myTranslationUnit != null);
} }
/*** /***************************************************************************
* Simple sanity tests to make sure TranslationUnit.getChildren seems to * Simple sanity tests to make sure TranslationUnit.getChildren seems to
* basicly work * basicly work
*/ */
@ -101,8 +108,8 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
ExpectedStrings expectedString = new ExpectedStrings(expectedStringList); ExpectedStrings expectedString = new ExpectedStrings(expectedStringList);
myTranslationUnit=CProjectHelper.findTranslationUnit(testProject,"exetest.c"); myTranslationUnit = CProjectHelper.findTranslationUnit(testProject,
"exetest.c");
if (myTranslationUnit.hasChildren()) { if (myTranslationUnit.hasChildren()) {
elements = myTranslationUnit.getChildren(); elements = myTranslationUnit.getChildren();
@ -110,12 +117,13 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
expectedString.foundString(elements[x].getElementName()); expectedString.foundString(elements[x].getElementName());
} }
} }
assertTrue("PR:23603 " +expectedString.getMissingString(),expectedString.gotAll()); assertTrue("PR:23603 " + expectedString.getMissingString(),
expectedString.gotAll());
assertTrue(expectedString.getExtraString(), !expectedString.gotExtra()); assertTrue(expectedString.getExtraString(), !expectedString.gotExtra());
} }
/*** /***************************************************************************
* Simple sanity tests for the getElement() call * Simple sanity tests for the getElement() call
*/ */
public void testGetElement() throws CModelException { public void testGetElement() throws CModelException {
@ -123,15 +131,17 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
ICElement myElement; ICElement myElement;
Stack missing = new Stack(); Stack missing = new Stack();
int x; int x;
myTranslationUnit=CProjectHelper.findTranslationUnit(testProject,"exetest.c"); myTranslationUnit = CProjectHelper.findTranslationUnit(testProject,
"exetest.c");
for (x = 0; x < expectedStringList.length; x++) { for (x = 0; x < expectedStringList.length; x++) {
myElement = myTranslationUnit.getElement(expectedStringList[x]); myElement = myTranslationUnit.getElement(expectedStringList[x]);
if (myElement == null) if (myElement == null)
missing.push(expectedStringList[x]); missing.push(expectedStringList[x]);
else { else {
assertTrue("Expected:" + expectedStringList[x] + " Got:" + myElement.getElementName(), assertTrue("Expected:" + expectedStringList[x] + " Got:"
expectedStringList[x].equals(myElement.getElementName())); + myElement.getElementName(), expectedStringList[x]
.equals(myElement.getElementName()));
} }
} }
@ -144,14 +154,15 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
} }
/*** /***************************************************************************
* Simple sanity tests for the getInclude call * Simple sanity tests for the getInclude call
*/ */
public void testBug23478A() throws CModelException { public void testBug23478A() throws CModelException {
IInclude myInclude; IInclude myInclude;
int x; int x;
String includes[] = { "stdio.h", "unistd.h" }; String includes[] = { "stdio.h", "unistd.h" };
ITranslationUnit myTranslationUnit=CProjectHelper.findTranslationUnit(testProject,"exetest.c"); ITranslationUnit myTranslationUnit = CProjectHelper
.findTranslationUnit(testProject, "exetest.c");
for (x = 0; x < includes.length; x++) { for (x = 0; x < includes.length; x++) {
myInclude = myTranslationUnit.getInclude(includes[x]); myInclude = myTranslationUnit.getInclude(includes[x]);
@ -160,13 +171,15 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
else { else {
// Failed test: Include.getIncludeName() always returns ""; // Failed test: Include.getIncludeName() always returns "";
// assertTrue // assertTrue
assertTrue("PR:23478 Expected:"+ new String("") +" Got:"+ myInclude.getIncludeName(), includes[x].equals(myInclude.getIncludeName())); assertTrue("PR:23478 Expected:" + new String("") + " Got:"
+ myInclude.getIncludeName(), includes[x]
.equals(myInclude.getIncludeName()));
} }
} }
} }
/***
/***************************************************************************
* Simple sanity tests for the getIncludes call * Simple sanity tests for the getIncludes call
*/ */
public void testBug23478B() throws CModelException { public void testBug23478B() throws CModelException {
@ -174,7 +187,8 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
String includes[] = { "stdio.h", "unistd.h" }; String includes[] = { "stdio.h", "unistd.h" };
ExpectedStrings myExp = new ExpectedStrings(includes); ExpectedStrings myExp = new ExpectedStrings(includes);
int x; int x;
ITranslationUnit myTranslationUnit=CProjectHelper.findTranslationUnit(testProject,"exetest.c"); ITranslationUnit myTranslationUnit = CProjectHelper
.findTranslationUnit(testProject, "exetest.c");
myIncludes = myTranslationUnit.getIncludes(); myIncludes = myTranslationUnit.getIncludes();
for (x = 0; x < myIncludes.length; x++) { for (x = 0; x < myIncludes.length; x++) {
@ -186,9 +200,7 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
assertTrue(myExp.getExtraString(), !myExp.gotExtra()); assertTrue(myExp.getExtraString(), !myExp.gotExtra());
} }
/***************************************************************************
/***
* Simple sanity tests for the getElementAtLine() call * Simple sanity tests for the getElementAtLine() call
*/ */
public void testGetElementAtLine() throws CoreException { public void testGetElementAtLine() throws CoreException {
@ -196,7 +208,8 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
ICElement myElement; ICElement myElement;
Stack missing = new Stack(); Stack missing = new Stack();
int x; int x;
myTranslationUnit=CProjectHelper.findTranslationUnit(testProject,"exetest.c"); myTranslationUnit = CProjectHelper.findTranslationUnit(testProject,
"exetest.c");
for (x = 0; x < expectedStringList.length; x++) { for (x = 0; x < expectedStringList.length; x++) {
myElement = myTranslationUnit.getElementAtLine(expectedLines[x]); myElement = myTranslationUnit.getElementAtLine(expectedLines[x]);
@ -204,11 +217,14 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
missing.push(expectedStringList[x]); missing.push(expectedStringList[x]);
else { else {
if (expectedStringList[x].equals("mystruct_t")) { if (expectedStringList[x].equals("mystruct_t")) {
assertTrue("PR:23603 expected:" + expectedStringList[x] + " Got:" + myElement.getElementName(), assertTrue("PR:23603 expected:" + expectedStringList[x]
expectedStringList[x].equals(myElement.getElementName())); + " Got:" + myElement.getElementName(),
expectedStringList[x].equals(myElement
.getElementName()));
} else { } else {
assertTrue("Expected:" + expectedStringList[x] + " Got:" + myElement.getElementName(), assertTrue("Expected:" + expectedStringList[x] + " Got:"
expectedStringList[x].equals(myElement.getElementName())); + myElement.getElementName(), expectedStringList[x]
.equals(myElement.getElementName()));
} }
} }
@ -220,6 +236,12 @@ public class TranslationUnitTests extends TranslationUnitBaseTest {
output += missing.pop() + " "; output += missing.pop() + " ";
assertTrue(output, false); assertTrue(output, false);
} }
}
public void testIsValidSourceUnitName() {
assertTrue(CoreModel.isValidSourceUnitName(testProject.getProject(), "test.c"));
assertFalse(CoreModel.isValidSourceUnitName(testProject.getProject(), "test.h"));
assertTrue(CoreModel.isValidSourceUnitName(testProject.getProject(), "test.cc"));
assertFalse(CoreModel.isValidSourceUnitName(testProject.getProject(), "test.hh"));
} }
} }

View file

@ -233,6 +233,10 @@ public class CoreModel {
IContentType contentType = CCorePlugin.getContentType(project, name); IContentType contentType = CCorePlugin.getContentType(project, name);
if (contentType != null) { if (contentType != null) {
String id = contentType.getId(); String id = contentType.getId();
if (CCorePlugin.CONTENT_TYPE_CHEADER.equals(id)
|| CCorePlugin.CONTENT_TYPE_CXXHEADER.equals(id))
return false;
return CCorePlugin.CONTENT_TYPE_CSOURCE.equals(id) return CCorePlugin.CONTENT_TYPE_CSOURCE.equals(id)
|| CCorePlugin.CONTENT_TYPE_CXXSOURCE.equals(id) || CCorePlugin.CONTENT_TYPE_CXXSOURCE.equals(id)
|| CCorePlugin.CONTENT_TYPE_ASMSOURCE.equals(id) || CCorePlugin.CONTENT_TYPE_ASMSOURCE.equals(id)