1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
12 #include <ecoli_malloc.h>
13 #include <ecoli_log.h>
14 #include <ecoli_strvec.h>
15 #include <ecoli_node.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_node_str.h>
19 #include <ecoli_test.h>
20 #include <ecoli_node_option.h>
22 EC_LOG_TYPE_REGISTER(node_option);
24 struct ec_node_option {
26 struct ec_node *child;
30 ec_node_option_parse(const struct ec_node *gen_node,
31 struct ec_parse *state,
32 const struct ec_strvec *strvec)
34 struct ec_node_option *node = (struct ec_node_option *)gen_node;
37 ret = ec_node_parse_child(node->child, state, strvec);
41 if (ret == EC_PARSE_NOMATCH)
48 ec_node_option_complete(const struct ec_node *gen_node,
50 const struct ec_strvec *strvec)
52 struct ec_node_option *node = (struct ec_node_option *)gen_node;
54 return ec_node_complete_child(node->child, comp, strvec);
57 static void ec_node_option_free_priv(struct ec_node *gen_node)
59 struct ec_node_option *node = (struct ec_node_option *)gen_node;
61 ec_node_free(node->child);
65 ec_node_option_get_children_count(const struct ec_node *gen_node)
67 struct ec_node_option *node = (struct ec_node_option *)gen_node;
74 static struct ec_node *
75 ec_node_option_get_child(const struct ec_node *gen_node, size_t i)
77 struct ec_node_option *node = (struct ec_node_option *)gen_node;
85 static struct ec_node_type ec_node_option_type = {
87 .parse = ec_node_option_parse,
88 .complete = ec_node_option_complete,
89 .size = sizeof(struct ec_node_option),
90 .free_priv = ec_node_option_free_priv,
91 .get_children_count = ec_node_option_get_children_count,
92 .get_child = ec_node_option_get_child,
95 EC_NODE_TYPE_REGISTER(ec_node_option_type);
97 int ec_node_option_set(struct ec_node *gen_node, struct ec_node *child)
99 struct ec_node_option *node = (struct ec_node_option *)gen_node;
101 if (gen_node == NULL || child == NULL) {
106 if (ec_node_check_type(gen_node, &ec_node_option_type) < 0)
118 struct ec_node *ec_node_option(const char *id, struct ec_node *child)
120 struct ec_node *gen_node = NULL;
125 gen_node = __ec_node(&ec_node_option_type, id);
126 if (gen_node == NULL)
129 ec_node_option_set(gen_node, child);
139 /* LCOV_EXCL_START */
140 static int ec_node_option_testcase(void)
142 struct ec_node *node;
145 node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
147 EC_LOG(EC_LOG_ERR, "cannot create node\n");
150 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
151 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
152 testres |= EC_TEST_CHECK_PARSE(node, 0, "bar");
153 testres |= EC_TEST_CHECK_PARSE(node, 0);
156 /* test completion */
157 node = ec_node_option(EC_NO_ID, ec_node_str(EC_NO_ID, "foo"));
159 EC_LOG(EC_LOG_ERR, "cannot create node\n");
162 testres |= EC_TEST_CHECK_COMPLETE(node,
164 "foo", EC_NODE_ENDLIST);
165 testres |= EC_TEST_CHECK_COMPLETE(node,
166 "f", EC_NODE_ENDLIST,
167 "foo", EC_NODE_ENDLIST);
168 testres |= EC_TEST_CHECK_COMPLETE(node,
169 "b", EC_NODE_ENDLIST,
177 static struct ec_test ec_node_option_test = {
178 .name = "node_option",
179 .test = ec_node_option_testcase,
182 EC_TEST_REGISTER(ec_node_option_test);