Commit 4d53f8d3 authored by Slava Monich's avatar Slava Monich

[test] Updated test_media_type to use glib test framework

parent a454d94d
/*
* 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
......@@ -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
......
/*
* 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
......@@ -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;
......@@ -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 */
/*
......
# -*- Mode: makefile-gmake -*-
EXE = test_media_type
SRC = $(EXE).c
COMMON_SRC = test_util.c
include ../common/Makefile
/*
* 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
......@@ -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;
......@@ -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;
}
}
if (*p2) ok = FALSE;
g_free(unparsed);
char** p1 = parsed;
char** p2 = test->parsed;
g_assert(unparsed);
g_assert_cmpstr(unparsed, == ,test->output);
while (*p1) {
g_assert(*p2);
g_assert_cmpstr(*p1, == ,*p2);
p1++;
p2++;
}
g_free(unparsed);
}
} else if (!test->output) {
/* Test is expected to fail */
ok = TRUE;
g_strfreev(parsed);
} else {
g_assert(!test->output); /* Test is expected to fail */
}
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;
}
}
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();
}
/*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment