save
[protos/libecoli.git] / lib / ecoli_tk_space.c
index 20facf8..8b26d99 100644 (file)
 #include <string.h>
 #include <ctype.h>
 
+#include <ecoli_log.h>
+#include <ecoli_test.h>
 #include <ecoli_malloc.h>
+#include <ecoli_strvec.h>
 #include <ecoli_tk.h>
 #include <ecoli_tk_space.h>
 
+struct ec_tk_space {
+       struct ec_tk gen;
+};
+
 static struct ec_parsed_tk *ec_tk_space_parse(const struct ec_tk *gen_tk,
-       const char *str)
+       const struct ec_strvec *strvec)
 {
-       struct ec_parsed_tk *parsed_tk;
+       struct ec_parsed_tk *parsed_tk = NULL;
+       struct ec_strvec *match_strvec;
+       const char *str;
        size_t len = 0;
 
-       if (!isspace(str[0]))
-               return NULL;
-
-       parsed_tk = ec_parsed_tk_new(gen_tk);
+       parsed_tk = ec_parsed_tk_new();
        if (parsed_tk == NULL)
-               return NULL;
+               goto fail;
+
+       if (ec_strvec_len(strvec) == 0)
+               return parsed_tk;
 
+       str = ec_strvec_val(strvec, 0);
        while (isspace(str[len]))
                len++;
+       if (len == 0 || len != strlen(str))
+               return parsed_tk;
 
-       parsed_tk->str = ec_strndup(str, len);
+       match_strvec = ec_strvec_ndup(strvec, 0, 1);
+       if (match_strvec == NULL)
+               goto fail;
+
+       ec_parsed_tk_set_match(parsed_tk, gen_tk, match_strvec);
 
        return parsed_tk;
+
+ fail:
+       ec_parsed_tk_free(parsed_tk);
+       return NULL;
 }
 
-static struct ec_tk_ops ec_tk_space_ops = {
+static struct ec_tk_type ec_tk_space_type = {
+       .name = "space",
        .parse = ec_tk_space_parse,
+       .complete = ec_tk_default_complete,
 };
 
+EC_TK_TYPE_REGISTER(ec_tk_space_type);
+
 struct ec_tk *ec_tk_space_new(const char *id)
 {
-       return ec_tk_new(id, &ec_tk_space_ops, sizeof(struct ec_tk_space));
+       return ec_tk_new(id, &ec_tk_space_type, sizeof(struct ec_tk_space));
 }
 
+static int ec_tk_space_testcase(void)
+{
+       struct ec_tk *tk;
+       int ret = 0;
+
+       tk = ec_tk_space_new(NULL);
+       if (tk == NULL) {
+               ec_log(EC_LOG_ERR, "cannot create tk\n");
+               return -1;
+       }
+       ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, " ");
+       ret |= EC_TEST_CHECK_TK_PARSE(tk, 1, " ", "foo");
+       ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "");
+       ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, " foo");
+       ret |= EC_TEST_CHECK_TK_PARSE(tk, -1, "foo ");
+       ec_tk_free(tk);
+
+       /* test completion */
+       tk = ec_tk_space_new(NULL);
+       if (tk == NULL) {
+               ec_log(EC_LOG_ERR, "cannot create tk\n");
+               return -1;
+       }
+       ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
+               "", EC_TK_ENDLIST,
+               EC_TK_ENDLIST,
+               "");
+       ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
+               " ", EC_TK_ENDLIST,
+               EC_TK_ENDLIST,
+               "");
+       ret |= EC_TEST_CHECK_TK_COMPLETE(tk,
+               "foo", EC_TK_ENDLIST,
+               EC_TK_ENDLIST,
+               "");
+       ec_tk_free(tk);
+
+       return ret;
+}
+
+static struct ec_test ec_tk_space_test = {
+       .name = "tk_space",
+       .test = ec_tk_space_testcase,
+};
+
+EC_TEST_REGISTER(ec_tk_space_test);