add meson support
[protos/libecoli.git] / src / ecoli_node_space.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <ctype.h>
9
10 #include <ecoli_log.h>
11 #include <ecoli_test.h>
12 #include <ecoli_malloc.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_space.h>
18
19 EC_LOG_TYPE_REGISTER(node_space);
20
21 struct ec_node_space {
22         struct ec_node gen;
23 };
24
25 static int
26 ec_node_space_parse(const struct ec_node *gen_node,
27                 struct ec_parse *state,
28                 const struct ec_strvec *strvec)
29 {
30         const char *str;
31         size_t len = 0;
32
33         (void)state;
34         (void)gen_node;
35
36         if (ec_strvec_len(strvec) == 0)
37                 return EC_PARSE_NOMATCH;
38
39         str = ec_strvec_val(strvec, 0);
40         while (isspace(str[len]))
41                 len++;
42         if (len == 0 || len != strlen(str))
43                 return EC_PARSE_NOMATCH;
44
45         return 1;
46 }
47
48 static struct ec_node_type ec_node_space_type = {
49         .name = "space",
50         .parse = ec_node_space_parse,
51         .complete = ec_node_complete_unknown,
52         .size = sizeof(struct ec_node_space),
53 };
54
55 EC_NODE_TYPE_REGISTER(ec_node_space_type);
56
57 /* LCOV_EXCL_START */
58 static int ec_node_space_testcase(void)
59 {
60         struct ec_node *node;
61         int testres = 0;
62
63         node = ec_node("space", EC_NO_ID);
64         if (node == NULL) {
65                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
66                 return -1;
67         }
68         testres |= EC_TEST_CHECK_PARSE(node, 1, " ");
69         testres |= EC_TEST_CHECK_PARSE(node, 1, " ", "foo");
70         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
71         testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
72         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo ");
73         ec_node_free(node);
74
75         /* test completion */
76         node = ec_node("space", EC_NO_ID);
77         if (node == NULL) {
78                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
79                 return -1;
80         }
81         /* never completes whatever the input */
82         testres |= EC_TEST_CHECK_COMPLETE(node,
83                 "", EC_NODE_ENDLIST,
84                 EC_NODE_ENDLIST);
85         testres |= EC_TEST_CHECK_COMPLETE(node,
86                 " ", EC_NODE_ENDLIST,
87                 EC_NODE_ENDLIST);
88         testres |= EC_TEST_CHECK_COMPLETE(node,
89                 "foo", EC_NODE_ENDLIST,
90                 EC_NODE_ENDLIST);
91         ec_node_free(node);
92
93         return testres;
94 }
95 /* LCOV_EXCL_STOP */
96
97 static struct ec_test ec_node_space_test = {
98         .name = "space",
99         .test = ec_node_space_testcase,
100 };
101
102 EC_TEST_REGISTER(ec_node_space_test);