4ab6ad32540862267be914cbb290181dc9556ced
[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 };
23
24 static int
25 ec_node_space_parse(const struct ec_node *node,
26                 struct ec_pnode *state,
27                 const struct ec_strvec *strvec)
28 {
29         const char *str;
30         size_t len = 0;
31
32         (void)state;
33         (void)node;
34
35         if (ec_strvec_len(strvec) == 0)
36                 return EC_PARSE_NOMATCH;
37
38         str = ec_strvec_val(strvec, 0);
39         while (isspace(str[len]))
40                 len++;
41         if (len == 0 || len != strlen(str))
42                 return EC_PARSE_NOMATCH;
43
44         return 1;
45 }
46
47 static struct ec_node_type ec_node_space_type = {
48         .name = "space",
49         .parse = ec_node_space_parse,
50         .complete = ec_complete_unknown,
51         .size = sizeof(struct ec_node_space),
52 };
53
54 EC_NODE_TYPE_REGISTER(ec_node_space_type);
55
56 /* LCOV_EXCL_START */
57 static int ec_node_space_testcase(void)
58 {
59         struct ec_node *node;
60         int testres = 0;
61
62         node = ec_node("space", EC_NO_ID);
63         if (node == NULL) {
64                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
65                 return -1;
66         }
67         testres |= EC_TEST_CHECK_PARSE(node, 1, " ");
68         testres |= EC_TEST_CHECK_PARSE(node, 1, " ", "foo");
69         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
70         testres |= EC_TEST_CHECK_PARSE(node, -1, " foo");
71         testres |= EC_TEST_CHECK_PARSE(node, -1, "foo ");
72         ec_node_free(node);
73
74         /* test completion */
75         node = ec_node("space", EC_NO_ID);
76         if (node == NULL) {
77                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
78                 return -1;
79         }
80         /* never completes whatever the input */
81         testres |= EC_TEST_CHECK_COMPLETE(node,
82                 "", EC_VA_END,
83                 EC_VA_END);
84         testres |= EC_TEST_CHECK_COMPLETE(node,
85                 " ", EC_VA_END,
86                 EC_VA_END);
87         testres |= EC_TEST_CHECK_COMPLETE(node,
88                 "foo", EC_VA_END,
89                 EC_VA_END);
90         ec_node_free(node);
91
92         return testres;
93 }
94 /* LCOV_EXCL_STOP */
95
96 static struct ec_test ec_node_space_test = {
97         .name = "space",
98         .test = ec_node_space_testcase,
99 };
100
101 EC_TEST_REGISTER(ec_node_space_test);