Add more tests for lets with errors

This commit is contained in:
2022-11-02 20:22:32 -04:00
parent 1451970ca8
commit 9c1a81811c
2 changed files with 15 additions and 3 deletions

View File

@@ -163,7 +163,7 @@ struct ucl_object *ucl_builtin_let(struct ucl_state *state, struct ucl_object *a
if (value->type == UCL_TYPE_ERROR) { if (value->type == UCL_TYPE_ERROR) {
// TODO cleanup // TODO cleanup
assert(0); return value;
} }
ucl_state_put(let_state, sym->symbol, 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) { FOREACH_LIST(expressions, iter, item) {
result = ucl_evaluate(let_state, item); result = ucl_evaluate(let_state, item);
if (result->type == UCL_TYPE_ERROR) { if (result->type == UCL_TYPE_ERROR) {
assert(0); return result;
break;
} }
} }

View File

@@ -20,6 +20,7 @@ void setUp(void) {
state = ucl_state_create(); state = ucl_state_create();
ucl_state_put(state, "let", ucl_builtin_create(ucl_builtin_let)); 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, "+", ucl_builtin_create(ucl_builtin_add));
ucl_state_put(state, "error", ucl_builtin_create(ucl_builtin_error));
} }
void tearDown(void) { void tearDown(void) {
@@ -50,12 +51,22 @@ static void test_simple_let(void) {
TEST_ASSERT_EQUAL(response->integer, 5); 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) { static void test_nested_let(void) {
response = eval("(let ((x 2)) (let ((y 5)) (+ x y)))"); response = eval("(let ((x 2)) (let ((y 5)) (+ x y)))");
TEST_ASSERT_OBJ_INT(response); TEST_ASSERT_OBJ_INT(response);
TEST_ASSERT_EQUAL(response->integer, 7); 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) { static void test_multi_let(void) {
response = eval("(let ((x 2)(y 5)) (+ x y))"); response = eval("(let ((x 2)(y 5)) (+ x y))");
TEST_ASSERT_OBJ_INT(response); TEST_ASSERT_OBJ_INT(response);
@@ -97,7 +108,9 @@ int main(void) {
RUN_TEST(test_simple_add); RUN_TEST(test_simple_add);
RUN_TEST(test_simple_let); RUN_TEST(test_simple_let);
RUN_TEST(test_let_assignment_error);
RUN_TEST(test_nested_let); RUN_TEST(test_nested_let);
RUN_TEST(test_nested_let_scope);
RUN_TEST(test_multi_let); RUN_TEST(test_multi_let);
RUN_TEST(test_let_return_sym); RUN_TEST(test_let_return_sym);
RUN_TEST(test_eval_int); RUN_TEST(test_eval_int);