Skip to content
Snippets Groups Projects
Commit 56cf07ee authored by Vladimir Davydov's avatar Vladimir Davydov
Browse files

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 a235080c)
parent d74dd308
No related branches found
No related tags found
No related merge requests found
......@@ -82,7 +82,7 @@ create_unit_test(PREFIX rope_avl
)
create_unit_test(PREFIX rope_stress
SOURCES rope_stress.c
LIBRARIES salad
LIBRARIES salad unit
)
create_unit_test(PREFIX rope
SOURCES rope.c
......
#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]);
}
......
......@@ -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)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment