From e8d10523ae0667e7a407c86540e6c34ed6e466d7 Mon Sep 17 00:00:00 2001 From: matteo Date: Sat, 17 Jan 2026 17:53:37 +0100 Subject: [PATCH] feat(testsuite): autotools working, testsuite and debug implemented and working --- README.md | 26 ++++---- src/Makefile.am | 44 ++++++++----- src/main.c | 2 +- tests/unit/lexer/lexer_tests.c | 116 +++++++++++++++++++++++---------- 4 files changed, 125 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index be08206..f9130ce 100644 --- a/README.md +++ b/README.md @@ -7,22 +7,26 @@ TODO ### Build -run this command: - `autoreconf --force --verbose --install` - -### Test -run this command: - `./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla'` -then: +run theses commands: + `autoreconf --force --verbose --install && ./configure` `make` + `./src/42sh --help` + +### Testing -#### asan run this command: - `./configure CFLAGS='-std=c99 -Werror -Wall -Wextra -Wvla -g -fsanitize=address'` - -then: `make check` +#### debug (asan) + +run this command: + `./src/debug` + +#### testsuite + +run this command: + `./src/testsuite` + ## Authors - Matteo Flebus diff --git a/src/Makefile.am b/src/Makefile.am index 411f4cb..c5c19e7 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,4 +1,3 @@ -# define the subdirectories SUBDIRS = \ parser \ lexer \ @@ -9,6 +8,8 @@ SUBDIRS = \ bin_PROGRAMS = 42sh +42sh_CFLAGS = -std=c99 -Werror -Wall -Wextra -Wvla + 42sh_SOURCES = main.c 42sh_CPPFLAGS = -I%D% @@ -21,18 +22,31 @@ bin_PROGRAMS = 42sh execution/libexecution.a \ utils/libutils.a +# ================ TESTS ================ -################################################# Test -# -#42sh_asan_SOURCES = main.c -# -#42sh_asan_CPPFLAGS = -I%D% -# -#42sh_asan_LDADD = \ -# ast/lib_asan_ast.a \ -# parser/lib_asan_parser.a \ -# lexer/lib_asan_lexer.a \ -# io_backend/lib_asan_io_backend.a \ -# expansion/lib_asan_expansion.a \ -# execution/lib_asan_execution.a \ -# utils/lib_asan_utils.a +# ------------- Unit tests -------------- + +check_PROGRAMS = testsuite + +testsuite_CFLAGS = $(42sh_CFLAGS) +testsuite_CFLAGS += $(CRITERION_CFLAGS) + +testsuite_SOURCES = ../tests/unit/lexer/lexer_tests.c \ + ../tests/unit/utils/utils_tests.c + +testsuite_CPPFLAGS = $(42sh_CPPFLAGS) + +testsuite_LDADD = $(42sh_LDADD) $(CRITERION_LIBS) + +# ------------- asan debug -------------- + +check_PROGRAMS += debug + +debug_CFLAGS = $(42sh_CFLAGS) +debug_CFLAGS += -fsanitize=address -g + +debug_SOURCES = $(42sh_SOURCES) + +debug_CPPFLAGS = $(42sh_CPPFLAGS) + +debug_LDADD = $(42sh_LDADD) diff --git a/src/main.c b/src/main.c index 399b874..423a141 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,5 @@ // === Includes -#include +//#include #include #include diff --git a/tests/unit/lexer/lexer_tests.c b/tests/unit/lexer/lexer_tests.c index d9046d4..910a81a 100644 --- a/tests/unit/lexer/lexer_tests.c +++ b/tests/unit/lexer/lexer_tests.c @@ -6,51 +6,95 @@ #include #include "lexer/lexer.h" +#include "io_backend/io_backend.h" -TestSuite(token_creation); +TestSuite(peek_token); -Test(token_creation, basic) +Test(peek_token, basic_empty) { - char input[] = "Hello World"; + // simulates input + char command[] = ""; + struct iob_context context = + { + IOB_MODE_CMD, + command + }; + iob_init(&context); - char actual[] = new_token(input, 5); - char expected[] = "Hello"; - cr_expect(eq(str, actual, expected)); - free(actual); + // test + struct token *tok = peek_token(); + + // expected + enum token_type type_expected = TOKEN_EOF; + char *string_expected = NULL; + + cr_assert(eq(str, string_expected, tok->data)); + cr_assert(type_expected == tok->type); + + free_token(&tok); } -Test(token_creation, nul) +Test(peek_token, basic_word) { - char input[] = NULL; + // simulates input + char command[] = "hello"; + struct iob_context context = + { + IOB_MODE_CMD, + command + }; + iob_init(&context); - char actual[] = new_token(input, 5); - cr_expect(actual == NULL); + // test + struct token *tok = peek_token(); + + // expected + enum token_type type_expected = TOKEN_WORD; + char string_expected[] = "hello"; + + cr_assert(eq(str, string_expected, tok->data)); + cr_assert(type_expected == tok->type); + free_token(&tok); } - -Test(token_creation, too_large) +Test(peek_token, basic_words) { - char input[] = "Hel"; + // simulates input + char command[] = "echo hello there"; + struct iob_context context = + { + IOB_MODE_CMD, + command + }; + iob_init(&context); - char actual[] = new_token(input, 5); - cr_expect(actual == NULL); -} - -Test(token_creation, empty) -{ - char input[] = ""; - - char actual[] = new_token(input, 5); - cr_expect(actual == NULL); -} - -Test(token_creation, basic_long) -{ - char input[] = "Hello World! This project is a mini shell, I love BIG G."; - - char actual[] = new_token(input, 42); - char expected[] = calloc(42 + 1, sizeof(char)); - strncpy(input, expected, 42); - cr_expect(eq(str, actual, expected)); - free(actual); - free(expected); + // ======= echo + + struct token *tok = peek_token(); + + enum token_type type_expected = TOKEN_WORD; + char string_expected[] = "echo"; + + cr_assert(eq(str, string_expected, tok->data)); + cr_assert(type_expected == tok->type); + free_token(&tok); + + // ======= hello + + tok = peek_token(); + + char string_expected2[] = "hello"; + + cr_assert(eq(str, string_expected2, tok->data)); + cr_assert(type_expected == tok->type); + free_token(&tok); + + // ======= there + + tok = peek_token(); + + char string_expected3[] = "there"; + + cr_assert(eq(str, string_expected3, tok->data)); + cr_assert(type_expected == tok->type); + free_token(&tok); }