gux/lang/tests/parser.c

70 lines
1.6 KiB
C

#include "commons.h"
#include <criterion/criterion.h>
#include <parser.h>
#include <node.h>
static void test_parser(char const* oracle, char const* source)
{
struct parser parser;
parser_init(&parser);
struct node* ast = parser_try_new_root(&parser, source);
cr_assert_neq(NULL, ast, "%s", parser.error_msg);
{
char buf[GUX_STR_SIZE];
node_str(ast, buf, GUX_STR_SIZE);
cr_assert_str_eq(oracle, buf);
}
parser_free(&parser);
}
static void test_parser_fail(char const* source)
{
struct parser parser;
parser_init(&parser);
struct node* ast = parser_try_new_root(&parser, source);
if (ast)
{
char msg[GUX_STR_SIZE];
node_str(ast, msg, GUX_STR_SIZE);
cr_assert_eq(NULL, ast, "ast = %s", msg);
}
cr_assert_eq(NULL, ast);
}
Test(parser, boolean) {
test_parser_fail("true");
test_parser_fail(";");
test_parser("ROOT(BOOL[true])", "true;");
}
Test(parser, assert) {
test_parser("ROOT(ASSERT(BOOL[false]))",
"assert false;");
test_parser_fail("assert;");
}
Test(parser, bool_arith) {
test_parser("ROOT(NOT(NOT(BOOL[true])))",
"!!true;");
test_parser("ROOT(OR(OR(BOOL[true],BOOL[false]),BOOL[false]))",
"true || false || false;");
test_parser("ROOT(OR(AND(BOOL[true],BOOL[false]),BOOL[true]))",
"true && false || true;");
test_parser("ROOT(AND(BOOL[true],OR(BOOL[false],BOOL[true])))",
"true && (false || true);");
test_parser("ROOT(OR(AND(BOOL[true],NOT(BOOL[false])),BOOL[true]))",
"true && !false || true;");
}