?? console.c
字號:
/*
* CUnit - A Unit testing framework library for C.
* Copyright (C) 2001 Anil Kumar
* Copyright (C) 2004,2005,2006 Anil Kumar, Jerry St.Clair
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Implementation of the Console Test Interface.
*
* Aug 2001 Initial implementation (AK)
*
* 19/Aug/2001 Added initial console interface functions without
* any run functionality. (AK)
*
* 24/Aug/2001 Added compare_strings, show_errors, list_suites,
* list_tests function declarations. (AK)
*
* 17-Jul-2004 New interface, doxygen comments, reformat console output. (JDS)
*
* 30-Apr-2005 Added notification of suite cleanup failure. (JDS)
*/
/** @file
* Console test interface with interactive output (implementation).
*/
/** @addtogroup Console
@{
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>
#include "CUnit.h"
#include "TestDB.h"
#include "Util.h"
#include "TestRun.h"
#include "Console.h"
/** Console interface status flag. */
typedef enum
{
CONTINUE = 1, /**< Continue processing commands in current menu. */
MOVE_UP, /**< Move up to the previous menu. */
STOP /**< Stop processing (user selected 'Quit'). */
} STATUS;
/** Pointer to the currently running suite. */
static CU_pSuite f_pRunningSuite = NULL;
/* Forward declaration of module functions */
static void console_registry_level_run(CU_pTestRegistry pRegistry);
static STATUS console_suite_level_run(CU_pSuite pSuite);
static CU_ErrorCode console_run_all_tests(CU_pTestRegistry pRegistry);
static CU_ErrorCode console_run_suite(CU_pSuite pSuite);
static CU_ErrorCode console_run_single_test(CU_pSuite pSuite, CU_pTest pTest);
static void console_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite);
static void console_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailure);
static void console_all_tests_complete_message_handler(const CU_pFailureRecord pFailure);
static void console_suite_init_failure_message_handler(const CU_pSuite pSuite);
static void console_suite_cleanup_failure_message_handler(const CU_pSuite pSuite);
static CU_ErrorCode select_test(CU_pSuite pSuite, CU_pTest* ppTest);
static CU_ErrorCode select_suite(CU_pTestRegistry pRegistry, CU_pSuite* ppSuite);
static void list_suites(CU_pTestRegistry pRegistry);
static void list_tests(CU_pSuite pSuite);
static void show_failures(void);
/*------------------------------------------------------------------------*/
/** Run registered CUnit tests using the console interface. */
void CU_console_run_tests(void)
{
/*
* To avoid user from cribbing about the output not coming onto
* screen at the moment of SIGSEGV.
*/
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
fprintf(stdout, "\n\n CUnit - A Unit testing framework for C - Version " CU_VERSION
"\n http://cunit.sourceforge.net/\n\n");
if (NULL == CU_get_registry()) {
fprintf(stderr, "\n\nFATAL ERROR - Test registry is not initialized.\n");
CU_set_error(CUE_NOREGISTRY);
}
else {
CU_set_test_start_handler(console_test_start_message_handler);
CU_set_test_complete_handler(console_test_complete_message_handler);
CU_set_all_test_complete_handler(console_all_tests_complete_message_handler);
CU_set_suite_init_failure_handler(console_suite_init_failure_message_handler);
CU_set_suite_cleanup_failure_handler(console_suite_cleanup_failure_message_handler);
console_registry_level_run(NULL);
}
}
/*------------------------------------------------------------------------*/
/** Main loop for console interface.
* Displays actions and responds based on user imput.
* If pRegistry is NULL, will use the default internal CUnit
* test registry.
* @param pRegistry The CU_pTestRegistry to use for testing.
*/
static void console_registry_level_run(CU_pTestRegistry pRegistry)
{
int chChoice;
char szTemp[256];
CU_pSuite pSuite = NULL;
STATUS eStatus = CONTINUE;
while (CONTINUE == eStatus)
{
fprintf(stdout, "\n*************** CUNIT CONSOLE - MAIN MENU ***********************"
"\n(R)un all, (S)elect suite, (L)ist suites, Show (F)ailures, (Q)uit"
"\nEnter Command : ");
chChoice = getchar();
fgets(szTemp, sizeof(szTemp), stdin);
switch (tolower(chChoice)) {
case 'r':
console_run_all_tests(pRegistry);
break;
case 's':
if (CUE_SUCCESS == select_suite(pRegistry, &pSuite)) {
if (STOP == console_suite_level_run(pSuite)) {
eStatus = STOP;
}
}
else {
fprintf(stdout, "\nSuite not found.\n");
}
break;
case 'l':
list_suites(pRegistry);
break;
case 'f':
show_failures();
break;
case 'q':
eStatus = STOP;
break;
/* To stop gcc from cribbing */
default:
break;
}
}
}
/*------------------------------------------------------------------------*/
/** Run a selected suite within the console interface.
* Displays actions and responds based on user imput.
* @param pSuite The suite to use for testing (non-NULL).
*/
static STATUS console_suite_level_run(CU_pSuite pSuite)
{
int chChoice;
char szTemp[256];
CU_pTest pTest = NULL;
STATUS eStatus = CONTINUE;
assert(NULL != pSuite);
while (CONTINUE == eStatus) {
fprintf(stdout, "\n*************** CUNIT CONSOLE - SUITE MENU *******************************"
"\n(R)un All, (S)elect test, (L)ist tests, Show (F)ailures, (M)ove up, (Q)uit"
"\nEnter Command : ");
chChoice = getchar();
fgets(szTemp, sizeof(szTemp), stdin);
switch (tolower(chChoice)) {
case 'r':
console_run_suite(pSuite);
break;
case 's':
if (CUE_SUCCESS == select_test(pSuite, &pTest)) {
console_run_single_test(pSuite, pTest);
}
break;
case 'l':
list_tests(pSuite);
break;
case 'f':
show_failures();
break;
case 'm':
eStatus = MOVE_UP;
break;
case 'q':
eStatus = STOP;
break;
/* To stop gcc from cribbing */
default:
break;
}
}
return eStatus;
}
/*------------------------------------------------------------------------*/
/** Run all tests within the console interface.
* The test registry is changed to the specified registry
* before running the tests, and reset to the original
* registry when done. If pRegistry is NULL, the default
* internal CUnit test registry is used.
* @param pRegistry The CU_pTestRegistry containing the tests
* to be run.
* @return An error code indicating the error status
* during the test run.
*/
static CU_ErrorCode console_run_all_tests(CU_pTestRegistry pRegistry)
{
CU_pTestRegistry pOldRegistry = NULL;
CU_ErrorCode result;
f_pRunningSuite = NULL;
if (NULL != pRegistry) {
pOldRegistry = CU_set_registry(pRegistry);
}
result = CU_run_all_tests();
if (NULL != pRegistry) {
CU_set_registry(pOldRegistry);
}
return result;
}
/*------------------------------------------------------------------------*/
/** Run a specified suite within the console interface.
* @param pSuite The suite to be run (non-NULL).
* @return An error code indicating the error status
* during the test run.
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -