2024-03-15 21:59:34 +01:00
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
#include "mrk/lexer.h"
|
|
|
|
#include "mrk/parser_internal.h"
|
|
|
|
|
|
|
|
#define LEXER_INIT() \
|
|
|
|
mrk_lexer *lxr; \
|
|
|
|
TEST_CHECK(mrk_lexer_init(&lxr) == mrk_err_ok)
|
|
|
|
|
|
|
|
#define PARSER_INIT() \
|
|
|
|
mrk_parser *parser; \
|
|
|
|
TEST_CHECK(mrk_parser_init(&parser) == mrk_err_ok)
|
|
|
|
|
|
|
|
#define PARSER_OPEN(buf) \
|
|
|
|
mrk_lexer *lxr; \
|
|
|
|
TEST_CHECK(mrk_lexer_init(&lxr) == mrk_err_ok); \
|
|
|
|
mrk_parser *parser; \
|
|
|
|
TEST_CHECK(mrk_parser_init(&parser) == mrk_err_ok); \
|
|
|
|
mrk_lexer_open(lxr, buf, 0); \
|
|
|
|
mrk_parser_open(parser, lxr)
|
|
|
|
|
|
|
|
|
|
|
|
void test_parse_header() {
|
|
|
|
const char *buf = "### hello world";
|
|
|
|
PARSER_OPEN(buf);
|
|
|
|
|
2024-03-15 22:03:39 +01:00
|
|
|
mrk_ast_node *header;
|
|
|
|
mrk_ast_node_init(&header);
|
|
|
|
TEST_CHECK(mrk_parser_parse_header(header, parser) == mrk_err_ok);
|
2024-03-15 21:59:34 +01:00
|
|
|
|
|
|
|
TEST_CHECK(header->type == mrk_ast_node_type_header);
|
2024-03-15 22:03:39 +01:00
|
|
|
TEST_CHECK(header->d.header.depth == 3);
|
|
|
|
TEST_CHECK(header->children.len == 1);
|
|
|
|
|
|
|
|
mrk_ast_node *header_text = header->children.arr[0];
|
|
|
|
TEST_CHECK(header_text->type == mrk_ast_node_type_text);
|
|
|
|
TEST_CHECK(header_text->d.text.start == 3);
|
|
|
|
TEST_CHECK(header_text->d.text.end == 15);
|
2024-03-15 21:59:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_parse_link() {
|
|
|
|
const char *buf = "[hello world](https://example.com)";
|
|
|
|
PARSER_OPEN(buf);
|
|
|
|
|
|
|
|
mrk_ast_node *link;
|
|
|
|
mrk_ast_node_init(&link);
|
|
|
|
TEST_CHECK(mrk_parser_parse_link(link, parser) == mrk_err_ok);
|
|
|
|
|
|
|
|
TEST_CHECK(link->type == mrk_ast_node_type_link);
|
|
|
|
TEST_CHECK(link->d.link.url_start == 14);
|
|
|
|
TEST_CHECK(link->d.link.url_end == 33);
|
|
|
|
TEST_CHECK(link->children.len == 1);
|
|
|
|
|
|
|
|
mrk_ast_node *link_text = link->children.arr[0];
|
|
|
|
TEST_CHECK(link_text->type == mrk_ast_node_type_text);
|
|
|
|
TEST_CHECK(link_text->d.text.start == 1);
|
|
|
|
TEST_CHECK(link_text->d.text.end == 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_LIST = {
|
|
|
|
{ "parser header", test_parse_header },
|
|
|
|
{ "parser link", test_parse_link },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|