aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/main.c1
-rw-r--r--tests/sema_test.c48
-rw-r--r--tests/tests.h1
3 files changed, 50 insertions, 0 deletions
diff --git a/tests/main.c b/tests/main.c
index f0caf52..dfd3ed9 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -8,6 +8,7 @@ int main(void)
run_lexer_tests(&context);
run_parser_tests(&context);
+ run_sema_tests(&context);
run_codegen_tests(&context);
printf("\n%d checks, %d failure(s)\n", context.checks, context.failures);
diff --git a/tests/sema_test.c b/tests/sema_test.c
new file mode 100644
index 0000000..a1946d9
--- /dev/null
+++ b/tests/sema_test.c
@@ -0,0 +1,48 @@
+#include <stdbool.h>
+
+#include "parser/parser.h"
+#include "sema/sema.h"
+#include "tests.h"
+
+static bool analyze_source(const char* source)
+{
+ struct Lexer lexer = create_lexer(source);
+ struct Program program;
+ if (!parse_program(&lexer, &program))
+ {
+ free_program(&program);
+ return false;
+ }
+
+ bool ok = analyze_program(&program);
+ free_program(&program);
+ return ok;
+}
+
+static void test_valid_program(struct TestContext* context)
+{
+ check(context, analyze_source("const N = 1\ndata msg = \"hi\"\nproc main\n{\nsyscall\n}\n"));
+}
+
+static void test_missing_main(struct TestContext* context)
+{
+ check(context, !analyze_source("proc helper\n{\nsyscall\n}\n"));
+}
+
+static void test_duplicate_const(struct TestContext* context)
+{
+ check(context, !analyze_source("const X = 1\nconst X = 2\nproc main\n{\nsyscall\n}\n"));
+}
+
+static void test_duplicate_across_kinds(struct TestContext* context)
+{
+ check(context, !analyze_source("data foo = \"a\"\nproc foo\n{\nsyscall\n}\nproc main\n{\nsyscall\n}\n"));
+}
+
+void run_sema_tests(struct TestContext* context)
+{
+ test_valid_program(context);
+ test_missing_main(context);
+ test_duplicate_const(context);
+ test_duplicate_across_kinds(context);
+}
diff --git a/tests/tests.h b/tests/tests.h
index 32ee4a6..2caf9b3 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -4,4 +4,5 @@
void run_lexer_tests(struct TestContext* context);
void run_parser_tests(struct TestContext* context);
+void run_sema_tests(struct TestContext* context);
void run_codegen_tests(struct TestContext* context);