mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-05 04:25:19 +00:00
tests: json: Add test for the deflattening function
Add a few test cases to verify that the old behaviour does not break and that new one behaves sanely.
This commit is contained in:
parent
f43b7d60d8
commit
7f1209ad1e
8
tests/virjsondata/deflatten-basic-file-in.json
Normal file
8
tests/virjsondata/deflatten-basic-file-in.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"file.int": 1,
|
||||||
|
"file.string": "string",
|
||||||
|
"file.object": {
|
||||||
|
"data":"value",
|
||||||
|
"foo":"bar"
|
||||||
|
}
|
||||||
|
}
|
10
tests/virjsondata/deflatten-basic-file-out.json
Normal file
10
tests/virjsondata/deflatten-basic-file-out.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"file": {
|
||||||
|
"int": 1,
|
||||||
|
"string": "string",
|
||||||
|
"object": {
|
||||||
|
"data": "value",
|
||||||
|
"foo": "bar"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
tests/virjsondata/deflatten-basic-generic-in.json
Normal file
14
tests/virjsondata/deflatten-basic-generic-in.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"foo.int": 1,
|
||||||
|
"bar.int": 1,
|
||||||
|
"foo.string": "string",
|
||||||
|
"foo.object": {
|
||||||
|
"data":"value",
|
||||||
|
"foo":"bar"
|
||||||
|
},
|
||||||
|
"blurb.string": "string",
|
||||||
|
"blurb.object": {
|
||||||
|
"data":"value",
|
||||||
|
"foo":"bar"
|
||||||
|
}
|
||||||
|
}
|
7
tests/virjsondata/deflatten-concat-double-key-in.json
Normal file
7
tests/virjsondata/deflatten-concat-double-key-in.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"file.nest":{
|
||||||
|
"into":"is already here"
|
||||||
|
},
|
||||||
|
"file.nest.into":2,
|
||||||
|
"file.nest.there":"too"
|
||||||
|
}
|
9
tests/virjsondata/deflatten-concat-double-key-out.json
Normal file
9
tests/virjsondata/deflatten-concat-double-key-out.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"file": {
|
||||||
|
"nest": {
|
||||||
|
"into": "is already here"
|
||||||
|
},
|
||||||
|
"nest.into": 2,
|
||||||
|
"nest.there": "too"
|
||||||
|
}
|
||||||
|
}
|
5
tests/virjsondata/deflatten-concat-in.json
Normal file
5
tests/virjsondata/deflatten-concat-in.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"file.nest":{},
|
||||||
|
"file.nest.into":2,
|
||||||
|
"file.nest.there":"too"
|
||||||
|
}
|
9
tests/virjsondata/deflatten-concat-out.json
Normal file
9
tests/virjsondata/deflatten-concat-out.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"file": {
|
||||||
|
"nest": {
|
||||||
|
|
||||||
|
},
|
||||||
|
"nest.into": 2,
|
||||||
|
"nest.there": "too"
|
||||||
|
}
|
||||||
|
}
|
9
tests/virjsondata/deflatten-deep-file-in.json
Normal file
9
tests/virjsondata/deflatten-deep-file-in.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"file.double.nest1":"some",
|
||||||
|
"file.double.nest2":"more",
|
||||||
|
"file.double.nest3": {
|
||||||
|
"even":"objects"
|
||||||
|
},
|
||||||
|
"file.very.deeply.nested.object.chains.nest1":"some",
|
||||||
|
"file.very.deeply.nested.object.chains.nest2":"stuff"
|
||||||
|
}
|
11
tests/virjsondata/deflatten-deep-file-out.json
Normal file
11
tests/virjsondata/deflatten-deep-file-out.json
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"file": {
|
||||||
|
"double.nest1": "some",
|
||||||
|
"double.nest2": "more",
|
||||||
|
"double.nest3": {
|
||||||
|
"even": "objects"
|
||||||
|
},
|
||||||
|
"very.deeply.nested.object.chains.nest1": "some",
|
||||||
|
"very.deeply.nested.object.chains.nest2": "stuff"
|
||||||
|
}
|
||||||
|
}
|
9
tests/virjsondata/deflatten-deep-generic-in.json
Normal file
9
tests/virjsondata/deflatten-deep-generic-in.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"foo.double.nest1":"some",
|
||||||
|
"foo.double.nest2":"more",
|
||||||
|
"bar.double.nest3": {
|
||||||
|
"even":"objects"
|
||||||
|
},
|
||||||
|
"foo.very.deeply.nested.object.chains.nest1":"some",
|
||||||
|
"foo.very.deeply.nested.object.chains.nest2":"stuff"
|
||||||
|
}
|
4
tests/virjsondata/deflatten-double-key-in.json
Normal file
4
tests/virjsondata/deflatten-double-key-in.json
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"file.nest":1,
|
||||||
|
"file.nest.into":2
|
||||||
|
}
|
6
tests/virjsondata/deflatten-double-key-out.json
Normal file
6
tests/virjsondata/deflatten-double-key-out.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"file": {
|
||||||
|
"nest": 1,
|
||||||
|
"nest.into": 2
|
||||||
|
}
|
||||||
|
}
|
16
tests/virjsondata/deflatten-nested-in.json
Normal file
16
tests/virjsondata/deflatten-nested-in.json
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"file.nest": {
|
||||||
|
"even.objects":"can",
|
||||||
|
"even.contain":"some",
|
||||||
|
"even.more":{
|
||||||
|
"deeply.nested":"objects",
|
||||||
|
"deeply.needing":"deflattening",
|
||||||
|
"deeply.some.even":"more",
|
||||||
|
"deeply.some.than":{
|
||||||
|
"others.thought.was":"even",
|
||||||
|
"others.thought.completely":"necessary"
|
||||||
|
},
|
||||||
|
"perhaps":"flat value"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
18
tests/virjsondata/deflatten-nested-out.json
Normal file
18
tests/virjsondata/deflatten-nested-out.json
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"file": {
|
||||||
|
"nest": {
|
||||||
|
"even.objects": "can",
|
||||||
|
"even.contain": "some",
|
||||||
|
"even.more": {
|
||||||
|
"deeply.nested": "objects",
|
||||||
|
"deeply.needing": "deflattening",
|
||||||
|
"deeply.some.even": "more",
|
||||||
|
"deeply.some.than": {
|
||||||
|
"others.thought.was": "even",
|
||||||
|
"others.thought.completely": "necessary"
|
||||||
|
},
|
||||||
|
"perhaps": "flat value"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
tests/virjsondata/deflatten-unflattened-in.json
Normal file
12
tests/virjsondata/deflatten-unflattened-in.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"file.blah": {
|
||||||
|
"any":"possible",
|
||||||
|
"combination":{
|
||||||
|
"of":"json",
|
||||||
|
"nested":{
|
||||||
|
"objects":"should",
|
||||||
|
"be":"possible"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
tests/virjsondata/deflatten-unflattened-out.json
Normal file
14
tests/virjsondata/deflatten-unflattened-out.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"file": {
|
||||||
|
"blah": {
|
||||||
|
"any": "possible",
|
||||||
|
"combination": {
|
||||||
|
"of": "json",
|
||||||
|
"nested": {
|
||||||
|
"objects": "should",
|
||||||
|
"be": "possible"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -9,6 +9,8 @@
|
|||||||
#include "virjson.h"
|
#include "virjson.h"
|
||||||
#include "testutils.h"
|
#include "testutils.h"
|
||||||
|
|
||||||
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
||||||
|
|
||||||
struct testInfo {
|
struct testInfo {
|
||||||
const char *doc;
|
const char *doc;
|
||||||
const char *expect;
|
const char *expect;
|
||||||
@ -311,6 +313,63 @@ testJSONCopy(const void *data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
testJSONDeflatten(const void *data)
|
||||||
|
{
|
||||||
|
const struct testInfo *info = data;
|
||||||
|
virJSONValuePtr injson = NULL;
|
||||||
|
virJSONValuePtr deflattened = NULL;
|
||||||
|
char *infile = NULL;
|
||||||
|
char *indata = NULL;
|
||||||
|
char *outfile = NULL;
|
||||||
|
char *actual = NULL;
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
if (virAsprintf(&infile, "%s/virjsondata/deflatten-%s-in.json",
|
||||||
|
abs_srcdir, info->doc) < 0 ||
|
||||||
|
virAsprintf(&outfile, "%s/virjsondata/deflatten-%s-out.json",
|
||||||
|
abs_srcdir, info->doc) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (virTestLoadFile(infile, &indata) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (!(injson = virJSONValueFromString(indata)))
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if ((deflattened = virJSONValueObjectDeflatten(injson))) {
|
||||||
|
if (!info->pass) {
|
||||||
|
VIR_TEST_VERBOSE("%s: deflattening should have failed\n", info->doc);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!info->pass)
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(actual = virJSONValueToString(deflattened, true)))
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (virTestCompareToFile(actual, outfile) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
virJSONValueFree(injson);
|
||||||
|
virJSONValueFree(deflattened);
|
||||||
|
VIR_FREE(infile);
|
||||||
|
VIR_FREE(indata);
|
||||||
|
VIR_FREE(outfile);
|
||||||
|
VIR_FREE(actual);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
mymain(void)
|
mymain(void)
|
||||||
{
|
{
|
||||||
@ -448,6 +507,19 @@ mymain(void)
|
|||||||
"{ \"a\": {}, \"b\": 1, \"c\": \"str\", \"d\": [] }",
|
"{ \"a\": {}, \"b\": 1, \"c\": \"str\", \"d\": [] }",
|
||||||
NULL, true);
|
NULL, true);
|
||||||
|
|
||||||
|
#define DO_TEST_DEFLATTEN(name, pass) \
|
||||||
|
DO_TEST_FULL(name, Deflatten, name, NULL, pass)
|
||||||
|
|
||||||
|
DO_TEST_DEFLATTEN("unflattened", true);
|
||||||
|
DO_TEST_DEFLATTEN("basic-file", true);
|
||||||
|
DO_TEST_DEFLATTEN("basic-generic", false);
|
||||||
|
DO_TEST_DEFLATTEN("deep-file", true);
|
||||||
|
DO_TEST_DEFLATTEN("deep-generic", false);
|
||||||
|
DO_TEST_DEFLATTEN("nested", true);
|
||||||
|
DO_TEST_DEFLATTEN("double-key", true);
|
||||||
|
DO_TEST_DEFLATTEN("concat", true);
|
||||||
|
DO_TEST_DEFLATTEN("concat-double-key", true);
|
||||||
|
|
||||||
return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user