From 1451970ca87d714d9ad852c30137fd8ba4630a17 Mon Sep 17 00:00:00 2001 From: Max Regan Date: Wed, 2 Nov 2022 20:16:29 -0400 Subject: [PATCH] Add more "integration" tests --- SConstruct | 4 +- src/state.c | 3 +- test/test_e2e.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 test/test_e2e.c diff --git a/SConstruct b/SConstruct index 87d874c..f1410c6 100644 --- a/SConstruct +++ b/SConstruct @@ -1,3 +1,5 @@ +# -*- python -*- + from pathlib import Path # @@ -12,7 +14,7 @@ lib_srcs = ["src/parse.c", "src/memory.c", "src/builtins.c", "src/evaluate.c", " lib_includes = ["src/"] -test_srcs = ["test/test_parse.c", "test/test_utility.c", "test/test_state.c"] +test_srcs = ["test/test_parse.c", "test/test_utility.c", "test/test_state.c", "test/test_e2e.c"] test_lib_srcs = ["third-party/unity/src/unity.c"] test_lib_includes = ["third-party/unity/src/"] diff --git a/src/state.c b/src/state.c index f172676..021ea1c 100644 --- a/src/state.c +++ b/src/state.c @@ -69,5 +69,6 @@ struct ucl_state *ucl_state_create_child(struct ucl_state *parent) { } void ucl_state_delete(struct ucl_state *state) { - ucl_object_delete(state->list); + // TODO: Cleanup + // ucl_object_delete(state->list); } diff --git a/test/test_e2e.c b/test/test_e2e.c new file mode 100644 index 0000000..4f53cd7 --- /dev/null +++ b/test/test_e2e.c @@ -0,0 +1,109 @@ +#include +#include +#include + +#include "uclisp.h" +#include "internal.h" +#include "utility.h" +#include "testing_helpers.h" +#include "state.h" +#include "builtins.h" + +static struct ucl_object *input; +static struct ucl_object *response; +static struct ucl_state *state; + +void setUp(void) { + input = NULL; + response = NULL; + + state = ucl_state_create(); + ucl_state_put(state, "let", ucl_builtin_create(ucl_builtin_let)); + ucl_state_put(state, "+", ucl_builtin_create(ucl_builtin_add)); +} + +void tearDown(void) { + // TODO: Implement GC so we can clean these both up + //ucl_object_delete(input); + input = NULL; + ucl_object_delete(response); + response = NULL; + state = NULL; +} + + +static struct ucl_object *eval (const char *code) { + struct ucl_object *sexp = ucl_parse(code); + return ucl_evaluate(state, ucl_car(sexp)); +} + +static void test_simple_add(void) { + ucl_state_put(state, "+", ucl_builtin_create(ucl_builtin_add)); + response = eval("(+ 2 3)"); + TEST_ASSERT_OBJ_INT(response); + TEST_ASSERT_EQUAL(response->integer, 5); +} + +static void test_simple_let(void) { + response = eval("(let ((x 2)) (+ x 3))"); + TEST_ASSERT_OBJ_INT(response); + TEST_ASSERT_EQUAL(response->integer, 5); +} + +static void test_nested_let(void) { + response = eval("(let ((x 2)) (let ((y 5)) (+ x y)))"); + TEST_ASSERT_OBJ_INT(response); + TEST_ASSERT_EQUAL(response->integer, 7); +} + +static void test_multi_let(void) { + response = eval("(let ((x 2)(y 5)) (+ x y))"); + TEST_ASSERT_OBJ_INT(response); + TEST_ASSERT_EQUAL(response->integer, 7); +} + +static void test_let_return_sym(void) { + response = eval("(let ((x 2)) x)"); + TEST_ASSERT_OBJ_INT(response); + TEST_ASSERT_EQUAL(response->integer, 2); +} + +static void test_eval_int(void) { + response = eval("2"); + TEST_ASSERT_OBJ_INT(response); + TEST_ASSERT_EQUAL(response->integer, 2); +} + +static void test_eval_string(void) { + response = eval("\"foo\""); + TEST_ASSERT_OBJ_STRING(response); + TEST_ASSERT_EQUAL_STRING(response->string, "foo"); +} + +static void test_eval_sym_defined(void) { + ucl_state_put(state, "foo", ucl_int_create(2)); + response = eval("foo"); + TEST_ASSERT_OBJ_INT(response); + TEST_ASSERT_EQUAL(response->integer, 2); +} + +static void test_eval_sym_undefined(void) { + response = eval("foo"); + TEST_ASSERT_OBJ_ERROR(response); +} + +int main(void) { + UNITY_BEGIN(); + + RUN_TEST(test_simple_add); + RUN_TEST(test_simple_let); + RUN_TEST(test_nested_let); + RUN_TEST(test_multi_let); + RUN_TEST(test_let_return_sym); + RUN_TEST(test_eval_int); + RUN_TEST(test_eval_string); + RUN_TEST(test_eval_sym_defined); + RUN_TEST(test_eval_sym_undefined); + + return UNITY_END(); +}