gargoyle/test/gargoyle.c

147 lines
4 KiB
C

#include <CUnit/Basic.h>
#include <gargoyle.h>
#include <gargoyle/test/gargoyle.h>
int init_suite_gargoyle(void) {
return CUE_SUCCESS;
}
int clean_suite_gargoyle(void) {
return CUE_SUCCESS;
}
void gargoyle_test_digest_argv(void) {
struct gargoyle_err err;
uint64_t uint_val = 0;
int64_t sint_val = 0;
char rope_val[20];
uint64_t smut_val = 0;
struct gargoyle_optn optv[] = {
{ GARGOYLE_EZ_OPTN("uint", uint_val), GARGOYLE_TYPE_UINT },
{ GARGOYLE_EZ_OPTN("sint", sint_val), GARGOYLE_TYPE_SINT },
{ GARGOYLE_CS_OPTN("rope", rope_val), GARGOYLE_TYPE_ROPE },
{ GARGOYLE_EM_OPTN("smut", 'S', smut_val), GARGOYLE_TYPE_UINT }
};
uint16_t optc = sizeof(optv) / sizeof(struct gargoyle_optn);
char *normal_args[] = {
"gargoyle",
"--uint",
"42",
"--sint=-42",
"--",
"--foobar",
NULL
};
int argc = sizeof(normal_args) / sizeof(char *);
char **argv = normal_args;
uint8_t res = gargoyle_digest_argv(optc, optv, &argc, &argv, &err, 0);
CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS);
CU_ASSERT_EQUAL(uint_val, 42);
CU_ASSERT_EQUAL(sint_val, -42);
CU_ASSERT_NSTRING_EQUAL(*argv, "--foobar", 8);
}
void gargoyle_test_digest_envh(void) {
const char prefix[] = "GARGOYLE";
const uint16_t prefix_sz = sizeof(prefix) - 1;
struct gargoyle_err err;
uint64_t uint_val = 0;
int64_t sint_val = 0;
struct gargoyle_optn optv[] = {
{ GARGOYLE_EZ_OPTN("uint", uint_val), GARGOYLE_TYPE_UINT },
{ GARGOYLE_EZ_OPTN("sint", sint_val), GARGOYLE_TYPE_SINT }
};
uint16_t optc = sizeof(optv) / sizeof(struct gargoyle_optn);
char *dirty_env[] = {
"GARGOYLE_*(#&(%&)*=",
"==============",
"GARGOYLE_\0=straw",
NULL
};
char **env = dirty_env;
uint8_t res = gargoyle_digest_envh(optc, optv, prefix, prefix_sz, &env, &err, 0);
CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS);
env = dirty_env;
res = gargoyle_digest_envh(optc, optv, prefix, prefix_sz, &env, &err, GARGOYLE_FLG_STRCT);
CU_ASSERT_EQUAL(res, GARGOYLE_ERR_UNKNOWN_OPTN);
char *normal_env[] = {
"SHELL=/bin/bash",
"PWD=/Users/uplime/gargoyle",
"HOME=/Users/uplime",
"LANG=en_US.UTF-8",
"TMPDIR=/tmp",
"USER=uplime",
"PAGER=less",
"PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:.",
NULL
};
env = normal_env;
res = gargoyle_digest_envh(optc, optv, prefix, prefix_sz, &env, &err, 0);
CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS);
env = normal_env;
res = gargoyle_digest_envh(optc, optv, prefix, prefix_sz, &env, &err, GARGOYLE_FLG_STRCT);
CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS);
char *option_env[] = {
"SHELL=/bin/bash",
"PWD=/Users/uplime/gargoyle",
"HOME=/Users/uplime",
"LANG=en_US.UTF-8",
"TMPDIR=/tmp",
"USER=uplime",
"PAGER=less",
"PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:.",
"GARGOYLE_sint=-2022",
"GARGOYLE_uint=42",
"GARGOYLE_UINT=43",
"GARGOYLE_NOT_AN_OPTN=racket",
NULL
};
env = option_env;
res = gargoyle_digest_envh(optc, optv, prefix, prefix_sz, &env, &err, 0);
CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS);
CU_ASSERT_EQUAL(uint_val, 42);
CU_ASSERT_EQUAL(sint_val, -2022);
sint_val = 0;
env = option_env;
res = gargoyle_digest_envh(optc, optv, prefix, prefix_sz, &env, &err, GARGOYLE_FLG_BCASE);
CU_ASSERT_EQUAL(res, GARGOYLE_ERR_SUCCESS);
CU_ASSERT_EQUAL(uint_val, 43);
CU_ASSERT_EQUAL(sint_val, -2022);
env = option_env;
res = gargoyle_digest_envh(optc, optv, prefix, prefix_sz, &env, &err, GARGOYLE_FLG_STRCT);
CU_ASSERT_EQUAL(res, GARGOYLE_ERR_UNKNOWN_OPTN);
char *uint_env[] = {
"GARGOYLE_UINT=43l",
NULL
};
env = uint_env;
res = gargoyle_digest_envh(optc, optv, prefix, prefix_sz, &env, &err, GARGOYLE_FLG_BCASE);
CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_UINT);
char *sint_env[] = {
"GARGOYLE_SINT=thunderfury",
NULL
};
env = sint_env;
res = gargoyle_digest_envh(optc, optv, prefix, prefix_sz, &env, &err, GARGOYLE_FLG_BCASE);
CU_ASSERT_EQUAL(res, GARGOYLE_ERR_INVALID_SINT);
}