Merge branch 'testsuite' into dev
This commit is contained in:
commit
668bba5b02
6 changed files with 96 additions and 85 deletions
|
|
@ -35,7 +35,13 @@ check_PROGRAMS = testsuite
|
||||||
#testsuite_CFLAGS += $(CRITERION_CFLAGS)
|
#testsuite_CFLAGS += $(CRITERION_CFLAGS)
|
||||||
|
|
||||||
testsuite_SOURCES = ../tests/unit/lexer/lexer_tests.c \
|
testsuite_SOURCES = ../tests/unit/lexer/lexer_tests.c \
|
||||||
../tests/unit/utils/utils_tests.c
|
../tests/unit/utils/utils_tests.c \
|
||||||
|
../tests/unit/expansion/expand.c \
|
||||||
|
../tests/unit/expansion/parse_var.c \
|
||||||
|
../tests/unit/io_backend/io_backend.c \
|
||||||
|
../tests/unit/utils/args.c \
|
||||||
|
../tests/unit/utils/hash_map.c \
|
||||||
|
../tests/unit/utils/insert_into.c
|
||||||
|
|
||||||
testsuite_CPPFLAGS = $(42sh_CPPFLAGS)
|
testsuite_CPPFLAGS = $(42sh_CPPFLAGS)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "../utils/ast/ast.h"
|
||||||
#include "../utils/hash_map/hash_map.h"
|
#include "../utils/hash_map/hash_map.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,9 @@ Test(expand, no_expansion)
|
||||||
struct ast *ast = ast_create_command(list);
|
struct ast *ast = ast_create_command(list);
|
||||||
struct ast_command *ast_command = ast_get_command(ast);
|
struct ast_command *ast_command = ast_get_command(ast);
|
||||||
|
|
||||||
struct ast_command *command2 = expand(ast_command, NULL);
|
bool ret = expand(ast_command, NULL);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
cr_assert_str_eq((char *)command2->command->data, "echo something",
|
cr_expect_str_eq((char *)ast_command->command->data, "echo something",
|
||||||
"String without variables should remain unchanged");
|
"String without variables should remain unchanged");
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
}
|
}
|
||||||
|
|
@ -38,9 +38,9 @@ Test(expand, single_quotes_no_expansion)
|
||||||
struct hash_map *vars = vars_init();
|
struct hash_map *vars = vars_init();
|
||||||
set_var_copy(vars, "VAR", "expanded");
|
set_var_copy(vars, "VAR", "expanded");
|
||||||
|
|
||||||
struct ast_command *command2 = expand(ast_command, vars);
|
bool ret = expand(ast_command, vars);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
cr_assert_str_eq((char *)command2->command->data, "echo $VAR",
|
cr_expect_str_eq((char *)ast_command->command->data, "echo $VAR",
|
||||||
"Variable should not expand inside single quotes");
|
"Variable should not expand inside single quotes");
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
hash_map_free(&vars);
|
hash_map_free(&vars);
|
||||||
|
|
@ -57,9 +57,9 @@ Test(expand, single_dollar)
|
||||||
struct hash_map *vars = vars_init();
|
struct hash_map *vars = vars_init();
|
||||||
set_var_copy(vars, "VAR", "expanded");
|
set_var_copy(vars, "VAR", "expanded");
|
||||||
|
|
||||||
struct ast_command *command2 = expand(ast_command, vars);
|
bool ret = expand(ast_command, vars);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
cr_assert_str_eq((char *)command2->command->data, "echo $ sign",
|
cr_expect_str_eq((char *)ast_command->command->data, "echo $ sign",
|
||||||
"Variable should not expand inside single quotes");
|
"Variable should not expand inside single quotes");
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
hash_map_free(&vars);
|
hash_map_free(&vars);
|
||||||
|
|
@ -76,8 +76,8 @@ Test(expand, empty_braces_no_expansion)
|
||||||
struct hash_map *vars = vars_init();
|
struct hash_map *vars = vars_init();
|
||||||
set_var_copy(vars, "VAR", "expanded");
|
set_var_copy(vars, "VAR", "expanded");
|
||||||
|
|
||||||
struct ast_command *command2 = expand(ast_command, vars);
|
bool ret = expand(ast_command, vars);
|
||||||
cr_assert_null(command2, "Expansion should fail on empty braces");
|
cr_expect(ret == false, "expansion should fail with %s", str);
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
hash_map_free(&vars);
|
hash_map_free(&vars);
|
||||||
}
|
}
|
||||||
|
|
@ -93,9 +93,9 @@ Test(expand, basic_expansion)
|
||||||
struct hash_map *vars = vars_init();
|
struct hash_map *vars = vars_init();
|
||||||
set_var_copy(vars, "VAR", "expanded");
|
set_var_copy(vars, "VAR", "expanded");
|
||||||
|
|
||||||
struct ast_command *command2 = expand(ast_command, vars);
|
bool ret = expand(ast_command, vars);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
cr_assert_str_eq((char *)command2->command->data, "echo expanded",
|
cr_expect_str_eq((char *)ast_command->command->data, "echo expanded",
|
||||||
"Variable should expand correctly");
|
"Variable should expand correctly");
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
hash_map_free(&vars);
|
hash_map_free(&vars);
|
||||||
|
|
@ -114,9 +114,9 @@ Test(expand, multiple_expansion)
|
||||||
set_var_copy(vars, "VAR2", "values");
|
set_var_copy(vars, "VAR2", "values");
|
||||||
set_var_copy(vars, "VAR3", "here");
|
set_var_copy(vars, "VAR3", "here");
|
||||||
|
|
||||||
struct ast_command *command2 = expand(ast_command, vars);
|
bool ret = expand(ast_command, vars);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
cr_assert_str_eq((char *)command2->command->data,
|
cr_expect_str_eq((char *)ast_command->command->data,
|
||||||
"echo expanded values here",
|
"echo expanded values here",
|
||||||
"Multiple variables should expand correctly");
|
"Multiple variables should expand correctly");
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
|
|
@ -133,9 +133,9 @@ Test(expand, env_variable)
|
||||||
|
|
||||||
setenv("MY_ENV_VAR", "environment", 0);
|
setenv("MY_ENV_VAR", "environment", 0);
|
||||||
|
|
||||||
struct ast_command *command2 = expand(ast_command, NULL);
|
bool ret = expand(ast_command, NULL);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
cr_assert_str_eq((char *)command2->command->data, "echo environment",
|
cr_expect_str_eq((char *)ast_command->command->data, "echo environment",
|
||||||
"Environment variable should expand correctly");
|
"Environment variable should expand correctly");
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
}
|
}
|
||||||
|
|
@ -150,9 +150,9 @@ Test(expand, undefined_variable)
|
||||||
|
|
||||||
struct hash_map *vars = vars_init();
|
struct hash_map *vars = vars_init();
|
||||||
|
|
||||||
struct ast_command *command2 = expand(ast_command, vars);
|
bool ret = expand(ast_command, vars);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
cr_assert_str_eq((char *)command2->command->data, "echo ",
|
cr_expect_str_eq((char *)ast_command->command->data, "echo ",
|
||||||
"Undefined variable should expand to empty string");
|
"Undefined variable should expand to empty string");
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
hash_map_free(&vars);
|
hash_map_free(&vars);
|
||||||
|
|
@ -170,9 +170,9 @@ Test(expand, nested_expansion)
|
||||||
set_var_copy(vars, "A", "expanded");
|
set_var_copy(vars, "A", "expanded");
|
||||||
set_var_copy(vars, "B", "$A");
|
set_var_copy(vars, "B", "$A");
|
||||||
|
|
||||||
struct ast_command *command2 = expand(ast_command, vars);
|
bool ret = expand(ast_command, vars);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
cr_assert_str_eq((char *)command2->command->data, "echo expanded",
|
cr_expect_str_eq((char *)ast_command->command->data, "echo expanded",
|
||||||
"Nested variable should expand correctly");
|
"Nested variable should expand correctly");
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
hash_map_free(&vars);
|
hash_map_free(&vars);
|
||||||
|
|
@ -190,9 +190,9 @@ Test(expand, mixed_quotes_expansion)
|
||||||
set_var_copy(vars, "VAR1", "expanded");
|
set_var_copy(vars, "VAR1", "expanded");
|
||||||
set_var_copy(vars, "VAR2", "not_expanded");
|
set_var_copy(vars, "VAR2", "not_expanded");
|
||||||
|
|
||||||
struct ast_command *command2 = expand(ast_command, vars);
|
bool ret = expand(ast_command, vars);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
cr_assert_str_eq((char *)command2->command->data,
|
cr_expect_str_eq((char *)ast_command->command->data,
|
||||||
"echo \"expanded and $VAR2\"",
|
"echo \"expanded and $VAR2\"",
|
||||||
"Variable in double quotes should expand, while variable "
|
"Variable in double quotes should expand, while variable "
|
||||||
"in single quotes should not");
|
"in single quotes should not");
|
||||||
|
|
@ -212,9 +212,9 @@ Test(expand, adjacent_variables)
|
||||||
set_var_copy(vars, "VAR1", "hello");
|
set_var_copy(vars, "VAR1", "hello");
|
||||||
set_var_copy(vars, "VAR2", "world");
|
set_var_copy(vars, "VAR2", "world");
|
||||||
|
|
||||||
struct ast_command *command2 = expand(ast_command, vars);
|
bool ret = expand(ast_command, vars);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
cr_assert_str_eq((char *)command2->command->data, "echo helloworld",
|
cr_expect_str_eq((char *)ast_command->command->data, "echo helloworld",
|
||||||
"Adjacent variables should expand correctly");
|
"Adjacent variables should expand correctly");
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
hash_map_free(&vars);
|
hash_map_free(&vars);
|
||||||
|
|
@ -228,9 +228,9 @@ Test(expand, random)
|
||||||
struct ast *ast = ast_create_command(list);
|
struct ast *ast = ast_create_command(list);
|
||||||
struct ast_command *ast_command = ast_get_command(ast);
|
struct ast_command *ast_command = ast_get_command(ast);
|
||||||
|
|
||||||
struct ast_command *command2 = expand(ast_command, NULL);
|
bool ret = expand(ast_command, NULL);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
int rnd = atoi((char *)command2->command->data);
|
int rnd = atoi((char *)ast_command->command->data);
|
||||||
cr_assert(rnd >= 0 && rnd <= 32767,
|
cr_assert(rnd >= 0 && rnd <= 32767,
|
||||||
"RANDOM variable should expand to a value between 0 and 32767");
|
"RANDOM variable should expand to a value between 0 and 32767");
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
|
|
@ -245,9 +245,9 @@ Test(expand, pid)
|
||||||
struct ast_command *ast_command = ast_get_command(ast);
|
struct ast_command *ast_command = ast_get_command(ast);
|
||||||
|
|
||||||
struct hash_map *vars = vars_init();
|
struct hash_map *vars = vars_init();
|
||||||
struct ast_command *command2 = expand(ast_command, vars);
|
bool ret = expand(ast_command, vars);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
int pid = atoi((char *)command2->command->data);
|
int pid = atoi((char *)ast_command->command->data);
|
||||||
cr_assert(pid == getpid(),
|
cr_assert(pid == getpid(),
|
||||||
"$$ variable should expand to the pid of the process");
|
"$$ variable should expand to the pid of the process");
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
|
|
@ -263,9 +263,9 @@ Test(expand, default_last_exit_code)
|
||||||
struct ast_command *ast_command = ast_get_command(ast);
|
struct ast_command *ast_command = ast_get_command(ast);
|
||||||
|
|
||||||
struct hash_map *vars = vars_init();
|
struct hash_map *vars = vars_init();
|
||||||
struct ast_command *command2 = expand(ast_command, vars);
|
bool ret = expand(ast_command, vars);
|
||||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
cr_expect(ret, "expansion failed with %s", str);
|
||||||
int code = atoi((char *)command2->command->data);
|
int code = atoi((char *)ast_command->command->data);
|
||||||
cr_assert(code == 0,
|
cr_assert(code == 0,
|
||||||
"$? variable should expand to the last exit code (default 0)");
|
"$? variable should expand to the last exit code (default 0)");
|
||||||
ast_free(&ast);
|
ast_free(&ast);
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,10 @@ Test(IO_Backend, init_null)
|
||||||
{
|
{
|
||||||
struct iob_context ctx =
|
struct iob_context ctx =
|
||||||
{
|
{
|
||||||
.iob_mode = IOB_MODE_NULL;
|
.mode = IOB_MODE_NULL,
|
||||||
.args = NULL;
|
.args = NULL
|
||||||
};
|
};
|
||||||
int actual = iob_init(ctx);
|
int actual = iob_init(&ctx);
|
||||||
int expected = IOB_ERROR_BAD_ARG;
|
int expected = IOB_ERROR_BAD_ARG;
|
||||||
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
||||||
}
|
}
|
||||||
|
|
@ -23,10 +23,10 @@ Test(IO_Backend, init_stdin)
|
||||||
{
|
{
|
||||||
struct iob_context ctx =
|
struct iob_context ctx =
|
||||||
{
|
{
|
||||||
.iob_mode = IOB_MODE_STDIN;
|
.mode = IOB_MODE_STDIN,
|
||||||
.args = NULL;
|
.args = NULL
|
||||||
};
|
};
|
||||||
int actual = iob_init(ctx);
|
int actual = iob_init(&ctx);
|
||||||
int expected = 0;
|
int expected = 0;
|
||||||
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
||||||
iob_close();
|
iob_close();
|
||||||
|
|
@ -36,15 +36,16 @@ iob_close();
|
||||||
// Same applies for other tests
|
// Same applies for other tests
|
||||||
Test(IO_Backend, init_script)
|
Test(IO_Backend, init_script)
|
||||||
{
|
{
|
||||||
char *script_name = "script.tmp" struct iob_context ctx = {
|
char *script_name = "script.tmp";
|
||||||
.iob_mode = IOB_MODE_SCRIPT;
|
struct iob_context ctx = {
|
||||||
.args = script_name;
|
.mode = IOB_MODE_SCRIPT,
|
||||||
|
.args = script_name
|
||||||
};
|
};
|
||||||
// Create file
|
// Create file
|
||||||
FILE *f = fopen(script_name, "w");
|
FILE *f = fopen(script_name, "w");
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
int actual = iob_init(ctx);
|
int actual = iob_init(&ctx);
|
||||||
int expected = 0;
|
int expected = 0;
|
||||||
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
||||||
iob_close();
|
iob_close();
|
||||||
|
|
@ -53,11 +54,12 @@ remove(script_name);
|
||||||
|
|
||||||
Test(IO_Backend, init_script_not_a_file)
|
Test(IO_Backend, init_script_not_a_file)
|
||||||
{
|
{
|
||||||
char *script_name = "not_a_file.tmp" struct iob_context ctx = {
|
char *script_name = "not_a_file.tmp";
|
||||||
.iob_mode = IOB_MODE_SCRIPT;
|
struct iob_context ctx = {
|
||||||
.args = script_name;
|
.mode = IOB_MODE_SCRIPT,
|
||||||
|
.args = script_name
|
||||||
};
|
};
|
||||||
int actual = iob_init(ctx);
|
int actual = iob_init(&ctx);
|
||||||
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
|
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
|
||||||
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
||||||
}
|
}
|
||||||
|
|
@ -66,21 +68,22 @@ Test(IO_Backend, init_script_null)
|
||||||
{
|
{
|
||||||
struct iob_context ctx =
|
struct iob_context ctx =
|
||||||
{
|
{
|
||||||
.iob_mode = IOB_MODE_SCRIPT;
|
.mode = IOB_MODE_SCRIPT,
|
||||||
.args = NULL;
|
.args = NULL
|
||||||
};
|
};
|
||||||
int actual = iob_init(ctx);
|
int actual = iob_init(&ctx);
|
||||||
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
|
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
|
||||||
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
Test(IO_Backend, init_cmd)
|
Test(IO_Backend, init_cmd)
|
||||||
{
|
{
|
||||||
char *cmd = "iamacommand --yesido" struct iob_context ctx = {
|
char *cmd = "iamacommand --yesido";
|
||||||
.iob_mode = IOB_MODE_CMD;
|
struct iob_context ctx = {
|
||||||
.args = cmd;
|
.mode = IOB_MODE_CMD,
|
||||||
|
.args = cmd
|
||||||
};
|
};
|
||||||
int actual = iob_init(ctx);
|
int actual = iob_init(&ctx);
|
||||||
int expected = 0;
|
int expected = 0;
|
||||||
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
||||||
iob_close();
|
iob_close();
|
||||||
|
|
@ -90,23 +93,24 @@ Test(IO_Backend, init_cmd_null)
|
||||||
{
|
{
|
||||||
struct iob_context ctx =
|
struct iob_context ctx =
|
||||||
{
|
{
|
||||||
.iob_mode = IOB_MODE_CMD;
|
.mode = IOB_MODE_CMD,
|
||||||
.args = NULL;
|
.args = NULL
|
||||||
};
|
};
|
||||||
int actual = iob_init(ctx);
|
int actual = iob_init(&ctx);
|
||||||
int expected = IOB_ERROR_BAD_ARG;
|
int expected = IOB_ERROR_BAD_ARG;
|
||||||
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
Test(IO_Backend, init_already_init)
|
Test(IO_Backend, init_already_init)
|
||||||
{
|
{
|
||||||
char *cmd = "iamacommand --yesido" struct iob_context ctx = {
|
char *cmd = "iamacommand --yesido";
|
||||||
.iob_mode = IOB_MODE_CMD;
|
struct iob_context ctx = {
|
||||||
.args = cmd;
|
.mode = IOB_MODE_CMD,
|
||||||
|
.args = cmd
|
||||||
};
|
};
|
||||||
iob_init(ctx);
|
iob_init(&ctx);
|
||||||
int actual = iob_init(ctx);
|
int actual = iob_init(&ctx);
|
||||||
int expected = IOB_ERROR_ALREADY_INITIALIZED;
|
int expected = IOB_ERROR_MODULE_ALREADY_INITIALIZED;
|
||||||
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
|
||||||
iob_close();
|
iob_close();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ then export BIN_PATH="$(pwd)/42sh"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$COVERAGE" = "yes" ]; #coverage
|
if [ "$COVERAGE" = "yes" ]; #coverage
|
||||||
then ./testsuite && ../tests/functional/run-tests.sh
|
then (./testsuite || true) && ../tests/functional/run-tests.sh
|
||||||
else ../tests/functional/run-tests.sh
|
else ../tests/functional/run-tests.sh
|
||||||
fi
|
fi
|
||||||
echo bin path: "$BIN_PATH"
|
echo bin path: "$BIN_PATH"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue