From: Niki Roo Date: Wed, 17 Jul 2024 09:01:39 +0000 (+0200) Subject: first test X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=e2ac9865d1c3002e813cc3e73d692bac32f8e3e6;p=cbook.git first test --- diff --git a/src/tests-cbook/main.c b/src/tests-cbook/main.c index 3527333..55a83fa 100644 --- a/src/tests-cbook/main.c +++ b/src/tests-cbook/main.c @@ -3,14 +3,12 @@ // The tests we intend to run: -// SRunner *test_XXX(SRunner *runner, int more); -// ... +SRunner *test_pl1(SRunner *runner, int more); SRunner *get_tests(int more) { SRunner *runner = NULL; - // runner = test_XXX(runner, more); - // ... + runner = test_pl1(runner, more); return runner; } diff --git a/src/tests-cbook/test-pl1.c b/src/tests-cbook/test-pl1.c new file mode 100644 index 0000000..989533d --- /dev/null +++ b/src/tests-cbook/test-pl1.c @@ -0,0 +1,99 @@ +/* Note: fmemopen, open_memstream require gnu99 standard instead of c99 */ + +#include +#include +#include +#include + +#include "cutils/check/launcher.h" +#include "cutils/array.h" + +#include "cbook/cbook.h" + +static void setup() { +} + +static void teardown() { +} + +static void reset() { + teardown(); + setup(); +} + +static void print_line(line_t *line); +static void print_line(line_t *line) { + if (!line) { + printf("DCL: (null)\n"); + return; + } + + printf("DCL "); + if (line->level) + for (int i = 0 ; i < line->level ; i++) + printf("\t"); + printf("%i %s: type %i (size: %zu = %zu bytes)\n", + line->level, + line->name, + line->type, + line->size, + line->bytes + ); +} + +START(simple_read_book) + char book_content[] = + " DCL 1 book, \n" + " 2 titi CHAR(10), \n" + " *; \n"; + + book_t *book = new_book(); + FILE *fbook = fmemopen((void*)book_content, strlen(book_content), "rb"); + + int ok = read_book(fbook, book); + if (!ok) + failure("Simple book failed to be read"); + + assert_str("Book name", "BOOK", book->name); + if (book->err_line || book->err_field || book->err_mess) + failure("Unexpected error at line %zu in field %s: %s\n", + book->err_line, book->err_field, book->err_mess); + assert_sz("Book size in bytes", 10, book->bytes); + assert_sz("Number of lines 1", 1, book->lines->count); + line_t *line = *((line_t **)(array_first(book->lines))); + assert_int("Type of line", CBOOK_FMT_CHAR, line->type); + assert_sz("Number of sub lines", 0, line->children->count); + + fclose(fbook); +END + +SRunner *test_pl1(SRunner *runner, int more) { + Suite *suite = suite_create("pl/1"); + + TCase *core = tcase_create("core"); + tcase_add_checked_fixture(core, setup, teardown); + + tcase_add_test(core, simple_read_book); + // ... + + suite_add_tcase(suite, core); + + if (!runner) + runner = srunner_create(suite); + else + srunner_add_suite(runner, suite); + + if (more) { + Suite *suite = suite_create("pl/1 -- more (longer)"); + + TCase *tmore = tcase_create("more"); + tcase_add_checked_fixture(tmore, setup, teardown); + // TODO + + suite_add_tcase(suite, tmore); + srunner_add_suite(runner, suite); + } + + return runner; +} +