s2tcase.c 2.94 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2009 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "config.h"

#if HAVE_CHECK

29 30 31 32
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
33
#include <assert.h>
34 35 36

#include <check.h>

Pekka Pessi's avatar
Pekka Pessi committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#if HAVE_FNMATCH_H
#include <fnmatch.h>
#endif

static char const * const default_patterns[] = { "*", NULL };
static char const * const *test_patterns = default_patterns;

/** tcase_add_test() replacement.
 *
 * A special version of tcase_add_test() that inserts test function into
 * tcase only if its name matches given pattern.
 */
void s2_tcase_add_test(TCase *tc, TFun tf, char const *name)
{
  char const * const *patterns;

#if HAVE_FNMATCH_H
  for (patterns = test_patterns; *patterns; patterns++) {
    if (!fnmatch(*patterns, name, 0)) {
      if (strcmp(*patterns, "*")) {
	printf("%s: running\n", name);
      }
59
      _tcase_add_test(tc, tf, name, 0, 0, 1);
Pekka Pessi's avatar
Pekka Pessi committed
60 61 62 63 64 65 66 67 68
      return;
    }
  }
#else
  for (patterns = test_patterns; *patterns; patterns++) {
    if (!strcmp(*patterns, name) || !strcmp(*patterns, "*")) {
      if (strcmp(*patterns, "*")) {
	printf("%s: running\n", name);
      }
69
      _tcase_add_test(tc, tf, name, 0, 0, 1);
Pekka Pessi's avatar
Pekka Pessi committed
70 71 72 73 74 75 76 77 78 79 80 81 82
      return;
    }
  }
#endif
}

/** Select tests based on pattern.
 *
 * Allow easy selection of test cases to run. Pattern can be a shell glob,
 * or consists of multiple comma-separated patterns.
 */
void s2_select_tests(char const *pattern)
{
83
  size_t i, n;
Pekka Pessi's avatar
Pekka Pessi committed
84 85 86 87 88 89 90 91 92 93 94
  char *cases, *s, **patterns;

  if (!pattern)
    return;

  cases = strdup(pattern);

  /* Count commas */
  for (i = 2, s = cases; (s = strchr(s, ',')); s++)
    i++;

95 96 97 98 99 100 101 102 103 104 105 106 107
  if (test_patterns != default_patterns) {
    patterns = (char **)test_patterns;
    for (n = 0; patterns[n]; n++)
      ;
    patterns = realloc(patterns, (n + i) * (sizeof *patterns));
  }
  else {
    n = 0;
    patterns = malloc(i * (sizeof *patterns));
  }

  assert(patterns);
  memset(&patterns[n], 0, i * (sizeof *patterns));
Pekka Pessi's avatar
Pekka Pessi committed
108 109

  /* Split by commas */
110
  for (i = n, s = cases;; i++) {
Pekka Pessi's avatar
Pekka Pessi committed
111 112 113 114 115 116 117 118 119 120 121 122
    patterns[i] = s;
    if (s == NULL)
      break;
    s = strchr(s, ',');
    if (s)
      *s++ = '\0';
  }

  test_patterns = (char const * const *)patterns;
}

#endif	/* HAVE_CHECK */