From: alex Date: Sun, 25 Oct 2020 21:06:54 +0000 (-0700) Subject: removed hand-rolled opt parser X-Git-Tag: v1.0.0~3 X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=9d3d7017284c520b7a22c274e54245c2548ff3b6;p=workouts removed hand-rolled opt parser added getopt_long added new unit tests fixed integration tests accordingly added custom logging functionality --- diff --git a/Dockerfile b/Dockerfile index a131cd2..9c057c1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,7 +28,7 @@ WORKDIR /build COPY --from=core /workouts-${VERSION}/ /build/ RUN ./configure -RUN make check +RUN make check || cat test/unit/test-suite.log FROM node:latest as integration-tester ARG VERSION diff --git a/Makefile.am b/Makefile.am index c1173f4..fa9774e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -6,10 +6,10 @@ workouts_SOURCES = \ src/attr.c \ src/data/attr.c \ src/default.c \ + src/log.c \ src/ls.c \ src/main.c \ src/new.c \ - src/opt.c \ src/recent.c \ src/toggle.c \ src/usage.c \ @@ -17,13 +17,14 @@ workouts_SOURCES = \ src/data/setup.c \ src/data/workout.c \ src/opt/homedir.c \ - src/opt/rows.c \ - src/opt/verbose.c + src/opt/loglvl.c \ + src/opt/rows.c workouts_SOURCES += \ include/add.h \ include/attr.h \ include/data.h \ include/default.h \ + include/log.h \ include/ls.h \ include/main.h \ include/new.h \ diff --git a/include/add.h b/include/add.h index f120771..0c656be 100644 --- a/include/add.h +++ b/include/add.h @@ -6,8 +6,9 @@ #include #include +#include #include -int add(int,int,char**); +int add(int,char**); #endif \ No newline at end of file diff --git a/include/attr.h b/include/attr.h index 1a47e0d..3b51239 100644 --- a/include/attr.h +++ b/include/attr.h @@ -6,9 +6,17 @@ #include #include +#include #include -int attr(int,int,char**); +#define ATTR_ADD_STRING "add" +#define ATTR_LS_STRING "ls" +#define ATTR_MESSAGE_WRONG_NO_ARGS "wrong number of arguments for attr add\n" +#define ATTR_MESSAGE_UNKNOWN_COMMAND "unknown command attr \"%s\"\n" +#define ATTR_MESSAGE_ATTR_INSERT_FAILED "attribute insert failed\n" +#define ATTR_MESSAGE_ATTR_ADDED "New attribute added: %s\n" + +int attr(int,char**); int attr_add(char*); int attr_ls(); void print_attr(const unsigned char*); diff --git a/include/default.h b/include/default.h index 7706e2e..5def141 100644 --- a/include/default.h +++ b/include/default.h @@ -4,6 +4,7 @@ #include #include +#include #include int defaults(); diff --git a/include/log.h b/include/log.h new file mode 100644 index 0000000..6f5e669 --- /dev/null +++ b/include/log.h @@ -0,0 +1,22 @@ +#ifndef __LOG_H_ +#define __LOG_H_ + +#include +#include + +extern int verbose_flag; + +enum log_level { + LOG_LEVEL_SILENT = 0, /* suppresses all output */ + LOG_LEVEL_ERRORS = 1, /* only prints errors */ + LOG_LEVEL_DEFAULT = 2, /* normal output */ + LOG_LEVEL_VERBOSE = 3 /* logging and debugging info */ +}; + +#define log_err(...) log_message(LOG_LEVEL_ERRORS,stderr,__VA_ARGS__) +#define log_info(...) log_message(LOG_LEVEL_VERBOSE,stdout,__VA_ARGS__) +#define log_msg(...) log_message(LOG_LEVEL_DEFAULT,stdout,__VA_ARGS__) + +void log_message(enum log_level,FILE*,const char*,...); + +#endif \ No newline at end of file diff --git a/include/ls.h b/include/ls.h index d87e3ef..a1490b4 100644 --- a/include/ls.h +++ b/include/ls.h @@ -1,6 +1,7 @@ #ifndef __LS_H_ #define __LS_H_ +#include #include #include #include @@ -8,7 +9,7 @@ #include #include -int ls(int,int,char**); +int ls(int,char**); void print_header(const unsigned char*); void print_workout(const unsigned char*,int,const unsigned char*); diff --git a/include/main.h b/include/main.h index 7d60911..30c865a 100644 --- a/include/main.h +++ b/include/main.h @@ -1,6 +1,7 @@ #ifndef __MAIN_H_ #define __MAIN_H_ +#include #include #include diff --git a/include/new.h b/include/new.h index 4040060..9880eaa 100644 --- a/include/new.h +++ b/include/new.h @@ -8,6 +8,10 @@ #include #include -int new_workout(int,int,char**); +#define NEW_MESSAGE_NUM_ATTRS_MISMATCH "number of attributes don't match\n" +#define NEW_MESSAGE_INSERT_FAILED "workout insert failed\n" +#define NEW_MESSAGE_WORKOUT_ADDED "New workout added: %s\n" + +int new_workout(int,char**); #endif \ No newline at end of file diff --git a/include/opt.h b/include/opt.h index 980d45f..db46dff 100644 --- a/include/opt.h +++ b/include/opt.h @@ -7,32 +7,21 @@ #include #include +#include + // global options struct options { char *db_location; char *homedir; int rows; - unsigned char verbose; + enum log_level verbose; }; extern struct options global_opts; -enum option_code { - OPTION_ERROR, - OPTION_EXIT, - OPTION_HELP, - OPTION_INVALID, - OPTION_NO_MORE, - OPTION_SUCCESS -}; - -int option(char*); -int long_option(char*); -int short_option(char*); - // specific option setters -int set_homedir(char*); -void set_rows(int); -void set_verbose(unsigned char); +int opt_set_homedir(char*); +void opt_set_rows(int); +void opt_set_log_level(enum log_level); #endif \ No newline at end of file diff --git a/include/recent.h b/include/recent.h index fe5337b..fd99363 100644 --- a/include/recent.h +++ b/include/recent.h @@ -5,7 +5,9 @@ #include +#define RECENT_COMMAND_FAILED "command failed\n" + void print_recent(const unsigned char*,const unsigned char*); -int recent(int,int,char**); +int recent(); #endif \ No newline at end of file diff --git a/include/toggle.h b/include/toggle.h index 8e7cce8..1c03d3b 100644 --- a/include/toggle.h +++ b/include/toggle.h @@ -7,6 +7,10 @@ #include #include -int toggle(int,int,char**); +#define TOGGLE_MESSAGE_WRONG_NUM_ARGS "wrong number of arguments for toggle\n" +#define TOGGLE_MESSAGE_UNABLE "unable to toggle attribute %s for workout %s\n" +#define TOGGLE_MESSAGE_SUCCESS "Successfully toggled %s attribute for workout %s\n" + +int toggle(int,char**); #endif \ No newline at end of file diff --git a/include/usage.h b/include/usage.h index 70ae527..97a1b21 100644 --- a/include/usage.h +++ b/include/usage.h @@ -3,6 +3,8 @@ #include +#include + void usage(); #endif \ No newline at end of file diff --git a/src/add.c b/src/add.c index bbad5b1..636ada6 100644 --- a/src/add.c +++ b/src/add.c @@ -1,15 +1,15 @@ #include -int add(int i, int argc, char **argv) { - if(i==argc) { - printf("`workouts add` requires at least 1 argument\n"); +int add(int argc, char **argv) { + if(1==argc) { + log_err("`workouts add` requires at least 1 argument\n"); usage(); return EXIT_FAILURE; } char buf[11]; - if(i+1==argc) { // no date given + if(2==argc) { // no date given time_t t = time(NULL); struct tm now = *localtime(&t); if(strftime(buf,11,"%Y-%m-%d",&now)==0) { return EXIT_FAILURE; } @@ -17,8 +17,8 @@ int add(int i, int argc, char **argv) { int YY, MM, DD; struct tm when = {0}; - if(sscanf(argv[i+1],"%d-%d-%d", &YY, &MM, &DD)!=3) { - printf("invalid date given\n"); + if(sscanf(argv[2],"%d-%d-%d", &YY, &MM, &DD)!=3) { + log_err("invalid date given\n"); return EXIT_FAILURE; } @@ -28,12 +28,12 @@ int add(int i, int argc, char **argv) { if(strftime(buf,11,"%Y-%m-%d",&when)==0) { return EXIT_FAILURE; } } - if(recent_insert(argv[i],buf)<0) { - printf("add failed\n"); + if(recent_insert(argv[1],buf)<0) { + log_err("add failed\n"); return EXIT_FAILURE; } - printf("added workout %s on %s\n",argv[i],buf); + log_msg("added workout %s on %s\n",argv[1],buf); return EXIT_SUCCESS; } \ No newline at end of file diff --git a/src/attr.c b/src/attr.c index 227f57b..a5cea7e 100644 --- a/src/attr.c +++ b/src/attr.c @@ -1,22 +1,20 @@ #include -int attr(int i, int argc, char **argv) { - if(i>argc) { return EXIT_FAILURE; } +int attr(int argc, char **argv) { + if(1==argc) { return attr_ls(); } - if(i==argc) { return attr_ls(); } - - if(0==memcmp(argv[i],"add",3)) { - if(i+2!=argc) { - printf("wrong number of arguments for attr add\n"); + if(0==strcmp(argv[1],ATTR_ADD_STRING)) { + if(3!=argc) { + log_err(ATTR_MESSAGE_WRONG_NO_ARGS); usage(); return EXIT_FAILURE; } - return attr_add(argv[i+1]); + return attr_add(argv[2]); } - - if(i+1!=argc) { - printf("unknown command attr \"%s\"\n",argv[i+1]); + + if((2!=argc)||(0!=strcmp(argv[1],ATTR_LS_STRING))) { + log_err(ATTR_MESSAGE_UNKNOWN_COMMAND,argv[1]); usage(); return EXIT_FAILURE; } @@ -26,19 +24,16 @@ int attr(int i, int argc, char **argv) { int attr_add(char *name) { if(attribute_insert(name)<0) { - printf("attribute insert failed\n"); + log_err(ATTR_MESSAGE_ATTR_INSERT_FAILED); return EXIT_FAILURE; } - printf("New attribute added: %s\n",name); + log_msg(ATTR_MESSAGE_ATTR_ADDED,name); return EXIT_SUCCESS; } int attr_ls() { - if(attribute_get(&print_attr)<0) { - printf("command failed\n"); - return EXIT_FAILURE; - } + if(attribute_get(&print_attr)<0) { return EXIT_FAILURE; } return EXIT_SUCCESS; } diff --git a/src/default.c b/src/default.c index b7f48e1..8bfd754 100644 --- a/src/default.c +++ b/src/default.c @@ -1,5 +1,12 @@ #include +struct options global_opts = { + NULL, /* db_location */ + NULL, /* homedir */ + -1, /* rows */ + 0 /* verbose */ +}; + int defaults() { char *p; @@ -8,19 +15,19 @@ int defaults() { if(p==NULL) { p = getenv("HOME"); if(NULL==p) { - printf("HOME or WORKOUTS_HOME env variable must be defined\n"); + log_err("HOME or WORKOUTS_HOME env variable must be defined\n"); return -1; } } - if(set_homedir(p)<0) { - printf("HOME or WORKOUTS_HOME env value invalid\n"); + if(opt_set_homedir(p)<0) { + log_err("HOME or WORKOUTS_HOME env value invalid\n"); return -1; } - set_rows(-1); + opt_set_rows(-1); - set_verbose(0); + opt_set_log_level(LOG_LEVEL_DEFAULT); return 0; } \ No newline at end of file diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..732d1a1 --- /dev/null +++ b/src/log.c @@ -0,0 +1,12 @@ +#include + +int verbose_flag = LOG_LEVEL_DEFAULT; + +void log_message(enum log_level level, FILE *out_stream, const char *format,...) { + if(level<=verbose_flag) { + va_list args; + va_start(args,format); + vfprintf(out_stream,format,args); + va_end(args); + } +} \ No newline at end of file diff --git a/src/ls.c b/src/ls.c index 2ed755e..06a641a 100644 --- a/src/ls.c +++ b/src/ls.c @@ -10,60 +10,86 @@ struct ls_helper helper = { 0 }; -int ls(int i, int argc, char **argv) { - char *filter_p = NULL; +static struct option ls_long_options[] = { + {"filter", required_argument, 0, 'f'}, + {0,0,0,0} +}; + +int ls(int argc, char **argv) { /* - Cases: - -filter by attribute value: workouts --filter 011xx100 - -search by name: workouts P90X - - All together: workouts --filter 01xx1001 P90X + * Cases: + * -filter by attribute value: workouts --filter 011xx100 + * -search by name: workouts P90X + * + * All together: workouts --filter 01xx1001 P90X + * + * 0: must not have attribute + * 1: must have attribute + * x: can have attribute + */ + + int c; + char *filter_p = NULL; + char *search_term = NULL; - 0: must not have attribute - 1: must have attribute - x: can have attribute - */ + // reset optind; see `man getopt.3` NOTES section for justification + optind = 0; + while(1) { + int option_index = 0; - helper.attr_count = attribute_count(); - if(attribute_get(&print_header)<0) { return -1; } + if((c = getopt_long(argc,argv,"f:",ls_long_options,&option_index))==-1) { break; } + + switch(c) { + case 0: + if(ls_long_options[option_index].flag!=0) { break; } - if(i+10)&&(helper.i==0)) { - printf("Attributes:\t"); + printf(ATTRIBUTE_PRINT_HEADER); } - printf("%s\t",name); + printf(ATTRIBUTE_PRINT_ROW,name); helper.i++; if(helper.i==helper.attr_count) { - printf("\n"); + printf(ATTRIBUTE_PRINT_END); } } diff --git a/src/main.c b/src/main.c index ddfbce9..f182d39 100644 --- a/src/main.c +++ b/src/main.c @@ -1,61 +1,85 @@ -#include #include +static struct option long_options[] = { + {"help", no_argument, 0, 'h'}, + {"homedir", required_argument, 0, 'd'}, + {"quiet", no_argument, &verbose_flag, LOG_LEVEL_SILENT}, + {"rows", required_argument, 0, 'r'}, + {"verbose", no_argument, &verbose_flag, LOG_LEVEL_VERBOSE}, + {0,0,0,0} +}; + int main(int argc, char **argv) { - int i,ret; + int c; if(defaults()<0) { return EXIT_FAILURE; } - i = 1; - do { - ret = option(argv[i]); - switch(ret) { - case OPTION_NO_MORE: - goto exit_options; - case OPTION_HELP: - usage(); - return EXIT_SUCCESS; - case OPTION_INVALID: - printf("invalid option: %s\n\n",argv[i]); - usage(); - return EXIT_FAILURE; - case OPTION_ERROR: - printf("unknown error occured\n"); + while(1) { + int option_index = 0; + + /* The '+' at the beginning of the string means getopt_long will + * stop processing after finding the first non-option argument. + */ + if((c = getopt_long(argc,argv,"+d:hqr:v",long_options,&option_index))==-1) { break; } + + switch(c) { + case 0: + if(long_options[option_index].flag!=0) { break; } + + log_err("option %s",long_options[option_index].name); + if(optarg) { + log_err(" with arg %s",optarg); + } + log_err("\n"); return EXIT_FAILURE; - case OPTION_EXIT: + + break; + case 'd': + if(opt_set_homedir(optarg)<0) { return EXIT_FAILURE; } + break; + case 'h': + usage(); return EXIT_FAILURE; - default: - i++; break; + case 'q': + opt_set_log_level(LOG_LEVEL_SILENT); + break; + case 'r': + opt_set_rows(strtoul(optarg,NULL,10)); + break; + case 'v': + opt_set_log_level(LOG_LEVEL_VERBOSE); + break; + case '?': + default: + return EXIT_FAILURE; } - } while(ret>0); - - exit_options: - if(setup()<0) { return EXIT_FAILURE; } - char *cmd = argv[i]; - - if(NULL!=cmd) { - i++; - if(strcmp(cmd,UTIL_ADD)==0) { - return add(i,argc,argv); - } else if(strcmp(cmd,UTIL_ATTR)==0) { - return attr(i,argc,argv); - } else if(strcmp(cmd,UTIL_LS)==0) { - return ls(i,argc,argv); - } else if(strcmp(cmd,UTIL_NEW)==0) { - return new_workout(i,argc,argv); - } else if(strcmp(cmd,UTIL_RECENT)==0) { - return recent(i,argc,argv); - } else if(strcmp(cmd,UTIL_TOGGLE)==0) { - return toggle(i,argc,argv); - } else if(strcmp(cmd,UTIL_VERSION)==0) { - printf("Version: 0.0.0\n"); - return EXIT_SUCCESS; - } else { - // decrement to allow for args to ls - i--; - } + } + + if(setup()<0) { return EXIT_FAILURE; } + + if(optind -int new_workout(int i, int argc, char **argv) { - if(i>=argc) { - printf("not enough arguments for new\n"); - usage(); - return EXIT_FAILURE; - } - +int new_workout(int argc, char **argv) { + if(argc<2) { return EXIT_FAILURE; } + // check if attribute template provided unsigned int attr_flags = 0; - if(i+1 - -struct options global_opts = { - NULL, - NULL, - -1, - 0 -}; - -int option(char *str) { - int len; - - if(NULL==str) { return OPTION_NO_MORE; } - len = strlen(str); - if(len<2) { return OPTION_NO_MORE; } - - if('-'==str[0]) { - if('-'==str[1]) { - return long_option(&(str[2])); - } else { - return short_option(&(str[1])); - } - } - - return OPTION_NO_MORE; -} - -#define LONG_OPTION_HELP "help" -#define LONG_OPTION_HELP_LENGTH 4 -#define LONG_OPTION_HOMEDIR "homedir" -#define LONG_OPTION_HOMEDIR_LENGTH 7 -#define LONG_OPTION_ROWS "rows" -#define LONG_OPTION_ROWS_LENGTH 4 -#define LONG_OPTION_VERBOSE "verbose" -#define LONG_OPTION_VERBOSE_LENGTH 7 - -int long_option(char *str) { - if(memcmp(str,LONG_OPTION_HELP,LONG_OPTION_HELP_LENGTH)==0) { - return OPTION_HELP; - } else if(memcmp(str,LONG_OPTION_VERBOSE,LONG_OPTION_VERBOSE_LENGTH)==0) { - set_verbose(1); - return OPTION_SUCCESS; - } else if(memcmp(str,LONG_OPTION_HOMEDIR,LONG_OPTION_HOMEDIR_LENGTH)==0) { - int len = strlen(str); - len -= (LONG_OPTION_HOMEDIR_LENGTH+1); - if(len<1) { return OPTION_INVALID; } - - if(set_homedir(&(str[LONG_OPTION_HOMEDIR_LENGTH+1]))<0) { - printf("unable to set home directory: %s\nCheck directory exists and has correct permissions\n",&(str[LONG_OPTION_HOMEDIR_LENGTH+1])); - return OPTION_EXIT; - } - return OPTION_SUCCESS; - } else if(memcmp(str,LONG_OPTION_ROWS,LONG_OPTION_ROWS_LENGTH)==0) { - char *end_p; - set_rows(strtoul(&(str[LONG_OPTION_ROWS_LENGTH+1]),&end_p,10)); - return OPTION_SUCCESS; - } else { - return OPTION_INVALID; - } -} - -#define SHORT_OPTION_HELP 'h' -#define SHORT_OPTION_VERBOSE 'v' - -int short_option(char *str) { - if(SHORT_OPTION_HELP==str[0]) { - return OPTION_HELP; - } else if(SHORT_OPTION_VERBOSE==str[0]) { - set_verbose(1); - return OPTION_SUCCESS; - } else { - return OPTION_INVALID; - } -} \ No newline at end of file diff --git a/src/opt/homedir.c b/src/opt/homedir.c index f79d628..7890513 100644 --- a/src/opt/homedir.c +++ b/src/opt/homedir.c @@ -3,7 +3,7 @@ #define DB_FILENAME "workouts.db" #define DB_FILENAME_LENGTH 11 -int set_homedir(char *to_set) { +int opt_set_homedir(char *to_set) { int len = strlen(to_set); if(len<1) { return -1; } diff --git a/src/opt/loglvl.c b/src/opt/loglvl.c new file mode 100644 index 0000000..e11716b --- /dev/null +++ b/src/opt/loglvl.c @@ -0,0 +1,5 @@ +#include + +void opt_set_log_level(enum log_level level) { + global_opts.verbose = level; +} \ No newline at end of file diff --git a/src/opt/rows.c b/src/opt/rows.c index b65a674..18b67b5 100644 --- a/src/opt/rows.c +++ b/src/opt/rows.c @@ -1,5 +1,5 @@ #include -void set_rows(int to_set) { +void opt_set_rows(int to_set) { global_opts.rows = to_set; } \ No newline at end of file diff --git a/src/opt/verbose.c b/src/opt/verbose.c deleted file mode 100644 index 934eb9e..0000000 --- a/src/opt/verbose.c +++ /dev/null @@ -1,9 +0,0 @@ -#include - -void set_verbose(unsigned char val) { - if(val>0) { - global_opts.verbose = 1; - } else { - global_opts.verbose = 0; - } -} \ No newline at end of file diff --git a/src/recent.c b/src/recent.c index c5eaf80..2c23adf 100644 --- a/src/recent.c +++ b/src/recent.c @@ -1,17 +1,14 @@ #include +#define RECENT_PRINT_FORMAT "%s\t%s\n" + void print_recent(const unsigned char *workout, const unsigned char *date) { - printf("%s\t%s\n",workout,date); + printf(RECENT_PRINT_FORMAT,workout,date); } -int recent(int i, int argc, char **argv) { - if(i!=argc) { - printf("wrong number of arguments for recent\n"); - return EXIT_FAILURE; - } - +int recent() { if(recent_get(global_opts.rows,&print_recent)<0) { - printf("command failed\n"); + log_err(RECENT_COMMAND_FAILED); return EXIT_FAILURE; } diff --git a/src/toggle.c b/src/toggle.c index f485fea..4a2541c 100644 --- a/src/toggle.c +++ b/src/toggle.c @@ -1,18 +1,18 @@ #include -int toggle(int i, int argc, char **argv) { - if(i+2!=argc) { - printf("wrong number of arguments for toggle\n"); +int toggle(int argc, char **argv) { + if(3!=argc) { + log_err(TOGGLE_MESSAGE_WRONG_NUM_ARGS); usage(); return EXIT_FAILURE; } - if(workout_toggle(argv[i],argv[i+1])<0) { - printf("unable to toggle attribute %s for workout %s\n",argv[i+1],argv[i]); + if(workout_toggle(argv[1],argv[2])<0) { + log_err(TOGGLE_MESSAGE_UNABLE,argv[2],argv[1]); return EXIT_FAILURE; } - printf("Successfully toggled %s attribute for workout %s\n",argv[i+1],argv[i]); + log_msg(TOGGLE_MESSAGE_SUCCESS,argv[2],argv[1]); return EXIT_SUCCESS; } \ No newline at end of file diff --git a/src/usage.c b/src/usage.c index bac721c..4149c6b 100644 --- a/src/usage.c +++ b/src/usage.c @@ -1,21 +1,23 @@ #include void usage() { - printf("Usage:\n"); - printf("\tworkouts [options] [ls] [--filter {attribute filter}] [search term]\n"); - printf("\tworkouts [options] add [!name] [date]\n"); - printf("\tworkouts [options] new [!name] [attributes]\n"); - printf("\tworkouts [options] attr [ls]\n"); - printf("\tworkouts [options] attr add [!name]\n"); - printf("\tworkouts [options] toggle [!workout name] [!attr]\n"); - printf("\tworkouts [options] recent\n"); - printf("\n"); - printf("Options:\n"); - printf("\t--help,-h\n"); - printf("\t--homedir=\n"); - printf("\t--rows=\n"); - printf("\t--verbose,-v\n"); - printf("\n"); - printf("{attribute filter} refers to string in bit flags corresponding to active attributes.\n\n"); - printf("The character '!' in front of a variable name means required.\n"); + log_err("Usage:\n"); + log_err("\tworkouts [options] [ls] [--filter {attribute filter}] [search term]\n"); + log_err("\tworkouts [options] add [!name] [date]\n"); + log_err("\tworkouts [options] new [!name] [attributes]\n"); + log_err("\tworkouts [options] attr [ls]\n"); + log_err("\tworkouts [options] attr add [!name]\n"); + log_err("\tworkouts [options] toggle [!workout name] [!attr]\n"); + log_err("\tworkouts [options] recent\n"); + log_err("\n"); + log_err("Options:\n"); + log_err("\t--help, -h\n"); + log_err("\t--homedir, -d \n"); + log_err("\t--quiet, -q\n"); + log_err("\t--rows, -r \n"); + log_err("\t--verbose, -v\n"); + log_err("\n"); + log_err("{attribute filter} refers to a string of bit flags (0 = not, 1 = has, x = either) corresponding to active attributes.\n"); + log_err("Ex: 0100x00\n\n"); + log_err("The character '!' in front of a variable name means required.\n"); } \ No newline at end of file diff --git a/test/integration/test/add.integration.test.js b/test/integration/test/add.integration.test.js index 5a2265d..9f38357 100644 --- a/test/integration/test/add.integration.test.js +++ b/test/integration/test/add.integration.test.js @@ -55,7 +55,7 @@ describe('add integration tests', () => { await assert.doesNotReject(async() => await exec('./workouts add workout1 2020-06-10')); await assert.doesNotReject(async() => { - const {stdout,stderr} = await exec('./workouts workout1'); + const {stdout,stderr} = await exec('./workouts ls workout1'); let expected = `Attributes:\tlower\tupper\t\n`; expected += `workout1 [2] [Last done: 2020-07-10]\n`; diff --git a/test/integration/test/attr.integration.test.js b/test/integration/test/attr.integration.test.js index d560e1c..f76b406 100644 --- a/test/integration/test/attr.integration.test.js +++ b/test/integration/test/attr.integration.test.js @@ -57,7 +57,6 @@ describe('attr integration tests', () => { await assert.doesNotReject(async() => { const {stdout,stderr} = await exec(`./workouts attr ls`); - let expected = ""; for(const attr of attributes) { expected += `${attr}\n`; diff --git a/test/integration/test/basic.test.js b/test/integration/test/basic.test.js index 7bcfa69..c286dcf 100644 --- a/test/integration/test/basic.test.js +++ b/test/integration/test/basic.test.js @@ -14,7 +14,6 @@ describe('basic tests', () => { }); it('should print usage options when run with --help option', async() => { - const {stdout,stderr} = await exec('./workouts --help'); let usage = `Usage:\n`; usage += `\tworkouts [options] [ls] [--filter {attribute filter}] [search term]\n`; usage += `\tworkouts [options] add [!name] [date]\n`; @@ -25,18 +24,32 @@ describe('basic tests', () => { usage += `\tworkouts [options] recent\n`; usage += `\n`; usage += `Options:\n`; - usage += `\t--help,-h\n`; - usage += `\t--homedir=\n`; - usage += `\t--rows=\n`; - usage += `\t--verbose,-v\n`; + usage += `\t--help, -h\n`; + usage += `\t--homedir, -d \n`; + usage += `\t--quiet, -q\n`; + usage += `\t--rows, -r \n`; + usage += `\t--verbose, -v\n`; usage += `\n`; - usage += `{attribute filter} refers to string in bit flags corresponding to active attributes.\n\n`; + usage += `{attribute filter} refers to a string of bit flags (0 = not, 1 = has, x = either) corresponding to active attributes.\n`; + usage += `Ex: 0100x00\n\n` usage += `The character '!' in front of a variable name means required.\n`; - assert.strictEqual(stdout,usage); + + await assert.rejects(async() => await exec('./workouts --help'),(err) => { + assert.strictEqual(err.code,1); + assert.strictEqual(err.message,`Command failed: ./workouts --help\n`+usage); + return true; + }); + + await assert.rejects(async() => await exec('./workouts -h'),(err) => { + assert.strictEqual(err.code,1); + assert.strictEqual(err.message,`Command failed: ./workouts -h\n`+usage); + return true; + }); }); it(`should throw when given option --homedir which doesn't exist`, async() => { await assert.rejects(async() => await exec('./workouts --homedir=`pwd`/doesnt_exist')); + await assert.rejects(async() => await exec('./workouts -d `pwd`/doesnt_exist')); }); after(async() => { diff --git a/test/integration/test/ls.integration.test.js b/test/integration/test/ls.integration.test.js index 8755ce0..773093e 100644 --- a/test/integration/test/ls.integration.test.js +++ b/test/integration/test/ls.integration.test.js @@ -36,7 +36,7 @@ describe('ls integration tests', () => { it('should successfully limit rows grabbed', async() => { await assert.doesNotReject(async() => { - const {stdout,stderr} = await exec('./workouts --rows=1'); + const {stdout,stderr} = await exec('./workouts --rows=1 ls'); let expected = `Attributes:\tlower\tupper\t\n`; expected += `apple [3] [Last done: N/A]\n`; @@ -46,7 +46,7 @@ describe('ls integration tests', () => { it('should successfully search for workouts', async() => { await assert.doesNotReject(async() => { - const {stdout,stderr} = await exec('./workouts workout'); + const {stdout,stderr} = await exec('./workouts ls workout'); let expected = `Attributes:\tlower\tupper\t\n`; expected += `workout1 [2] [Last done: N/A]\n`; diff --git a/test/integration/test/new.integration.test.js b/test/integration/test/new.integration.test.js index c084c2b..88fb120 100644 --- a/test/integration/test/new.integration.test.js +++ b/test/integration/test/new.integration.test.js @@ -24,7 +24,10 @@ describe('new integration tests', () => { }); it('should fail to create a workout when given the wrong number of attributes', async() => { - await assert.rejects(async() => await exec('./workouts new workout1 0')); + await assert.rejects(async() => { + const res = await exec('./workouts new workout1 0'); + console.log(res); + }); await assert.rejects(async() => await exec('./workouts new workout1 110')); }); diff --git a/test/integration/test/toggle.integration.test.js b/test/integration/test/toggle.integration.test.js index 7c9d7c7..e6affab 100644 --- a/test/integration/test/toggle.integration.test.js +++ b/test/integration/test/toggle.integration.test.js @@ -36,7 +36,7 @@ describe('toggle integration tests', () => { }); await assert.doesNotReject(async() => { - const {stdout,stderr} = await exec('./workouts workout1'); + const {stdout,stderr} = await exec('./workouts ls workout1'); let expected = `Attributes:\tlower\tupper\t\n`; expected += `workout1 [3] [Last done: N/A]\n`; @@ -51,7 +51,7 @@ describe('toggle integration tests', () => { }); await assert.doesNotReject(async() => { - const {stdout,stderr} = await exec('./workouts workout1'); + const {stdout,stderr} = await exec('./workouts ls workout1'); let expected = `Attributes:\tlower\tupper\t\n`; expected += `workout1 [2] [Last done: N/A]\n`; diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 53bfe83..84d1af8 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -6,11 +6,17 @@ AM_CPPFLAGS = \ EXTRA_DIST = \ test_utils.h \ - workout.tests.h \ + add.tests.h \ + attr.tests.h \ + data.attr.tests.h \ + data.recent.tests.h \ + data.workout.tests.h \ + ls.tests.h \ + new.tests.h \ recent.tests.h \ - attr.tests.h + toggle.tests.h -check_PROGRAMS = attr.tests recent.tests workout.tests +check_PROGRAMS = add.tests attr.tests data.attr.tests data.recent.tests data.workout.tests ls.tests new.tests recent.tests toggle.tests TESTS = $(check_PROGRAMS) if ENABLE_MEMCHECK @@ -22,25 +28,71 @@ common_SOURCES = test_utils.c TEST_SRC_DIR = $(top_srcdir)/src -common_SOURCES += $(TEST_SRC_DIR)/default.c $(TEST_SRC_DIR)/opt.c +common_SOURCES += $(TEST_SRC_DIR)/default.c $(TEST_SRC_DIR)/log.c common_SOURCES += $(TEST_SRC_DIR)/data/setup.c -common_SOURCES += $(TEST_SRC_DIR)/opt/homedir.c $(TEST_SRC_DIR)/opt/rows.c $(TEST_SRC_DIR)/opt/verbose.c +common_SOURCES += $(TEST_SRC_DIR)/opt/homedir.c $(TEST_SRC_DIR)/opt/loglvl.c $(TEST_SRC_DIR)/opt/rows.c + +add_tests_SOURCES = \ + $(common_SOURCES) \ + add.tests.c \ + $(TEST_SRC_DIR)/add.c \ + $(TEST_SRC_DIR)/data/attr.c \ + $(TEST_SRC_DIR)/data/recent.c \ + $(TEST_SRC_DIR)/data/workout.c \ + $(TEST_SRC_DIR)/usage.c attr_tests_SOURCES = \ $(common_SOURCES) \ attr.tests.c \ + $(TEST_SRC_DIR)/attr.c \ + $(TEST_SRC_DIR)/data/attr.c \ + $(TEST_SRC_DIR)/usage.c + +data_attr_tests_SOURCES = \ + $(common_SOURCES) \ + data.attr.tests.c \ $(TEST_SRC_DIR)/data/attr.c -recent_tests_SOURCES = \ +data_recent_tests_SOURCES = \ $(common_SOURCES) \ - recent.tests.c \ + data.recent.tests.c \ $(TEST_SRC_DIR)/data/recent.c \ $(TEST_SRC_DIR)/data/workout.c \ $(TEST_SRC_DIR)/data/attr.c -workout_tests_SOURCES = \ +data_workout_tests_SOURCES = \ $(common_SOURCES) \ - workout.tests.c \ + data.workout.tests.c \ $(TEST_SRC_DIR)/data/workout.c \ $(TEST_SRC_DIR)/data/attr.c \ - $(TEST_SRC_DIR)/data/recent.c \ No newline at end of file + $(TEST_SRC_DIR)/data/recent.c + +ls_tests_SOURCES = \ + $(common_SOURCES) \ + ls.tests.c \ + $(TEST_SRC_DIR)/data/attr.c \ + $(TEST_SRC_DIR)/data/workout.c \ + $(TEST_SRC_DIR)/ls.c \ + $(TEST_SRC_DIR)/usage.c + +new_tests_SOURCES = \ + $(common_SOURCES) \ + new.tests.c \ + $(TEST_SRC_DIR)/data/attr.c \ + $(TEST_SRC_DIR)/data/workout.c \ + $(TEST_SRC_DIR)/new.c \ + $(TEST_SRC_DIR)/usage.c + +recent_tests_SOURCES = \ + $(common_SOURCES) \ + recent.tests.c \ + $(TEST_SRC_DIR)/data/recent.c \ + $(TEST_SRC_DIR)/recent.c + +toggle_tests_SOURCES = \ + $(common_SOURCES) \ + toggle.tests.c \ + $(TEST_SRC_DIR)/data/attr.c \ + $(TEST_SRC_DIR)/data/workout.c \ + $(TEST_SRC_DIR)/toggle.c \ + $(TEST_SRC_DIR)/usage.c \ No newline at end of file diff --git a/test/unit/add.tests.c b/test/unit/add.tests.c new file mode 100644 index 0000000..0c8bedc --- /dev/null +++ b/test/unit/add.tests.c @@ -0,0 +1,40 @@ +#include + +int main() { + setup_env(); + + add_basic_test(); + + clean(); + + return EXIT_SUCCESS; +} + +void add_basic_test() { + char *bad_argv[] = { + "add", + "testworkout1notindb", + "sdofjasdfiasjdof", + "sldfkjalskdfjasd", + NULL + }; + + char *argv[] = { + "add", + "testworkout1", + "2020-10-06", + NULL + }; + + assert(1==workout_insert("testworkout1",0)); + + assert(EXIT_FAILURE==add(1,argv)); + assert(EXIT_SUCCESS==add(2,argv)); + assert(EXIT_SUCCESS==add(3,argv)); + + assert(EXIT_FAILURE==add(1,bad_argv)); + assert(EXIT_FAILURE==add(2,bad_argv)); + assert(EXIT_FAILURE==add(3,bad_argv)); + + reset_env(); +} \ No newline at end of file diff --git a/test/unit/add.tests.h b/test/unit/add.tests.h new file mode 100644 index 0000000..13cfe79 --- /dev/null +++ b/test/unit/add.tests.h @@ -0,0 +1,11 @@ +#ifndef __ADD_TESTS_H_ +#define __ADD_TESTS_H_ + +#include + +#include + +int main(); +void add_basic_test(); + +#endif \ No newline at end of file diff --git a/test/unit/attr.tests.c b/test/unit/attr.tests.c index dbb0e48..a6f46cb 100644 --- a/test/unit/attr.tests.c +++ b/test/unit/attr.tests.c @@ -3,87 +3,44 @@ int main() { setup_env(); - attribute_count_test(); - attribute_get_test(); - attribute_index_test(); - attribute_insert_test(); - attribute_parse_test(); + attr_basic_test(); clean(); return EXIT_SUCCESS; } -void attribute_count_test() { - assert(attribute_count()==0); - - assert(attribute_insert("test")==1); - assert(attribute_count()==1); - - assert(attribute_insert("test2")==1); - assert(attribute_count()==2); +void attr_basic_test() { + char *argv[] = { + "attr", + "add", + "alsdkfjalksdfj", + NULL + }; + + char *ls_argv[] = { + "attr", + "ls", + NULL + }; + + char *bad_argv[] = { + "attr", + "hahaha", + "asdfoiasdf", + NULL + }; + + assert(EXIT_SUCCESS==attr(1,ls_argv)); + assert(EXIT_SUCCESS==attr(2,ls_argv)); + + assert(EXIT_SUCCESS==attr(1,bad_argv)); + assert(EXIT_FAILURE==attr(2,bad_argv)); + assert(EXIT_FAILURE==attr(3,bad_argv)); + + assert(EXIT_SUCCESS==attr(1,argv)); + assert(EXIT_FAILURE==attr(2,argv)); + assert(EXIT_SUCCESS==attr(3,argv)); reset_env(); -} - -int i; - -void attribute_get_test() { - i = 0; - assert(attribute_get(&attribute_get_test_helper)==1); - assert(i==0); - - assert(attribute_insert("test")==1); - assert(attribute_get(&attribute_get_test_helper)==1); - assert(i==1); - - reset_env(); -} - -void attribute_get_test_helper(const unsigned char *attr) { - i++; -} - -void attribute_index_test() { - assert(attribute_index("test")<0); - - assert(attribute_insert("test")==1); - assert(attribute_index("test")==0); - assert(attribute_index("test2")<0); - - assert(attribute_insert("test2")==1); - assert(attribute_index("test")==0); - assert(attribute_index("test2")==1); - - assert(attribute_insert("hello")==1); - assert(attribute_index("test")==0); - assert(attribute_index("test2")==1); - assert(attribute_index("hello")==2); - - reset_env(); -} - -void attribute_insert_test() { - assert(attribute_insert("test")==1); - - reset_env(); -} - -void attribute_parse_test() { - int required = 0; - int exclude = 0; - - assert(attribute_parse("311",&required,&exclude)<0); - - required = 0; - exclude = 0; - assert(attribute_parse("011",&required,&exclude)==1); - assert(required==3); - assert(exclude==4); - - required = 0; - exclude = 0; - assert(attribute_parse("x01",&required,&exclude)==1); - assert(required==1); - assert(exclude==2); } \ No newline at end of file diff --git a/test/unit/attr.tests.h b/test/unit/attr.tests.h index ee0abac..f12cde3 100644 --- a/test/unit/attr.tests.h +++ b/test/unit/attr.tests.h @@ -3,12 +3,9 @@ #include +#include + int main(); -void attribute_count_test(); -void attribute_get_test(); -void attribute_get_test_helper(const unsigned char*); -void attribute_index_test(); -void attribute_insert_test(); -void attribute_parse_test(); +void attr_basic_test(); #endif \ No newline at end of file diff --git a/test/unit/data.attr.tests.c b/test/unit/data.attr.tests.c new file mode 100644 index 0000000..4953dd7 --- /dev/null +++ b/test/unit/data.attr.tests.c @@ -0,0 +1,89 @@ +#include + +int main() { + setup_env(); + + attribute_count_test(); + attribute_get_test(); + attribute_index_test(); + attribute_insert_test(); + attribute_parse_test(); + + clean(); + + return EXIT_SUCCESS; +} + +void attribute_count_test() { + assert(attribute_count()==0); + + assert(attribute_insert("test")==1); + assert(attribute_count()==1); + + assert(attribute_insert("test2")==1); + assert(attribute_count()==2); + + reset_env(); +} + +int i; + +void attribute_get_test() { + i = 0; + assert(attribute_get(&attribute_get_test_helper)==1); + assert(i==0); + + assert(attribute_insert("test")==1); + assert(attribute_get(&attribute_get_test_helper)==1); + assert(i==1); + + reset_env(); +} + +void attribute_get_test_helper(const unsigned char *attr) { + i++; +} + +void attribute_index_test() { + assert(attribute_index("test")<0); + + assert(attribute_insert("test")==1); + assert(attribute_index("test")==0); + assert(attribute_index("test2")<0); + + assert(attribute_insert("test2")==1); + assert(attribute_index("test")==0); + assert(attribute_index("test2")==1); + + assert(attribute_insert("hello")==1); + assert(attribute_index("test")==0); + assert(attribute_index("test2")==1); + assert(attribute_index("hello")==2); + + reset_env(); +} + +void attribute_insert_test() { + assert(attribute_insert("test")==1); + + reset_env(); +} + +void attribute_parse_test() { + int required = 0; + int exclude = 0; + + assert(attribute_parse("311",&required,&exclude)<0); + + required = 0; + exclude = 0; + assert(attribute_parse("011",&required,&exclude)==1); + assert(required==3); + assert(exclude==4); + + required = 0; + exclude = 0; + assert(attribute_parse("x01",&required,&exclude)==1); + assert(required==1); + assert(exclude==2); +} \ No newline at end of file diff --git a/test/unit/data.attr.tests.h b/test/unit/data.attr.tests.h new file mode 100644 index 0000000..ee0abac --- /dev/null +++ b/test/unit/data.attr.tests.h @@ -0,0 +1,14 @@ +#ifndef __ATTR_TESTS_H_ +#define __ATTR_TESTS_H_ + +#include + +int main(); +void attribute_count_test(); +void attribute_get_test(); +void attribute_get_test_helper(const unsigned char*); +void attribute_index_test(); +void attribute_insert_test(); +void attribute_parse_test(); + +#endif \ No newline at end of file diff --git a/test/unit/data.recent.tests.c b/test/unit/data.recent.tests.c new file mode 100644 index 0000000..c9fc484 --- /dev/null +++ b/test/unit/data.recent.tests.c @@ -0,0 +1,50 @@ +#include + +int main() { + setup_env(); + + recent_get_test(); + recent_insert_test(); + + clean(); + + return EXIT_SUCCESS; +} + +int i; + +void recent_get_test() { + i = 0; + assert(recent_get(-1,&recent_get_test_helper)==1); + assert(i==0); + + assert(workout_insert("test",0)==1); + assert(workout_insert("test2",0)==1); + + assert(recent_insert("test","2020-07-04")==1); + assert(recent_insert("test2","2020-07-04")==1); + + assert(recent_get(-1,&recent_get_test_helper)==1); + assert(i==2); + + i = 0; + assert(recent_get(1,&recent_get_test_helper)==1); + assert(i==1); + + reset_env(); +} + +void recent_get_test_helper(const unsigned char *name, const unsigned char *date) { + i++; +} + +void recent_insert_test() { + assert(workout_insert("test",0)==1); + assert(workout_insert("test2",0)==1); + + assert(recent_insert("test","2020-07-04")==1); + assert(recent_insert("test2","2020-07-04")==1); + assert(recent_insert("test2","2020-07-04")==-1); + + reset_env(); +} \ No newline at end of file diff --git a/test/unit/data.recent.tests.h b/test/unit/data.recent.tests.h new file mode 100644 index 0000000..ffb2f70 --- /dev/null +++ b/test/unit/data.recent.tests.h @@ -0,0 +1,12 @@ +#ifndef __RECENT_TESTS_H_ +#define __RECENT_TESTS_H_ + +#include + +int main(); +void recent_get_test(); +void recent_get_test_helper(const unsigned char*,const unsigned char*); +void recent_insert_test(); + + +#endif \ No newline at end of file diff --git a/test/unit/workout.tests.c b/test/unit/data.workout.tests.c similarity index 98% rename from test/unit/workout.tests.c rename to test/unit/data.workout.tests.c index 6276e42..62f93fa 100644 --- a/test/unit/workout.tests.c +++ b/test/unit/data.workout.tests.c @@ -1,4 +1,4 @@ -#include +#include int main() { setup_env(); diff --git a/test/unit/workout.tests.h b/test/unit/data.workout.tests.h similarity index 100% rename from test/unit/workout.tests.h rename to test/unit/data.workout.tests.h diff --git a/test/unit/ls.tests.c b/test/unit/ls.tests.c new file mode 100644 index 0000000..c4c86f2 --- /dev/null +++ b/test/unit/ls.tests.c @@ -0,0 +1,68 @@ +#include + +int main() { + setup_env(); + + ls_basic_test(); + + clean(); + + return EXIT_SUCCESS; +} + +void ls_basic_test() { + char *bad_opt[] = { + "ls", + "-b", + NULL + }; + + assert(attribute_insert("test")==1); + assert(attribute_insert("test2")==1); + assert(attribute_insert("test3")==1); + assert(attribute_insert("test4")==1); + assert(attribute_insert("test5")==1); + assert(attribute_insert("test6")==1); + + assert(EXIT_SUCCESS==ls(1,bad_opt)); + assert(EXIT_FAILURE==ls(2,bad_opt)); + + char *badfilter[] = { + "ls", + "--filter", + "lkasdlfkjsldkf", + NULL + }; + + assert(EXIT_SUCCESS==ls(1,badfilter)); + assert(EXIT_FAILURE==ls(2,badfilter)); + assert(EXIT_FAILURE==ls(3,badfilter)); + + char *argv[] = { + "ls", + "--filter", + "00x0x1", + "P90X", + NULL + }; + + assert(EXIT_SUCCESS==ls(1,argv)); + assert(EXIT_FAILURE==ls(2,argv)); + assert(EXIT_SUCCESS==ls(3,argv)); + assert(EXIT_SUCCESS==ls(4,argv)); + + char *argv_reversed[] = { + "ls", + "P90X", + "-f", + "01x0x0", + NULL + }; + + assert(EXIT_SUCCESS==ls(1,argv_reversed)); + assert(EXIT_SUCCESS==ls(2,argv_reversed)); + assert(EXIT_FAILURE==ls(3,argv_reversed)); + assert(EXIT_SUCCESS==ls(4,argv_reversed)); + + reset_env(); +} \ No newline at end of file diff --git a/test/unit/ls.tests.h b/test/unit/ls.tests.h new file mode 100644 index 0000000..6c416fe --- /dev/null +++ b/test/unit/ls.tests.h @@ -0,0 +1,11 @@ +#ifndef __LS_TESTS_H_ +#define __LS_TESTS_H_ + +#include + +#include + +int main(); +void ls_basic_test(); + +#endif \ No newline at end of file diff --git a/test/unit/new.tests.c b/test/unit/new.tests.c new file mode 100644 index 0000000..69a5f67 --- /dev/null +++ b/test/unit/new.tests.c @@ -0,0 +1,42 @@ +#include + +int main() { + setup_env(); + + new_basic_test(); + + clean(); + + return EXIT_SUCCESS; +} + +void new_basic_test() { + char *bad[] = { + "new", + "testworkout", + "012001", + NULL + }; + + assert(attribute_insert("test")==1); + assert(attribute_insert("test2")==1); + assert(attribute_insert("test3")==1); + assert(attribute_insert("test4")==1); + assert(attribute_insert("test5")==1); + assert(attribute_insert("test6")==1); + + assert(EXIT_FAILURE==new_workout(1,bad)); + assert(EXIT_SUCCESS==new_workout(2,bad)); + assert(EXIT_FAILURE==new_workout(3,bad)); + + char *argv[] = { + "new", + "test workout", + "010001", + NULL + }; + + assert(EXIT_SUCCESS==new_workout(3,argv)); + + reset_env(); +} \ No newline at end of file diff --git a/test/unit/new.tests.h b/test/unit/new.tests.h new file mode 100644 index 0000000..e276ac8 --- /dev/null +++ b/test/unit/new.tests.h @@ -0,0 +1,11 @@ +#ifndef __NEW_TESTS_H_ +#define __NEW_TESTS_H_ + +#include + +#include + +int main(); +void new_basic_test(); + +#endif \ No newline at end of file diff --git a/test/unit/recent.tests.c b/test/unit/recent.tests.c index c976677..b651811 100644 --- a/test/unit/recent.tests.c +++ b/test/unit/recent.tests.c @@ -2,49 +2,14 @@ int main() { setup_env(); - - recent_get_test(); - recent_insert_test(); + + recent_basic_test(); clean(); return EXIT_SUCCESS; } -int i; - -void recent_get_test() { - i = 0; - assert(recent_get(-1,&recent_get_test_helper)==1); - assert(i==0); - - assert(workout_insert("test",0)==1); - assert(workout_insert("test2",0)==1); - - assert(recent_insert("test","2020-07-04")==1); - assert(recent_insert("test2","2020-07-04")==1); - - assert(recent_get(-1,&recent_get_test_helper)==1); - assert(i==2); - - i = 0; - assert(recent_get(1,&recent_get_test_helper)==1); - assert(i==1); - - reset_env(); -} - -void recent_get_test_helper(const unsigned char *name, const unsigned char *date) { - i++; -} - -void recent_insert_test() { - assert(workout_insert("test",0)==1); - assert(workout_insert("test2",0)==1); - - assert(recent_insert("test","2020-07-04")==1); - assert(recent_insert("test2","2020-07-04")==1); - assert(recent_insert("test2","2020-07-04")==-1); - - reset_env(); +void recent_basic_test() { + assert(EXIT_SUCCESS==recent()); } \ No newline at end of file diff --git a/test/unit/recent.tests.h b/test/unit/recent.tests.h index ffb2f70..031ba8a 100644 --- a/test/unit/recent.tests.h +++ b/test/unit/recent.tests.h @@ -3,10 +3,9 @@ #include -int main(); -void recent_get_test(); -void recent_get_test_helper(const unsigned char*,const unsigned char*); -void recent_insert_test(); +#include +int main(); +void recent_basic_test(); #endif \ No newline at end of file diff --git a/test/unit/test_utils.c b/test/unit/test_utils.c index 61b8fca..634451a 100644 --- a/test/unit/test_utils.c +++ b/test/unit/test_utils.c @@ -6,11 +6,11 @@ void clean() { } void reset_env() { - assert(remove(global_opts.db_location)==0); + assert((remove(global_opts.db_location)==0)||(errno==ENOENT)); assert(setup()==1); } void setup_env() { assert(defaults()==0); - assert(setup()==1); + reset_env(); } \ No newline at end of file diff --git a/test/unit/test_utils.h b/test/unit/test_utils.h index 5172929..1fb9639 100644 --- a/test/unit/test_utils.h +++ b/test/unit/test_utils.h @@ -2,6 +2,7 @@ #define __TEST_UTILS_H_ #include +#include #include #include diff --git a/test/unit/toggle.tests.c b/test/unit/toggle.tests.c new file mode 100644 index 0000000..309b515 --- /dev/null +++ b/test/unit/toggle.tests.c @@ -0,0 +1,46 @@ +#include + +int main() { + setup_env(); + + toggle_basic_test(); + + clean(); + + return EXIT_SUCCESS; +} + +void toggle_basic_test() { + assert(attribute_insert("test")==1); + assert(attribute_insert("test2")==1); + assert(attribute_insert("test3")==1); + assert(attribute_insert("test4")==1); + assert(attribute_insert("test5")==1); + assert(attribute_insert("test6")==1); + + assert(workout_insert("test",0)==1); + + char *bad[] = { + "toggle", + "test", + "notanattribute", + NULL + }; + + assert(EXIT_FAILURE==toggle(1,bad)); + assert(EXIT_FAILURE==toggle(2,bad)); + assert(EXIT_FAILURE==toggle(3,bad)); + + char *argv[] = { + "toggle", + "test", + "test3", + NULL + }; + + assert(EXIT_FAILURE==toggle(1,argv)); + assert(EXIT_FAILURE==toggle(2,argv)); + assert(EXIT_SUCCESS==toggle(3,argv)); + + reset_env(); +} \ No newline at end of file diff --git a/test/unit/toggle.tests.h b/test/unit/toggle.tests.h new file mode 100644 index 0000000..3c9bbb8 --- /dev/null +++ b/test/unit/toggle.tests.h @@ -0,0 +1,11 @@ +#ifndef __TOGGLE_TESTS_H_ +#define __TOGGLE_TESTS_H_ + +#include + +#include + +int main(); +void toggle_basic_test(); + +#endif \ No newline at end of file