1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
10 #include <ecoli_log.h>
11 #include <ecoli_malloc.h>
12 #include <ecoli_test.h>
13 #include <ecoli_strvec.h>
14 #include <ecoli_node.h>
15 #include <ecoli_parse.h>
16 #include <ecoli_complete.h>
17 #include <ecoli_node_str.h>
19 EC_LOG_TYPE_REGISTER(node_str);
28 ec_node_str_parse(const struct ec_node *gen_node,
29 struct ec_parse *state,
30 const struct ec_strvec *strvec)
32 struct ec_node_str *node = (struct ec_node_str *)gen_node;
37 if (ec_strvec_len(strvec) == 0)
38 return EC_PARSE_NOMATCH;
40 str = ec_strvec_val(strvec, 0);
41 if (strcmp(str, node->string) != 0)
42 return EC_PARSE_NOMATCH;
48 ec_node_str_complete(const struct ec_node *gen_node,
50 const struct ec_strvec *strvec)
52 struct ec_node_str *node = (struct ec_node_str *)gen_node;
56 if (ec_strvec_len(strvec) != 1)
59 str = ec_strvec_val(strvec, 0);
60 for (n = 0; n < node->len; n++) {
61 if (str[n] != node->string[n])
67 return EC_PARSE_NOMATCH;
69 if (ec_comp_add_item(comp, gen_node, NULL, EC_COMP_FULL,
70 str, node->string) < 0)
76 static const char *ec_node_str_desc(const struct ec_node *gen_node)
78 struct ec_node_str *node = (struct ec_node_str *)gen_node;
83 static void ec_node_str_free_priv(struct ec_node *gen_node)
85 struct ec_node_str *node = (struct ec_node_str *)gen_node;
87 ec_free(node->string);
90 static struct ec_node_type ec_node_str_type = {
92 .parse = ec_node_str_parse,
93 .complete = ec_node_str_complete,
94 .desc = ec_node_str_desc,
95 .size = sizeof(struct ec_node_str),
96 .free_priv = ec_node_str_free_priv,
99 EC_NODE_TYPE_REGISTER(ec_node_str_type);
101 int ec_node_str_set_str(struct ec_node *gen_node, const char *str)
103 struct ec_node_str *node = (struct ec_node_str *)gen_node;
107 ret = ec_node_check_type(gen_node, &ec_node_str_type);
120 ec_free(node->string);
122 node->len = strlen(node->string);
127 struct ec_node *ec_node_str(const char *id, const char *str)
129 struct ec_node *gen_node = NULL;
131 gen_node = __ec_node(&ec_node_str_type, id);
132 if (gen_node == NULL)
135 if (ec_node_str_set_str(gen_node, str) < 0)
141 ec_node_free(gen_node);
145 /* LCOV_EXCL_START */
146 static int ec_node_str_testcase(void)
148 struct ec_node *node;
151 node = ec_node_str(EC_NO_ID, "foo");
153 EC_LOG(EC_LOG_ERR, "cannot create node\n");
156 testres |= EC_TEST_CHECK(!strcmp(ec_node_desc(node), "foo"),
157 "Invalid node description.");
158 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
159 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
160 testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
161 testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
162 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
165 node = ec_node_str(EC_NO_ID, "Здравствуйте");
167 EC_LOG(EC_LOG_ERR, "cannot create node\n");
170 testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте");
171 testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте",
173 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
174 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
177 /* an empty string node always matches */
178 node = ec_node_str(EC_NO_ID, "");
180 EC_LOG(EC_LOG_ERR, "cannot create node\n");
183 testres |= EC_TEST_CHECK_PARSE(node, 1, "");
184 testres |= EC_TEST_CHECK_PARSE(node, 1, "", "foo");
185 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
188 /* test completion */
189 node = ec_node_str(EC_NO_ID, "foo");
191 EC_LOG(EC_LOG_ERR, "cannot create node\n");
194 testres |= EC_TEST_CHECK_COMPLETE(node,
197 testres |= EC_TEST_CHECK_COMPLETE(node,
199 "foo", EC_NODE_ENDLIST);
200 testres |= EC_TEST_CHECK_COMPLETE(node,
201 "f", EC_NODE_ENDLIST,
202 "foo", EC_NODE_ENDLIST);
203 testres |= EC_TEST_CHECK_COMPLETE(node,
204 "foo", EC_NODE_ENDLIST,
205 "foo", EC_NODE_ENDLIST);
206 testres |= EC_TEST_CHECK_COMPLETE(node,
207 "x", EC_NODE_ENDLIST,
215 static struct ec_test ec_node_str_test = {
217 .test = ec_node_str_testcase,
220 EC_TEST_REGISTER(ec_node_str_test);