Skip to content

Commit

Permalink
Add unit tests for aggregate by file.
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenCppCoverage committed Aug 16, 2015
1 parent f2c3601 commit b87c199
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 12 deletions.
60 changes: 51 additions & 9 deletions CppCoverageTest/CodeCoverageRunnerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ namespace CppCoverageTest
}

//---------------------------------------------------------------------
cov::CoverageData ComputeCoverageData(
cov::CoverageData ComputeCoverageDataPatterns(
const std::vector<std::wstring>& arguments,
std::wstring modulePattern,
std::wstring sourcePattern,
const std::vector<std::wstring>& modulePatternCollection,
const std::vector<std::wstring>& sourcePatternCollection,
bool coverChildren = true)
{
cov::CodeCoverageRunner codeCoverageRunner;
Expand All @@ -94,11 +94,17 @@ namespace CppCoverageTest

cov::CoverageSettings coverageSettings{modulePatterns, sourcePatterns};

boost::to_lower(modulePattern);
boost::to_lower(sourcePattern);

modulePatterns.AddSelectedPatterns(modulePattern);
sourcePatterns.AddSelectedPatterns(sourcePattern);
for (auto modulePattern : modulePatternCollection)
{
boost::to_lower(modulePattern);
modulePatterns.AddSelectedPatterns(modulePattern);
}

for (auto sourcePattern : sourcePatternCollection)
{
boost::to_lower(sourcePattern);
sourcePatterns.AddSelectedPatterns(sourcePattern);
}

cov::StartInfo startInfo{ TestCoverageConsole::GetOutputBinaryPath().wstring() };
for (const auto& argument: arguments)
Expand All @@ -107,6 +113,16 @@ namespace CppCoverageTest
return codeCoverageRunner.RunCoverage(startInfo, coverageSettings, coverChildren);
}

//---------------------------------------------------------------------
cov::CoverageData ComputeCoverageData(
const std::vector<std::wstring>& arguments,
const std::wstring& modulePattern,
const std::wstring& sourcePattern,
bool coverChildren = true)
{
return ComputeCoverageDataPatterns(arguments, { modulePattern }, { sourcePattern }, coverChildren);
}

//---------------------------------------------------------------------
cov::CoverageData ComputeCoverageDataInBothMode(
const std::wstring& programArg,
Expand Down Expand Up @@ -152,7 +168,7 @@ namespace CppCoverageTest
const auto& module = *modules.at(0);
const auto& files = module.GetFiles();

return *files.at(0).get();
return *files.at(0);
}

//---------------------------------------------------------------------
Expand Down Expand Up @@ -271,4 +287,30 @@ namespace CppCoverageTest
TestLine(file, mainLine + 21, false); // TestBreakPoint
TestLine(file, mainLine + 23, true); // TestChildProcess
}

//-------------------------------------------------------------------------
TEST_F(CodeCoverageRunnerTest, TestFileInSeveralModules)
{
auto coverageData = ComputeCoverageDataPatterns(
{ TestCoverageConsole::TestFileInSeveralModules },
{ TestCoverageSharedLib::GetOutputBinaryPath().wstring(), TestCoverageConsole::GetOutputBinaryPath().wstring()},
{ TestCoverageSharedLib::GetSharedFunctionFilename().wstring() });

const auto sharedFunctionLine = TestCoverageSharedLib::GetSharedFunctionLine();

const auto& fileFromFirstModule = *coverageData.GetModules().at(0)->GetFiles().at(0);
TestLine(fileFromFirstModule, sharedFunctionLine + 3, false);
TestLine(fileFromFirstModule, sharedFunctionLine + 4, true);

const auto& fileFromSecondModule = *coverageData.GetModules().at(1)->GetFiles().at(0);
TestLine(fileFromSecondModule, sharedFunctionLine + 3, true);
TestLine(fileFromSecondModule, sharedFunctionLine + 4, false);

cov::CoverageDataMerger().MergeFileCoverage(coverageData);
for (const auto* file : { &fileFromFirstModule, &fileFromSecondModule })
{
TestLine(*file, sharedFunctionLine + 3, true);
TestLine(*file, sharedFunctionLine + 4, true);
}
}
}
52 changes: 50 additions & 2 deletions CppCoverageTest/CoverageDataMergerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ namespace CppCoverageTest
return coverageDataCollection;
}

//-------------------------------------------------------------------------
void AddLinesToFileCoverage(
cov::FileCoverage& fileCoverage,
const std::initializer_list<std::pair<int, bool>>& lineArgCollection)
{
for (const auto& lineArg : lineArgCollection)
fileCoverage.AddLine(lineArg.first, lineArg.second);
}

//-------------------------------------------------------------------------
void AddLine(
cov::CoverageData& coverageData,
Expand All @@ -68,8 +77,7 @@ namespace CppCoverageTest
{
auto& file = coverageData.AddModule(modulePath).AddFile(filePath);

for (const auto& lineArg : lineArgCollection)
file.AddLine(lineArg.first, lineArg.second);
AddLinesToFileCoverage(file, lineArgCollection);
}

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -152,4 +160,44 @@ namespace CppCoverageTest
CheckLineHasBeenExecuted(mergedFile, 2, true);
CheckLineHasBeenExecuted(mergedFile, 3, true);
}

//-------------------------------------------------------------------------
TEST(CoverageDataMergerTest, MergeFileCoverageEmpty)
{
cov::CoverageData coverageData{L"test", 0};

AddLine(coverageData, modulePath, filePath, { { 0, false }, { 1, false }, { 2, true } });
cov::CoverageDataMerger{}.MergeFileCoverage(coverageData);

auto& mergedFile = coverageData.GetModules().at(0)->GetFiles().at(0);
ASSERT_EQ(3, mergedFile->GetLines().size());
CheckLineHasBeenExecuted(mergedFile, 0, false);
CheckLineHasBeenExecuted(mergedFile, 1, false);
CheckLineHasBeenExecuted(mergedFile, 2, true);
}

//-------------------------------------------------------------------------
TEST(CoverageDataMergerTest, MergeFileCoverageMultipleFiles)
{
cov::CoverageData coverageData{ L"test", 0 };

auto& fileCoverage1 = coverageData.AddModule(modulePath).AddFile(filePath);
auto& fileCoverage2 = coverageData.AddModule(L"otherModule").AddFile(filePath);

AddLinesToFileCoverage(fileCoverage1, { { 0, false }, { 1, false }, { 2, true } });
AddLinesToFileCoverage(fileCoverage2, { { 1, true }, { 2, false }, { 3, true } });
cov::CoverageDataMerger{}.MergeFileCoverage(coverageData);

const auto& modules = coverageData.GetModules();
ASSERT_EQ(2, modules.size());
for (const auto& module : modules)
{
auto& mergedFile = module->GetFiles().at(0);
ASSERT_EQ(4, mergedFile->GetLines().size());
CheckLineHasBeenExecuted(mergedFile, 0, false);
CheckLineHasBeenExecuted(mergedFile, 1, true);
CheckLineHasBeenExecuted(mergedFile, 2, true);
CheckLineHasBeenExecuted(mergedFile, 3, true);
}
}
}
10 changes: 10 additions & 0 deletions CppCoverageTest/OptionsParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace CppCoverageTest
ASSERT_FALSE(options->IsVerboseModeEnabled());
ASSERT_FALSE(options->IsPlugingModeEnabled());
ASSERT_FALSE(options->IsCoverChildrenModeEnabled());
ASSERT_TRUE(options->IsAggregateByFileModeEnabled());
}

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -90,6 +91,15 @@ namespace CppCoverageTest
{ TestTools::OptionPrefix + cov::ProgramOptions::CoverChildrenOption })->IsCoverChildrenModeEnabled());
}

//-------------------------------------------------------------------------
TEST(OptionsParserTest, FileAggregate)
{
cov::OptionsParser parser;

ASSERT_FALSE(TestTools::Parse(parser,
{ TestTools::OptionPrefix + cov::ProgramOptions::NoAggregateByFileOption })->IsAggregateByFileModeEnabled());
}

//-------------------------------------------------------------------------
TEST(OptionsParserTest, WorkingDirectory)
{
Expand Down
9 changes: 9 additions & 0 deletions TestCoverageConsole/TestCoverageConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ namespace
{
}
}

//-----------------------------------------------------------------------------
void TestFileInSeveralModules()
{
TestCoverageSharedLib::CallSharedFunctionFromSharedLib();
TestCoverageSharedLib::SharedFunction(false);
}
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -66,6 +73,8 @@ int _tmain(int argc, _TCHAR* argv[])
DebugBreak();
else if (type == TestCoverageConsole::TestChildProcess)
TestCoverageConsole::RunChildProcesses(argc, argv);
else if (type == TestCoverageConsole::TestFileInSeveralModules)
TestFileInSeveralModules();
else
std::wcerr << L"Unsupported type:" << type << std::endl;
}
Expand Down
3 changes: 2 additions & 1 deletion TestCoverageConsole/TestCoverageConsole.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace TestCoverageConsole
//-------------------------------------------------------------------------
inline int GetTestCoverageConsoleCppMainLine()
{
return 45;
return 52;
}

const std::wstring TestBasic = L"TestBasic";
Expand All @@ -60,4 +60,5 @@ namespace TestCoverageConsole
const std::wstring TestThrowUnHandledSEHException = L"TestThrowUnHandledSEHException";
const std::wstring TestBreakPoint = L"TestBreakPoint";
const std::wstring TestChildProcess = L"ChildProcess";
const std::wstring TestFileInSeveralModules = L"FileInSeveralModules";
}
6 changes: 6 additions & 0 deletions TestCoverageSharedLib/TestCoverageSharedLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ namespace TestCoverageSharedLib
{
return TARGET_FILE_NAME;
}

//-------------------------------------------------------------------------
void CallSharedFunctionFromSharedLib()
{
SharedFunction(true);
}
}
22 changes: 22 additions & 0 deletions TestCoverageSharedLib/TestCoverageSharedLib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,26 @@ namespace TestCoverageSharedLib
__declspec(dllexport) boost::filesystem::path GetMainCppPath();
__declspec(dllexport) boost::filesystem::path GetOutputBinaryPath();
__declspec(dllexport) bool IsOdd(int n);

__declspec(dllexport) void CallSharedFunctionFromSharedLib();

//-------------------------------------------------------------------------
inline boost::filesystem::path GetSharedFunctionFilename()
{
return boost::filesystem::path(__FILE__).filename();
}

//-------------------------------------------------------------------------
inline int GetSharedFunctionLine()
{
return __LINE__ + 4;
}

//-------------------------------------------------------------------------
inline bool SharedFunction(bool calledFromSharedLib)
{
if (calledFromSharedLib)
return true;
return false;
}
}

0 comments on commit b87c199

Please sign in to comment.