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_test.h>
15 #include <ecoli_strvec.h>
16 #include <ecoli_node.h>
17 #include <ecoli_parse.h>
18 #include <ecoli_complete.h>
19 #include <ecoli_node_str.h>
20 #include <ecoli_node_option.h>
21 #include <ecoli_node_weakref.h>
22 #include <ecoli_node_int.h>
23 #include <ecoli_node_seq.h>
24 #include <ecoli_node_or.h>
26 EC_LOG_TYPE_REGISTER(node_weakref);
28 struct ec_node_weakref {
30 struct ec_node *child;
34 ec_node_weakref_parse(const struct ec_node *gen_node,
35 struct ec_parse *state,
36 const struct ec_strvec *strvec)
38 struct ec_node_weakref *node = (struct ec_node_weakref *)gen_node;
40 return ec_node_parse_child(node->child, state, strvec);
44 ec_node_weakref_complete(const struct ec_node *gen_node,
46 const struct ec_strvec *strvec)
48 struct ec_node_weakref *node = (struct ec_node_weakref *)gen_node;
50 return ec_node_complete_child(node->child, comp, strvec);
53 static struct ec_node_type ec_node_weakref_type = {
55 .parse = ec_node_weakref_parse,
56 .complete = ec_node_weakref_complete,
57 .size = sizeof(struct ec_node_weakref),
60 EC_NODE_TYPE_REGISTER(ec_node_weakref_type);
62 int ec_node_weakref_set(struct ec_node *gen_node, struct ec_node *child)
64 struct ec_node_weakref *node = (struct ec_node_weakref *)gen_node;
73 if (ec_node_check_type(gen_node, &ec_node_weakref_type) < 0)
81 /* do not free child */
85 struct ec_node *ec_node_weakref(const char *id, struct ec_node *child)
87 struct ec_node *gen_node = NULL;
92 gen_node = __ec_node(&ec_node_weakref_type, id);
96 ec_node_weakref_set(gen_node, child);
101 /* LCOV_EXCL_START */
102 static int ec_node_weakref_testcase(void)
104 struct ec_node *weak = NULL, *expr = NULL, *val = NULL;
105 struct ec_node *seq = NULL, *op = NULL;
108 expr = ec_node("or", EC_NO_ID);
109 val = ec_node_int(EC_NO_ID, 0, 10, 10);
110 op = ec_node_str(EC_NO_ID, "!");
111 weak = ec_node_weakref(EC_NO_ID, expr);
112 if (weak == NULL || expr == NULL || val == NULL || op == NULL)
114 seq = EC_NODE_SEQ(EC_NO_ID, op, weak);
118 if (ec_node_or_add(expr, seq) < 0)
121 if (ec_node_or_add(expr, val) < 0)
125 testres |= EC_TEST_CHECK_PARSE(expr, 1, "1");
126 testres |= EC_TEST_CHECK_PARSE(expr, 2, "!", "1");
127 testres |= EC_TEST_CHECK_PARSE(expr, 3, "!", "!", "1");
129 testres |= EC_TEST_CHECK_COMPLETE(expr,
131 "!", EC_NODE_ENDLIST);
132 testres |= EC_TEST_CHECK_COMPLETE(expr,
133 "!", "", EC_NODE_ENDLIST,
134 "!", EC_NODE_ENDLIST);
150 static struct ec_test ec_node_weakref_test = {
151 .name = "node_weakref",
152 .test = ec_node_weakref_testcase,
155 EC_TEST_REGISTER(ec_node_weakref_test);