2023-09-04 12:21:37 +02:00
|
|
|
/* Unit tests for libjansson helpers.
|
2017-08-03 00:19:27 +02:00
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-08-03 00:19:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <criterion/criterion.h>
|
|
|
|
#include <criterion/parameterized.h>
|
|
|
|
|
2019-03-26 07:01:10 +01:00
|
|
|
#include <villas/config_helper.hpp>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2020-01-26 16:17:59 +01:00
|
|
|
#include "helpers.hpp"
|
2018-10-21 19:54:23 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
using namespace villas::node;
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
struct param {
|
2020-01-26 16:17:59 +01:00
|
|
|
char *argv[32];
|
|
|
|
char *json;
|
2017-08-03 00:19:27 +02:00
|
|
|
};
|
|
|
|
|
2020-09-11 14:57:05 +02:00
|
|
|
// cppcheck-suppress syntaxError
|
2017-08-03 00:19:27 +02:00
|
|
|
ParameterizedTestParameters(json, json_load_cli) {
|
2020-01-26 16:17:59 +01:00
|
|
|
const auto d = cr_strdup;
|
|
|
|
|
|
|
|
static criterion::parameters<struct param> params = {
|
2017-08-03 00:19:27 +02:00
|
|
|
// Combined long option
|
2020-01-26 16:17:59 +01:00
|
|
|
{.argv = {d("dummy"), d("--option=value")},
|
|
|
|
.json = d("{ \"option\" : \"value\" }")},
|
2017-08-03 00:19:27 +02:00
|
|
|
// Separated long option
|
2020-01-26 16:17:59 +01:00
|
|
|
{.argv = {d("dummy"), d("--option"), d("value")},
|
|
|
|
.json = d("{ \"option\" : \"value\" }")},
|
2017-08-03 00:19:27 +02:00
|
|
|
// All kinds of data types
|
2020-01-26 16:17:59 +01:00
|
|
|
{.argv = {d("dummy"), d("--integer"), d("1"), d("--real"), d("1.1"),
|
|
|
|
d("--bool"), d("true"), d("--null"), d("null"), d("--string"),
|
|
|
|
d("hello world")},
|
|
|
|
.json = d("{ \"integer\" : 1, \"real\" : 1.1, \"bool\" : true, \"null\" "
|
|
|
|
": null, \"string\" : \"hello world\" }")},
|
2017-08-03 00:19:27 +02:00
|
|
|
// Array generation
|
2020-01-26 16:17:59 +01:00
|
|
|
{.argv = {d("dummy"), d("--bool"), d("true"), d("--bool"), d("false")},
|
|
|
|
.json = d("{ \"bool\" : [ true, false ] }")},
|
2017-08-03 00:19:27 +02:00
|
|
|
// Dots in the option name generate sub objects
|
2020-01-26 16:17:59 +01:00
|
|
|
{.argv = {d("dummy"), d("--sub.option"), d("value")},
|
|
|
|
.json = d("{ \"sub\" : { \"option\" : \"value\" } }")},
|
2017-08-03 00:19:27 +02:00
|
|
|
// Nesting is possible
|
2020-01-26 16:17:59 +01:00
|
|
|
{.argv = {d("dummy"), d("--sub.sub.option"), d("value")},
|
|
|
|
.json = d("{ \"sub\" : { \"sub\" : { \"option\" : \"value\" } } }")},
|
2017-08-03 00:19:27 +02:00
|
|
|
// Multiple subgroups are merged
|
2020-01-26 16:17:59 +01:00
|
|
|
{.argv = {d("dummy"), d("--sub.sub.option"), d("value1"),
|
|
|
|
d("--sub.option"), d("value2")},
|
|
|
|
.json = d("{ \"sub\" : { \"option\" : \"value2\", \"sub\" : { "
|
|
|
|
"\"option\" : \"value1\" } } }")}};
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2020-01-26 16:17:59 +01:00
|
|
|
return params;
|
2017-08-03 00:19:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ParameterizedTest(struct param *p, json, json_load_cli) {
|
|
|
|
json_error_t err;
|
|
|
|
json_t *json, *cli;
|
|
|
|
|
2020-01-26 16:17:59 +01:00
|
|
|
json = json_loads(p->json, 0, &err);
|
2017-08-03 00:19:27 +02:00
|
|
|
cr_assert_not_null(json);
|
|
|
|
|
2020-01-26 16:17:59 +01:00
|
|
|
int argc = 0;
|
|
|
|
while (p->argv[argc])
|
|
|
|
argc++;
|
2018-10-21 19:54:23 +01:00
|
|
|
|
2020-01-26 16:17:59 +01:00
|
|
|
cli = json_load_cli(argc, (const char **)p->argv);
|
2017-08-03 00:19:27 +02:00
|
|
|
cr_assert_not_null(cli);
|
|
|
|
|
|
|
|
//json_dumpf(json, stdout, JSON_INDENT(2)); putc('\n', stdout);
|
|
|
|
//json_dumpf(cli, stdout, JSON_INDENT(2)); putc('\n', stdout);
|
|
|
|
|
|
|
|
cr_assert(json_equal(json, cli));
|
|
|
|
}
|