From 5f376b0b3503d84b1b960472707e04a2f61f8d38 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Wed, 12 Jul 2023 16:22:54 -0300 Subject: [PATCH] First meaningul tests --- shortcodes.h | 22 +++++++++++++++- shortcodes.rl | 72 +++++++++++++++++++-------------------------------- tests.c | 31 ++++++++++++++++++---- 3 files changed, 74 insertions(+), 51 deletions(-) diff --git a/shortcodes.h b/shortcodes.h index e3e4a5e..1d0a2f6 100644 --- a/shortcodes.h +++ b/shortcodes.h @@ -1,4 +1,24 @@ #ifndef SHORTCODES_H #define SHORTCODES_H -int parse(char *); +struct chunk +{ + int start, len; +}; + +typedef struct chunk chunk; + +struct shortcode +{ + int start; + int len; + chunk name; + chunk data; + char matching; + chunk argnames[100]; + chunk argvals[100]; + int argcount; +}; +typedef struct shortcode shortcode; + +shortcode *parse(char *); #endif diff --git a/shortcodes.rl b/shortcodes.rl index cb81242..d2b2a3d 100644 --- a/shortcodes.rl +++ b/shortcodes.rl @@ -1,6 +1,6 @@ -#include #include -#include "bglibs/str.h" +#include "shortcodes.h" + %%{ machine shortcode; @@ -83,12 +83,12 @@ start + sc_list[c_sc].name.start, sc_list[c_sc-1].name.len) !=0) { - printf("Mismatched tags!\n"); - str_copyb(&sc,start + sc_list[c_sc-1].name.start, sc_list[c_sc-1].name.len); - printf("opened: %s\n", sc.s); - str_copyb(&sc,start + sc_list[c_sc].name.start, sc_list[c_sc].name.len); - printf("closed: %s\n", sc.s); - return 1; + // printf("Mismatched tags!\n"); + // str_copyb(&sc,start + sc_list[c_sc-1].name.start, sc_list[c_sc-1].name.len); + // printf("opened: %s\n", sc.s); + // str_copyb(&sc,start + sc_list[c_sc].name.start, sc_list[c_sc].name.len); + // printf("closed: %s\n", sc.s); + return NULL; } // Reuse this shortcode entry for next one sc_list[c_sc].name.start = 0; @@ -98,25 +98,8 @@ }%% -struct chunk { - int start, len; -}; +shortcode *parse(char *input) { -typedef struct chunk chunk; - -struct shortcode { - int start; - int len; - chunk name; - chunk data; - char matching; - chunk argnames[100]; - chunk argvals[100]; - int argcount; -}; -typedef struct shortcode shortcode; - -int parse(char *input) { %%write data; char *eof, *ts, *te = 0; int cs, act = 0; @@ -133,30 +116,29 @@ int parse(char *input) { char *mark = p; char *data_mark = p; - str sc; - str_init(&sc); %% write init; %% write exec; - for (int i=0; sc_list[i].name.start!=0; i++) { - str_copyb(&sc, start + sc_list[i].name.start, sc_list[i].name.len); - printf("sc_name: %s (%d)\n", sc.s, sc_list[i].matching ); - str_copyb(&sc, start + sc_list[i].start, sc_list[i].len); - printf("full_sc: %s\n", sc.s); - if (sc_list[i].matching) { - str_copyb(&sc, start + sc_list[i].data.start, sc_list[i].data.len); - printf("sc_data: %s\n", sc.s); - } + // for (int i=0; sc_list[i].name.start!=0; i++) { + // str_copyb(&sc, start + sc_list[i].name.start, sc_list[i].name.len); + // printf("sc_name: %s (%d)\n", sc.s, sc_list[i].matching ); + // str_copyb(&sc, start + sc_list[i].start, sc_list[i].len); + // printf("full_sc: %s\n", sc.s); + // if (sc_list[i].matching) { + // str_copyb(&sc, start + sc_list[i].data.start, sc_list[i].data.len); + // printf("sc_data: %s\n", sc.s); + // } - for (int j=0; j< sc_list[i].argcount; j++) { - str_copyb(&sc, start + sc_list[i].argnames[j].start, sc_list[i].argnames[j].len); - printf("argname %d: %s\n", j, sc.s); - str_copyb(&sc, start + sc_list[i].argvals[j].start, sc_list[i].argvals[j].len); - printf("argval %d: %s\n", j, sc.s); - } - } - return 0; + // for (int j=0; j< sc_list[i].argcount; j++) { + // str_copyb(&sc, start + sc_list[i].argnames[j].start, sc_list[i].argnames[j].len); + // printf("argname %d: %s\n", j, sc.s); + // str_copyb(&sc, start + sc_list[i].argvals[j].start, sc_list[i].argvals[j].len); + // printf("argval %d: %s\n", j, sc.s); + // } + // } + // TODO: handle errors + return sc_list; } // int main(int argc, char **argv) { diff --git a/tests.c b/tests.c index 6d84b0f..82db8a5 100755 --- a/tests.c +++ b/tests.c @@ -1,19 +1,40 @@ #include +#include #include "shortcodes.h" Describe(parse); BeforeEach(parse) {} AfterEach(parse) {} -Ensure(parse, empty_string) { - char *input = ""; - int output = parse(input); - assert_that(output, is_equal_to(0)); +shortcode *result; +str s; + +chunk_s(char *buffer, chunk c){ + str_copyb(&s, buffer + c.start, c.len); } +Ensure(parse, empty_string) +{ + char *input = ""; + result = parse(input); + // This means no shortcodes in it + assert_that(result[0].name.len, is_equal_to(0)); +} -int main(int argc, char **argv) { +Ensure(parse, simple_shortcode) +{ + char *input = "{{% shortcode %}}"; + result = parse(input); + chunk_s(input, result[0].name); + assert_that(s.s, is_equal_to_string("shortcode")); + assert_that(result[0].argcount, is_equal_to(0)); +} + +int main(int argc, char **argv) +{ + str_init(&s); TestSuite *suite = create_test_suite(); add_test_with_context(suite, parse, empty_string); + add_test_with_context(suite, parse, simple_shortcode); return run_test_suite(suite, create_text_reporter()); }