2024-03-04 14:06:17 +01:00
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
#include "mrk/lexer.h"
|
|
|
|
|
|
|
|
#define LEXER_INIT() \
|
|
|
|
mrk_lexer *lxr; \
|
|
|
|
TEST_CHECK(mrk_lexer_init(&lxr) == mrk_err_ok)
|
|
|
|
|
|
|
|
void test_lexer_header() {
|
|
|
|
LEXER_INIT();
|
|
|
|
|
2024-03-12 20:51:48 +01:00
|
|
|
const char *buf = "#### hallo hallo";
|
2024-03-04 14:06:17 +01:00
|
|
|
mrk_lexer_open(lxr, buf, 0);
|
|
|
|
|
|
|
|
mrk_token t;
|
|
|
|
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
|
2024-03-10 22:54:13 +01:00
|
|
|
TEST_CHECK(t.type == mrk_token_type_header_start);
|
2024-03-04 14:06:17 +01:00
|
|
|
TEST_CHECK_(t.start == 0, "t.start == %lu", t.start);
|
|
|
|
TEST_CHECK(t.end == 4);
|
|
|
|
|
2024-03-12 20:51:48 +01:00
|
|
|
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
|
|
|
|
TEST_CHECK(t.type == mrk_token_type_text);
|
|
|
|
TEST_CHECK_(t.start == 4, "t.start == %lu", t.start);
|
|
|
|
TEST_CHECK(t.end == 16);
|
|
|
|
|
2024-03-04 14:06:17 +01:00
|
|
|
mrk_lexer_free(lxr);
|
|
|
|
}
|
|
|
|
|
2024-03-04 14:50:00 +01:00
|
|
|
void test_lexer_line_break() {
|
|
|
|
LEXER_INIT();
|
|
|
|
|
|
|
|
const char *buf = " \n";
|
|
|
|
mrk_lexer_open(lxr, buf, 0);
|
|
|
|
|
|
|
|
mrk_token t;
|
|
|
|
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
|
|
|
|
TEST_CHECK(t.type == mrk_token_type_line_break);
|
|
|
|
|
|
|
|
TEST_CHECK(mrk_lexer_done(lxr));
|
|
|
|
|
|
|
|
const char *buf2 = " ";
|
|
|
|
mrk_lexer_open(lxr, buf2, 0);
|
|
|
|
|
|
|
|
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
|
2024-03-10 22:54:13 +01:00
|
|
|
TEST_CHECK(t.type == mrk_token_type_text);
|
2024-03-04 14:50:00 +01:00
|
|
|
|
|
|
|
TEST_CHECK(mrk_lexer_done(lxr));
|
|
|
|
|
|
|
|
mrk_lexer_free(lxr);
|
2024-03-07 11:02:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_lexer_simple1() {
|
|
|
|
LEXER_INIT();
|
2024-03-04 14:50:00 +01:00
|
|
|
|
2024-03-07 11:02:28 +01:00
|
|
|
const char *buf = "### hello world\n\nthis is a paragraph";
|
|
|
|
mrk_lexer_open(lxr, buf, 0);
|
|
|
|
|
|
|
|
mrk_token t;
|
|
|
|
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
|
2024-03-10 22:54:13 +01:00
|
|
|
TEST_CHECK(t.type == mrk_token_type_header_start);
|
2024-03-07 11:02:28 +01:00
|
|
|
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
|
2024-03-10 22:54:13 +01:00
|
|
|
TEST_CHECK(t.type == mrk_token_type_text);
|
2024-03-07 11:02:28 +01:00
|
|
|
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
|
2024-03-12 20:51:48 +01:00
|
|
|
TEST_CHECK(t.type == mrk_token_type_blank_line);
|
2024-03-07 11:02:28 +01:00
|
|
|
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
|
2024-03-10 22:54:13 +01:00
|
|
|
TEST_CHECK(t.type == mrk_token_type_text);
|
2024-03-04 14:50:00 +01:00
|
|
|
}
|
|
|
|
|
2024-03-25 17:03:00 +01:00
|
|
|
void test_lexer_code() {
|
|
|
|
LEXER_INIT();
|
|
|
|
|
|
|
|
const char *buf = "`world [hello](link)`";
|
|
|
|
mrk_lexer_open(lxr, buf, 0);
|
|
|
|
|
|
|
|
mrk_token t;
|
|
|
|
|
|
|
|
TEST_CHECK(mrk_lexer_next(&t, lxr) == mrk_lexer_err_ok);
|
|
|
|
TEST_CHECK(t.type == mrk_token_type_backtick);
|
|
|
|
TEST_CHECK(t.start == 0);
|
|
|
|
TEST_CHECK(t.end == 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-04 14:06:17 +01:00
|
|
|
TEST_LIST = {
|
|
|
|
{ "lexer header", test_lexer_header },
|
2024-03-04 14:50:00 +01:00
|
|
|
{ "lexer line break", test_lexer_line_break},
|
2024-03-07 11:02:28 +01:00
|
|
|
{ "lexer simple 1", test_lexer_simple1 },
|
2024-03-25 17:03:00 +01:00
|
|
|
{ "lexer code", test_lexer_code },
|
2024-03-04 14:06:17 +01:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|