2017-12-08 23:06:21 +00:00
|
|
|
#include <criterion/criterion.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "option.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup()
|
|
|
|
{
|
|
|
|
option_set_error("");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
teardown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TestSuite(options, .init = setup, .fini = teardown);
|
|
|
|
|
2018-01-08 01:39:16 +00:00
|
|
|
// No need to do this one...
|
|
|
|
/* Test(option, print_help) */
|
|
|
|
|
|
|
|
/* Test(option, get_error) */
|
|
|
|
/* Test(option, set_error) */
|
2018-01-07 22:31:00 +00:00
|
|
|
Test(option, error)
|
2017-12-08 23:06:21 +00:00
|
|
|
{
|
|
|
|
char *str = "hahaha FUN";
|
|
|
|
|
|
|
|
cr_assert_str_empty(option_get_error());
|
|
|
|
|
|
|
|
option_set_error(str);
|
|
|
|
cr_assert_str_eq(option_get_error(), str);
|
|
|
|
}
|
|
|
|
|
2018-03-13 20:19:26 +00:00
|
|
|
Test(option, open_file)
|
2017-12-08 23:06:21 +00:00
|
|
|
{
|
|
|
|
char *str = "so much FUN";
|
|
|
|
char *bad_file = "/tmp/BLEH";
|
|
|
|
char *file = "/tmp/erc-test.txt";
|
|
|
|
char buf[256];
|
2018-03-08 02:25:39 +00:00
|
|
|
FILE *stream_a;
|
|
|
|
FILE *stream_b;
|
2017-12-08 23:06:21 +00:00
|
|
|
|
|
|
|
// Maybe we should use sterror(ENOENT)?
|
2018-03-09 22:44:20 +00:00
|
|
|
cr_assert_eq(option_open_file(&stream_a, bad_file, "r"), 0);
|
|
|
|
cr_assert_str_eq(option_get_error(), "open file for /tmp/BLEH: No such file or directory");
|
2017-12-08 23:06:21 +00:00
|
|
|
|
|
|
|
option_set_error("");
|
|
|
|
|
2018-03-08 02:25:39 +00:00
|
|
|
stream_a = fopen(file, "w");
|
|
|
|
cr_assert_neq(stream_a, NULL);
|
|
|
|
fwrite(str, sizeof(char), strlen(str), stream_a);
|
|
|
|
fclose(stream_a);
|
2017-12-08 23:06:21 +00:00
|
|
|
|
2018-03-09 22:44:20 +00:00
|
|
|
option_open_file(&stream_b, file, "r");
|
2018-03-08 02:25:39 +00:00
|
|
|
fread(buf, sizeof(char), 255, stream_b);
|
2017-12-08 23:06:21 +00:00
|
|
|
cr_assert_str_eq(buf, str);
|
2018-03-08 02:25:39 +00:00
|
|
|
fclose(stream_b);
|
2017-12-08 23:06:21 +00:00
|
|
|
|
|
|
|
unlink(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This test is really imperfect... that's because option_parse() does
|
|
|
|
* a ton of stuff, and is quite complex. I'm punting a lot on the
|
|
|
|
* complexity here, while pushing as much of the logic as I can into
|
|
|
|
* other functions that are more easily testable.
|
|
|
|
*/
|
2018-01-07 22:31:00 +00:00
|
|
|
Test(option, parse)
|
2017-12-08 23:06:21 +00:00
|
|
|
{
|
|
|
|
int argc = 2;
|
|
|
|
char *argv[] = {
|
|
|
|
"prog_name",
|
|
|
|
"--disk1=etc",
|
|
|
|
};
|
|
|
|
|
|
|
|
cr_assert_eq(option_parse(argc, argv), 0);
|
|
|
|
}
|
2017-12-27 04:35:50 +00:00
|
|
|
|
2018-01-08 01:39:16 +00:00
|
|
|
Test(option, flag)
|
|
|
|
{
|
|
|
|
int argc = 2;
|
|
|
|
char *argv[] = {
|
|
|
|
"prog_name",
|
|
|
|
"--flash",
|
|
|
|
};
|
|
|
|
|
|
|
|
cr_assert_eq(option_flag(OPTION_FLASH), false);
|
|
|
|
cr_assert_eq(option_parse(argc, argv), 1);
|
|
|
|
cr_assert_eq(option_flag(OPTION_FLASH), true);
|
|
|
|
}
|