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,51 +9,52 @@
#include "scope.h"
/* static struct ucl_parse_result *result; */
static struct ucl *state;
static struct ucl_scope *scope;
static struct ucl_object *response;
void setUp(void) {
scope = ucl_scope_create();
state = ucl_create();
scope = ucl_scope_create(state);
}
void tearDown(void) {
ucl_scope_delete(scope);
ucl_gc();
state = NULL;
scope = NULL;
}
static void test_get_empty(void) {
response = ucl_scope_get(scope, "foo");
response = ucl_scope_get(state, scope, "foo");
TEST_ASSERT_OBJ_ERROR(response);
}
static void test_put_get(void) {
ucl_scope_put(scope, "foo", ucl_t_create());
response = ucl_scope_get(scope, "foo");
ucl_scope_put(state, scope, "foo", ucl_t_create(state));
response = ucl_scope_get(state, scope, "foo");
TEST_ASSERT_T(response);
}
static void test_put2_get(void) {
ucl_scope_put(scope, "foo1", ucl_t_create());
ucl_scope_put(scope, "foo2", ucl_nil_create());
response = ucl_scope_get(scope, "foo1");
ucl_scope_put(state, scope, "foo1", ucl_t_create(state));
ucl_scope_put(state, scope, "foo2", ucl_nil_create(state));
response = ucl_scope_get(state, scope, "foo1");
TEST_ASSERT_T(response);
}
static void test_put_modify_get(void) {
struct ucl_object *obj = ucl_tuple_create(
ucl_string_create("bar"),
ucl_string_create("baz"));
struct ucl_object *obj = ucl_tuple_create(state,
ucl_string_create(state, "bar"),
ucl_string_create(state, "baz"));
ucl_scope_put(scope, "foo", obj);
ucl_list_append(obj, ucl_string_create("quux"));
response = ucl_scope_get(scope, "foo");
ucl_scope_put(state, scope, "foo", obj);
ucl_list_append(state, obj, ucl_string_create(state, "quux"));
response = ucl_scope_get(state, scope, "foo");
TEST_ASSERT_OBJ_STRING(ucl_list_nth(response, 2));
TEST_ASSERT_EQUAL_STRING(ucl_list_nth(response, 2)->string, "quux");
TEST_ASSERT_OBJ_STRING(ucl_list_nth(state, response, 2));
TEST_ASSERT_EQUAL_STRING(ucl_list_nth(state, response, 2)->string, "quux");
TEST_ASSERT_OBJ_STRING(ucl_list_nth(obj, 2));
TEST_ASSERT_EQUAL_STRING(ucl_list_nth(obj, 2)->string, "quux");
TEST_ASSERT_OBJ_STRING(ucl_list_nth(state, obj, 2));
TEST_ASSERT_EQUAL_STRING(ucl_list_nth(state, obj, 2)->string, "quux");
}