test: just always provide a context for all entrypoints

This commit is contained in:
wm4 2019-11-08 14:21:40 +01:00
parent 53b7a10f54
commit 3ed9c1c970
6 changed files with 24 additions and 19 deletions

View File

@ -30,7 +30,7 @@ static void test_sel(const char *input, const char *expected_selection,
mp_chmap_to_str(&expected_map)); mp_chmap_to_str(&expected_map));
} }
static void run(void) static void run(struct test_ctx *ctx)
{ {
struct mp_chmap a; struct mp_chmap a;
struct mp_chmap b; struct mp_chmap b;
@ -89,5 +89,5 @@ static void run(void)
const struct unittest test_chmap = { const struct unittest test_chmap = {
.name = "chmap", .name = "chmap",
.run_simple = run, .run = run,
}; };

View File

@ -2,7 +2,7 @@
#include "test_helpers.h" #include "test_helpers.h"
#include "video/out/gpu/video.h" #include "video/out/gpu/video.h"
static void run(void) static void run(struct test_ctx *ctx)
{ {
float x; float x;
@ -26,5 +26,5 @@ static void run(void)
const struct unittest test_gl_video = { const struct unittest test_gl_video = {
.name = "gl_video", .name = "gl_video",
.run_simple = run, .run = run,
}; };

View File

@ -22,6 +22,11 @@ bool run_tests(struct MPContext *mpctx)
return true; return true;
} }
struct test_ctx ctx = {
.global = mpctx->global,
.log = mpctx->log,
};
int num_run = 0; int num_run = 0;
for (int n = 0; unittests[n]; n++) { for (int n = 0; unittests[n]; n++) {
@ -29,18 +34,15 @@ bool run_tests(struct MPContext *mpctx)
// Exactly 1 entrypoint please. // Exactly 1 entrypoint please.
assert(MP_IS_POWER_OF_2( assert(MP_IS_POWER_OF_2(
(t->run_simple ? (1 << 0) : 0) |
(t->run ? (1 << 1) : 0))); (t->run ? (1 << 1) : 0)));
bool run = false; bool run = false;
run |= strcmp(sel, "all-simple") == 0 && !!t->run_simple; run |= strcmp(sel, "all-simple") == 0 && !t->is_complex;
run |= strcmp(sel, t->name); run |= strcmp(sel, t->name);
if (run) { if (run) {
if (t->run_simple)
t->run_simple();
if (t->run) if (t->run)
t->run(mpctx->global, mpctx->log); t->run(&ctx);
num_run++; num_run++;
} }
} }

View File

@ -2,25 +2,28 @@
#include <stdbool.h> #include <stdbool.h>
struct mpv_global;
struct mp_log;
struct MPContext; struct MPContext;
bool run_tests(struct MPContext *mpctx); bool run_tests(struct MPContext *mpctx);
struct test_ctx {
struct mpv_global *global;
struct mp_log *log;
};
struct unittest { struct unittest {
// This is used to select the test on command line with --unittest=<name>. // This is used to select the test on command line with --unittest=<name>.
const char *name; const char *name;
// Cannot run without additional arguments supplied.
bool is_complex;
// Entrypoints. There are various for various purposes. Only 1 of them must // Entrypoints. There are various for various purposes. Only 1 of them must
// be set. // be set.
// Entrypoint for tests which don't depend on the mpv core.
void (*run_simple)(void);
// Entrypoint for tests which have a simple dependency on the mpv core. The // Entrypoint for tests which have a simple dependency on the mpv core. The
// core is sufficiently initialized at this point. // core is sufficiently initialized at this point.
void (*run)(struct mpv_global *global, struct mp_log *log); void (*run)(struct test_ctx *ctx);
}; };
extern const struct unittest test_chmap; extern const struct unittest test_chmap;

View File

@ -67,7 +67,7 @@ static const struct entry entries[] = {
#define MAX_DEPTH 10 #define MAX_DEPTH 10
static void run(void) static void run(struct test_ctx *ctx)
{ {
for (int n = 0; n < MP_ARRAY_SIZE(entries); n++) { for (int n = 0; n < MP_ARRAY_SIZE(entries); n++) {
const struct entry *e = &entries[n]; const struct entry *e = &entries[n];
@ -91,5 +91,5 @@ static void run(void)
const struct unittest test_json = { const struct unittest test_json = {
.name = "json", .name = "json",
.run_simple = run, .run = run,
}; };

View File

@ -56,7 +56,7 @@ static bool do_check_list(struct the_list *lst, int *c, int num_c)
return true; return true;
} }
static void run(void) static void run(struct test_ctx *ctx)
{ {
struct the_list lst = {0}; struct the_list lst = {0};
struct list_item e1 = {1}; struct list_item e1 = {1};
@ -161,5 +161,5 @@ static void run(void)
const struct unittest test_linked_list = { const struct unittest test_linked_list = {
.name = "linked_list", .name = "linked_list",
.run_simple = run, .run = run,
}; };