context FEATURE public API for parsing schemas
diff --git a/tests/src/CMakeLists.txt b/tests/src/CMakeLists.txt
index b369789..04d0d0d 100644
--- a/tests/src/CMakeLists.txt
+++ b/tests/src/CMakeLists.txt
@@ -5,7 +5,8 @@
src_hash_table
src_xml
src_parser_yang
- src_tree_schema_compile)
+ src_tree_schema_compile
+ src_tree_schema_helpers)
set(local_tests_wraps
" "
"-Wl,--wrap=realloc"
@@ -13,6 +14,7 @@
" "
" "
" "
+ " "
" ")
set(tests ${tests} ${local_tests} PARENT_SCOPE)
set(tests_wraps ${tests_wraps} ${local_tests_wraps} PARENT_SCOPE)
diff --git a/tests/src/test_common.c b/tests/src/test_common.c
index 60c4f48..0499c42 100644
--- a/tests/src/test_common.c
+++ b/tests/src/test_common.c
@@ -14,8 +14,6 @@
#include "../../src/common.c"
-#define _BSD_SOURCE
-#define _DEFAULT_SOURCE
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
@@ -84,35 +82,6 @@
assert_int_equal(LY_EINVAL, ly_getutf8(&str, &c, &len));
}
-static void
-test_date(void **state)
-{
- (void) state; /* unused */
-
- assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
- assert_string_equal(logbuf, "Invalid argument date (lysp_check_date()).");
- assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
- assert_string_equal(logbuf, "Invalid argument date_len (lysp_check_date()).");
- assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"nonsencexx\" of \"date\".");
- assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"123x-11-11\" of \"date\".");
- assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"2018-13-11\" of \"date\".");
- assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"2018-11-41\" of \"date\".");
- assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"2018-02-29\" of \"date\".");
- assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"2018.02-28\" of \"date\".");
- assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
- assert_string_equal(logbuf, "Invalid value \"2018-02.28\" of \"date\".");
-
- assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
- assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
- assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
-}
-
void *__real_realloc(void *ptr, size_t size);
void *__wrap_realloc(void *ptr, size_t size)
{
@@ -154,7 +123,6 @@
{
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup(test_utf8, logger_setup),
- cmocka_unit_test_setup(test_date, logger_setup),
cmocka_unit_test(test_lyrealloc),
};
diff --git a/tests/src/test_parser_yang.c b/tests/src/test_parser_yang.c
index 2d160e8..e6fec3c 100644
--- a/tests/src/test_parser_yang.c
+++ b/tests/src/test_parser_yang.c
@@ -12,8 +12,9 @@
* https://opensource.org/licenses/BSD-3-Clause
*/
-#define _BSD_SOURCE
-#define _DEFAULT_SOURCE
+#include "../../src/tree_schema.c"
+#include "../../src/parser_yang.c"
+
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
@@ -23,8 +24,6 @@
#include <string.h>
#include "libyang.h"
-#include "../../src/parser_yang.c"
-#include "../../src/tree_schema.c"
#define BUFSIZE 1024
char logbuf[BUFSIZE] = {0};
@@ -761,6 +760,10 @@
assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
logbuf_assert("Prefix \"x\" already used as module prefix. Line number 2.");
mod = mod_renew(&ctx, mod, 0);
+ str = SCHEMA_BEGINNING "import test1 {prefix y;}import test2 {prefix y;}}";
+ assert_int_equal(LY_EVALID, parse_sub_module(&ctx, &str, mod));
+ logbuf_assert("Prefix \"y\" already used to import \"test1\" module. Line number 2.");
+ mod = mod_renew(&ctx, mod, 0);
/* include */
TEST_GENERIC("include test;}", mod->includes,
diff --git a/tests/src/test_tree_schema_compile.c b/tests/src/test_tree_schema_compile.c
index 291e515..5f30a9d 100644
--- a/tests/src/test_tree_schema_compile.c
+++ b/tests/src/test_tree_schema_compile.c
@@ -12,8 +12,9 @@
* https://opensource.org/licenses/BSD-3-Clause
*/
-#define _BSD_SOURCE
-#define _DEFAULT_SOURCE
+#include "../../src/tree_schema.c"
+#include "../../src/parser_yang.c"
+
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
@@ -23,8 +24,6 @@
#include <string.h>
#include "libyang.h"
-#include "../../src/parser_yang.c"
-#include "../../src/tree_schema.c"
#define BUFSIZE 1024
char logbuf[BUFSIZE] = {0};
@@ -91,14 +90,23 @@
assert_ptr_equal(mod.parsed->name, mod.compiled->name);
assert_ptr_equal(mod.parsed->ns, mod.compiled->ns);
- lysc_module_free(mod.compiled);
+ lysc_module_free(mod.compiled, NULL);
assert_int_equal(LY_SUCCESS, lys_compile(mod.parsed, LYSC_OPT_FREE_SP, &mod.compiled));
assert_non_null(mod.compiled);
assert_string_equal("test", mod.compiled->name);
assert_string_equal("urn:test", mod.compiled->ns);
- lysc_module_free(mod.compiled);
+ lysc_module_free(mod.compiled, NULL);
+ mod.compiled = NULL;
+
+ str = "submodule test {belongs-to xxx {prefix x;}}";
+ assert_int_equal(LY_SUCCESS, yang_parse(ctx, str, &mod.parsed));
+ assert_int_equal(LY_EINVAL, lys_compile(mod.parsed, 0, &mod.compiled));
+ logbuf_assert("Submodules (test) are not supposed to be compiled, compile only the main modules.");
+ assert_null(mod.compiled);
+
+ lysp_module_free(mod.parsed);
ly_ctx_destroy(ctx, NULL);
}
diff --git a/tests/src/test_tree_schema_helpers.c b/tests/src/test_tree_schema_helpers.c
new file mode 100644
index 0000000..53cbded
--- /dev/null
+++ b/tests/src/test_tree_schema_helpers.c
@@ -0,0 +1,82 @@
+/*
+ * @file set.c
+ * @author: Radek Krejci <rkrejci@cesnet.cz>
+ * @brief unit tests for functions from common.c
+ *
+ * Copyright (c) 2018 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://opensource.org/licenses/BSD-3-Clause
+ */
+
+#include "../../src/tree_schema_helpers.c"
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include "libyang.h"
+
+#define BUFSIZE 1024
+char logbuf[BUFSIZE] = {0};
+
+static void
+logger(LY_LOG_LEVEL level, const char *msg, const char *path)
+{
+ (void) level; /* unused */
+ (void) path; /* unused */
+
+ strncpy(logbuf, msg, BUFSIZE - 1);
+}
+
+static int
+logger_setup(void **state)
+{
+ (void) state; /* unused */
+
+ ly_set_log_clb(logger, 0);
+
+ return 0;
+}
+
+static void
+test_date(void **state)
+{
+ (void) state; /* unused */
+
+ assert_int_equal(LY_EINVAL, lysp_check_date(NULL, NULL, 0, "date"));
+ assert_string_equal(logbuf, "Invalid argument date (lysp_check_date()).");
+ assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "x", 1, "date"));
+ assert_string_equal(logbuf, "Invalid argument date_len (lysp_check_date()).");
+ assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "nonsencexx", 10, "date"));
+ assert_string_equal(logbuf, "Invalid value \"nonsencexx\" of \"date\".");
+ assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "123x-11-11", 10, "date"));
+ assert_string_equal(logbuf, "Invalid value \"123x-11-11\" of \"date\".");
+ assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-13-11", 10, "date"));
+ assert_string_equal(logbuf, "Invalid value \"2018-13-11\" of \"date\".");
+ assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-11-41", 10, "date"));
+ assert_string_equal(logbuf, "Invalid value \"2018-11-41\" of \"date\".");
+ assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02-29", 10, "date"));
+ assert_string_equal(logbuf, "Invalid value \"2018-02-29\" of \"date\".");
+ assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018.02-28", 10, "date"));
+ assert_string_equal(logbuf, "Invalid value \"2018.02-28\" of \"date\".");
+ assert_int_equal(LY_EINVAL, lysp_check_date(NULL, "2018-02.28", 10, "date"));
+ assert_string_equal(logbuf, "Invalid value \"2018-02.28\" of \"date\".");
+
+ assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-11-11", 10, "date"));
+ assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2018-02-28", 10, "date"));
+ assert_int_equal(LY_SUCCESS, lysp_check_date(NULL, "2016-02-29", 10, "date"));
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test_setup(test_date, logger_setup),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}