2021-07-30 23:57:32 -04:00
|
|
|
#include "greatest.h"
|
|
|
|
#include "../exec.h"
|
|
|
|
|
|
|
|
#define mkfun(f, n) \
|
2021-07-31 20:36:22 -04:00
|
|
|
int f(struct libscomm_line *x __attribute__((unused)), \
|
|
|
|
void *ptr __attribute__((unused))) { \
|
2021-07-30 23:57:32 -04:00
|
|
|
return n; \
|
|
|
|
}
|
|
|
|
|
|
|
|
mkfun(foo, LIBSCOMM_CMD_OK)
|
|
|
|
mkfun(bar, 1)
|
|
|
|
mkfun(ocmd, 2)
|
|
|
|
mkfun(baz, 3)
|
|
|
|
mkfun(quox, 4)
|
|
|
|
mkfun(test1, 5)
|
|
|
|
mkfun(apple, 6)
|
|
|
|
mkfun(under, 7)
|
|
|
|
mkfun(rex, 8)
|
|
|
|
mkfun(yan, 9)
|
|
|
|
mkfun(val, 10)
|
|
|
|
mkfun(cal, 11)
|
|
|
|
|
|
|
|
struct libscomm_cmd cmdarr[] = {
|
|
|
|
{"apple", apple}, {"bar", bar}, {"baz", baz}, {"cal", cal},
|
|
|
|
{"foo", foo}, {"ocmd", ocmd}, {"quox", quox}, {"rex", rex},
|
|
|
|
{"test1", test1}, {"under", under}, {"val", val}, {"yan", yan}
|
|
|
|
};
|
|
|
|
#define arrlen(x) (sizeof(x) / sizeof((x)[0]))
|
|
|
|
const struct libscomm_cmd_store cmds = {cmdarr, arrlen(cmdarr)};
|
|
|
|
|
2021-07-31 20:36:22 -04:00
|
|
|
#define doexec(f, n) do{\
|
|
|
|
x.buf[0] = f; \
|
|
|
|
ASSERT_EQ(libscomm_exec(&cmds, &x, NULL), n); \
|
|
|
|
} while(0)
|
|
|
|
|
2021-07-30 23:57:32 -04:00
|
|
|
TEST execute_one(void) {
|
|
|
|
struct libscomm_line x = {0};
|
|
|
|
x.len = 1;
|
|
|
|
|
2021-07-31 20:36:22 -04:00
|
|
|
doexec("foo", LIBSCOMM_CMD_OK);
|
2021-07-30 23:57:32 -04:00
|
|
|
PASS();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST execute_multiple(void) {
|
|
|
|
struct libscomm_line x = {0};
|
|
|
|
x.len = 1;
|
|
|
|
|
|
|
|
doexec("foo", LIBSCOMM_CMD_OK);
|
|
|
|
doexec("bar", 1);
|
|
|
|
doexec("ocmd", 2);
|
|
|
|
doexec("baz", 3);
|
|
|
|
doexec("quox", 4);
|
|
|
|
doexec("test1", 5);
|
|
|
|
doexec("apple", 6);
|
|
|
|
doexec("under", 7);
|
|
|
|
doexec("rex", 8);
|
|
|
|
doexec("yan", 9);
|
|
|
|
doexec("val", 10);
|
|
|
|
doexec("cal", 11);
|
|
|
|
|
|
|
|
PASS();
|
|
|
|
}
|
|
|
|
|
2021-07-30 23:59:40 -04:00
|
|
|
TEST not_in(void) {
|
|
|
|
struct libscomm_line x = {0};
|
|
|
|
x.len = 1;
|
|
|
|
|
|
|
|
doexec("test2", LIBSCOMM_NOT_FOUND);
|
|
|
|
doexec("flub", LIBSCOMM_NOT_FOUND);
|
|
|
|
doexec("doexec", LIBSCOMM_NOT_FOUND);
|
|
|
|
|
|
|
|
PASS();
|
|
|
|
}
|
|
|
|
|
2021-07-30 23:57:32 -04:00
|
|
|
GREATEST_MAIN_DEFS();
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
GREATEST_MAIN_BEGIN();
|
|
|
|
|
|
|
|
RUN_TEST(execute_one);
|
|
|
|
RUN_TEST(execute_multiple);
|
2021-07-30 23:59:40 -04:00
|
|
|
RUN_TEST(not_in);
|
2021-07-30 23:57:32 -04:00
|
|
|
|
|
|
|
GREATEST_MAIN_END();
|
|
|
|
}
|