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
#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

View File

@ -1,6 +1,6 @@
#include <stdio.h>
#include <string.h>
#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) {

31
tests.c
View File

@ -1,19 +1,40 @@
#include <cgreen/cgreen.h>
#include <bglibs/str.h>
#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());
}