blob: 559b1932fd881d3b01c1ba6f603a4e447452116f [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
Radek Krejcica376bd2020-06-11 16:04:06 +020021#include <string.h>
22
Radek Krejci3a4889a2020-05-19 17:01:58 +020023#include "../../../src/common.h"
24#include "../../../src/context.h"
Radek Krejcica376bd2020-06-11 16:04:06 +020025#include "../../../src/log.h"
26#include "../../../src/parser_yin.h"
Radek Krejci3a4889a2020-05-19 17:01:58 +020027#include "../../../src/tree_schema.h"
28#include "../../../src/tree_schema_internal.h"
Radek Krejci3a4889a2020-05-19 17:01:58 +020029#include "../../../src/xml.h"
30
31
32#define BUFSIZE 1024
33char logbuf[BUFSIZE] = {0};
34int store = -1; /* negative for infinite logging, positive for limited logging */
35
36/* set to 0 to printing error messages to stderr instead of checking them in code */
37#define ENABLE_LOGGER_CHECKING 1
38
39#if ENABLE_LOGGER_CHECKING
40static void
41logger(LY_LOG_LEVEL level, const char *msg, const char *path)
42{
43 (void) level; /* unused */
44 if (store) {
45 if (path && path[0]) {
46 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
47 } else {
48 strncpy(logbuf, msg, BUFSIZE - 1);
49 }
50 if (store > 0) {
51 --store;
52 }
53 }
54}
55#endif
56
57static int
58logger_setup(void **state)
59{
60 (void) state; /* unused */
61
62#if ENABLE_LOGGER_CHECKING
63 /* setup logger */
64 ly_set_log_clb(logger, 1);
65#endif
66
67 return 0;
68}
69
70static int
71logger_teardown(void **state)
72{
73 (void) state; /* unused */
74#if ENABLE_LOGGER_CHECKING
75 if (*state) {
76 fprintf(stderr, "%s\n", logbuf);
77 }
78#endif
79 return 0;
80}
81
82void
83logbuf_clean(void)
84{
85 logbuf[0] = '\0';
86}
87
88#if ENABLE_LOGGER_CHECKING
89# define logbuf_assert(str) assert_string_equal(logbuf, str)
90#else
91# define logbuf_assert(str)
92#endif
93
94/**
95 * INCLUDE OTHER SCHEMA TESTS
96 */
97#include "test_schema_common.c"
98#include "test_schema_stmts.c"
99
100int main(void)
101{
102 const struct CMUnitTest tests[] = {
103 /** test_schema_common.c */
104 cmocka_unit_test_setup_teardown(test_getnext, logger_setup, logger_teardown),
105 cmocka_unit_test_setup_teardown(test_date, logger_setup, logger_teardown),
106 cmocka_unit_test_setup_teardown(test_revisions, logger_setup, logger_teardown),
107 cmocka_unit_test_setup_teardown(test_typedef, logger_setup, logger_teardown),
108
109 /** test_schema_stmts.c */
110 cmocka_unit_test_setup_teardown(test_identity, logger_setup, logger_teardown),
111 cmocka_unit_test_setup_teardown(test_feature, logger_setup, logger_teardown),
112 };
113
114 return cmocka_run_group_tests(tests, NULL, NULL);
115}