TestExecWindow
Class SourceFileParser - look for test macros within files of project dir

Overview:

Scan project dir for .cpp files

public void ScanProjectDir(ProjectInfo projectInfo)
{
WriteLine(3, "ScanProjectDir-Begin");
// Recursively scan for .cpp files in project dir and all sub directories
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(projectInfo.sourceDirPath);
var files = dir.GetFiles("*.cpp", System.IO.SearchOption.AllDirectories);
foreach (var file in files)
{
// Search for test macros and store them within "projectInfo"
ParseFile(projectInfo, file);
}
WriteLine(3, "ScanProjectDir-End");
}

Parsing a source file

During parsing a hard coded set of test macros is searched. Depending on the type of found macro the relevant test case information is extracted at different positions within the macro:

private void ParseFile(ProjectInfo projectInfo, System.IO.FileInfo in_file)
{
WriteLine(3, "ParseFile-Begin " + in_file.FullName);
WriteLine(2, "File " + in_file.FullName);
// Read whole source file into array of strings
string[] lines = System.IO.File.ReadAllLines(in_file.FullName);
int numLines = lines.Length;
// During parsing we store information about found description,
// test suite and current line index within ParseInfo
ParseInfo parseInfo = new ParseInfo(in_file.FullName);
while (parseInfo.lineIdx < numLines)
{
string line = lines[parseInfo.lineIdx];
if (LineContainsMacro("TTB_TEST_FUNC_DESC",line))
{
MacroInfo macro = ReadMacro(projectInfo, parseInfo,
"TTB_TEST_FUNC_DESC", line, lines);
if (macro != null)
{
parseInfo.description = macro.param2;
projectInfo.appType = AppType.TTB;
parseInfo.RemoveAllTestGroups();
parseInfo.AddTestGroup(in_file.Name);
CreateTestFuncInfo(projectInfo, parseInfo);
}
}
else if (LineContainsMacro("TTB_TEST_FUNC",line))
{
MacroInfo macro = ReadMacro(projectInfo, parseInfo,
"TTB_TEST_FUNC", line, lines);
if (macro != null)
{
parseInfo.description = macro.param1;
projectInfo.appType = AppType.TTB;
parseInfo.RemoveAllTestGroups();
parseInfo.AddTestGroup(in_file.Name);
CreateTestFuncInfo(projectInfo, parseInfo);
}
}
else if (LineContainsMacro("BOOST_AUTO_TEST_CASE_TEMPLATE", line))
{
MacroInfo macro = ReadMacro(projectInfo, parseInfo,
"BOOST_AUTO_TEST_CASE_TEMPLATE", line, lines);
if (macro != null)
{
parseInfo.description = macro.param1;
// Add "*" for correct call of test case
parseInfo.description += "*";
projectInfo.appType = AppType.BOOST;
CreateTestFuncInfo(projectInfo, parseInfo);
}
}
else if (LineContainsMacro("BOOST_AUTO_TEST_CASE",line))
{
MacroInfo macro = ReadMacro(projectInfo, parseInfo,
"BOOST_AUTO_TEST_CASE", line, lines);
if (macro != null)
{
parseInfo.description = macro.param1;
projectInfo.appType = AppType.BOOST;
CreateTestFuncInfo(projectInfo, parseInfo);
}
}
else if(LineContainsMacro("TTB_BOOST_TEST_CASE",line))
/// ...

Storing info for each test case

private void CreateTestFuncInfo(ProjectInfo projectInfo, ParseInfo parseInfo)
{
// Store the found test group if not already existing
string[] groupNames = parseInfo.GetTestGroupHierarchyString().Split(new Char[] { '/'}, StringSplitOptions.RemoveEmptyEntries);
string groupHierarchyString = "";
List<string> groups = new System.Collections.Generic.List<string>();
if (groupNames.Length == 0)
{
if (!projectInfo.testFuncWithoutTestGroupFound)
{
projectInfo.testFuncWithoutTestGroupFound = true;
groups.Add(ProjectInfo.NO_TESTSUITE_STR);
TestGroupEntry tg = new TestGroupEntry(projectInfo.appType == AppType.BOOST);
tg.fileFullPath = parseInfo.fileFullPath;
tg.lineNum = parseInfo.GetLineNum();
tg.AddTestGroupHierarchy(groups);
projectInfo.testGroups.Add(tg);
}
}
else
{
foreach (string grp in groupNames)
{
groups.Add(grp);
groupHierarchyString += (groupHierarchyString.Length > 0 ? "/" : "");
groupHierarchyString += grp;
if (projectInfo.testGroups.Find(x => (x.GetTestGroupHierarchyString() == groupHierarchyString)) == null)
{
TestGroupEntry tg = new TestGroupEntry(projectInfo.appType == AppType.BOOST);
tg.fileFullPath = parseInfo.fileFullPath;
tg.lineNum = parseInfo.GetLineNum();
tg.AddTestGroupHierarchy(groups);
projectInfo.testGroups.Add(tg);
}
}
}
// Store the found test function description
TestFuncEntry tf = new TestFuncEntry(projectInfo.appType == AppType.BOOST);
tf.testFunc = parseInfo.description;
tf.fileFullPath = parseInfo.fileFullPath;
tf.lineNum = parseInfo.GetLineNum();
tf.AddTestGroupHierarchy(parseInfo.testGroups);
projectInfo.testFuncs.Add(tf);
WriteLine(3, "CreateTestFuncInfo: " + tf.testFunc + " within group: " + tf.GetTestGroupHierarchyString()
+ " file: " + tf.fileFullPath + " line: " + tf.lineNum);
}