From 669059db08d79ad942b1610ee7ae48405d74cfb5 Mon Sep 17 00:00:00 2001 From: Vladimir Davydov <vdavydov@tarantool.org> Date: Thu, 24 Aug 2023 14:29:02 +0300 Subject: [PATCH] test: eliminate code duplication in unit test helpers There's no need to duplicate all unit test helpers for TAP compatible tests. The only difference between them is that plan() prints the TAP version so let's do just that. NO_DOC=code cleanup NO_CHANGELOG=code cleanup (cherry picked from commit a235080c648e313131dfb465f15a66b67c974414) --- test/unit/unit.c | 6 ++- test/unit/unit.h | 110 +++++------------------------------------------ 2 files changed, 15 insertions(+), 101 deletions(-) diff --git a/test/unit/unit.c b/test/unit/unit.c index d12b66f915..38326793d9 100644 --- a/test/unit/unit.c +++ b/test/unit/unit.c @@ -1,5 +1,6 @@ #include "unit.h" +#include <stdbool.h> #include <stdio.h> #include <stdarg.h> @@ -19,13 +20,16 @@ _space(FILE *stream) } void -plan(int count) +_plan(int count, bool tap) { ++level; plan_test[level] = count; tests_done[level] = 0; tests_failed[level] = 0; + if (tap && level == 0) + printf("TAP version 13\n"); + _space(stdout); printf("%d..%d\n", 1, plan_test[level]); } diff --git a/test/unit/unit.h b/test/unit/unit.h index bd1c194ee9..77d0847b56 100644 --- a/test/unit/unit.h +++ b/test/unit/unit.h @@ -30,6 +30,7 @@ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> /* exit() */ @@ -66,16 +67,8 @@ extern "C" { return check_plan(); // print resume } @endcode - - */ -#if !UNIT_TAP_COMPATIBLE - -#define header() printf("\t*** %s ***\n", __func__) -#define footer() printf("\t*** %s: done ***\n", __func__) - - /* private function, use ok(...) instead */ int _ok(int condition, const char *fmt, ...); @@ -89,15 +82,14 @@ Before anything else, you need a testing plan. This basically declares how many tests your program is going to run to protect against premature failure. */ -void plan(int count); +void +_plan(int count, bool tap); /** @brief check if plan is reached and print report */ int check_plan(void); -#endif - #define ok(condition, fmt, args...) { \ int res = _ok(condition, fmt, ##args); \ if (!res) { \ @@ -136,10 +128,8 @@ int check_plan(void); #if UNIT_TAP_COMPATIBLE -#include <stdarg.h> /* va_start(), va_end() */ - #define header() \ - do { \ + do { \ _space(stdout); \ printf("# *** %s ***\n", __func__); \ } while (0) @@ -150,95 +140,15 @@ int check_plan(void); printf("# *** %s: done ***\n", __func__); \ } while (0) -enum { MAX_LEVELS = 10 }; - -static int tests_done[MAX_LEVELS]; -static int tests_failed[MAX_LEVELS]; -static int plan_test[MAX_LEVELS]; +#define plan(count) _plan(count, true) -static int level = -1; - -/** - * private function, use note(...) or diag(...) instead - */ -static inline void -_space(FILE *stream) -{ - for (int i = 0; i < level; i++) { - fprintf(stream, " "); - } -} +#else /* !UNIT_TAP_COMPATIBLE */ -/** - * private function, use ok(...) instead - */ -static inline int -_ok(int condition, const char *fmt, ...) -{ - va_list ap; - - _space(stdout); - printf("%s %d - ", condition ? "ok" : "not ok", ++tests_done[level]); - if (!condition) - tests_failed[level]++; - va_start(ap, fmt); - vprintf(fmt, ap); - printf("\n"); - va_end(ap); - return condition; -} - -/** - * @brief set and print plan - * @param count - * Before anything else, you need a testing plan. This basically declares - * how many tests your program is going to run to protect against premature - * failure. - */ -static inline void -plan(int count) -{ - ++level; - plan_test[level] = count; - tests_done[level] = 0; - tests_failed[level] = 0; - - if (level == 0) - printf("TAP version 13\n"); - - _space(stdout); - printf("%d..%d\n", 1, plan_test[level]); -} - -/** - * @brief check if plan is reached and print report - */ -static inline int -check_plan(void) -{ - int r = 0; - if (tests_done[level] != plan_test[level]) { - _space(stderr); - fprintf(stderr, - "# Looks like you planned %d tests but ran %d.\n", - plan_test[level], tests_done[level]); - r = -1; - } - - if (tests_failed[level]) { - _space(stderr); - fprintf(stderr, "# Looks like you failed %d test of %d run.\n", - tests_failed[level], tests_done[level]); - r = tests_failed[level]; - } - --level; - if (level >= 0) { - is(r, 0, "subtests"); - } - return r; -} +#define header() printf("\t*** %s ***\n", __func__) +#define footer() printf("\t*** %s: done ***\n", __func__) +#define plan(count) _plan(count, false) -#endif +#endif /* !UNIT_TAP_COMPATIBLE */ #if defined(__cplusplus) } -- GitLab