From: Adam Dickmeiss Date: Fri, 27 Jan 2006 18:58:57 +0000 (+0000) Subject: A little more flexible test unit X-Git-Tag: YAZ.2.1.12~17 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=886c6715463eba74b37575caa5df206ee9b970b6;p=yaz-moved-to-github.git A little more flexible test unit --- diff --git a/include/yaz/test.h b/include/yaz/test.h index 93ecb06..871e05c 100644 --- a/include/yaz/test.h +++ b/include/yaz/test.h @@ -2,7 +2,7 @@ * Copyright (C) 1995-2005, Index Data ApS * See the file LICENSE for details. * - * $Id: test.h,v 1.1 2006-01-27 17:30:44 adam Exp $ + * $Id: test.h,v 1.2 2006-01-27 18:58:57 adam Exp $ */ #ifndef YAZ_TEST_H @@ -11,18 +11,26 @@ #include #include -#define YAZ_CHECK_INIT(thename) \ -static char *yaz_unit_test_name = #thename; -static FILE *yaz_unit_file = 0; \ -static int yaz_unit_test_number = 0; \ +#define YAZ_TEST_TYPE_OK 1 +#define YAZ_TEST_TYPE_FAIL 2 -#define YAZ_CHECK(as) \ -yaz_unit_test_number++; \ -if (!yaz_unit_file) yaz_unit_file = stderr; \ -if (!as) { \ - fprintf(yaz_unit_file, "%s:%d test failed: %s\n", __FILE__, __LINE__, #as); \ +#define YAZ_CHECK(as) { \ + if (as) { \ + yaz_check_print1(YAZ_TEST_TYPE_OK, __FILE__, __LINE__, #as); \ + } else { \ + yaz_check_print1(YAZ_TEST_TYPE_FAIL, __FILE__, __LINE__, #as); \ + } \ } +#define YAZ_CHECK_INIT(argc, argv) yaz_check_init1(&argc, &argv) + +YAZ_BEGIN_CDECL +YAZ_EXPORT void yaz_check_init1(int *argc, char ***argv); +YAZ_EXPORT void yaz_check_print1(int type, const char *file, int line, + const char *expr); +YAZ_END_CDECL + + #endif /* * Local variables: diff --git a/src/Makefile.am b/src/Makefile.am index 2195611..5f4989c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,6 @@ ## This file is part of the YAZ toolkit. ## Copyright (C) 1994-2005, Index Data, All rights reserved. -## $Id: Makefile.am,v 1.26 2006-01-27 17:31:37 adam Exp $ +## $Id: Makefile.am,v 1.27 2006-01-27 18:58:58 adam Exp $ YAZ_VERSION_INFO=2:0:0 @@ -64,7 +64,7 @@ libyaz_la_SOURCES=version.c options.c log.c marcdisp.c oid.c wrbuf.c \ cql.y cqlstdio.c cqltransform.c cqlutil.c xcqlutil.c cqlstring.c \ cqlstrer.c querytowrbuf.c \ eventl.c seshigh.c statserv.c requestq.c tcpdchk.c \ - eventl.h service.c service.h session.h \ + eventl.h service.c service.h session.h test.c \ xmlquery.c libyaz_la_LDFLAGS=-version-info $(YAZ_VERSION_INFO) diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..79083d1 --- /dev/null +++ b/src/test.c @@ -0,0 +1,103 @@ +/* + * Copyright (C) 1995-2005, Index Data ApS + * See the file LICENSE for details. + * + * $Id: test.c,v 1.1 2006-01-27 18:58:58 adam Exp $ + */ +#if HAVE_CONFIG_H +#include +#endif + +#include +#include + +#include + +static FILE *test_fout; +static int test_number = 0; +static int test_verbose = 0; + +void yaz_check_init1(int *argc_p, char ***argv_p) +{ + int i = 0; + int argc = *argc_p; + char **argv = *argv_p; + + test_fout = 0; + for (i = 1; i= 7 && !memcmp(argv[i], "--test-", 7)) + { + const char *suf = argv[i]+7; + if (i < argc-1 && !strcmp(suf, "file")) + { + i++; + if (test_fout) + fclose(test_fout); + test_fout = fopen(argv[i], "w"); + continue; + } + else if (i < argc-1 && !strcmp(suf, "verbose")) + { + i++; + test_verbose = atoi(argv[i]); + continue; + } + else if (!strcmp(suf, "help")) + { + fprintf(stderr, + "--test-help help\n" + "--test-verbose level verbose; 0=quiet; 1=normal; 2=more\n" + "--test-file fname output to fname\n"); + exit(0); + } + else + { + fprintf(stderr, "Unrecognized option for YAZ test: %s\n", + argv[i]); + fprintf(stderr, "Use --test-help for more info\n"); + exit(1); + } + + } + break; + } + /* remove --test- options from argc, argv so that they disappear */ + (*argv_p)[i-1] = **argv_p; /* program name */ + --i; + *argc_p -= i; + *argv_p += i; + if (!test_fout) + test_fout = stdout; +} + +void yaz_check_print1(int type, const char *file, int line, const char *expr) +{ + const char *msg = "unknown"; + + test_number++; + switch(type) + { + case YAZ_TEST_TYPE_FAIL: + msg = "failed"; + if (test_verbose < 1) + return; + break; + case YAZ_TEST_TYPE_OK: + msg = "OK"; + if (test_verbose < 2) + return; + break; + } + fprintf(test_fout, "%s:%d %s: %s\n", file, line, msg, expr); +} + + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/test/tstxmlquery.c b/test/tstxmlquery.c index c72343e..c84d1e9 100644 --- a/test/tstxmlquery.c +++ b/test/tstxmlquery.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2005, Index Data ApS * See the file LICENSE for details. * - * $Id: tstxmlquery.c,v 1.1 2006-01-27 17:33:15 adam Exp $ + * $Id: tstxmlquery.c,v 1.2 2006-01-27 18:58:58 adam Exp $ */ #include @@ -40,6 +40,8 @@ static void pqf2xml_text(const char *pqf) int main (int argc, char **argv) { + YAZ_CHECK_INIT(argc, argv); + pqf2xml_text("@attr 1=4 computer"); exit(0);