Implement first iteration of parser and write test scripts

This commit is contained in:
2024-10-25 13:33:31 +01:00
parent 63facb3954
commit 584e92c29c
6 changed files with 190 additions and 1 deletions

76
tests/parser_tests.c Normal file
View File

@@ -0,0 +1,76 @@
/*
* Copyright (c) Camden Dixie O'Brien
* SPDX-License-Identifier: AGPL-3.0-only
*/
#include "parser.h"
#include "testing.h"
#define PARSE_REGEX_STRING(s, r) parse_regex(s, strlen(s), r)
static void a_has_no_alternative(void)
{
regex_t r = { 0 };
const int result = PARSE_REGEX_STRING("a", &r);
ASSERT_NE(-1, result);
ASSERT_NULL(r.alternative);
regex_free_children(&r);
}
static void a_pipe_b_has_alternative(void)
{
regex_t r = { 0 };
const int result = PARSE_REGEX_STRING("a|b", &r);
ASSERT_NE(-1, result);
ASSERT_NOT_NULL(r.alternative);
regex_free_children(&r);
}
static void a_pipe_b_pipe_c_result_alternative_has_alternative(void)
{
regex_t r = { 0 };
const int result = PARSE_REGEX_STRING("a|b|c", &r);
ASSERT_NE(-1, result);
ASSERT_NOT_NULL(r.alternative);
ASSERT_NOT_NULL(r.alternative->alternative);
regex_free_children(&r);
}
static void a_is_parsed_as_unquantified_literal(void)
{
regex_t r = { 0 };
const int result = PARSE_REGEX_STRING("a", &r);
ASSERT_NE(-1, result);
ASSERT_EQ(1, r.sequence.len);
ASSERT_EQ(QUANTIFIER_NONE, r.sequence.contents[0].quantifier);
ASSERT_EQ(TERM_TYPE_LITERAL, r.sequence.contents[0].type);
ASSERT_EQ('a', r.sequence.contents[0].literal);
regex_free_children(&r);
}
static void b_is_parsed_as_unquantified_literal(void)
{
regex_t r = { 0 };
const int result = PARSE_REGEX_STRING("b", &r);
ASSERT_NE(-1, result);
ASSERT_EQ(1, r.sequence.len);
ASSERT_EQ(QUANTIFIER_NONE, r.sequence.contents[0].quantifier);
ASSERT_EQ(TERM_TYPE_LITERAL, r.sequence.contents[0].type);
ASSERT_EQ('b', r.sequence.contents[0].literal);
regex_free_children(&r);
}
int main(void)
{
TESTING_BEGIN();
a_has_no_alternative();
a_pipe_b_has_alternative();
a_pipe_b_pipe_c_result_alternative_has_alternative();
a_is_parsed_as_unquantified_literal();
b_is_parsed_as_unquantified_literal();
return TESTING_END();
}