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_config.h>
16 #include <ecoli_parse.h>
17 #include <ecoli_complete.h>
18 #include <ecoli_node_str.h>
20 EC_LOG_TYPE_REGISTER(node_str);
29 ec_node_str_parse(const struct ec_node *gen_node,
30 struct ec_parse *state,
31 const struct ec_strvec *strvec)
33 struct ec_node_str *node = (struct ec_node_str *)gen_node;
38 if (node->string == NULL) {
43 if (ec_strvec_len(strvec) == 0)
44 return EC_PARSE_NOMATCH;
46 str = ec_strvec_val(strvec, 0);
47 if (strcmp(str, node->string) != 0)
48 return EC_PARSE_NOMATCH;
54 ec_node_str_complete(const struct ec_node *gen_node,
56 const struct ec_strvec *strvec)
58 struct ec_node_str *node = (struct ec_node_str *)gen_node;
62 if (ec_strvec_len(strvec) != 1)
65 str = ec_strvec_val(strvec, 0);
66 for (n = 0; n < node->len; n++) {
67 if (str[n] != node->string[n])
73 return EC_PARSE_NOMATCH;
75 if (ec_comp_add_item(comp, gen_node, NULL, EC_COMP_FULL,
76 str, node->string) < 0)
82 static const char *ec_node_str_desc(const struct ec_node *gen_node)
84 struct ec_node_str *node = (struct ec_node_str *)gen_node;
89 static void ec_node_str_free_priv(struct ec_node *gen_node)
91 struct ec_node_str *node = (struct ec_node_str *)gen_node;
93 ec_free(node->string);
96 static const struct ec_config_schema ec_node_str_schema[] = {
99 .desc = "The string to match.",
100 .type = EC_CONFIG_TYPE_STRING,
104 static int ec_node_str_set_config(struct ec_node *gen_node,
105 const struct ec_config *config)
107 struct ec_node_str *node = (struct ec_node_str *)gen_node;
108 const struct ec_config *value = NULL;
111 value = ec_config_dict_get(config, "string");
117 s = ec_strdup(value->string);
121 ec_free(node->string);
123 node->len = strlen(node->string);
132 static struct ec_node_type ec_node_str_type = {
134 .schema = ec_node_str_schema,
135 .schema_len = EC_COUNT_OF(ec_node_str_schema),
136 .set_config = ec_node_str_set_config,
137 .parse = ec_node_str_parse,
138 .complete = ec_node_str_complete,
139 .desc = ec_node_str_desc,
140 .size = sizeof(struct ec_node_str),
141 .free_priv = ec_node_str_free_priv,
144 EC_NODE_TYPE_REGISTER(ec_node_str_type);
146 int ec_node_str_set_str(struct ec_node *gen_node, const char *str)
148 struct ec_config *config = NULL;
151 if (ec_node_check_type(gen_node, &ec_node_str_type) < 0)
159 config = ec_config_dict();
163 ret = ec_config_dict_set(config, "string", ec_config_string(str));
167 ret = ec_node_set_config(gen_node, config);
175 ec_config_free(config);
179 struct ec_node *ec_node_str(const char *id, const char *str)
181 struct ec_node *gen_node = NULL;
183 gen_node = __ec_node(&ec_node_str_type, id);
184 if (gen_node == NULL)
187 if (ec_node_str_set_str(gen_node, str) < 0)
193 ec_node_free(gen_node);
197 /* LCOV_EXCL_START */
198 static int ec_node_str_testcase(void)
200 struct ec_node *node;
203 node = ec_node_str(EC_NO_ID, "foo");
205 EC_LOG(EC_LOG_ERR, "cannot create node\n");
208 testres |= EC_TEST_CHECK(!strcmp(ec_node_desc(node), "foo"),
209 "Invalid node description.");
210 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo");
211 testres |= EC_TEST_CHECK_PARSE(node, 1, "foo", "bar");
212 testres |= EC_TEST_CHECK_PARSE(node, -1, "foobar");
213 testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
214 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
217 node = ec_node_str(EC_NO_ID, "Здравствуйте");
219 EC_LOG(EC_LOG_ERR, "cannot create node\n");
222 testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте");
223 testres |= EC_TEST_CHECK_PARSE(node, 1, "Здравствуйте",
225 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
226 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
229 /* an empty string node always matches */
230 node = ec_node_str(EC_NO_ID, "");
232 EC_LOG(EC_LOG_ERR, "cannot create node\n");
235 testres |= EC_TEST_CHECK_PARSE(node, 1, "");
236 testres |= EC_TEST_CHECK_PARSE(node, 1, "", "foo");
237 testres |= EC_TEST_CHECK_PARSE(node, -1, "foo");
240 /* test completion */
241 node = ec_node_str(EC_NO_ID, "foo");
243 EC_LOG(EC_LOG_ERR, "cannot create node\n");
246 testres |= EC_TEST_CHECK_COMPLETE(node,
249 testres |= EC_TEST_CHECK_COMPLETE(node,
251 "foo", EC_NODE_ENDLIST);
252 testres |= EC_TEST_CHECK_COMPLETE(node,
253 "f", EC_NODE_ENDLIST,
254 "foo", EC_NODE_ENDLIST);
255 testres |= EC_TEST_CHECK_COMPLETE(node,
256 "foo", EC_NODE_ENDLIST,
257 "foo", EC_NODE_ENDLIST);
258 testres |= EC_TEST_CHECK_COMPLETE(node,
259 "x", EC_NODE_ENDLIST,
267 static struct ec_test ec_node_str_test = {
269 .test = ec_node_str_testcase,
272 EC_TEST_REGISTER(ec_node_str_test);