blob: 0a14116242fa676780ef1eb7dd492562467c9801 [file] [log] [blame]
Radek Krejci80dd33e2018-09-26 15:57:18 +02001/*
2 * @file test_parser_yang.c
3 * @author: Radek Krejci <rkrejci@cesnet.cz>
4 * @brief unit tests for functions from parser_yang.c
5 *
6 * Copyright (c) 2018 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#define _BSD_SOURCE
16#define _DEFAULT_SOURCE
17#include <stdarg.h>
18#include <stddef.h>
19#include <setjmp.h>
20#include <cmocka.h>
21
22#include <stdio.h>
23#include <string.h>
24
25#include "libyang.h"
26#include "../../src/parser_yang.c"
27
28#define BUFSIZE 1024
29char logbuf[BUFSIZE] = {0};
30
31/* set to 0 to printing error messages to stderr instead of checking them in code */
32#define ENABLE_LOGGER_CHECKING 1
33
34static void
35logger(LY_LOG_LEVEL level, const char *msg, const char *path)
36{
37 (void) level; /* unused */
38
39 if (path) {
40 snprintf(logbuf, BUFSIZE - 1, "%s %s", msg, path);
41 } else {
42 strncpy(logbuf, msg, BUFSIZE - 1);
43 }
44}
45
46static int
47logger_setup(void **state)
48{
49 (void) state; /* unused */
50#if ENABLE_LOGGER_CHECKING
51 ly_set_log_clb(logger, 1);
52#endif
53 return 0;
54}
55
56void
57logbuf_clean(void)
58{
59 logbuf[0] = '\0';
60}
61
62#if ENABLE_LOGGER_CHECKING
63# define logbuf_assert(str) assert_string_equal(logbuf, str)
64#else
65# define logbuf_assert(str)
66#endif
67
68
69static void
70test_comments(void **state)
71{
72 (void) state; /* unused */
73
74 const char *str, *p;
75
76 str = " this is a text of / one * line */ comment\nx";
77 assert_int_equal(LY_SUCCESS, skip_comment(NULL, &str, 1));
78 assert_string_equal("x", str);
79
80 str = " this is a \n * text // of / block * comment */x";
81 assert_int_equal(LY_SUCCESS, skip_comment(NULL, &str, 2));
82 assert_string_equal("x", str);
83
84 str = p = " this is one line comment on last line";
85 assert_int_equal(LY_SUCCESS, skip_comment(NULL, &str, 1));
86 assert_true(str[0] == '\0');
87
88 str = p = " this is a not terminated comment x";
89 assert_int_equal(LY_EVALID, skip_comment(NULL, &str, 2));
90 logbuf_assert("Unexpected end-of-file, non-terminated comment.");
91 assert_true(str[0] == '\0');
92}
93
94int main(void)
95{
96 const struct CMUnitTest tests[] = {
97 cmocka_unit_test_setup(test_comments, logger_setup),
98 };
99
100 return cmocka_run_group_tests(tests, NULL, NULL);
101}