blob: 2599db3406abffc7a4b7e5f48aefe33dea3357b3 [file] [log] [blame]
Radek Krejci3a4889a2020-05-19 17:01:58 +02001/*
2 * @file macros.h
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief macros for schema tests
5 *
6 * Copyright (c) 2018-2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Radek Krejcib4ac5a92020-11-23 17:54:33 +010014
Radek Krejci18abde42020-06-13 20:04:39 +020015#ifndef TESTS_UTESTS_SCHEMA_TEST_SCHEMA_H_
16#define TESTS_UTESTS_SCHEMA_TEST_SCHEMA_H_
17
Radek Krejci18abde42020-06-13 20:04:39 +020018#include "log.h"
19#include "parser_schema.h"
Radek Krejci70593c12020-06-13 20:48:09 +020020#include "tests/config.h"
Radek Krejci18abde42020-06-13 20:04:39 +020021
22/* set to 0 to printing error messages to stderr instead of checking them in code */
23#define ENABLE_LOGGER_CHECKING 1
24
25#if ENABLE_LOGGER_CHECKING
Radek Krejcib4ac5a92020-11-23 17:54:33 +010026extern char logbuf[];
Radek Krejci18abde42020-06-13 20:04:39 +020027# define logbuf_assert(str) assert_string_equal(logbuf, str)
28#else
29# define logbuf_assert(str)
30#endif
31
32void logbuf_clean(void);
33
34LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name),
Radek Krejcib4ac5a92020-11-23 17:54:33 +010035 const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT * format,
36 const char **module_data, void (**free_module_data)(void *model_data, void *user_data));
Radek Krejci3a4889a2020-05-19 17:01:58 +020037
38#define TEST_YANG_MODULE_10(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
39 "module "MOD_NAME" { namespace "MOD_NS"; prefix "MOD_PREFIX"; "CONTENT"}"
40
41#define TEST_YANG_MODULE_11(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
42 "module "MOD_NAME" {yang-version 1.1; namespace "MOD_NS"; prefix "MOD_PREFIX"; "CONTENT"}"
43
44#define TEST_YIN_MODULE_10(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
45 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\""MOD_NAME"\">" \
46 "<namespace uri=\""MOD_NS"\"/><prefix value=\""MOD_PREFIX"\"/>"CONTENT"</module>"
47
48#define TEST_YIN_MODULE_11(MOD_NAME, MOD_PREFIX, MOD_NS, CONTENT) \
49 "<module xmlns=\"urn:ietf:params:xml:ns:yang:yin:1\" name=\""MOD_NAME"\"><yang-version value=\"1.1\"/>" \
50 "<namespace uri=\""MOD_NS"\"/><prefix value=\""MOD_PREFIX"\"/>"CONTENT"</module>"
51
52#define TEST_SCHEMA_STR(CTX, RFC7950, YIN, MOD_NAME, CONTENT, STR) \
53 if (YIN) { \
54 if (RFC7950) { \
55 STR = TEST_YIN_MODULE_11(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
56 } else { \
57 STR = TEST_YIN_MODULE_10(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
58 } \
59 } else { /* YANG */ \
60 if (RFC7950) { \
61 STR = TEST_YANG_MODULE_11(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
62 } else { \
63 STR = TEST_YANG_MODULE_10(MOD_NAME, MOD_NAME, "urn:libyang:test:"MOD_NAME, CONTENT); \
64 } \
65 }
66
67#define TEST_SCHEMA_OK(CTX, RFC7950, YIN, MOD_NAME, CONTENT, RESULT) \
68 { \
69 const char *test_str__; \
70 TEST_SCHEMA_STR(CTX, RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \
Michal Vasko3a41dff2020-07-15 14:30:28 +020071 assert_int_equal(LY_SUCCESS, lys_parse_mem(CTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, &(RESULT))); \
Radek Krejci3a4889a2020-05-19 17:01:58 +020072 }
73
74#define TEST_SCHEMA_ERR(CTX, RFC7950, YIN, MOD_NAME, CONTENT, ERRMSG) \
75 { \
76 const char *test_str__; \
77 TEST_SCHEMA_STR(CTX, RFC7950, YIN, MOD_NAME, CONTENT, test_str__) \
Michal Vasko3a41dff2020-07-15 14:30:28 +020078 assert_int_not_equal(lys_parse_mem(CTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG, NULL), LY_SUCCESS); \
Radek Krejci3a4889a2020-05-19 17:01:58 +020079 logbuf_assert(ERRMSG); \
80 }
81
82#define TEST_STMT_DUP(CTX, RFC7950, YIN, STMT, MEMBER, VALUE1, VALUE2, LINE) \
83 if (YIN) { \
84 TEST_SCHEMA_ERR(CTX, RFC7950, YIN, "dup", "", "Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
85 } else { \
86 TEST_SCHEMA_ERR(CTX, RFC7950, YIN, "dup", STMT"{"MEMBER" "VALUE1";"MEMBER" "VALUE2";}", \
87 "Duplicate keyword \""MEMBER"\". Line number "LINE"."); \
88 }
89
Radek Krejcib4ac5a92020-11-23 17:54:33 +010090#define TEST_STMT_SUBSTM_ERR(CTX, RFC7950, STMT, SUBSTMT, VALUE) ;\
Radek Krejci3a4889a2020-05-19 17:01:58 +020091 TEST_SCHEMA_ERR(CTX, RFC7950, 0, "inv", STMT" test {"SUBSTMT" "VALUE";}", "Invalid keyword \""SUBSTMT"\" as a child of \""STMT"\". Line number 1.");
92
Radek Krejci18abde42020-06-13 20:04:39 +020093#endif /* TESTS_UTESTS_SCHEMA_TEST_SCHEMA_H_ */