From 385f6ad488e4d36d2d32e8a3ad2ef1502efaf21e Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Thu, 30 Nov 2023 14:16:28 +0100 Subject: [PATCH] tests/pkg/mjson: Add test for mjson package --- tests/pkg/mjson/Makefile | 6 +++ tests/pkg/mjson/app.config.test | 2 + tests/pkg/mjson/main.c | 80 +++++++++++++++++++++++++++++++++ tests/pkg/mjson/tests/01-run.py | 14 ++++++ 4 files changed, 102 insertions(+) create mode 100644 tests/pkg/mjson/Makefile create mode 100644 tests/pkg/mjson/app.config.test create mode 100644 tests/pkg/mjson/main.c create mode 100755 tests/pkg/mjson/tests/01-run.py diff --git a/tests/pkg/mjson/Makefile b/tests/pkg/mjson/Makefile new file mode 100644 index 0000000000..cac857c6c4 --- /dev/null +++ b/tests/pkg/mjson/Makefile @@ -0,0 +1,6 @@ +include ../Makefile.pkg_common + +USEMODULE += embunit +USEPKG += mjson + +include $(RIOTBASE)/Makefile.include diff --git a/tests/pkg/mjson/app.config.test b/tests/pkg/mjson/app.config.test new file mode 100644 index 0000000000..a81192b504 --- /dev/null +++ b/tests/pkg/mjson/app.config.test @@ -0,0 +1,2 @@ +CONFIG_MODULE_EMBUNIT=y +CONFIG_PACKAGE_MJSON=y diff --git a/tests/pkg/mjson/main.c b/tests/pkg/mjson/main.c new file mode 100644 index 0000000000..91fb5754c5 --- /dev/null +++ b/tests/pkg/mjson/main.c @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2023 Koen Zandberg + * + * 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 Test application for mjson + * + * @author Koen Zandberg + * + * @} + */ + +#include +#include "embUnit.h" +#include "mjson.h" + +void test_mjson_encode(void) +{ + /* Print into a statically allocated buffer */ + char buf[100]; + mjson_snprintf(buf, sizeof(buf), "{%Q:%d}", "a", 123); + + static const char expected[] = "{\"a\":123}"; + + TEST_ASSERT_EQUAL_INT(0, strcmp(expected, buf)); +} + +void test_mjson_decode(void) +{ + static const char input[] = "{\"a\":1,\"b\":[2,false]}"; /* {"a":1,"b":[2,false]} */ + + double dval = 0; + int res = mjson_get_number(input, strlen(input), "$.a", &dval); + TEST_ASSERT_EQUAL_INT(1, res); + TEST_ASSERT_EQUAL_INT(1, dval); + + const char *buf; + int buf_len; + res = mjson_find(input, strlen(input), "$.b", &buf, &buf_len); + TEST_ASSERT_EQUAL_INT(91, res); + TEST_ASSERT_EQUAL_INT(0, memcmp(buf, "[2,false]", buf_len)); + + res = mjson_get_number(input, strlen(input), "$.b[0]", &dval); + TEST_ASSERT_EQUAL_INT(1, res); + TEST_ASSERT_EQUAL_INT(2, dval); + + int ival = 0; + res = mjson_get_bool(input, strlen(input), "$.b[1]", &ival); + TEST_ASSERT_EQUAL_INT(1, res); + TEST_ASSERT_EQUAL_INT(0, ival); + +} + +Test *tests_mjson(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_mjson_encode), + new_TestFixture(test_mjson_decode), + }; + + EMB_UNIT_TESTCALLER(mjson_tests, NULL, NULL, fixtures); + return (Test*)&mjson_tests; +} + +int main(void) +{ + TESTS_START(); + TESTS_RUN(tests_mjson()); + TESTS_END(); + + return 0; +} diff --git a/tests/pkg/mjson/tests/01-run.py b/tests/pkg/mjson/tests/01-run.py new file mode 100755 index 0000000000..593eef2621 --- /dev/null +++ b/tests/pkg/mjson/tests/01-run.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2023 Koen Zandberg +# +# 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_check_unittests + + +if __name__ == "__main__": + sys.exit(run_check_unittests())