tests more consistent
[protos/libecoli.git] / lib / ecoli_node_option.c
1 /*
2  * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <stdarg.h>
33 #include <errno.h>
34
35 #include <ecoli_malloc.h>
36 #include <ecoli_log.h>
37 #include <ecoli_strvec.h>
38 #include <ecoli_node.h>
39 #include <ecoli_parsed.h>
40 #include <ecoli_completed.h>
41 #include <ecoli_node_str.h>
42 #include <ecoli_test.h>
43 #include <ecoli_node_option.h>
44
45 EC_LOG_TYPE_REGISTER(node_option);
46
47 struct ec_node_option {
48         struct ec_node gen;
49         struct ec_node *child;
50 };
51
52 static int
53 ec_node_option_parse(const struct ec_node *gen_node,
54                 struct ec_parsed *state,
55                 const struct ec_strvec *strvec)
56 {
57         struct ec_node_option *node = (struct ec_node_option *)gen_node;
58         int ret;
59
60         ret = ec_node_parse_child(node->child, state, strvec);
61         if (ret < 0)
62                 return ret;
63
64         if (ret == EC_PARSED_NOMATCH)
65                 return 0;
66
67         return ret;
68 }
69
70 static int
71 ec_node_option_complete(const struct ec_node *gen_node,
72                         struct ec_completed *completed,
73                         const struct ec_strvec *strvec)
74 {
75         struct ec_node_option *node = (struct ec_node_option *)gen_node;
76
77         return ec_node_complete_child(node->child, completed, strvec);
78 }
79
80 static void ec_node_option_free_priv(struct ec_node *gen_node)
81 {
82         struct ec_node_option *node = (struct ec_node_option *)gen_node;
83
84         ec_node_free(node->child);
85 }
86
87 static struct ec_node_type ec_node_option_type = {
88         .name = "option",
89         .parse = ec_node_option_parse,
90         .complete = ec_node_option_complete,
91         .size = sizeof(struct ec_node_option),
92         .free_priv = ec_node_option_free_priv,
93 };
94
95 EC_NODE_TYPE_REGISTER(ec_node_option_type);
96
97 int ec_node_option_set(struct ec_node *gen_node, struct ec_node *child)
98 {
99         struct ec_node_option *node = (struct ec_node_option *)gen_node;
100
101         if (gen_node == NULL || child == NULL) {
102                 errno = EINVAL;
103                 goto fail;
104         }
105
106         if (ec_node_check_type(gen_node, &ec_node_option_type) < 0)
107                 goto fail;
108
109         if (ec_node_add_child(gen_node, child) < 0)
110                 goto fail;
111
112         node->child = child;
113
114         return 0;
115
116 fail:
117         ec_node_free(child);
118         return -1;
119 }
120
121 struct ec_node *ec_node_option(const char *id, struct ec_node *child)
122 {
123         struct ec_node *gen_node = NULL;
124
125         if (child == NULL)
126                 goto fail;
127
128         gen_node = __ec_node(&ec_node_option_type, id);
129         if (gen_node == NULL)
130                 goto fail;
131
132         ec_node_option_set(gen_node, child);
133         child = NULL;
134
135         return gen_node;
136
137 fail:
138         ec_node_free(child);
139         return NULL;
140 }
141
142 /* LCOV_EXCL_START */
143 static int ec_node_option_testcase(void)
144 {
145         struct ec_node *node;
146         int testres = 0;
147
148         node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
149         if (node == NULL) {
150                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
151                 return -1;
152         }
153         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
154         testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
155         testres |= EC_TEST_CHECK_PARSE(node, 0, "bar");
156         testres |= EC_TEST_CHECK_PARSE(node, 0);
157         ec_node_free(node);
158
159         /* test completion */
160         node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
161         if (node == NULL) {
162                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
163                 return -1;
164         }
165         testres |= EC_TEST_CHECK_COMPLETE(node,
166                 "", EC_NODE_ENDLIST,
167                 "foo", EC_NODE_ENDLIST);
168         testres |= EC_TEST_CHECK_COMPLETE(node,
169                 "f", EC_NODE_ENDLIST,
170                 "foo", EC_NODE_ENDLIST);
171         testres |= EC_TEST_CHECK_COMPLETE(node,
172                 "b", EC_NODE_ENDLIST,
173                 EC_NODE_ENDLIST);
174         ec_node_free(node);
175
176         return testres;
177 }
178 /* LCOV_EXCL_STOP */
179
180 static struct ec_test ec_node_option_test = {
181         .name = "node_option",
182         .test = ec_node_option_testcase,
183 };
184
185 EC_TEST_REGISTER(ec_node_option_test);