[libvirt] [PATCH 5/9] tests: json: Add test for the deflattening function
John Ferlan
jferlan at redhat.com
Mon Jul 10 13:04:26 UTC 2017
On 06/27/2017 08:46 AM, Peter Krempa wrote:
> Add a few test cases to verify that the old behaviour does not break and
> that new one behaves sanely.
> ---
> tests/virjsondata/deflatten-basic-file-in.json | 8 +++
> tests/virjsondata/deflatten-basic-file-out.json | 10 +++
> tests/virjsondata/deflatten-basic-generic-in.json | 14 ++++
> .../deflatten-concat-double-key-in.json | 7 ++
> .../deflatten-concat-double-key-out.json | 9 +++
> tests/virjsondata/deflatten-concat-in.json | 5 ++
> tests/virjsondata/deflatten-concat-out.json | 9 +++
> tests/virjsondata/deflatten-deep-file-in.json | 9 +++
> tests/virjsondata/deflatten-deep-file-out.json | 11 ++++
> tests/virjsondata/deflatten-deep-generic-in.json | 9 +++
> tests/virjsondata/deflatten-double-key-in.json | 4 ++
> tests/virjsondata/deflatten-double-key-out.json | 6 ++
> tests/virjsondata/deflatten-nested-in.json | 16 +++++
> tests/virjsondata/deflatten-nested-out.json | 18 ++++++
> tests/virjsondata/deflatten-unflattened-in.json | 12 ++++
> tests/virjsondata/deflatten-unflattened-out.json | 14 ++++
> tests/virjsontest.c | 74 ++++++++++++++++++++++
> 17 files changed, 235 insertions(+)
> create mode 100644 tests/virjsondata/deflatten-basic-file-in.json
> create mode 100644 tests/virjsondata/deflatten-basic-file-out.json
> create mode 100644 tests/virjsondata/deflatten-basic-generic-in.json
> create mode 100644 tests/virjsondata/deflatten-concat-double-key-in.json
> create mode 100644 tests/virjsondata/deflatten-concat-double-key-out.json
> create mode 100644 tests/virjsondata/deflatten-concat-in.json
> create mode 100644 tests/virjsondata/deflatten-concat-out.json
> create mode 100644 tests/virjsondata/deflatten-deep-file-in.json
> create mode 100644 tests/virjsondata/deflatten-deep-file-out.json
> create mode 100644 tests/virjsondata/deflatten-deep-generic-in.json
> create mode 100644 tests/virjsondata/deflatten-double-key-in.json
> create mode 100644 tests/virjsondata/deflatten-double-key-out.json
> create mode 100644 tests/virjsondata/deflatten-nested-in.json
> create mode 100644 tests/virjsondata/deflatten-nested-out.json
> create mode 100644 tests/virjsondata/deflatten-unflattened-in.json
> create mode 100644 tests/virjsondata/deflatten-unflattened-out.json
>
[...]
>
> 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) {
> + if (virTestGetVerbose())
> + fprintf(stderr, "deflattening should have failed\n");
This (and Copy) should probably use VIR_TEST_VERBOSE, right? At least
fix this one - and consider fixing Copy in separate patch which could be
considered trivial if you'd like ...
For extra, extra credit you could prefix with %s: and use info->doc to
further specify which test tailed.
> +
> + 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(outfile);
> + VIR_FREE(outfile);
DRD - only need to free once, but the first one could be replaced w/ indata
Reviewed-by: John Ferlan <jferlan at redhat.com>
John
> + VIR_FREE(actual);
> +
> + return ret;
> +
> +}
> +
> +
> +static int
> mymain(void)
> {
> int ret = 0;
> @@ -448,6 +509,19 @@ mymain(void)
> "{ \"a\": {}, \"b\": 1, \"c\": \"str\", \"d\": [] }",
> 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;
> }
>
More information about the libvir-list
mailing list