Basic.c 11.2 KB
Newer Older
1 2
/*
 *  CUnit - A Unit testing framework library for C.
3
 *  Copyright (C) 2004-2006  Jerry St.Clair, Anil Kumar
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 *  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
 */

/*
21
 *  Implementation for basic test runner interface.
22
 *
23
 *  11-Aug-2004   Initial implementation of basic test runner interface.  (JDS)
24
 *
25
 *  8-Jan-2005    Fixed reporting bug (bug report cunit-Bugs-1093861).  (JDS)
26
 *
27
 *  30-Apr-2005   Added notification of suite cleanup failure.  (JDS)
28 29
 *
 *  02-May-2006   Added internationalization hooks.  (JDS)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 */

/** @file
 * Basic interface with output to stdout.
 */
/** @addtogroup Basic
 * @{
 */

#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 "Basic.h"
50
#include "CUnit_intl.h"
51

52 53 54
/*=================================================================
 *  Global/Static Definitions
 *=================================================================*/
55
/** Pointer to the currently running suite. */
56
static CU_pSuite f_pRunningSuite = NULL;
57
/** Current run mode. */
58 59
static CU_BasicRunMode f_run_mode = CU_BRM_NORMAL;

60 61 62
/*=================================================================
 *  Forward declaration of module functions *
 *=================================================================*/
63 64 65 66 67 68
static CU_ErrorCode basic_initialize(void);
static CU_ErrorCode basic_run_all_tests(CU_pTestRegistry pRegistry);
static CU_ErrorCode basic_run_suite(CU_pSuite pSuite);
static CU_ErrorCode basic_run_single_test(CU_pSuite pSuite, CU_pTest pTest);

static void basic_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite);
69
static void basic_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailureList);
70 71
static void basic_all_tests_complete_message_handler(const CU_pFailureRecord pFailure);
static void basic_suite_init_failure_message_handler(const CU_pSuite pSuite);
72
static void basic_suite_cleanup_failure_message_handler(const CU_pSuite pSuite);
73

74 75 76
/*=================================================================
 *  Public Interface functions
 *=================================================================*/
77 78 79 80
CU_ErrorCode CU_basic_run_tests(void)
{
  CU_ErrorCode error;

81
  if (NULL == CU_get_registry()) {
82
    if (CU_BRM_SILENT != f_run_mode)
83
      fprintf(stderr, "\n\n%s\n", _("FATAL ERROR - Test registry is not initialized."));
84 85 86
    error = CUE_NOREGISTRY;
  }
  else if (CUE_SUCCESS == (error = basic_initialize()))
87
    error = basic_run_all_tests(NULL);
88 89 90 91 92 93 94 95 96

  return error;
}

/*------------------------------------------------------------------------*/
CU_ErrorCode CU_basic_run_suite(CU_pSuite pSuite)
{
  CU_ErrorCode error;

97
  if (NULL != pSuite)
98 99 100 101 102 103 104 105 106 107 108 109
    error = CUE_NOSUITE;
  else if (CUE_SUCCESS == (error = basic_initialize()))
    error = basic_run_suite(pSuite);

  return error;
}

/*------------------------------------------------------------------------*/
CU_ErrorCode CU_basic_run_test(CU_pSuite pSuite, CU_pTest pTest)
{
  CU_ErrorCode error;

110
  if (NULL != pSuite)
111
    error = CUE_NOSUITE;
112
  else if (NULL != pTest)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    error = CUE_NOTEST;
  else if (CUE_SUCCESS == (error = basic_initialize()))
    error = basic_run_single_test(pSuite, pTest);

  return error;
}

/*------------------------------------------------------------------------*/
void CU_basic_set_mode(CU_BasicRunMode mode)
{
  f_run_mode = mode;
}

/*------------------------------------------------------------------------*/
CU_BasicRunMode CU_basic_get_mode(void)
{
  return f_run_mode;
}

/*------------------------------------------------------------------------*/
void CU_basic_show_failures(CU_pFailureRecord pFailure)
{
  int i;

137 138 139
  for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) {
    fprintf(stdout, "\n  %d. %s:%u  - %s", i,
        (NULL != pFailure->strFileName) ? pFailure->strFileName : "",
140
        pFailure->uiLineNumber,
141
        (NULL != pFailure->strCondition) ? pFailure->strCondition : "");
142 143 144
  }
}

145 146 147 148
/*=================================================================
 *  Static module functions
 *=================================================================*/
/** Performs inialization actions for the basic interface.
149 150 151
 *  This includes setting output to unbuffered, printing a
 *  welcome message, and setting the test run handlers.
 *  @return An error code indicating the framework error condition.
152 153 154 155 156 157 158 159 160 161
 */
static CU_ErrorCode basic_initialize(void)
{
  /* Unbuffered output so everything reaches the screen */
  setvbuf(stdout, NULL, _IONBF, 0);
  setvbuf(stderr, NULL, _IONBF, 0);

  CU_set_error(CUE_SUCCESS);

  if (CU_BRM_SILENT != f_run_mode)
162 163 164 165
    fprintf(stdout, "\n\n     %s" CU_VERSION
                      "\n     %s\n\n",
                    _("CUnit - A unit testing framework for C - Version "),
                    _("http://cunit.sourceforge.net/"));
166 167 168 169 170

  CU_set_test_start_handler(basic_test_start_message_handler);
  CU_set_test_complete_handler(basic_test_complete_message_handler);
  CU_set_all_test_complete_handler(basic_all_tests_complete_message_handler);
  CU_set_suite_init_failure_handler(basic_suite_init_failure_message_handler);
171
  CU_set_suite_cleanup_failure_handler(basic_suite_cleanup_failure_message_handler);
172 173 174 175 176

  return CU_get_error();
}

/*------------------------------------------------------------------------*/
177 178 179 180
/** Runs all tests within the basic interface.
 *  If non-NULL, the test registry is changed to the specified registry 
 *  before running the tests, and reset to the original registry when 
 *  done.  If NULL, the default CUnit test registry will be used.
181 182 183 184
 *  @param pRegistry The CU_pTestRegistry containing the tests
 *                   to be run.  If NULL, use the default registry.
 *  @return An error code indicating the error status
 *          during the test run.
185 186 187 188 189 190 191 192
 */
static CU_ErrorCode basic_run_all_tests(CU_pTestRegistry pRegistry)
{
  CU_pTestRegistry pOldRegistry = NULL;
  CU_ErrorCode result;

  f_pRunningSuite = NULL;

193 194
  if (NULL != pRegistry)
    pOldRegistry = CU_set_registry(pRegistry);
195
  result = CU_run_all_tests();
196 197
  if (NULL != pRegistry)
    CU_set_registry(pOldRegistry);
198 199 200 201
  return result;
}

/*------------------------------------------------------------------------*/
202
/** Runs a specified suite within the basic interface.
203 204 205
 *  @param pSuite The suite to be run (non-NULL).
 *  @return An error code indicating the error status
 *          during the test run.
206 207 208 209 210 211 212 213
 */
static CU_ErrorCode basic_run_suite(CU_pSuite pSuite)
{
  f_pRunningSuite = NULL;
  return CU_run_suite(pSuite);
}

/*------------------------------------------------------------------------*/
214
/** Runs a single test for the specified suite within
215 216 217 218 219
 *  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.
220 221 222 223 224 225 226 227 228
 */
static CU_ErrorCode basic_run_single_test(CU_pSuite pSuite, CU_pTest pTest)
{
  f_pRunningSuite = NULL;
  return CU_run_test(pSuite, pTest);
}

/*------------------------------------------------------------------------*/
/** Handler function called at start of each test.
229 230
 *  @param pTest  The test being run.
 *  @param pSuite The suite containing the test.
231 232 233
 */
static void basic_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite)
{
234 235
  assert(NULL != pSuite);
  assert(NULL != pTest);
236 237

  if (CU_BRM_VERBOSE == f_run_mode) {
238
    assert(NULL != pTest->pName);
239
    if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) {
240 241 242
      assert(NULL != pSuite->pName);
      fprintf(stdout, "\n%s: %s", _("Suite"), pSuite->pName);
      fprintf(stdout, "\n  %s: %s ...", _("Test"), pTest->pName);
243 244 245
      f_pRunningSuite = pSuite;
    }
    else {
246
      fprintf(stdout, "\n  %s: %s ...", _("Test"), pTest->pName);
247 248 249 250 251 252
    }
  }
}

/*------------------------------------------------------------------------*/
/** Handler function called at completion of each test.
253 254 255
 *  @param pTest   The test being run.
 *  @param pSuite  The suite containing the test.
 *  @param pFailure Pointer to the 1st failure record for this test.
256
 */
257 258 259
static void basic_test_complete_message_handler(const CU_pTest pTest, 
                                                const CU_pSuite pSuite, 
                                                const CU_pFailureRecord pFailureList)
260 261 262 263
{
  CU_pFailureRecord pFailure = pFailureList;
  int i;

264 265
  assert(NULL != pSuite);
  assert(NULL != pTest);
266

267 268
  if (NULL == pFailure) {
    if (CU_BRM_VERBOSE == f_run_mode) {
269
      fprintf(stdout, _("passed"));
270
    }
271 272 273 274
  }
  else {
    switch (f_run_mode) {
      case CU_BRM_VERBOSE:
275
        fprintf(stdout, _("FAILED"));
276 277
        break;
      case CU_BRM_NORMAL:
278 279 280
        assert(NULL != pSuite->pName);
        assert(NULL != pTest->pName);
        fprintf(stdout, _("\nSuite %s, Test %s had failures:"), pSuite->pName, pTest->pName);
281 282 283 284 285
        break;
      default:  /* gcc wants all enums covered.  ok. */
        break;
    }
    if (CU_BRM_SILENT != f_run_mode) {
286 287 288
      for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) {
        fprintf(stdout, "\n    %d. %s:%u  - %s", i,
            (NULL != pFailure->strFileName) ? pFailure->strFileName : "",
289
            pFailure->uiLineNumber,
290
            (NULL != pFailure->strCondition) ? pFailure->strCondition : "");
291 292 293 294 295 296 297
      }
    }
  }
}

/*------------------------------------------------------------------------*/
/** Handler function called at completion of all tests in a suite.
298
 *  @param pFailure Pointer to the test failure record list.
299 300 301
 */
static void basic_all_tests_complete_message_handler(const CU_pFailureRecord pFailure)
{
jds2's avatar
jds2 committed
302
  CU_UNREFERENCED_PARAMETER(pFailure); /* not used in basic interface */
303
  CU_print_run_results(stdout);
304 305 306 307
}

/*------------------------------------------------------------------------*/
/** Handler function called when suite initialization fails.
308
 *  @param pSuite The suite for which initialization failed.
309 310 311
 */
static void basic_suite_init_failure_message_handler(const CU_pSuite pSuite)
{
312
  assert(NULL != pSuite);
313
  assert(NULL != pSuite->pName);
314 315

  if (CU_BRM_SILENT != f_run_mode)
316
    fprintf(stdout, _("\nWARNING - Suite initialization failed for '%s'."), pSuite->pName);
317 318
}

319 320
/*------------------------------------------------------------------------*/
/** Handler function called when suite cleanup fails.
321
 *  @param pSuite The suite for which cleanup failed.
322 323 324
 */
static void basic_suite_cleanup_failure_message_handler(const CU_pSuite pSuite)
{
325
  assert(NULL != pSuite);
326
  assert(NULL != pSuite->pName);
327 328

  if (CU_BRM_SILENT != f_run_mode)
329
    fprintf(stdout, _("\nWARNING - Suite cleanup failed for '%s'."), pSuite->pName);
330 331
}

332
/** @} */