Skip to content

Commit

Permalink
[test] Updated test_media_type to use glib test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
monich committed Aug 13, 2020
1 parent a454d94 commit 4d53f8d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 46 deletions.
40 changes: 38 additions & 2 deletions mms-lib/test/common/test_util.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2016 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2016-2020 Jolla Ltd.
* Copyright (C) 2016-2020 Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
Expand Down Expand Up @@ -58,6 +58,42 @@ test_dirs_cleanup(
g_free(dirs->attic);
}

/* Should be invoked after g_test_init */
void
test_init(
TestOpt* opt,
int* argc,
char** argv)
{
int i;

memset(opt, 0, sizeof(*opt));
for (i = 1; i < (*argc); i++) {
const char* arg = argv[i];

if (!strcmp(arg, "-d") || !strcmp(arg, "--debug")) {
opt->flags |= TEST_FLAG_DEBUG;
} else if (!strcmp(arg, "-v")) {
GTestConfig* config = (GTestConfig*)g_test_config_vars;
config->test_verbose = TRUE;
} else {
/* Something that we don't recognize */
continue;
}
/* Remove consumed option from the command line */
(*argc)--;
if (i < (*argc)) {
memmove(argv + i, argv + i + 1, sizeof(char*) * ((*argc) - i));
}
i--;
}

/* Setup logging */
gutil_log_timestamp = FALSE;
gutil_log_default.level = g_test_verbose() ? GLOG_LEVEL_VERBOSE :
GLOG_LEVEL_NONE;
}

/*
* Local Variables:
* mode: C
Expand Down
17 changes: 15 additions & 2 deletions mms-lib/test/common/test_util.h
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2016 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2016-2020 Jolla Ltd.
* Copyright (C) 2016-2020 Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
Expand All @@ -18,6 +18,12 @@

#include "mms_lib_types.h"

typedef struct test_opt {
int flags;
} TestOpt;

#define TEST_FLAG_DEBUG (0x01)

typedef struct test_dirs {
char* root;
char* msg;
Expand All @@ -34,6 +40,13 @@ test_dirs_cleanup(
TestDirs* dirs,
gboolean remove);

/* Should be invoked after g_test_init */
void
test_init(
TestOpt* opt,
int* argc,
char** argv);

#endif /* TEST_UTIL_H */

/*
Expand Down
2 changes: 1 addition & 1 deletion mms-lib/test/test_media_type/Makefile
@@ -1,6 +1,6 @@
# -*- Mode: makefile-gmake -*-

EXE = test_media_type
SRC = $(EXE).c
COMMON_SRC = test_util.c

include ../common/Makefile
76 changes: 35 additions & 41 deletions mms-lib/test/test_media_type/test_media_type.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2013-2016 Jolla Ltd.
* Contact: Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2013-2020 Jolla Ltd.
* Copyright (C) 2013-2020 Slava Monich <slava.monich@jolla.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
Expand All @@ -12,9 +12,14 @@
* GNU General Public License for more details.
*/

#include "test_util.h"

#include "mms_codec.h"

#include <gutil_log.h>

static TestOpt test_opt;

typedef struct test_desc {
const char* name;
const char* input;
Expand Down Expand Up @@ -64,62 +69,51 @@ static const TestDesc media_type_tests[] = {
};

static
gboolean
void
run_test(
const TestDesc* test)
gconstpointer data)
{
gboolean ok = FALSE;
const TestDesc* test = data;
char** parsed = mms_parse_http_content_type(test->input);

if (parsed) {
if (test->output) {
char* unparsed = mms_unparse_http_content_type(parsed);
if (!strcmp(unparsed, test->output)) {
char** p1 = parsed;
char** p2 = test->parsed;
ok = TRUE;
while (*p1 && ok) {
if (*p2) {
ok = !strcmp(*p1++, *p2++);
} else {
ok = FALSE;
break;
}

g_assert(unparsed);
g_assert_cmpstr(unparsed, == ,test->output);
while (*p1) {
g_assert(*p2);
g_assert_cmpstr(*p1, == ,*p2);
p1++;
p2++;
}
if (*p2) ok = FALSE;
g_free(unparsed);
}
}
} else if (!test->output) {
/* Test is expected to fail */
ok = TRUE;
}
g_strfreev(parsed);
GINFO("%s: %s", ok ? "OK" : "FAILED", test->name);
return ok;
}

static
gboolean
run_tests(
const TestDesc* tests,
int count)
{
int i;
gboolean ok = TRUE;
for (i=0; i<count; i++) {
if (!run_test(tests + i)) {
ok = FALSE;
}
} else {
g_assert(!test->output); /* Test is expected to fail */
}
return ok;
}

#define TEST_(x) "/MediaType/" x

int main(int argc, char* argv[])
{
gutil_log_set_type(GLOG_TYPE_STDOUT, "test_media_type");
gutil_log_timestamp = FALSE;
gutil_log_default.level = GLOG_LEVEL_INFO;
return !run_tests(media_type_tests, G_N_ELEMENTS(media_type_tests));
guint i;

g_test_init(&argc, &argv, NULL);
test_init(&test_opt, &argc, argv);
for (i = 0; i < G_N_ELEMENTS(media_type_tests); i++) {
const TestDesc* test = media_type_tests + i;
char* name = g_strdup_printf(TEST_("%s"), test->name);

g_test_add_data_func(name, test, run_test);
g_free(name);
}
return g_test_run();
}

/*
Expand Down

0 comments on commit 4d53f8d

Please sign in to comment.