X-Git-Url: https://git.llucax.com/software/mutest.git/blobdiff_plain/5ce20f06cfd85f7498652f86a4111ed6c456b9f9..a894239e9a62676c861bdd817714b5f8b3c05265:/mutest.h?ds=sidebyside diff --git a/mutest.h b/mutest.h index 1f5561d..c84eb15 100644 --- a/mutest.h +++ b/mutest.h @@ -1,3 +1,18 @@ +/* + * This file is part of mutest, a simple micro unit testing framework for C. + * + * mutest was written by Leandro Lucarella and is released + * under the BOLA license, please see the LICENSE file or visit: + * http://blitiri.com.ar/p/bola/ + * + * This header file should be included in the source files that will make up + * a test suite. It's used for both C and Python implementation, but when + * using the Python implementation you should define the MUTEST_PY macro. + * If you implement your mu_run_suites() function yourself, you probably will + * need to include this header too (see mkmutest). + * + * Please, read the README file for more details. + */ #include /* fprintf() */ @@ -5,6 +20,27 @@ extern "C" { #endif +/* verbosity level (each level shows all the previous levels too) */ +enum { + MU_QUIET = 0, /* be completely quiet */ + MU_ERROR, /* shows errors only */ + MU_SUMMARY, /* shows a summary */ + MU_SUITE, /* shows test suites progress */ + MU_CASE, /* shows test cases progress */ + MU_CHECK /* shows the current running check */ +}; + +/* print a message according to the verbosity level */ +#define mu_print(level, ...) \ + do { \ + if (mutest_verbose_level >= level) { \ + if (mutest_verbose_level == MU_ERROR) \ + fprintf(stderr, __VA_ARGS__); \ + else \ + fprintf(stdout, __VA_ARGS__); \ + } \ + } while (0) + /* check that an expression evaluates to true, continue if the check fails */ #define mu_check(exp) \ do { \ @@ -37,32 +73,7 @@ extern "C" { } \ } while (0) -/* - * you don't need to pay any attention to what's next, unless you want to do - * some customization, of course, in which case, you're encouraged to take - * a look an play =) - */ - -/* verbosity level (each level shows all the previous levels too) */ -enum { - MU_QUIET = 0, /* be completely quiet */ - MU_ERROR, /* shows errors only */ - MU_SUMMARY, /* shows a summary */ - MU_SUITE, /* shows test suites progress */ - MU_CASE, /* shows test cases progress */ - MU_CHECK /* shows the current running check */ -}; - -/* print a message according to the verbosity level */ -#define mu_print(level, ...) \ - do { \ - if (mutest_verbose_level >= level) { \ - if (mutest_verbose_level == MU_ERROR) \ - fprintf(stderr, __VA_ARGS__); \ - else \ - fprintf(stdout, __VA_ARGS__); \ - } \ - } while (0) +#ifndef MUTEST_PY /* we are using the C implementation */ /* * this function implements the test suites execution, you should generate @@ -70,13 +81,32 @@ enum { * if you want to implement your own customized version */ void mu_run_suites(); +/* macro for running a single initialization function */ +#ifndef mu_run_init +#define mu_run_init(name) \ + { \ + int name(); \ + int r; \ + mu_print(MU_CASE, "\t+ Executing initialization function " \ + "'" #name "'...\n"); \ + if ((r = name())) { \ + mu_print(MU_ERROR, "%s:" #name ": initialization " \ + "function failed (returned %d), " \ + "skipping test suite...\n", \ + mutest_suite_name, r); \ + ++mutest_skipped_suites; \ + break; \ + } \ + } do { } while (0) +#endif /* mu_run_init */ + /* macro for running a single test case */ #ifndef mu_run_case #define mu_run_case(name) \ do { \ - mu_print(MU_CASE, "\t- executing test case \"" #name "\"\n"); \ - void name(); \ + mu_print(MU_CASE, "\t* Executing test case '" #name "'...\n");\ mutest_case_name = #name; \ + void name(); \ name(); \ if (mutest_case_failed) { \ ++mutest_failed_cases; \ @@ -86,6 +116,17 @@ void mu_run_suites(); } while (0) #endif /* mu_run_case */ +/* macro for running a single termination function */ +#ifndef mu_run_term +#define mu_run_term(name) \ + do { \ + mu_print(MU_CASE, "\t- Executing termination function '" \ + #name "'...\n"); \ + void name(); \ + name(); \ + } while (0) +#endif /* mu_run_term */ + /* * mutest exported variables for internal use, do not use directly unless you * know what you're doing. @@ -93,6 +134,7 @@ void mu_run_suites(); extern const char* mutest_suite_name; extern int mutest_failed_suites; extern int mutest_passed_suites; +extern int mutest_skipped_suites; extern int mutest_suite_failed; /* test cases */ extern const char* mutest_case_name; @@ -105,6 +147,27 @@ extern int mutest_passed_checks; /* verbosity */ extern int mutest_verbose_level; +#else /* MUTEST_PY is defined, using the Python implementation */ + +/* this increments when the "API" changes, it's just for sanity check */ +int mutest_api_version = 1; + +int mutest_case_failed; /* unused, for C implementation compatibility */ + +int mutest_passed_checks; +int mutest_failed_checks; +void mutest_reset_counters() { + mutest_passed_checks = 0; + mutest_failed_checks = 0; +} + +int mutest_verbose_level = MU_ERROR; +void mutest_set_verbose_level(int val) { + mutest_verbose_level = val; +} + +#endif /* MUTEST_PY */ + #ifdef __cplusplus } #endif