First meaningul tests

This commit is contained in:
Roberto Alsina 2023-07-12 16:22:54 -03:00
parent 06a65cab7a
commit 5f376b0b35
3 changed files with 74 additions and 51 deletions

View File

@ -1,4 +1,24 @@
#ifndef SHORTCODES_H #ifndef SHORTCODES_H
#define 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 #endif

View File

@ -1,6 +1,6 @@
#include <stdio.h>
#include <string.h> #include <string.h>
#include "bglibs/str.h" #include "shortcodes.h"
%%{ %%{
machine shortcode; machine shortcode;
@ -83,12 +83,12 @@
start + sc_list[c_sc].name.start, start + sc_list[c_sc].name.start,
sc_list[c_sc-1].name.len) !=0) sc_list[c_sc-1].name.len) !=0)
{ {
printf("Mismatched tags!\n"); // printf("Mismatched tags!\n");
str_copyb(&sc,start + sc_list[c_sc-1].name.start, sc_list[c_sc-1].name.len); // str_copyb(&sc,start + sc_list[c_sc-1].name.start, sc_list[c_sc-1].name.len);
printf("opened: %s\n", sc.s); // printf("opened: %s\n", sc.s);
str_copyb(&sc,start + sc_list[c_sc].name.start, sc_list[c_sc].name.len); // str_copyb(&sc,start + sc_list[c_sc].name.start, sc_list[c_sc].name.len);
printf("closed: %s\n", sc.s); // printf("closed: %s\n", sc.s);
return 1; return NULL;
} }
// Reuse this shortcode entry for next one // Reuse this shortcode entry for next one
sc_list[c_sc].name.start = 0; sc_list[c_sc].name.start = 0;
@ -98,25 +98,8 @@
}%% }%%
struct chunk { shortcode *parse(char *input) {
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;
int parse(char *input) {
%%write data; %%write data;
char *eof, *ts, *te = 0; char *eof, *ts, *te = 0;
int cs, act = 0; int cs, act = 0;
@ -133,30 +116,29 @@ int parse(char *input) {
char *mark = p; char *mark = p;
char *data_mark = p; char *data_mark = p;
str sc;
str_init(&sc);
%% write init; %% write init;
%% write exec; %% write exec;
for (int i=0; sc_list[i].name.start!=0; i++) { // 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); // 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 ); // printf("sc_name: %s (%d)\n", sc.s, sc_list[i].matching );
str_copyb(&sc, start + sc_list[i].start, sc_list[i].len); // str_copyb(&sc, start + sc_list[i].start, sc_list[i].len);
printf("full_sc: %s\n", sc.s); // printf("full_sc: %s\n", sc.s);
if (sc_list[i].matching) { // if (sc_list[i].matching) {
str_copyb(&sc, start + sc_list[i].data.start, sc_list[i].data.len); // str_copyb(&sc, start + sc_list[i].data.start, sc_list[i].data.len);
printf("sc_data: %s\n", sc.s); // printf("sc_data: %s\n", sc.s);
} // }
for (int j=0; j< sc_list[i].argcount; j++) { // 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); // str_copyb(&sc, start + sc_list[i].argnames[j].start, sc_list[i].argnames[j].len);
printf("argname %d: %s\n", j, sc.s); // printf("argname %d: %s\n", j, sc.s);
str_copyb(&sc, start + sc_list[i].argvals[j].start, sc_list[i].argvals[j].len); // str_copyb(&sc, start + sc_list[i].argvals[j].start, sc_list[i].argvals[j].len);
printf("argval %d: %s\n", j, sc.s); // printf("argval %d: %s\n", j, sc.s);
} // }
} // }
return 0; // TODO: handle errors
return sc_list;
} }
// int main(int argc, char **argv) { // int main(int argc, char **argv) {

31
tests.c
View File

@ -1,19 +1,40 @@
#include <cgreen/cgreen.h> #include <cgreen/cgreen.h>
#include <bglibs/str.h>
#include "shortcodes.h" #include "shortcodes.h"
Describe(parse); Describe(parse);
BeforeEach(parse) {} BeforeEach(parse) {}
AfterEach(parse) {} AfterEach(parse) {}
Ensure(parse, empty_string) { shortcode *result;
char *input = ""; str s;
int output = parse(input);
assert_that(output, is_equal_to(0)); 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(); TestSuite *suite = create_test_suite();
add_test_with_context(suite, parse, empty_string); add_test_with_context(suite, parse, empty_string);
add_test_with_context(suite, parse, simple_shortcode);
return run_test_suite(suite, create_text_reporter()); return run_test_suite(suite, create_text_reporter());
} }