1 | /*
|
---|
2 | * Copyright (c) 2015 Michal Koutny
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <assert.h>
|
---|
30 | #include <conf/ini.h>
|
---|
31 | #include <pcut/pcut.h>
|
---|
32 | #include <str.h>
|
---|
33 |
|
---|
34 | PCUT_INIT
|
---|
35 |
|
---|
36 | PCUT_TEST_SUITE(ini);
|
---|
37 |
|
---|
38 | static ini_configuration_t ini_conf;
|
---|
39 | static text_parse_t parse;
|
---|
40 |
|
---|
41 | PCUT_TEST_BEFORE {
|
---|
42 | ini_configuration_init(&ini_conf);
|
---|
43 | text_parse_init(&parse);
|
---|
44 | }
|
---|
45 |
|
---|
46 | PCUT_TEST_AFTER {
|
---|
47 | text_parse_deinit(&parse);
|
---|
48 | ini_configuration_deinit(&ini_conf);
|
---|
49 | }
|
---|
50 |
|
---|
51 | PCUT_TEST(simple_parsing) {
|
---|
52 | const char *data =
|
---|
53 | "[Section]\n"
|
---|
54 | "key = value\n"
|
---|
55 | "key2 = value2\n";
|
---|
56 |
|
---|
57 | int rc = ini_parse_string(data, &ini_conf, &parse);
|
---|
58 |
|
---|
59 | PCUT_ASSERT_INT_EQUALS(rc, EOK);
|
---|
60 |
|
---|
61 | ini_section_t *section = ini_get_section(&ini_conf, "Section");
|
---|
62 | PCUT_ASSERT_TRUE(section != NULL);
|
---|
63 |
|
---|
64 | ini_item_iterator_t it = ini_section_get_iterator(section, "key");
|
---|
65 | PCUT_ASSERT_TRUE(ini_item_iterator_valid(&it));
|
---|
66 |
|
---|
67 | PCUT_ASSERT_STR_EQUALS(ini_item_iterator_value(&it), "value");
|
---|
68 | }
|
---|
69 |
|
---|
70 | PCUT_TEST(default_section) {
|
---|
71 | const char *data =
|
---|
72 | "key = value\n"
|
---|
73 | "key2 = value2\n";
|
---|
74 |
|
---|
75 | int rc = ini_parse_string(data, &ini_conf, &parse);
|
---|
76 |
|
---|
77 | PCUT_ASSERT_INT_EQUALS(rc, EOK);
|
---|
78 |
|
---|
79 | ini_section_t *section = ini_get_section(&ini_conf, NULL);
|
---|
80 | PCUT_ASSERT_TRUE(section != NULL);
|
---|
81 |
|
---|
82 | ini_item_iterator_t it = ini_section_get_iterator(section, "key");
|
---|
83 | PCUT_ASSERT_TRUE(ini_item_iterator_valid(&it));
|
---|
84 |
|
---|
85 | PCUT_ASSERT_STR_EQUALS(ini_item_iterator_value(&it), "value");
|
---|
86 | }
|
---|
87 |
|
---|
88 | PCUT_TEST(multikey) {
|
---|
89 | const char *data =
|
---|
90 | "key = value\n"
|
---|
91 | "key = value2\n";
|
---|
92 |
|
---|
93 | int rc = ini_parse_string(data, &ini_conf, &parse);
|
---|
94 |
|
---|
95 | PCUT_ASSERT_INT_EQUALS(rc, EOK);
|
---|
96 |
|
---|
97 | ini_section_t *section = ini_get_section(&ini_conf, NULL);
|
---|
98 | PCUT_ASSERT_TRUE(section != NULL);
|
---|
99 |
|
---|
100 | ini_item_iterator_t it = ini_section_get_iterator(section, "key");
|
---|
101 | PCUT_ASSERT_TRUE(ini_item_iterator_valid(&it));
|
---|
102 | const char *first = ini_item_iterator_value(&it);
|
---|
103 |
|
---|
104 | ini_item_iterator_inc(&it);
|
---|
105 | PCUT_ASSERT_TRUE(ini_item_iterator_valid(&it));
|
---|
106 | const char *second = ini_item_iterator_value(&it);
|
---|
107 |
|
---|
108 | PCUT_ASSERT_TRUE(
|
---|
109 | (str_cmp(first, "value") == 0 && str_cmp(second, "value2") == 0) ||
|
---|
110 | (str_cmp(first, "value2") == 0 && str_cmp(second, "value") == 0));
|
---|
111 |
|
---|
112 | ini_item_iterator_inc(&it);
|
---|
113 | PCUT_ASSERT_FALSE(ini_item_iterator_valid(&it));
|
---|
114 | }
|
---|
115 |
|
---|
116 | PCUT_TEST(dup_section) {
|
---|
117 | const char *data =
|
---|
118 | "[Section]\n"
|
---|
119 | "key = value\n"
|
---|
120 | "key = value2\n"
|
---|
121 | "[Section]\n"
|
---|
122 | "key = val\n";
|
---|
123 |
|
---|
124 | int rc = ini_parse_string(data, &ini_conf, &parse);
|
---|
125 |
|
---|
126 | PCUT_ASSERT_INT_EQUALS(rc, EINVAL);
|
---|
127 | PCUT_ASSERT_TRUE(parse.has_error);
|
---|
128 |
|
---|
129 | link_t *li = list_first(&parse.errors);
|
---|
130 | text_parse_error_t *error = list_get_instance(li, text_parse_error_t, link);
|
---|
131 |
|
---|
132 | PCUT_ASSERT_INT_EQUALS(error->parse_errno, INI_EDUP_SECTION);
|
---|
133 | }
|
---|
134 |
|
---|
135 | PCUT_TEST(empty_section) {
|
---|
136 | const char *data =
|
---|
137 | "[Section1]\n"
|
---|
138 | "[Section2]\n"
|
---|
139 | "key = value\n"
|
---|
140 | "key2 = value2\n";
|
---|
141 |
|
---|
142 | int rc = ini_parse_string(data, &ini_conf, &parse);
|
---|
143 |
|
---|
144 | PCUT_ASSERT_INT_EQUALS(rc, EOK);
|
---|
145 |
|
---|
146 | ini_section_t *section = ini_get_section(&ini_conf, "Section1");
|
---|
147 | PCUT_ASSERT_TRUE(section != NULL);
|
---|
148 |
|
---|
149 | ini_item_iterator_t it = ini_section_get_iterator(section, "key");
|
---|
150 | PCUT_ASSERT_TRUE(!ini_item_iterator_valid(&it));
|
---|
151 | }
|
---|
152 |
|
---|
153 | PCUT_EXPORT(ini);
|
---|