📄 console.c
字号:
static CU_ErrorCode console_run_suite(CU_pSuite pSuite)
{
f_pRunningSuite = NULL;
return CU_run_suite(pSuite);
}
/*------------------------------------------------------------------------*/
/** Run a specific test for the specified suite within
* the console interface.
* @param pSuite The suite containing the test to be run (non-NULL).
* @param pTest The test to be run (non-NULL).
* @return An error code indicating the error status
* during the test run.
*/
static CU_ErrorCode console_run_single_test(CU_pSuite pSuite, CU_pTest pTest)
{
f_pRunningSuite = NULL;
return CU_run_test(pSuite, pTest);
}
/*------------------------------------------------------------------------*/
/** Read the name of a test from standard input and
* locate the test having the specified name.
* A pointer to the located test is stored in pTest
* upon return.
* @param pSuite The suite to be queried.
* @param ppTest Pointer to location to store the selected test.
* @return CUE_SUCCESS if a test was successfully selected,
* CUE_NOTEST otherwise. On return, ppTest points
* to the test selected.
*/
static CU_ErrorCode select_test(CU_pSuite pSuite, CU_pTest* ppTest)
{
char szTestName[CU_MAX_TEST_NAME_LENGTH];
fprintf(stdout,"\nEnter Test Name : ");
fgets(szTestName, CU_MAX_TEST_NAME_LENGTH, stdin);
sscanf(szTestName, "%[^\n]s", szTestName);
*ppTest = CU_get_test_by_name(szTestName, pSuite);
return (NULL != *ppTest) ? CUE_SUCCESS : CUE_NOTEST;
}
/*------------------------------------------------------------------------*/
/** Read the name of a suite from standard input and
* locate the suite having the specified name.
* The input string is used to locate the suite having the
* indicated name in the specified test registry. If pRegistry
* is NULL, the default CUnit registry will be used. The located
* pSuite is returned in ppSuite. ppSuite will be NULL if there is
* no suite in the registry having the input name. Returns NULL if
* the suite is successfully located, non-NULL otherwise.
* @param pRegistry The CU_pTestRegistry to query. If NULL, use the
* default internal CUnit test registry.
* @param ppSuite Pointer to location to store the selected suite.
* @return CUE_SUCCESS if a suite was successfully selected,
* CUE_NOSUITE otherwise. On return, ppSuite points
* to the suite selected.
*/
static CU_ErrorCode select_suite(CU_pTestRegistry pRegistry, CU_pSuite* ppSuite)
{
char szSuiteName[CU_MAX_SUITE_NAME_LENGTH];
fprintf(stdout,"\n\nEnter Suite Name : ");
fgets(szSuiteName, CU_MAX_SUITE_NAME_LENGTH, stdin);
sscanf(szSuiteName, "%[^\n]s", szSuiteName);
*ppSuite = CU_get_suite_by_name(szSuiteName, (NULL != pRegistry) ? pRegistry : CU_get_registry());
return (NULL != *ppSuite) ? CUE_SUCCESS : CUE_NOSUITE;
}
/*------------------------------------------------------------------------*/
/** List the suites in a registry to standard output.
* @param pRegistry The CU_pTestRegistry to query (non-NULL).
*/
static void list_suites(CU_pTestRegistry pRegistry)
{
CU_pSuite pCurSuite = NULL;
int i;
if (NULL == pRegistry) {
pRegistry = CU_get_registry();
}
assert(NULL != pRegistry);
if (0 == pRegistry->uiNumberOfSuites) {
fprintf(stdout, "\nNo suites registered.\n");
return;
}
assert(NULL != pRegistry->pSuite);
fprintf(stdout, "\n--------------------- Registered Suites --------------------------");
fprintf(stdout, "\n Suite Name Init? Cleanup? # Tests\n");
for (i = 1, pCurSuite = pRegistry->pSuite; (NULL != pCurSuite); pCurSuite = pCurSuite->pNext, ++i) {
fprintf(stdout, "\n%3d. %-34.33s %3s %3s %3u",
i,
(NULL != pCurSuite->pName) ? pCurSuite->pName : "",
(NULL != pCurSuite->pInitializeFunc) ? "YES" : "NO",
(NULL != pCurSuite->pCleanupFunc) ? "YES" : "NO",
pCurSuite->uiNumberOfTests);
}
fprintf(stdout, "\n------------------------------------------------------------------"
"\nTotal Number of Suites : %-u\n", pRegistry->uiNumberOfSuites);
}
/*------------------------------------------------------------------------*/
/** List the tests in a suite to standard output.
* @param pSuite The suite to query (non-NULL).
*/
static void list_tests(CU_pSuite pSuite)
{
CU_pTest pCurTest = NULL;
unsigned int uiCount;
assert(NULL != pSuite);
if (0 == pSuite->uiNumberOfTests) {
fprintf(stdout, "\nSuite %s contains no tests.\n",
(NULL != pSuite->pName) ? pSuite->pName : "");
return;
}
assert(NULL != pSuite->pTest);
fprintf(stdout, "\n--------------- Test List ---------------------------------");
fprintf(stdout, "\n Test Names (Suite: %s)\n",
(NULL != pSuite->pName) ? pSuite->pName : "");
for (uiCount = 1, pCurTest = pSuite->pTest; (NULL != pCurTest); uiCount++, pCurTest = pCurTest->pNext) {
fprintf(stdout, "\n%3u. %s", uiCount, (NULL != pCurTest->pName) ? pCurTest->pName : "");
}
fprintf(stdout, "\n-----------------------------------------------------------"
"\nTotal Number of Tests : %-u\n", pSuite->uiNumberOfTests);
}
/*------------------------------------------------------------------------*/
/** Display the record of test failures on standard output. */
static void show_failures(void)
{
int i;
CU_pFailureRecord pFailure = CU_get_failure_list();
if (NULL == pFailure) {
fprintf(stdout, "\nNo failures.\n");
}
else {
fprintf(stdout, "\n--------------- Test Run Failures -------------------------");
fprintf(stdout, "\n src_file:line# : (suite:test) : failure_condition\n");
for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) {
fprintf(stdout, "\n%d. %s:%u : (%s : %s) : %s", i,
(NULL != pFailure->strFileName) ? pFailure->strFileName : "",
pFailure->uiLineNumber,
((NULL != pFailure->pSuite) && (NULL != pFailure->pSuite->pName)) ? pFailure->pSuite->pName : "",
((NULL != pFailure->pTest) && (NULL != pFailure->pTest->pName)) ? pFailure->pTest->pName : "",
(NULL != pFailure->strCondition) ? pFailure->strCondition : "");
}
fprintf(stdout, "\n-----------------------------------------------------------"
"\nTotal Number of Failures : %-d\n", i - 1);
}
}
/*------------------------------------------------------------------------*/
/** Handler function called at start of each test.
* @param pTest The test being run.
* @param pSuite The suite containing the test.
*/
static void console_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite)
{
assert(NULL != pTest);
assert(NULL != pSuite);
/* Comparing the Addresses rather than the Group Names. */
if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) {
fprintf(stdout, "\nRunning Suite : %s", (NULL != pSuite->pName) ? pSuite->pName : "");
fprintf(stdout, "\n\tRunning test : %s", (NULL != pTest->pName) ? pTest->pName : "");
f_pRunningSuite = pSuite;
}
else {
fprintf(stdout, "\n\tRunning test : %s", (NULL != pTest->pName) ? pTest->pName : "");
}
}
/*------------------------------------------------------------------------*/
/** Handler function called at completion of each test.
* @param pTest The test being run.
* @param pSuite The suite containing the test.
* @param pFailure Pointer to the 1st failure record for this test.
*/
static void console_test_complete_message_handler(const CU_pTest pTest,
const CU_pSuite pSuite,
const CU_pFailureRecord pFailure)
{
/*
* For console interface do nothing. This is useful only for the test
* interface where UI is involved. Just silence compiler warnings.
*/
CU_UNREFERENCED_PARAMETER(pTest);
CU_UNREFERENCED_PARAMETER(pSuite);
CU_UNREFERENCED_PARAMETER(pFailure);
}
/*------------------------------------------------------------------------*/
/** Handler function called at completion of all tests in a suite.
* @param pFailure Pointer to the test failure record list.
*/
static void console_all_tests_complete_message_handler(const CU_pFailureRecord pFailure)
{
CU_pRunSummary pRunSummary = CU_get_run_summary();
CU_pTestRegistry pRegistry = CU_get_registry();
CU_UNREFERENCED_PARAMETER(pFailure); /* not used in console interface */
assert(NULL != pRunSummary);
assert(NULL != pRegistry);
fprintf(stdout,"\n\n--Run Summary: Type Total Ran Passed Failed"
"\n suites %8u%8u n/a%8u"
"\n tests %8u%8u%8u%8u"
"\n asserts%8u%8u%8u%8u\n",
pRegistry->uiNumberOfSuites,
pRunSummary->nSuitesRun,
pRunSummary->nSuitesFailed,
pRegistry->uiNumberOfTests,
pRunSummary->nTestsRun,
pRunSummary->nTestsRun - pRunSummary->nTestsFailed,
pRunSummary->nTestsFailed,
pRunSummary->nAsserts,
pRunSummary->nAsserts,
pRunSummary->nAsserts - pRunSummary->nAssertsFailed,
pRunSummary->nAssertsFailed);
}
/*------------------------------------------------------------------------*/
/** Handler function called when suite initialization fails.
* @param pSuite The suite for which initialization failed.
*/
static void console_suite_init_failure_message_handler(const CU_pSuite pSuite)
{
assert(NULL != pSuite);
fprintf(stdout, "\nWARNING - Suite initialization failed for %s.",
(NULL != pSuite->pName) ? pSuite->pName : "");
}
/*------------------------------------------------------------------------*/
/** Handler function called when suite cleanup fails.
* @param pSuite The suite for which cleanup failed.
*/
static void console_suite_cleanup_failure_message_handler(const CU_pSuite pSuite)
{
assert(NULL != pSuite);
fprintf(stdout, "\nWARNING - Suite cleanup failed for %s.",
(NULL != pSuite->pName) ? pSuite->pName : "");
}
/** @} */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -