1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-28 10:02:17 +00:00
mpv/test/json.c

95 lines
3.4 KiB
C
Raw Normal View History

#include "common/common.h"
#include "misc/json.h"
#include "misc/node.h"
#include "tests.h"
struct entry {
const char *src;
const char *out_txt;
struct mpv_node out_data;
bool expect_fail;
};
#define TEXT(...) #__VA_ARGS__
#define VAL_LIST(...) (struct mpv_node[]){__VA_ARGS__}
#define L(...) __VA_ARGS__
#define NODE_INT64(v) {.format = MPV_FORMAT_INT64, .u = { .int64 = (v) }}
#define NODE_STR(v) {.format = MPV_FORMAT_STRING, .u = { .string = (v) }}
#define NODE_BOOL(v) {.format = MPV_FORMAT_FLAG, .u = { .flag = (bool)(v) }}
#define NODE_FLOAT(v) {.format = MPV_FORMAT_DOUBLE, .u = { .double_ = (v) }}
#define NODE_NONE() {.format = MPV_FORMAT_NONE }
#define NODE_ARRAY(...) {.format = MPV_FORMAT_NODE_ARRAY, .u = { .list = \
&(struct mpv_node_list) { \
.num = sizeof(VAL_LIST(__VA_ARGS__)) / sizeof(struct mpv_node), \
.values = VAL_LIST(__VA_ARGS__)}}}
#define NODE_MAP(k, v) {.format = MPV_FORMAT_NODE_MAP, .u = { .list = \
&(struct mpv_node_list) { \
.num = sizeof(VAL_LIST(v)) / sizeof(struct mpv_node), \
.values = VAL_LIST(v), \
.keys = (char**)(const char *[]){k}}}}
static const struct entry entries[] = {
{ "null", "null", NODE_NONE()},
{ "true", "true", NODE_BOOL(true)},
{ "false", "false", NODE_BOOL(false)},
{ "", .expect_fail = true},
{ "abc", .expect_fail = true},
{ " 123 ", "123", NODE_INT64(123)},
{ "123.25", "123.250000", NODE_FLOAT(123.25)},
{ TEXT("a\n\\\/\\\""), TEXT("a\n\\/\\\""), NODE_STR("a\n\\/\\\"")},
{ TEXT("a\u2c29"), TEXT("aⰩ"), NODE_STR("a\342\260\251")},
{ "[1,2,3]", "[1,2,3]",
NODE_ARRAY(NODE_INT64(1), NODE_INT64(2), NODE_INT64(3))},
{ "[ ]", "[]", NODE_ARRAY()},
{ "[1,,2]", .expect_fail = true},
{ "[,]", .expect_fail = true},
{ TEXT({"a":1, "b":2}), TEXT({"a":1,"b":2}),
NODE_MAP(L("a", "b"), L(NODE_INT64(1), NODE_INT64(2)))},
{ "{ }", "{}", NODE_MAP(L(), L())},
{ TEXT({"a":b}), .expect_fail = true},
{ TEXT({1a:"b"}), .expect_fail = true},
// non-standard extensions
{ "[1,2,]", "[1,2]", NODE_ARRAY(NODE_INT64(1), NODE_INT64(2))},
{ TEXT({a:"b"}), TEXT({"a":"b"}),
NODE_MAP(L("a"), L(NODE_STR("b")))},
{ TEXT({a="b"}), TEXT({"a":"b"}),
NODE_MAP(L("a"), L(NODE_STR("b")))},
{ TEXT({a ="b"}), TEXT({"a":"b"}),
NODE_MAP(L("a"), L(NODE_STR("b")))},
{ TEXT({_a12="b"}), TEXT({"_a12":"b"}),
NODE_MAP(L("_a12"), L(NODE_STR("b")))},
};
#define MAX_DEPTH 10
static void run(struct test_ctx *ctx)
{
for (int n = 0; n < MP_ARRAY_SIZE(entries); n++) {
const struct entry *e = &entries[n];
void *tmp = talloc_new(NULL);
char *s = talloc_strdup(tmp, e->src);
json_skip_whitespace(&s);
struct mpv_node res;
bool ok = json_parse(tmp, &res, &s, MAX_DEPTH) >= 0;
assert_true(ok != e->expect_fail);
test: make tests part of the mpv binary Until now, each .c file in test/ was built as separate, self-contained binary. Each binary could be run to execute the tests it contained. Change this and make them part of the normal mpv binary. Now the tests have to be invoked via the --unittest option. Do this for two reasons: - Tests now run within a "properly" initialized mpv instance, so all services are available. - Possibly simplifying the situation for future build systems. The first point is the main motivation. The mpv code is entangled with mp_log and the option system. It feels like a bad idea to duplicate some of the initialization of this just so you can call code using them. I'm also getting rid of cmocka. There wouldn't be any problem to keep it (it's a perfectly sane set of helpers), but NIH calls. I would have had to aggregate all tests into a CMUnitTest list, and I don't see how I'd get different types of entry points easily. Probably easily solvable, but since we made only pretty basic use of this library, NIH-ing this is actually easier (I needed a list of tests with custom metadata anyway, so all what was left was reimplement the assert_* helpers). Unit tests now don't output anything, and if they fail, they'll simply crash and leave a message that typically requires inspecting the test code to figure out what went wrong (and probably editing the test code to get more information). I even merged the various test functions into single ones. Sucks, but here you go. chmap_sel.c is merged into chmap.c, because I didn't see the point of this being separate. json.c drops the print_message() to go along with the new silent-by-default idea, also there's a memory leak fix unrelated to the rest of this commit. The new code is enabled with --enable-tests (--enable-test goes away). Due to waf's option parser, --enable-test still works, because it's a unique prefix to --enable-tests.
2019-11-07 21:42:14 +00:00
if (!ok) {
talloc_free(tmp);
continue;
test: make tests part of the mpv binary Until now, each .c file in test/ was built as separate, self-contained binary. Each binary could be run to execute the tests it contained. Change this and make them part of the normal mpv binary. Now the tests have to be invoked via the --unittest option. Do this for two reasons: - Tests now run within a "properly" initialized mpv instance, so all services are available. - Possibly simplifying the situation for future build systems. The first point is the main motivation. The mpv code is entangled with mp_log and the option system. It feels like a bad idea to duplicate some of the initialization of this just so you can call code using them. I'm also getting rid of cmocka. There wouldn't be any problem to keep it (it's a perfectly sane set of helpers), but NIH calls. I would have had to aggregate all tests into a CMUnitTest list, and I don't see how I'd get different types of entry points easily. Probably easily solvable, but since we made only pretty basic use of this library, NIH-ing this is actually easier (I needed a list of tests with custom metadata anyway, so all what was left was reimplement the assert_* helpers). Unit tests now don't output anything, and if they fail, they'll simply crash and leave a message that typically requires inspecting the test code to figure out what went wrong (and probably editing the test code to get more information). I even merged the various test functions into single ones. Sucks, but here you go. chmap_sel.c is merged into chmap.c, because I didn't see the point of this being separate. json.c drops the print_message() to go along with the new silent-by-default idea, also there's a memory leak fix unrelated to the rest of this commit. The new code is enabled with --enable-tests (--enable-test goes away). Due to waf's option parser, --enable-test still works, because it's a unique prefix to --enable-tests.
2019-11-07 21:42:14 +00:00
}
char *d = talloc_strdup(tmp, "");
assert_true(json_write(&d, &res) >= 0);
assert_string_equal(e->out_txt, d);
assert_true(equal_mpv_node(&e->out_data, &res));
talloc_free(tmp);
}
}
test: make tests part of the mpv binary Until now, each .c file in test/ was built as separate, self-contained binary. Each binary could be run to execute the tests it contained. Change this and make them part of the normal mpv binary. Now the tests have to be invoked via the --unittest option. Do this for two reasons: - Tests now run within a "properly" initialized mpv instance, so all services are available. - Possibly simplifying the situation for future build systems. The first point is the main motivation. The mpv code is entangled with mp_log and the option system. It feels like a bad idea to duplicate some of the initialization of this just so you can call code using them. I'm also getting rid of cmocka. There wouldn't be any problem to keep it (it's a perfectly sane set of helpers), but NIH calls. I would have had to aggregate all tests into a CMUnitTest list, and I don't see how I'd get different types of entry points easily. Probably easily solvable, but since we made only pretty basic use of this library, NIH-ing this is actually easier (I needed a list of tests with custom metadata anyway, so all what was left was reimplement the assert_* helpers). Unit tests now don't output anything, and if they fail, they'll simply crash and leave a message that typically requires inspecting the test code to figure out what went wrong (and probably editing the test code to get more information). I even merged the various test functions into single ones. Sucks, but here you go. chmap_sel.c is merged into chmap.c, because I didn't see the point of this being separate. json.c drops the print_message() to go along with the new silent-by-default idea, also there's a memory leak fix unrelated to the rest of this commit. The new code is enabled with --enable-tests (--enable-test goes away). Due to waf's option parser, --enable-test still works, because it's a unique prefix to --enable-tests.
2019-11-07 21:42:14 +00:00
const struct unittest test_json = {
.name = "json",
.run = run,
test: make tests part of the mpv binary Until now, each .c file in test/ was built as separate, self-contained binary. Each binary could be run to execute the tests it contained. Change this and make them part of the normal mpv binary. Now the tests have to be invoked via the --unittest option. Do this for two reasons: - Tests now run within a "properly" initialized mpv instance, so all services are available. - Possibly simplifying the situation for future build systems. The first point is the main motivation. The mpv code is entangled with mp_log and the option system. It feels like a bad idea to duplicate some of the initialization of this just so you can call code using them. I'm also getting rid of cmocka. There wouldn't be any problem to keep it (it's a perfectly sane set of helpers), but NIH calls. I would have had to aggregate all tests into a CMUnitTest list, and I don't see how I'd get different types of entry points easily. Probably easily solvable, but since we made only pretty basic use of this library, NIH-ing this is actually easier (I needed a list of tests with custom metadata anyway, so all what was left was reimplement the assert_* helpers). Unit tests now don't output anything, and if they fail, they'll simply crash and leave a message that typically requires inspecting the test code to figure out what went wrong (and probably editing the test code to get more information). I even merged the various test functions into single ones. Sucks, but here you go. chmap_sel.c is merged into chmap.c, because I didn't see the point of this being separate. json.c drops the print_message() to go along with the new silent-by-default idea, also there's a memory leak fix unrelated to the rest of this commit. The new code is enabled with --enable-tests (--enable-test goes away). Due to waf's option parser, --enable-test still works, because it's a unique prefix to --enable-tests.
2019-11-07 21:42:14 +00:00
};