blob: b330a7d9bcf233d94aa87a7fb48f1d8f43e3fa35 [file] [log] [blame]
Radek Krejci3a4889a2020-05-19 17:01:58 +02001/*
2 * @file test_schema.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for schema related functions
5 *
6 * Copyright (c) 2018-2019 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 */
14
15#include <stdarg.h>
16#include <stddef.h>
17#include <stdio.h>
18#include <setjmp.h>
19#include <cmocka.h>
20
21#include "../../../src/common.h"
22#include "../../../src/context.h"
23#include "../../../src/tree_schema.h"
24#include "../../../src/tree_schema_internal.h"
25#include "../../../src/parser_yin.h"
26#include "../../../src/xml.h"
27
28
29#define BUFSIZE 1024
30char logbuf[BUFSIZE] = {0};
31int store = -1; /* negative for infinite logging, positive for limited logging */
32
33/* set to 0 to printing error messages to stderr instead of checking them in code */
34#define ENABLE_LOGGER_CHECKING 1
35
36#if ENABLE_LOGGER_CHECKING
37static void
38logger(LY_LOG_LEVEL level, const char *msg, const char *path)
39{
40 (void) level; /* unused */
41 if (store) {
42 if (path && path[0]) {
43 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
44 } else {
45 strncpy(logbuf, msg, BUFSIZE - 1);
46 }
47 if (store > 0) {
48 --store;
49 }
50 }
51}
52#endif
53
54static int
55logger_setup(void **state)
56{
57 (void) state; /* unused */
58
59#if ENABLE_LOGGER_CHECKING
60 /* setup logger */
61 ly_set_log_clb(logger, 1);
62#endif
63
64 return 0;
65}
66
67static int
68logger_teardown(void **state)
69{
70 (void) state; /* unused */
71#if ENABLE_LOGGER_CHECKING
72 if (*state) {
73 fprintf(stderr, "%s\n", logbuf);
74 }
75#endif
76 return 0;
77}
78
79void
80logbuf_clean(void)
81{
82 logbuf[0] = '\0';
83}
84
85#if ENABLE_LOGGER_CHECKING
86# define logbuf_assert(str) assert_string_equal(logbuf, str)
87#else
88# define logbuf_assert(str)
89#endif
90
91/**
92 * INCLUDE OTHER SCHEMA TESTS
93 */
94#include "test_schema_common.c"
95#include "test_schema_stmts.c"
96
97int main(void)
98{
99 const struct CMUnitTest tests[] = {
100 /** test_schema_common.c */
101 cmocka_unit_test_setup_teardown(test_getnext, logger_setup, logger_teardown),
102 cmocka_unit_test_setup_teardown(test_date, logger_setup, logger_teardown),
103 cmocka_unit_test_setup_teardown(test_revisions, logger_setup, logger_teardown),
104 cmocka_unit_test_setup_teardown(test_typedef, logger_setup, logger_teardown),
105
106 /** test_schema_stmts.c */
107 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
108 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
109 };
110
111 return cmocka_run_group_tests(tests, NULL, NULL);
112}