Skip to content

Commit

Permalink
test: make libmpv_test abort if an error is logged
Browse files Browse the repository at this point in the history
Among others this provides a trivial test that built-in scripts aren't throwing
an error at load or init time.
  • Loading branch information
sfan5 committed Apr 24, 2024
1 parent d255f31 commit 426be84
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions test/libmpv_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ static double double_ = 1.5;
// Global handle.
static mpv_handle *ctx;


MP_NORETURN PRINTF_ATTRIBUTE(1, 2)
static void fail(const char *fmt, ...)
{
Expand All @@ -56,16 +55,42 @@ static void fail(const char *fmt, ...)
vfprintf(stderr, fmt, va);
va_end(va);
}
mpv_destroy(ctx);
exit(1);
}


static void exit_cleanup(void)
{
if (ctx)
mpv_destroy(ctx);
}

static mpv_event *wrap_wait_event(void)
{
while (1) {
mpv_event *ev = mpv_wait_event(ctx, 1);

if (ev->event_id == MPV_EVENT_NONE) {
continue;
} else if (ev->event_id == MPV_EVENT_LOG_MESSAGE) {
mpv_event_log_message *msg = (mpv_event_log_message*)ev->data;
printf("[%s:%s] %s", msg->prefix, msg->level, msg->text);
if (msg->log_level <= MPV_LOG_LEVEL_ERROR)
fail("error was logged");
} else {
return ev;
}
}
}

static void check_api_error(int status)
{
if (status < 0)
fail("mpv API error: %s\n", mpv_error_string(status));
}

/****/

static void check_double(const char *property, double expect)
{
double result_double;
Expand Down Expand Up @@ -144,14 +169,16 @@ static void set_options_and_properties(const char *options[], const char *proper
}
}

/****/

static void test_file_loading(char *file)
{
const char *cmd[] = {"loadfile", file, NULL};
check_api_error(mpv_command(ctx, cmd));
int loaded = 0;
int finished = 0;
while (!finished) {
mpv_event *event = mpv_wait_event(ctx, 0);
mpv_event *event = wrap_wait_event();
switch (event->event_id) {
case MPV_EVENT_FILE_LOADED:
// make sure it loads before exiting
Expand All @@ -174,7 +201,7 @@ static void test_lavfi_complex(char *file)
int finished = 0;
int loaded = 0;
while (!finished) {
mpv_event *event = mpv_wait_event(ctx, 0);
mpv_event *event = wrap_wait_event();
switch (event->event_id) {
case MPV_EVENT_FILE_LOADED:
// Add file as external and toggle lavfi-complex on.
Expand Down Expand Up @@ -248,14 +275,16 @@ int main(int argc, char *argv[])
{
if (argc != 2)
return 1;
atexit(exit_cleanup);

ctx = mpv_create();
if (!ctx)
return 1;

check_api_error(mpv_set_option_string(ctx, "vo", "null"));
check_api_error(mpv_set_option_string(ctx, "terminal", "yes"));
check_api_error(mpv_set_option_string(ctx, "msg-level", "all=debug"));
// load osc too to see if it works
check_api_error(mpv_set_option_string(ctx, "osc", "yes"));
check_api_error(mpv_request_log_messages(ctx, "debug"));

const char *fmt = "================ TEST: %s ================\n";

Expand All @@ -266,6 +295,9 @@ int main(int argc, char *argv[])
printf(fmt, "test_lavfi_complex");
test_lavfi_complex(argv[1]);

mpv_destroy(ctx);
printf("================ SHUTDOWN ================\n");
mpv_command_string(ctx, "quit");
while (wrap_wait_event()->event_id != MPV_EVENT_SHUTDOWN) {}

return 0;
}

0 comments on commit 426be84

Please sign in to comment.