feat(args): $0, $1, ... in vars
This commit is contained in:
parent
82f35cfa08
commit
7bcf48f63e
4 changed files with 82 additions and 29 deletions
|
|
@ -4,6 +4,9 @@
|
|||
#include <criterion/new/assert.h>
|
||||
#include <criterion/redirect.h>
|
||||
|
||||
#include "../../../src/utils/hash_map/hash_map.h"
|
||||
#include "../../../src/utils/vars/vars.h"
|
||||
|
||||
TestSuite(utils_args);
|
||||
|
||||
Test(utils_args, basic_command)
|
||||
|
|
@ -12,13 +15,15 @@ Test(utils_args, basic_command)
|
|||
struct args_options options;
|
||||
char *input[] = { "program", "-c", "echo Hello, World!" };
|
||||
|
||||
int r = args_handler(argc, input, &options);
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
cr_expect(options.pretty_print == false);
|
||||
cr_expect(options.verbose == false);
|
||||
cr_expect(options.type == INPUT_CMD);
|
||||
cr_expect(eq(options.input_source, "echo Hello, World!"));
|
||||
hash_map_free(&vars);
|
||||
}
|
||||
|
||||
Test(utils_args, basic_command_with_flags)
|
||||
|
|
@ -28,13 +33,15 @@ Test(utils_args, basic_command_with_flags)
|
|||
char *input[] = { "program", "--pretty-print", "-c", "echo Hello, World!",
|
||||
"--verbose" };
|
||||
|
||||
int r = args_handler(argc, input, &options);
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
cr_expect(options.pretty_print == true);
|
||||
cr_expect(options.verbose == true);
|
||||
cr_expect(options.type == INPUT_CMD);
|
||||
cr_expect(eq(options.input_source, "echo Hello, World!"));
|
||||
hash_map_free(&vars);
|
||||
}
|
||||
|
||||
Test(utils_args, basic_file_input)
|
||||
|
|
@ -43,13 +50,15 @@ Test(utils_args, basic_file_input)
|
|||
struct args_options options;
|
||||
char *input[] = { "program", "input.txt" };
|
||||
|
||||
int r = args_handler(argc, input, &options);
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
cr_expect(options.pretty_print == false);
|
||||
cr_expect(options.verbose == false);
|
||||
cr_expect(options.type == INPUT_FILE);
|
||||
cr_expect(eq(options.input_source, "input.txt"));
|
||||
hash_map_free(&vars);
|
||||
}
|
||||
|
||||
Test(utils_args, basic_file_input_with_flags)
|
||||
|
|
@ -58,13 +67,15 @@ Test(utils_args, basic_file_input_with_flags)
|
|||
struct args_options options;
|
||||
char *input[] = { "program", "--verbose", "input.txt", "--pretty-print" };
|
||||
|
||||
int r = args_handler(argc, input, &options);
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
cr_expect(options.pretty_print == true);
|
||||
cr_expect(options.verbose == true);
|
||||
cr_expect(options.type == INPUT_FILE);
|
||||
cr_expect(eq(options.input_source, "input.txt"));
|
||||
hash_map_free(&vars);
|
||||
}
|
||||
|
||||
Test(utils_args, basic_stdin_input)
|
||||
|
|
@ -73,13 +84,15 @@ Test(utils_args, basic_stdin_input)
|
|||
struct args_options options;
|
||||
char *input[] = { "program" };
|
||||
|
||||
int r = args_handler(argc, input, &options);
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
cr_expect(options.pretty_print == false);
|
||||
cr_expect(options.verbose == false);
|
||||
cr_expect(options.type == INPUT_STDIN);
|
||||
cr_expect(options.input_source == NULL);
|
||||
hash_map_free(&vars);
|
||||
}
|
||||
|
||||
Test(utils_args, pretty_print_and_verbose_flags)
|
||||
|
|
@ -88,13 +101,15 @@ Test(utils_args, pretty_print_and_verbose_flags)
|
|||
struct args_options options;
|
||||
char *input[] = { "program", "--pretty-print", "--verbose" };
|
||||
|
||||
int r = args_handler(argc, input, &options);
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
cr_expect(options.pretty_print == true);
|
||||
cr_expect(options.verbose == true);
|
||||
cr_expect(options.type == INPUT_STDIN);
|
||||
cr_expect(options.input_source == NULL);
|
||||
hash_map_free(&vars);
|
||||
}
|
||||
|
||||
Test(utils_args, missing_command_after_c, .init = cr_redirect_stderr)
|
||||
|
|
@ -103,10 +118,12 @@ Test(utils_args, missing_command_after_c, .init = cr_redirect_stderr)
|
|||
struct args_options options;
|
||||
char *input[] = { "program", "-c" };
|
||||
|
||||
int r = args_handler(argc, input, &options);
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r != 0);
|
||||
cr_assert_stderr_eq_str("No command provided after -c\n");
|
||||
hash_map_free(&vars);
|
||||
}
|
||||
|
||||
Test(utils_args, unknown_option, .init = cr_redirect_stderr)
|
||||
|
|
@ -115,20 +132,42 @@ Test(utils_args, unknown_option, .init = cr_redirect_stderr)
|
|||
struct args_options options;
|
||||
char *input[] = { "program", "--unknown" };
|
||||
|
||||
int r = args_handler(argc, input, &options);
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r != 0);
|
||||
cr_assert_stderr_eq_str("Unknown option: --unknown\n");
|
||||
hash_map_free(&vars);
|
||||
}
|
||||
|
||||
Test(utils_args, conflicting_input_types, .init = cr_redirect_stderr)
|
||||
Test(utils_args, multiple_command, .init = cr_redirect_stderr)
|
||||
{
|
||||
int argc = 4;
|
||||
struct args_options options;
|
||||
char *input[] = { "program", "-c", "echo Hello", "input.txt" };
|
||||
char *input[] = { "program", "exec.sh", "-c", "echo World" };
|
||||
|
||||
int r = args_handler(argc, input, &options);
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r != 0);
|
||||
cr_assert_stderr_eq_str("Multiple input sources specified: input.txt\n");
|
||||
cr_assert_stderr_eq_str("Multiple input sources specified: echo World\n");
|
||||
hash_map_free(&vars);
|
||||
}
|
||||
|
||||
Test(utils_args, command_with_additional_arguments)
|
||||
{
|
||||
int argc = 5;
|
||||
struct args_options options;
|
||||
char *input[] = { "program", "-c", "echo Hello", "arg1", "arg2" };
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
int r = args_handler(argc, input, &options, vars);
|
||||
|
||||
cr_expect(r == 0);
|
||||
cr_expect(options.type == INPUT_CMD);
|
||||
cr_expect(eq(options.input_source, "echo Hello"));
|
||||
cr_expect_str_eq(get_var(vars, "0"), "program");
|
||||
cr_expect_str_eq(get_var(vars, "1"), "arg1");
|
||||
cr_expect_str_eq(get_var(vars, "2"), "arg2");
|
||||
hash_map_free(&vars);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue