71 lines
1.3 KiB
C
71 lines
1.3 KiB
C
#ifndef MK_TEST_PARSER_H
|
|
#define MK_TEST_PARSER_H
|
|
#include <check.h>
|
|
#include <commons.h>
|
|
#include <lexer.h>
|
|
#include <parser.h>
|
|
|
|
void test_parser(char const* oracle, char const* source)
|
|
{
|
|
struct status status;
|
|
status_init(&status);
|
|
|
|
struct lexer lex;
|
|
lexer_init(&lex, source, &status);
|
|
|
|
struct parser parser;
|
|
parser_init(&parser, &lex);
|
|
|
|
struct node* ast = parser_try_new_parse(&parser);
|
|
ck_assert(ast);
|
|
|
|
struct str my_node_str;
|
|
str_init(&my_node_str);
|
|
node_str(ast, &my_node_str);
|
|
|
|
ck_assert_str_eq(oracle, my_node_str.value);
|
|
|
|
bool ok = status_is_ok(&status);
|
|
if (!ok)
|
|
{
|
|
status_dump(&status);
|
|
}
|
|
ck_assert(ok);
|
|
|
|
str_free(&my_node_str);
|
|
node_free(ast);
|
|
free(ast);
|
|
parser_free(&parser);
|
|
lexer_free(&lex);
|
|
status_free(&status);
|
|
}
|
|
|
|
START_TEST(parser_atom)
|
|
{
|
|
test_parser("ROOT(INT[34])",
|
|
" 34 ");
|
|
|
|
test_parser("ROOT(FLOAT[0.8])",
|
|
" 0.8 ");
|
|
|
|
test_parser("ROOT(BOOL[true],BOOL[false])",
|
|
" true false ");
|
|
|
|
test_parser("ROOT(STRING[pizza!])",
|
|
" \"pizza!\" ");
|
|
|
|
test_parser("ROOT(SYMBOL[tea])",
|
|
" 'tea ");
|
|
}
|
|
END_TEST
|
|
|
|
void register_parser(Suite* suite)
|
|
{
|
|
TCase* tcase = tcase_create("Parser");
|
|
tcase_add_test(tcase, parser_atom);
|
|
|
|
suite_add_tcase(suite, tcase);
|
|
}
|
|
|
|
#endif
|