Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 1 | /* |
| 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 Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 14 | #ifndef TESTS_UTESTS_SCHEMA_TEST_SCHEMA_H_ |
| 15 | #define TESTS_UTESTS_SCHEMA_TEST_SCHEMA_H_ |
| 16 | |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 17 | #include "log.h" |
| 18 | #include "parser_schema.h" |
Radek Krejci | 70593c1 | 2020-06-13 20:48:09 +0200 | [diff] [blame] | 19 | #include "tests/config.h" |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 20 | |
| 21 | /* set to 0 to printing error messages to stderr instead of checking them in code */ |
| 22 | #define ENABLE_LOGGER_CHECKING 1 |
| 23 | |
| 24 | #if ENABLE_LOGGER_CHECKING |
| 25 | extern char logbuf[]; |
| 26 | # define logbuf_assert(str) assert_string_equal(logbuf, str) |
| 27 | #else |
| 28 | # define logbuf_assert(str) |
| 29 | #endif |
| 30 | |
| 31 | void logbuf_clean(void); |
| 32 | |
| 33 | LY_ERR test_imp_clb(const char *UNUSED(mod_name), const char *UNUSED(mod_rev), const char *UNUSED(submod_name), |
| 34 | const char *UNUSED(sub_rev), void *user_data, LYS_INFORMAT *format, |
| 35 | const char **module_data, void (**free_module_data)(void *model_data, void *user_data)); |
| 36 | |
Radek Krejci | 3a4889a | 2020-05-19 17:01:58 +0200 | [diff] [blame] | 37 | |
| 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__) \ |
| 71 | assert_non_null(RESULT = lys_parse_mem(CTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG)); \ |
| 72 | } |
| 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__) \ |
| 78 | assert_null(lys_parse_mem(CTX, test_str__, YIN ? LYS_IN_YIN : LYS_IN_YANG)); \ |
| 79 | 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 | |
| 90 | #define TEST_STMT_SUBSTM_ERR(CTX, RFC7950, STMT, SUBSTMT, VALUE); \ |
| 91 | TEST_SCHEMA_ERR(CTX, RFC7950, 0, "inv", STMT" test {"SUBSTMT" "VALUE";}", "Invalid keyword \""SUBSTMT"\" as a child of \""STMT"\". Line number 1."); |
| 92 | |
Radek Krejci | 18abde4 | 2020-06-13 20:04:39 +0200 | [diff] [blame] | 93 | #endif /* TESTS_UTESTS_SCHEMA_TEST_SCHEMA_H_ */ |