Files
uclisp/test/test_e2e.c
2022-11-02 20:36:07 -04:00

130 lines
3.3 KiB
C

#include <stdlib.h>
#include <unity.h>
#include <string.h>
#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));
ucl_state_put(state, "error", ucl_builtin_create(ucl_builtin_error));
}
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_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);
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);
}
static void test_eval_nested_error(void) {
response = eval("(+ (error \"foo\") 3)");
TEST_ASSERT_OBJ_ERROR(response);
}
int main(void) {
UNITY_BEGIN();
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);
RUN_TEST(test_eval_string);
RUN_TEST(test_eval_sym_defined);
RUN_TEST(test_eval_sym_undefined);
RUN_TEST(test_eval_nested_error);
return UNITY_END();
}