Refactor to encapsulate all state in a 'ucl'

This commit is contained in:
2022-11-22 15:50:46 -05:00
parent 13062a5b86
commit 5320c70dea
23 changed files with 641 additions and 663 deletions

View File

@@ -9,14 +9,17 @@
#include "parse.h"
/* static struct ucl_parse_result *result; */
static struct ucl *state;
static struct ucl_object *response;
void setUp(void) {
state = ucl_create();
response = NULL;
}
void tearDown(void) {
ucl_gc();
// TODO: Release state
state = NULL;
response = NULL;
}
@@ -24,7 +27,7 @@ static void test_token_next_empty_str(void) {
const char *input = "";
const char *curr = input;
response = ucl_token_next(&curr);
response = ucl_token_next(state, &curr);
TEST_ASSERT_NULL(response);
TEST_ASSERT_EQUAL('\0', *curr);
@@ -34,7 +37,7 @@ static void test_token_next_only_whitespace(void) {
const char *input = " \n";
const char *curr = input;
response = ucl_token_next(&curr);
response = ucl_token_next(state, &curr);
TEST_ASSERT_NULL(response);
TEST_ASSERT_EQUAL('\0', *curr);
@@ -44,7 +47,7 @@ static void test_token_next_lparen(void) {
const char *input = "(";
const char *curr = input;
response = ucl_token_next(&curr);
response = ucl_token_next(state, &curr);
TEST_ASSERT_OBJ_SYMBOL_V(response, "(");
TEST_ASSERT_EQUAL('\0', *curr);
}
@@ -53,7 +56,7 @@ static void test_token_next_rparen(void) {
const char *input = ")";
const char *curr = input;
response = ucl_token_next(&curr);
response = ucl_token_next(state, &curr);
TEST_ASSERT_OBJ_SYMBOL_V(response, ")");
TEST_ASSERT_EQUAL('\0', *curr);
@@ -63,12 +66,12 @@ static void test_token_next_lrparen(void) {
const char *input = "()";
const char *curr = input;
response = ucl_token_next(&curr);
response = ucl_token_next(state, &curr);
TEST_ASSERT_OBJ_SYMBOL_V(response, "(");
ucl_object_delete(response);
response = ucl_token_next(&curr);
ucl_object_delete(state, response);
response = ucl_token_next(state, &curr);
TEST_ASSERT_OBJ_SYMBOL_V(response, ")");
TEST_ASSERT_EQUAL('\0', *curr);
@@ -78,7 +81,7 @@ static void test_token_next_string(void) {
const char *input = "\"foo\"";
const char *curr = input;
response = ucl_token_next(&curr);
response = ucl_token_next(state, &curr);
TEST_ASSERT_OBJ_STRING_V(response, "foo");
TEST_ASSERT_EQUAL('\0', *curr);
@@ -88,7 +91,7 @@ static void test_token_next_string_w_whitespace(void) {
const char *input = " \"foo\" ";
const char *curr = input;
response = ucl_token_next(&curr);
response = ucl_token_next(state, &curr);
TEST_ASSERT_OBJ_STRING_V(response, "foo");
TEST_ASSERT_EQUAL_STRING(" ", curr);
@@ -98,7 +101,7 @@ static void test_token_next_symbol(void) {
const char *input = "foo";
const char *curr = input;
response = ucl_token_next(&curr);
response = ucl_token_next(state, &curr);
TEST_ASSERT_OBJ_SYMBOL_V(response, "foo");
TEST_ASSERT_EQUAL_STRING("", curr);
@@ -108,20 +111,20 @@ static void test_token_next_symbol_w_whitespace(void) {
const char *input = " foo ";
const char *curr = input;
response = ucl_token_next(&curr);
response = ucl_token_next(state, &curr);
TEST_ASSERT_OBJ_SYMBOL_V(response, "foo");
TEST_ASSERT_EQUAL_STRING(" ", curr);
}
static void test_tokenize_empty_str(void) {
response = ucl_tokenize("");
response = ucl_tokenize(state, "");
TEST_ASSERT_NIL(response);
}
static void test_tokenize_nil(void) {
response = ucl_tokenize("()");
response = ucl_tokenize(state, "()");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
@@ -141,7 +144,7 @@ static void test_tokenize_nil(void) {
}
static void test_tokenize_scopement(void) {
response = ucl_tokenize("(foo)");
response = ucl_tokenize(state, "(foo)");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
@@ -166,27 +169,27 @@ static void test_tokenize_scopement(void) {
}
static void test_parse_atom_symbol(void) {
response = ucl_parse_token_atom(ucl_symbol_create("foo"));
response = ucl_parse_token_atom(state, ucl_symbol_create(state, "foo"));
TEST_ASSERT_EQUAL(response->type, UCL_TYPE_SYMBOL);
TEST_ASSERT_EQUAL_STRING(response->symbol, "foo");
}
static void test_parse_atom_lparen(void) {
response = ucl_parse_token_atom(ucl_symbol_create("("));
response = ucl_parse_token_atom(state, ucl_symbol_create(state, "("));
TEST_ASSERT_NULL(response);
}
static void test_parse_atom_rparen(void) {
response = ucl_parse_token_atom(ucl_symbol_create(")"));
response = ucl_parse_token_atom(state, ucl_symbol_create(state, ")"));
TEST_ASSERT_NULL(response);
}
static void test_parse_empty_str(void) {
response = ucl_parse("");
response = ucl_parse(state, "");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
@@ -195,7 +198,7 @@ static void test_parse_empty_str(void) {
}
static void test_parse_symbol(void) {
response = ucl_parse("foo");
response = ucl_parse(state, "foo");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
@@ -206,7 +209,7 @@ static void test_parse_symbol(void) {
}
static void test_parse_nil(void) {
response = ucl_parse("()");
response = ucl_parse(state, "()");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
@@ -219,7 +222,7 @@ static void test_parse_nil(void) {
}
static void test_parse_list_1elem(void) {
response = ucl_parse("(foo)");
response = ucl_parse(state, "(foo)");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
@@ -235,7 +238,7 @@ static void test_parse_list_1elem(void) {
}
static void test_parse_list_2elem(void) {
response = ucl_parse("(foo bar)");
response = ucl_parse(state, "(foo bar)");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
@@ -256,7 +259,7 @@ static void test_parse_list_2elem(void) {
}
static void test_parse_2elem(void) {
response = ucl_parse("foo bar");
response = ucl_parse(state, "foo bar");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
@@ -273,7 +276,7 @@ static void test_parse_2elem(void) {
}
static void test_parse_2elem_str(void) {
response = ucl_parse("\"foo\" \"bar\"");
response = ucl_parse(state, "\"foo\" \"bar\"");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
@@ -290,7 +293,7 @@ static void test_parse_2elem_str(void) {
}
static void test_parse_nested(void) {
response = ucl_parse("((foo))");
response = ucl_parse(state, "((foo))");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_EQUAL(UCL_TYPE_CELL, response->type);
@@ -309,45 +312,45 @@ static void test_parse_nested(void) {
}
static void test_parse_mismatched_open(void) {
response = ucl_parse("(");
response = ucl_parse(state, "(");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_parse_mismatched_closed(void) {
response = ucl_parse(")");
response = ucl_parse(state, ")");
TEST_ASSERT_NOT_NULL(response);
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_parse_int(void) {
response = ucl_parse("123");
response = ucl_parse(state, "123");
TEST_ASSERT_OBJ_INT_V(response->cell.car, 123);
}
static void test_parse_int_hex(void) {
response = ucl_parse("0xa");
response = ucl_parse(state, "0xa");
TEST_ASSERT_OBJ_INT_V(response->cell.car, 0xa);
}
static void test_parse_int_neg(void) {
response = ucl_parse("-7");
response = ucl_parse(state, "-7");
TEST_ASSERT_OBJ_INT_V(response->cell.car, -7);
}
static void test_parse_int_plus(void) {
response = ucl_parse("+7");
response = ucl_parse(state, "+7");
TEST_ASSERT_OBJ_INT_V(response->cell.car, 7);
}
static void test_parse_int_garbled(void) {
response = ucl_parse("+1234g7");
response = ucl_parse(state, "+1234g7");
TEST_ASSERT_OBJ_ERROR(response);
}