Skip to content

Commit

Permalink
Add gh #157 : Adding changes for activate/toggle of suites.
Browse files Browse the repository at this point in the history
  • Loading branch information
kanjoe24 committed Jan 2, 2025
1 parent 2d98454 commit 1931716
Showing 1 changed file with 65 additions and 5 deletions.
70 changes: 65 additions & 5 deletions src/cpp_source/ut_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef enum
struct TestSuiteInfo {
int number;
std::string name;
bool isActive;
};

// Struct to hold test information within a suite
Expand Down Expand Up @@ -86,17 +87,38 @@ class UTTestRunner
const ::testing::UnitTest &unit_test = *::testing::UnitTest::GetInstance();

std::vector<TestSuiteInfo> suites;
std::string filter = ::testing::GTEST_FLAG(filter);

std::cout << "\n"
<< STRING_FORMAT("--------------------- Registered Suites -----------------------------") << "\n"
<< std::flush;
std::cout << std::setw(1) << "#" << " " // Right-aligned
<< std::left << std::setw(20) << STRING_FORMAT("Suite Name") // Left-aligned
<< std::left << std::setw(50) << STRING_FORMAT("Suite Name") // Left-aligned
<< std::left << std::setw(10) << STRING_FORMAT("#Tests") // Left-aligned
<< std::left << std::setw(10) << STRING_FORMAT("Active?") // Left-aligned
<< std::endl;
for (int i = 0; i < unit_test.total_test_suite_count(); ++i)
{
const ::testing::TestSuite *test_suite = unit_test.GetTestSuite(i);
suites.push_back({i + 1, test_suite->name()});
std::cout << i + 1 << ". " << test_suite->name() << "\n";
suites.push_back({i + 1, test_suite->name(), true});

// Check if the suite is active
std::string suite_filter = std::string(test_suite->name()) + ".*";
// Check if 'filter' starts with '-'
if (!filter.empty() && filter[0] == '-')
{
// Compare the remaining part of filter with suite_filter
if (filter.substr(1) == suite_filter)
{
suites[i].isActive = false;
}
}

std::cout << std::setw(1) << i + 1 << ". "
<< std::left << std::setw(50) << test_suite->name()
<< std::left << std::setw(10) << test_suite->total_test_count()
<< std::left << std::setw(10) << (suites[i].isActive ? "Yes" : "No")
<< "\n";
}
std::cout << STRING_FORMAT("---------------------------------------------------------------------") << "\n"
<< std::flush;
Expand Down Expand Up @@ -169,6 +191,27 @@ class UTTestRunner
return tests;
}

void ToggleTestSuiteExclusion(const std::string &test_suite_name)
{
// Get the current filter
std::string &filter = ::testing::GTEST_FLAG(filter);

// Construct patterns for inclusion and exclusion
std::string exclude_pattern = "-" + test_suite_name + ".*";

// Check if the suite is currently excluded
if (filter[0] == '-')
{
// If excluded, toggle to inclusion
filter = "*";
}
else
{
// If included, toggle to exclusion
filter = exclude_pattern;
}
}

// Function to list all tests in a specific suite and allow the user to select one
UT_STATUS selectTestFromSuite(const TestSuiteInfo &suite)
{
Expand Down Expand Up @@ -224,7 +267,17 @@ class UTTestRunner
{
testRunner.printUsage();
}
else if ((choice == STRING_FORMAT("A")[0]) || (choice == STRING_FORMAT("F")[0]) || (choice == STRING_FORMAT("O")[0]))
else if ((choice == STRING_FORMAT("A")[0]))
{
std::vector<TestInfo> tests = listTestsFromSuite(suite);
std::string testName = getUserSelectedTest(tests, suite);
if (testName.empty())
{ // Handle empty test name
continue; // Return to the menu without changing eStatus
}
::testing::GTEST_FLAG(filter) = "-" + testName;
}
else if ((choice == STRING_FORMAT("F")[0]) || (choice == STRING_FORMAT("O")[0]))
{
std::cout << "To be implemented soon\n" << std::flush;
}
Expand Down Expand Up @@ -349,7 +402,14 @@ UT_status_t UT_run_tests()
{
testRunner.printUsage();
}
else if ((choice == STRING_FORMAT("A")[0]) || (choice == STRING_FORMAT("F")[0]) || (choice == STRING_FORMAT("O")[0]))
else if ((choice == STRING_FORMAT("A")[0]))
{
auto suites = testRunner.listTestSuites();
int selected_suites = testRunner.getUserSelectedTestSuites(suites);
//::testing::GTEST_FLAG(filter) = "-" + suites[selected_suites - 1].name + ".*";
testRunner.ToggleTestSuiteExclusion(suites[selected_suites - 1].name);
}
else if ((choice == STRING_FORMAT("F")[0]) || (choice == STRING_FORMAT("O")[0]))
{
std::cout << "To be implemented soon\n" << std::flush;
}
Expand Down

0 comments on commit 1931716

Please sign in to comment.