diff --git a/tests/pkg_corejson/Makefile b/tests/pkg_corejson/Makefile new file mode 100644 index 0000000000..41d12ca8a9 --- /dev/null +++ b/tests/pkg_corejson/Makefile @@ -0,0 +1,6 @@ +include ../Makefile.tests_common + +# required packages +USEPKG += corejson + +include $(RIOTBASE)/Makefile.include diff --git a/tests/pkg_corejson/main.c b/tests/pkg_corejson/main.c new file mode 100644 index 0000000000..1d211f2a65 --- /dev/null +++ b/tests/pkg_corejson/main.c @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2021 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief coreJSON package test application + * + * Adapted to RIOT style from the [reference example](https://github.com/FreeRTOS/coreJSON#reference-example) + * + * @author Alexandre Abadie + * + * @} + */ + +#include +#include "core_json.h" + +int main(void) +{ + /* Variables used in this example. */ + JSONStatus_t result; + char buffer[] = "{\"foo\":\"abc\",\"bar\":{\"foo\":\"xyz\"}}"; + size_t bufferLength = sizeof(buffer) - 1; + char queryKey[] = "bar.foo"; + size_t queryKeyLength = sizeof(queryKey) - 1; + char *value; + size_t valueLength; + + /* Calling JSON_Validate() is not necessary if the document is guaranteed to be valid. */ + result = JSON_Validate(buffer, bufferLength); + + if (result == JSONSuccess) { + result = JSON_Search( + buffer, bufferLength, queryKey, queryKeyLength, + &value, &valueLength + ); + } + + if (result == JSONSuccess) { + /* The pointer "value" will point to a location in the "buffer". */ + char save = value[valueLength]; + /* After saving the character, set it to a null byte for printing. */ + value[valueLength] = '\0'; + /* "Found: bar.foo -> xyz" will be printed. */ + printf("Found: %s -> %s\n", queryKey, value); + /* Restore the original character. */ + value[valueLength] = save; + } + + return 0; +} diff --git a/tests/pkg_corejson/tests/01-run.py b/tests/pkg_corejson/tests/01-run.py new file mode 100755 index 0000000000..032cd8d229 --- /dev/null +++ b/tests/pkg_corejson/tests/01-run.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2021 Inria +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. + +import sys +from testrunner import run + + +def testfunc(child): + child.expect_exact("Found: bar.foo -> xyz") + + +if __name__ == "__main__": + sys.exit(run(testfunc))