changeset 164:f91caf9fe066

built-ins: Test `print` built-in
author Lewin Bormann <lbo@spheniscida.de>
date Tue, 03 Sep 2019 17:17:02 +0200
parents 2448ebe4a586
children 97e8d35ede6d
files src/built-ins_test.c
diffstat 1 files changed, 44 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/built-ins_test.c	Tue Sep 03 17:16:49 2019 +0200
+++ b/src/built-ins_test.c	Tue Sep 03 17:17:02 2019 +0200
@@ -1,5 +1,8 @@
 #include "built-ins.h"
 
+#define _POSIX_C_SOURCE 200809
+#include <stdio.h>
+
 #include "atom.h"
 #include "parse.h"
 #include "preprocess.h"
@@ -280,6 +283,45 @@
     yvec_destroy(&state.call_stack);
 }
 
+void test_builtin_print(void) {
+    fprintf(stderr, "test_builtin_print ============\n");
+
+    char buf[512];
+    memset(buf, 0, 512);
+    FILE* output = fmemopen(buf, 512, "w");
+
+    ybuiltin_set_output(output);
+
+    ystr_t input = ystr_new(
+            "(let a \"hello\")"
+            "(let b 'world)"
+            "(print 1 a b)");
+    ystr_t expected = ystr_new("1 \"hello\" world");
+
+    ystr_t error = ystr_new(NULL);
+    yexpr_t program = yexpr_new();
+    assert(yparse_str(&input, &program, &error));
+
+    ypreprocess(&program);
+
+    yeval_state_t state;
+    YVEC_INIT(&state.call_stack, 4, yexpr_t);
+
+    yexpr_t result = yeval_list_return_last(&state, &program.value.list, 0);
+    assert(result.typ == YEXPR_UNDEF);
+
+    fclose(output);
+
+    assert(0 == ystr_cmp_str(&expected, buf));
+
+    ystr_destroy(&expected);
+    yexpr_destroy(&result);
+    yexpr_destroy(&program);
+    ystr_destroy(&input);
+    ystr_destroy(&error);
+    yvec_destroy(&state.call_stack);
+}
+
 int main(void) {
     test_builtin_translate();
     test_builtin_for();
@@ -289,6 +331,8 @@
     test_builtin_let_parsed();
     test_builtin_let_recall();
     test_builtin_eq();
+    test_builtin_print();
+
     yvalue_free_all();
     yatom_free_all();
     return 0;