diff --git a/RunCPM/parseopt_test.c b/RunCPM/parseopt_test.c index 6672caa..589eeca 100644 --- a/RunCPM/parseopt_test.c +++ b/RunCPM/parseopt_test.c @@ -2,7 +2,113 @@ #include "cheat.h" #include "parseoptions_testsetup.h" +CHEAT_SET_UP(setupParseoptionsTest();) CHEAT_TEST(dummy, cheat_assert(1); ) + +CHEAT_TEST(no_opts, + char *myargv[] = {"runcpm"}; + _parse_options(1, myargv); + cheat_assert_not(streamInputActive); + cheat_assert(consoleOutputActive); + cheat_assert(streamInputFile == NULL); + cheat_assert(streamOutputFile == NULL); + cheat_assert_not(exit_value); + cheat_assert(cheat_compare(mystderr, "")); +) + +CHEAT_TEST(dash_i, + char *myargv[] = {"runcpm", "-i", "inputfile"}; + _parse_options(3, myargv); + cheat_assert(streamInputActive); + cheat_assert(consoleOutputActive); + cheat_assert(streamInputFile == myinfile); + cheat_assert(streamOutputFile == NULL); + cheat_assert(cheat_compare(infilename, "inputfile")); + cheat_assert(cheat_compare(outfilename, "")); + cheat_assert_not(exit_value); + cheat_assert(cheat_compare(mystderr, "")); +) + +CHEAT_TEST(dash_o, + char *myargv[] = {"runcpm", "-o", "outputfile"}; + _parse_options(3, myargv); + cheat_assert_not(streamInputActive); + cheat_assert(consoleOutputActive); + cheat_assert(streamInputFile == NULL); + cheat_assert(streamOutputFile == myoutfile); + cheat_assert(cheat_compare(infilename, "")); + cheat_assert(cheat_compare(outfilename, "outputfile")); + cheat_assert_not(exit_value); + cheat_assert(cheat_compare(mystderr, "")); +) + +CHEAT_TEST(dash_i_o, + char *myargv[] = {"runcpm", "-i", "inputfile", "-o", "outputfile"}; + _parse_options(5, myargv); + cheat_assert(streamInputActive); + cheat_assert(consoleOutputActive); + cheat_assert(streamInputFile == myinfile); + cheat_assert(streamOutputFile == myoutfile); + cheat_assert(cheat_compare(infilename, "inputfile")); + cheat_assert(cheat_compare(outfilename, "outputfile")); + cheat_assert_not(exit_value); + cheat_assert(cheat_compare(mystderr, "")); +) + +CHEAT_TEST(dash_o_i, + char *myargv[] = {"runcpm", "-o", "outputfile", "-i", "inputfile"}; + _parse_options(5, myargv); + cheat_assert(streamInputActive); + cheat_assert(consoleOutputActive); + cheat_assert(streamInputFile == myinfile); + cheat_assert(streamOutputFile == myoutfile); + cheat_assert(cheat_compare(infilename, "inputfile")); + cheat_assert(cheat_compare(outfilename, "outputfile")); + cheat_assert_not(exit_value); + cheat_assert(cheat_compare(mystderr, "")); +) + +CHEAT_TEST(dash_s, + char *myargv[] = {"runcpm", "-s"}; + _parse_options(2, myargv); + cheat_assert(streamInputActive); + cheat_assert_not(consoleOutputActive); + cheat_assert(streamInputFile == mystdin); + cheat_assert(streamOutputFile == mystdout); + cheat_assert(cheat_compare(infilename, "")); + cheat_assert(cheat_compare(outfilename, "")); + cheat_assert_not(exit_value); + cheat_assert(cheat_compare(mystderr, "")); +) + +CHEAT_TEST(dash_i_no_ifile, + char *myargv[] = {"myruncpm", "-i"}; + _parse_options(2, myargv); + cheat_assert(exit_value); + cheat_assert(strncmp(mystderr + 58, "usage: myruncpm", 15) == 0); +) + +CHEAT_TEST(dash_o_no_ofile, + char *myargv[] = {"myruncpm", "-o"}; + _parse_options(2, myargv); + cheat_assert(exit_value); + cheat_assert(strncmp(mystderr + 58, "usage: myruncpm", 15) == 0); +) + +CHEAT_TEST(dash_i_o_no_ifile, + char *myargv[] = {"myruncpm", "-o", "outfile", "-i"}; + _parse_options(4, myargv); + cheat_assert(exit_value); + cheat_assert(strncmp(mystderr + 58, "usage: myruncpm", 15) == 0); +) + +CHEAT_TEST(dash_i_o_no_ofile, + char *myargv[] = {"myruncpm", "-i", "infile", "-o"}; + _parse_options(4, myargv); + cheat_assert(exit_value); + cheat_assert(strncmp(mystderr + 58, "usage: myruncpm", 15) == 0); +) + diff --git a/RunCPM/parseoptions_testsetup.c b/RunCPM/parseoptions_testsetup.c index 2f253fd..736f107 100644 --- a/RunCPM/parseoptions_testsetup.c +++ b/RunCPM/parseoptions_testsetup.c @@ -11,6 +11,8 @@ FILE mystdin[1]; FILE mystdout[1]; FILE mystderr[2000]; +FILE myinfile[1]; +FILE myoutfile[1]; void myfprintf(FILE *out, const char *fmt, ...) { va_list va; @@ -23,19 +25,16 @@ char infilename[50]; char outfilename[50]; char unknownfilename[50]; -void clearfilenames(void) { - *infilename = 0; - *outfilename = 0; - *unknownfilename = 0; -} - char *myfopen(const char *fname, const char *mode) { if (*mode == 'r') { strcpy(infilename, fname); + return myinfile; } else if (*mode == 'w') { strcpy(outfilename, fname); + return myoutfile; } else { strcpy(unknownfilename, fname); + return NULL; } } @@ -67,4 +66,14 @@ void _file_failure_exit(char *argv[], char* fmt, char* filename) { } +void setupParseoptionsTest(void) { + *infilename = 0; + *outfilename = 0; + *unknownfilename = 0; + streamInputFile = NULL; + streamOutputFile = NULL; + streamInputActive = FALSE; + consoleOutputActive = TRUE; +} + #include "parseoptions_vstudio.h" diff --git a/RunCPM/parseoptions_testsetup.h b/RunCPM/parseoptions_testsetup.h index b3d90f3..2c04b3f 100644 --- a/RunCPM/parseoptions_testsetup.h +++ b/RunCPM/parseoptions_testsetup.h @@ -5,6 +5,8 @@ extern char mystdin[]; extern char mystdout[]; extern char mystderr[]; +extern char myinfile[]; +extern char myoutfile[]; extern char infilename[]; extern char outfilename[]; @@ -15,7 +17,9 @@ extern char *streamOutputFile; extern char streamInputActive; extern char consoleOutputActive; -void clearfilenames(void); +extern int exit_value; + +void setupParseoptionsTest(void); void _usage(char *argv[]); diff --git a/RunCPM/parseoptions_vstudio.h b/RunCPM/parseoptions_vstudio.h index 8b28a1c..a67cd43 100644 --- a/RunCPM/parseoptions_vstudio.h +++ b/RunCPM/parseoptions_vstudio.h @@ -5,18 +5,15 @@ void _usage(char *argv[]) { "usage: %s [-i input_file] [-o output_file] [-s]\n", argv[0]); fprintf(stderr, " -i input_file: console input will be read from the file " - "with the\ngiven name. " - "After input file's EOF, further console input\nwill be read " + "with the\n given name. " + "After input file's EOF, further console input\n will be read " "from the keyboard.\n"); fprintf(stderr, " -o output_file: console output will be written to the file " - "with the\ngiven name, in addition to the screen.\n"); + "with the\n given name, in addition to the screen.\n"); fprintf(stderr, " -s: console input and output is connected directly to " - "stdin and stdout.\nSince on Posix keyboard input is read from " - "stdin, switching from\nstdin to keyboard on stdin EOF is a " - "no-op. Therefore stdin EOF is an\nerror condition on Posix in " - "this case.\n"); + "stdin and stdout.\n"); } #define SET_OPTARG ++i; if (i >= argc) {++errflg; break;} optarg = argv[i]; @@ -24,7 +21,7 @@ void _usage(char *argv[]) { void _parse_options(int argc, char *argv[]) { int errflg = 0; char *optarg; - for (int i = 0; i < argc && errflg == 0; ++i) { + for (int i = 1; i < argc && errflg == 0; ++i) { if (strcmp("-i", argv[i]) == 0) { /* ++i; if (i >= argc) {