Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #131 from netspiri/fix/#130
Browse files Browse the repository at this point in the history
Source Code Discovery for BOOST_DATA_TEST_CASE
  • Loading branch information
guwirth authored Jun 13, 2016
2 parents 1ca1363 + 1d75de1 commit cd0b87e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 17 deletions.
61 changes: 44 additions & 17 deletions BoostTestAdapter/Discoverers/SourceCodeDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ private static class Constants
public const string FixtureTestSuiteIdentifier = "BOOST_FIXTURE_TEST_SUITE";
public const string AutoTestSuiteEndIdentifier = "BOOST_AUTO_TEST_SUITE_END";
public const string TestCaseTemplateIdentifier = "BOOST_AUTO_TEST_CASE_TEMPLATE";
public const string DataTestCaseIdentifier = "BOOST_DATA_TEST_CASE";
public const string FixtureDataTestCaseIdentifier = "BOOST_DATA_TEST_CASE_F";
}

#endregion Constants
Expand Down Expand Up @@ -274,6 +276,7 @@ private static Defines GetDefines(ProjectInfo projectInfo)
/// <param name="cppSourceFile">The C++ source file to scan for Boost Tests</param>
/// <param name="source">The associated test source EXE</param>
/// <param name="discoverySink">The discoverySink to which identified tests will be notified to</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
private static void DiscoverBoostTests(CppSourceFile cppSourceFile, string source, ITestCaseDiscoverySink discoverySink)
{
string[] code = cppSourceFile.SourceCode.TrimEnd(new[] { ' ', '\n', '\r' }).Split('\n');
Expand Down Expand Up @@ -327,13 +330,22 @@ private static void DiscoverBoostTests(CppSourceFile cppSourceFile, string sourc

case Constants.FixtureTestCaseIdentifier:
case Constants.AutoTestCaseIdentifier:
case Constants.DataTestCaseIdentifier:
{
string testCaseName = ParseTestCase(splitMacro, sourceInfo, code, ref line);
var testCase = TestCaseUtils.CreateTestCase(source, sourceInfo, suite, testCaseName);
TestCaseUtils.AddTestCase(testCase, discoverySink);
break;
}

case Constants.FixtureDataTestCaseIdentifier:
{
string testCaseName = ParseDataTestCaseF(splitMacro, sourceInfo, code, ref line);
var testCase = TestCaseUtils.CreateTestCase(source, sourceInfo, suite, testCaseName);
TestCaseUtils.AddTestCase(testCase, discoverySink);
break;
}

case Constants.AutoTestSuiteEndIdentifier:
{
suite.Pop();
Expand All @@ -356,13 +368,7 @@ private static void DiscoverBoostTests(CppSourceFile cppSourceFile, string sourc
/// <returns>A tuple consisting of the test case name and the list of defined templated types</returns>
private static KeyValuePair<string, IEnumerable<string>> ParseTemplateTestCase(string[] splitMacro, Dictionary<string, IEnumerable<string>> definedTemplates, SourceFileInfo sourceInfo, string[] code, ref string line)
{
int newLineNumber = ScrollLines(sourceInfo.LineNumber, code, ref line);
if (sourceInfo.LineNumber != newLineNumber)
{
// recalc splitMacro
splitMacro = SplitMacro(line);
sourceInfo.LineNumber = newLineNumber;
}
splitMacro = ParseTestDeclaration(splitMacro, sourceInfo, code, ref line);

//third parameter is the corresponding boost::mpl::list name
string listName = splitMacro[3].Trim();
Expand All @@ -388,26 +394,47 @@ private static KeyValuePair<string, IEnumerable<string>> ParseTemplateTestCase(s
/// <returns>The name of the test case</returns>
private static string ParseTestCase(string[] splitMacro, SourceFileInfo sourceInfo, string[] code, ref string line)
{
int newLineNumber = ScrollLines(sourceInfo.LineNumber, code, ref line);
if (sourceInfo.LineNumber != newLineNumber)
{
// recalc splitMacro
splitMacro = SplitMacro(line);
sourceInfo.LineNumber = newLineNumber;
}

splitMacro = ParseTestDeclaration(splitMacro, sourceInfo, code, ref line);
return splitMacro[1].Trim();
}

/// <summary>
/// Parses the beginning declaration of a BOOST_DATA_TEST_CASE_F
/// </summary>
/// <param name="splitMacro">The current source line split into tokens</param>
/// <param name="sourceInfo">Source file and line information/param>
/// <param name="code">The entire code split by line</param>
/// <param name="line">The current source code line which is being evaluated</param>
/// <returns>The name of the test case</returns>
private static string ParseDataTestCaseF(string[] splitMacro, SourceFileInfo sourceInfo, string[] code, ref string line)
{
splitMacro = ParseTestDeclaration(splitMacro, sourceInfo, code, ref line);
return splitMacro[2].Trim();
}

/// <summary>
/// Parses the beginning declaration of a fixture or auto test suite
/// </summary>
/// <param name="splitMacro">The current source line split into tokens</param>
/// <param name="sourceInfo">Source file and line information/param>
/// <param name="code">The entire code split by line</param>
/// <param name="line">The current source code line which is being evaluated</param>
/// <returns>The name of the test sute</returns>
/// <returns>The name of the test suite</returns>
private static string ParseBeginTestSuite(string[] splitMacro, SourceFileInfo sourceInfo, string[] code, ref string line)
{
splitMacro = ParseTestDeclaration(splitMacro, sourceInfo, code, ref line);
return splitMacro[1].Trim();
}

/// <summary>
/// Parses the <em>entire</em> test case/suite declaration
/// </summary>
/// <param name="splitMacro">The current source line split into tokens</param>
/// <param name="sourceInfo">Source file and line information/param>
/// <param name="code">The entire code split by line</param>
/// <param name="line">The current source code line which is being evaluated</param>
/// <returns>An array of string components which are of interest for test component evaluation</returns>
private static string[] ParseTestDeclaration(string[] splitMacro, SourceFileInfo sourceInfo, string[] code, ref string line)
{
int newLineNumber = ScrollLines(sourceInfo.LineNumber, code, ref line);
if (sourceInfo.LineNumber != newLineNumber)
Expand All @@ -417,7 +444,7 @@ private static string ParseBeginTestSuite(string[] splitMacro, SourceFileInfo so
sourceInfo.LineNumber = newLineNumber;
}

return splitMacro[1].Trim();
return splitMacro;
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions BoostTestAdapterNunit/BoostTestAdapterNunit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@
<EmbeddedResource Include="Resources\ReportsLogs\DataTestCase\sample.test.report.xml" />
<EmbeddedResource Include="Resources\ReportsLogs\DataTestCase\sample.test.log.xml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\CppSources\BoostDataTestCase.cpp" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
36 changes: 36 additions & 0 deletions BoostTestAdapterNunit/Resources/CppSources/BoostDataTestCase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#define BOOST_TEST_MODULE DataTestCaseSample

#include <boost/test/included/unit_test.hpp>
#include <boost/test/data/test_case.hpp>

struct Fixture
{
Fixture()
{
std::cout << "Fixtrure::Fixture()" << std::endl;
};

~Fixture() noexcept
{
std::cout << "Fixtrure::~Fixture()" << std::endl;
};
};

BOOST_AUTO_TEST_SUITE(data_test_suite)

BOOST_DATA_TEST_CASE(data_1, boost::unit_test::data::make({ 1, 2, 3 }))
{
BOOST_CHECK_NE(sample, 4);
}

BOOST_DATA_TEST_CASE_F(Fixture, data_2, boost::unit_test::data::make({ 1, 2, 3 }))
{
BOOST_FAIL("TODO: data_2");
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_DATA_TEST_CASE(data_3, boost::unit_test::data::make({ 1, 2, 3 }), number)
{
BOOST_CHECK_EQUAL(number, 1);
}
18 changes: 18 additions & 0 deletions BoostTestAdapterNunit/SourceCodeDiscovererNunit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ public void MultilineBoostMacroDefinitions()
}
}

/// <summary>
/// Tests the discovery of BOOST_DATA_TEST_CASE and BOOST_DATA_TEST_CASE_F test cases
/// </summary>
[Test]
public void BOOST_DATA_TEST_CASEDiscovery()
{
using (DummySolution solution = new DummySolution(DefaultSource, "BoostDataTestCase.cpp"))
{
IList<TestCase> testList = DiscoverTests(solution);

Assert.That(testList.Count, Is.EqualTo(3));

AssertTestDetails(testList[0], QualifiedNameBuilder.FromString("data_test_suite/data_1"));
AssertTestDetails(testList[1], QualifiedNameBuilder.FromString("data_test_suite/data_2"));
AssertTestDetails(testList[2], QualifiedNameBuilder.FromString("data_3"));
}
}

#endregion GetBoostTestsCase
}
}

0 comments on commit cd0b87e

Please sign in to comment.