107 lines
2.3 KiB
C
107 lines
2.3 KiB
C
#ifndef MK_TEST_LEXER_H
|
|
#define MK_TEST_LEXER_H
|
|
#include <commons.h>
|
|
#include <lexer.h>
|
|
#include <check.h>
|
|
|
|
static void test_lexer(char const* source, int n, ...)
|
|
{
|
|
va_list lst;
|
|
va_start(lst, n);
|
|
|
|
struct status status;
|
|
status_init(&status);
|
|
|
|
struct lexer lex;
|
|
lexer_init(&lex, source, &status);
|
|
|
|
for (int i=0; i<n; i++)
|
|
{
|
|
TokenKind kind = va_arg(lst, TokenKind);
|
|
char const* oracle = va_arg(lst, char const*);
|
|
|
|
struct token* tok;
|
|
tok = lexer_try_new_next(&lex);
|
|
bool ok = status_is_ok(&status);
|
|
if (!ok)
|
|
{
|
|
status_dump(&status);
|
|
}
|
|
ck_assert(ok);
|
|
ck_assert_msg(tok != NULL, "expected %s of value %s, got nothing",
|
|
TokenKindStr[kind] + strlen("TOKEN_"),
|
|
oracle);
|
|
ck_assert_str_eq(oracle, tok->value);
|
|
ck_assert_int_eq(kind, tok->kind);
|
|
token_free(tok);
|
|
free(tok);
|
|
}
|
|
|
|
status_free(&status);
|
|
|
|
lexer_free(&lex);
|
|
va_end(lst);
|
|
}
|
|
|
|
START_TEST(lexer_atom)
|
|
{
|
|
test_lexer(" 34 -2 0 ", 3,
|
|
TOKEN_INT, "34",
|
|
TOKEN_INT, "-2",
|
|
TOKEN_INT, "0"
|
|
);
|
|
|
|
test_lexer(" 3.4 -2.2 .6 7. ", 4,
|
|
TOKEN_FLOAT, "3.4",
|
|
TOKEN_FLOAT, "-2.2",
|
|
TOKEN_FLOAT, ".6",
|
|
TOKEN_FLOAT, "7."
|
|
);
|
|
|
|
test_lexer(" true false ", 2,
|
|
TOKEN_BOOL, "true",
|
|
TOKEN_BOOL, "false"
|
|
);
|
|
|
|
test_lexer(" \"\\\\hel\\rlo\" \"wo\\trld\\n\" \" \\\"bim\\\" \" ", 3,
|
|
TOKEN_STRING, "\\hel\rlo",
|
|
TOKEN_STRING, "wo\trld\n",
|
|
TOKEN_STRING, " \"bim\" "
|
|
);
|
|
|
|
test_lexer(" 'hello ", 1,
|
|
TOKEN_SYMBOL, "hello"
|
|
);
|
|
}
|
|
END_TEST
|
|
|
|
START_TEST(lexer_funcall)
|
|
{
|
|
test_lexer(" (_0pizza?!) ", 3,
|
|
TOKEN_OPAR, "(",
|
|
TOKEN_IDENT, "_0pizza?!",
|
|
TOKEN_CPAR, ")"
|
|
);
|
|
}
|
|
END_TEST
|
|
|
|
START_TEST(lexer_array)
|
|
{
|
|
test_lexer(" [] ", 2,
|
|
TOKEN_OSQUARE, "[",
|
|
TOKEN_CSQUARE, "]"
|
|
);
|
|
}
|
|
END_TEST
|
|
|
|
void register_lexer(Suite* suite)
|
|
{
|
|
TCase* tcase = tcase_create("Lexer");
|
|
tcase_add_test(tcase, lexer_atom);
|
|
tcase_add_test(tcase, lexer_funcall);
|
|
tcase_add_test(tcase, lexer_array);
|
|
suite_add_tcase(suite, tcase);
|
|
}
|
|
|
|
#endif
|