From 9c1a81811c95a6fbd628414644c27e700eeb4cd2 Mon Sep 17 00:00:00 2001 From: Max Regan Date: Wed, 2 Nov 2022 20:22:32 -0400 Subject: [PATCH] Add more tests for lets with errors --- src/builtins.c | 5 ++--- test/test_e2e.c | 13 +++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/builtins.c b/src/builtins.c index 0aaf52e..17cf078 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -163,7 +163,7 @@ struct ucl_object *ucl_builtin_let(struct ucl_state *state, struct ucl_object *a if (value->type == UCL_TYPE_ERROR) { // TODO cleanup - assert(0); + return value; } ucl_state_put(let_state, sym->symbol, value); } @@ -172,8 +172,7 @@ struct ucl_object *ucl_builtin_let(struct ucl_state *state, struct ucl_object *a FOREACH_LIST(expressions, iter, item) { result = ucl_evaluate(let_state, item); if (result->type == UCL_TYPE_ERROR) { - assert(0); - break; + return result; } } diff --git a/test/test_e2e.c b/test/test_e2e.c index 4f53cd7..69b6444 100644 --- a/test/test_e2e.c +++ b/test/test_e2e.c @@ -20,6 +20,7 @@ void setUp(void) { 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)); + ucl_state_put(state, "error", ucl_builtin_create(ucl_builtin_error)); } void tearDown(void) { @@ -50,12 +51,22 @@ static void test_simple_let(void) { TEST_ASSERT_EQUAL(response->integer, 5); } +static void test_let_assignment_error(void) { + response = eval("(let ((x (error \"foo\"))) (+ x 3))"); + TEST_ASSERT_OBJ_ERROR(response); +} + 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_nested_let_scope(void) { + response = eval("(let ((x 2)) (let ((y 5)) (+ x y)) y)"); + TEST_ASSERT_OBJ_ERROR(response); +} + static void test_multi_let(void) { response = eval("(let ((x 2)(y 5)) (+ x y))"); TEST_ASSERT_OBJ_INT(response); @@ -97,7 +108,9 @@ int main(void) { RUN_TEST(test_simple_add); RUN_TEST(test_simple_let); + RUN_TEST(test_let_assignment_error); RUN_TEST(test_nested_let); + RUN_TEST(test_nested_let_scope); RUN_TEST(test_multi_let); RUN_TEST(test_let_return_sym); RUN_TEST(test_eval_int);