test(parser): add unordered list parsing test

main
Jef Roosens 2024-03-16 15:03:56 +01:00
parent 07539403cc
commit 80a96a3e33
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 56 additions and 0 deletions

View File

@ -13,6 +13,8 @@ typedef enum mrk_ast_node_type {
mrk_ast_node_type_space, mrk_ast_node_type_space,
mrk_ast_node_type_link, mrk_ast_node_type_link,
mrk_ast_node_type_paragraph, mrk_ast_node_type_paragraph,
mrk_ast_node_type_list,
mrk_ast_node_type_list_item,
} mrk_ast_node_type; } mrk_ast_node_type;
typedef struct mrk_ast_node { typedef struct mrk_ast_node {
@ -33,6 +35,13 @@ typedef struct mrk_ast_node {
size_t url_start; size_t url_start;
size_t url_end; size_t url_end;
} link; } link;
struct {
bool ordered;
} list;
struct {
bool checkbox;
bool checked;
} list_item;
} d; } d;
} mrk_ast_node; } mrk_ast_node;

View File

@ -69,4 +69,9 @@ mrk_err mrk_parser_parse_text(mrk_ast_node *out, mrk_parser *parser);
*/ */
mrk_err mrk_parser_parse_link(mrk_ast_node *out, mrk_parser *parser); mrk_err mrk_parser_parse_link(mrk_ast_node *out, mrk_parser *parser);
/**
* Parse one of the three list types.
*/
mrk_err mrk_parser_parse_list(mrk_ast_node *out, mrk_parser *parser);
#endif #endif

View File

@ -57,8 +57,50 @@ void test_parse_link() {
TEST_CHECK(link_text->d.text.end == 12); TEST_CHECK(link_text->d.text.end == 12);
} }
void test_parse_unordered_list() {
const char *buf = "* element one\n* element two\n paragraph in element two";
PARSER_OPEN(buf);
mrk_ast_node *list;
mrk_ast_node_init(&list);
TEST_CHECK(mrk_parser_parse_list(list, parser) == mrk_err_ok);
TEST_CHECK(list->type == mrk_ast_node_type_list);
TEST_CHECK(!list->d.list.ordered);
TEST_CHECK(list->children.len == 2);
mrk_ast_node *child = list->children.arr[0];
TEST_CHECK(child->type == mrk_ast_node_type_list_item);
TEST_CHECK(!child->d.list_item.checkbox);
TEST_CHECK(child->children.len == 1);
mrk_ast_node *subchild = child->children.arr[0];
TEST_CHECK(subchild->type == mrk_ast_node_type_text);
TEST_CHECK(subchild->d.text.start == 2);
TEST_CHECK(subchild->d.text.end == 13);
child = list->children.arr[1];
TEST_CHECK(child->type == mrk_ast_node_type_list_item);
TEST_CHECK(!child->d.list_item.checkbox);
TEST_CHECK(child->children.len == 2);
subchild = child->children.arr[0];
TEST_CHECK(subchild->type == mrk_ast_node_type_text);
TEST_CHECK(subchild->d.text.start == 16);
TEST_CHECK(subchild->d.text.end == 27);
subchild = child->children.arr[1];
TEST_CHECK(subchild->type == mrk_ast_node_type_paragraph);
TEST_CHECK(subchild->children.len == 1);
TEST_CHECK(subchild->children.arr[0]->type == mrk_ast_node_type_text);
TEST_CHECK(subchild->children.arr[0]->d.text.start == 31);
TEST_CHECK(subchild->children.arr[0]->d.text.end == 55);
}
TEST_LIST = { TEST_LIST = {
{ "parser header", test_parse_header }, { "parser header", test_parse_header },
{ "parser link", test_parse_link }, { "parser link", test_parse_link },
{ "parser unordered list", test_parse_unordered_list },
{ NULL, NULL } { NULL, NULL }
}; };