Skip to content

Commit

Permalink
Merge pull request #150 from rdkcentral/feature/gh149-menu-system-cpp…
Browse files Browse the repository at this point in the history
…-test

gh#149 : Adding changes for menu driven gtest tests
  • Loading branch information
kanjoe24 authored Dec 5, 2024
2 parents c235f5a + 55e5420 commit 696e9bf
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 22 deletions.
62 changes: 61 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,66 @@
"ut_gtest.h": "c",
"ut_cunit_internal.h": "c",
"ut_test_runner.h": "c",
"ut_cunit.h": "c"
"ut_cunit.h": "c",
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
}
}
148 changes: 147 additions & 1 deletion src/cpp_source/ut_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,25 @@
*/

#include <ut.h>
#include <ut_log.h>
#include <ut_internal.h>

#include <iomanip>

static TestMode_t gTestMode;
#define STRING_FORMAT(x) x
typedef enum
{
UT_STATUS_CONTINUE = 1, /**< Continue processing commands in current menu. */
UT_STATUS_MOVE_UP, /**< Move up to the previous menu. */
UT_STATUS_STOP /**< Stop processing (user selected 'Quit'). */
} UT_STATUS;

struct TestSuiteInfo {
int number;
std::string name;
};

class UTTestRunner
{
public:
Expand Down Expand Up @@ -57,6 +74,63 @@ class UTTestRunner
}
return RUN_ALL_TESTS();
}

std::vector<TestSuiteInfo> listTestSuites()
{
const ::testing::UnitTest &unit_test = *::testing::UnitTest::GetInstance();

std::vector<TestSuiteInfo> suites;
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::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";
}
std::cout << STRING_FORMAT("---------------------------------------------------------------------") << "\n"
<< std::flush;
std::cout << "\n"
<<"Total Number of Suites : "<< unit_test.total_test_suite_count() << "\n";
return suites;
}

std::string getUserSelectedTestSuites(const std::vector<TestSuiteInfo>& suites)
{
std::cout << "\nEnter number of suite to select (1-" << suites.size() << ") : ";
int number;
std::cin >> number;

if (std::cin.fail() || number <= 0 || number > static_cast<int>(suites.size()))
{
// Clear the error state and ignore the invalid input
std::cin.clear(); // Clear the error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore invalid input
return "";
}

// Return the selected test suite's filter
return suites[number - 1].name + ".*";
}

void printUsage()
{
std::cout << "\n\n"
<< STRING_FORMAT("Commands: R - Run all tests in suite") << "\n"
<< STRING_FORMAT(" S - Select a suite to run or modify") << "\n"
<< STRING_FORMAT(" L - List all registered suites") << "\n"
<< STRING_FORMAT(" H - Show this help message") << "\n"
<< STRING_FORMAT(" Q - Quit the application") << "\n"
<< STRING_FORMAT(" A - Activate - implementation pending") << "\n"
<< STRING_FORMAT(" O - Option - implementation pending") << "\n"
<< STRING_FORMAT(" F - Failures - implementation pending") << "\n"
<< std::flush;

}
};

void UT_set_results_output_filename(const char* szFilenameRoot)
Expand All @@ -74,13 +148,21 @@ void UT_set_results_output_filename(const char* szFilenameRoot)

// Set the output format and path programmatically
::testing::FLAGS_gtest_output = std::string("xml:") + filepath + "-report.xml";
std::cout << "Listing Filename:[" << filepath << "-report.xml]\n" << std::flush;
std::cout << "Results Filename:[" << filepath << ".log]\n" << std::flush;
}

void UT_set_test_mode(TestMode_t mode)
{
gTestMode = mode;
return;
}

TestMode_t UT_get_test_mode()
{
return gTestMode;
}

void UT_Manage_Suite_Activation(int groupID, bool enable_disable)
{
return;
Expand All @@ -98,7 +180,71 @@ UT_status_t startup_system( void )

UT_status_t UT_run_tests()
{
UT_STATUS eStatus = UT_STATUS_CONTINUE;

UTTestRunner testRunner;
testRunner.runTests();

if (UT_get_test_mode() == UT_MODE_CONSOLE)
{
while (eStatus == UT_STATUS_CONTINUE)
{
std::cout << "\n\n"
<< STRING_FORMAT("***************** UT CORE CONSOLE - MAIN MENU ******************************") << "\n"
<< STRING_FORMAT("(R)un (S)elect (L)ist (A)ctivate (F)ailures (O)ptions (H)elp (Q)uit") << "\n"
<< STRING_FORMAT("Enter command: ")
<< std::flush; // Ensures the buffer is flushed immediately

char choice = '\0';
std::cin >> choice; // Read the user input
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Clear the buffer

choice = std::toupper(choice); // Convert input to uppercase for consistency

if (choice == STRING_FORMAT("L")[0])
{
testRunner.listTestSuites();
}
else if (choice == STRING_FORMAT("S")[0])
{
auto suites = testRunner.listTestSuites();
std::string selected_suites = testRunner.getUserSelectedTestSuites(suites);

if (!selected_suites.empty())
{
std::cout << "Suite '" << selected_suites << "' selected.\n";
// Set the GTest filter
::testing::GTEST_FLAG(filter) = selected_suites;
}
else
{
std::cout << "\nTest not found.\n";
}
}
else if (choice == STRING_FORMAT("R")[0])
{
testRunner.runTests();
}
else if (choice == STRING_FORMAT("Q")[0])
{
eStatus = UT_STATUS_STOP;
}
else if ((choice == STRING_FORMAT("H")[0]) || (choice == STRING_FORMAT("?")[0]))
{
testRunner.printUsage();
}
else if ((choice == STRING_FORMAT("A")[0]) || (choice == STRING_FORMAT("F")[0]) || (choice == STRING_FORMAT("O")[0]))
{
std::cout << "To be implemented soon\n" << std::flush;
}
}
}
else
{
testRunner.runTests();
}

UT_LOG( UT_LOG_ASCII_GREEN "Logfile" UT_LOG_ASCII_NC ":[" UT_LOG_ASCII_YELLOW "%s" UT_LOG_ASCII_NC "]\n", UT_log_getLogFilename() );
UT_exit();

return UT_STATUS_OK;
}
33 changes: 14 additions & 19 deletions tests/src/cpp_source/ut_test_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,18 @@ class UTKVPProfileTestL1 : public UTCore
}
};

TEST_F(UTKVPProfileTestL1, TestProfileGetInstance)
TEST_F(UTKVPProfileTestL1, TestProfileOpenSuccess)
{
// Getinstance from the profile passed via cli
ut_kvp_instance_t *instance = ut_kvp_profile_getInstance();
EXPECT_NE(instance, nullptr);
const char *validFileName = KVP_VALID_TEST_YAML_FILE;
ut_kvp_status_t status = ut_kvp_profile_open((char *)validFileName);
EXPECT_EQ(status, UT_KVP_STATUS_SUCCESS);
}

TEST_F(UTKVPProfileTestL1, TestProfileClose)
TEST_F(UTKVPProfileTestL1, TestProfileGetInstance)
{
// Getinstance from the profile passed via cli
ut_kvp_instance_t *instance = ut_kvp_profile_getInstance();
EXPECT_NE(instance, nullptr);

ut_kvp_profile_close();
ut_kvp_profile_close();
ut_kvp_profile_close();
// Since close doesn't return a status, we assume success if no exceptions were thrown.
SUCCEED();
}

TEST_F(UTKVPProfileTestL1, TestProfileOpenSuccess)
{
const char *validFileName = KVP_VALID_TEST_YAML_FILE;
ut_kvp_status_t status = ut_kvp_profile_open((char *)validFileName);
EXPECT_EQ(status, UT_KVP_STATUS_SUCCESS);
ut_kvp_profile_close();
}

TEST_F(UTKVPProfileTestL1, TestProfileOpenFailure)
Expand All @@ -77,7 +63,16 @@ TEST_F(UTKVPProfileTestL1, TestProfileOpenFailure)
// Test with a null file name.
status = ut_kvp_profile_open(nullptr);
EXPECT_EQ(status, UT_KVP_STATUS_INVALID_PARAM);
}

TEST_F(UTKVPProfileTestL1, TestProfileClose)
{
// Test profile close
ut_kvp_profile_close();
ut_kvp_profile_close();
ut_kvp_profile_close();
// Since close doesn't return a status, we assume success if no exceptions were thrown.
SUCCEED();
}

// Other test cases as needed...
60 changes: 59 additions & 1 deletion ut-core-dev.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,65 @@
"stdlib.h": "c",
"assert.h": "c",
"cstdlib": "c",
"ut_internal.h": "c"
"ut_internal.h": "c",
"any": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"semaphore": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
},
"python.autoComplete.extraPaths": [
"${workspaceFolder}/sources/poky/bitbake/lib",
Expand Down

0 comments on commit 696e9bf

Please sign in to comment.