From 5d31f28bedc3d045f01f390245b5afbbb316e414 Mon Sep 17 00:00:00 2001 From: Zuhaitz Méndez Fernández de Aránguiz Date: Thu, 22 Jan 2026 22:56:40 +0000 Subject: Fix for #91 --- src/codegen/codegen.h | 2 +- src/codegen/codegen_decl.c | 9 +++------ src/codegen/codegen_main.c | 4 ++-- tests/features/_fstring_mod.zc | 4 ++++ tests/features/fstring_mod.zc | 4 ---- tests/features/test_fstring.zc | 2 +- tests/modules/test_aliasing.zc | 4 ++-- tests/modules/test_modules/_math.zc | 19 +++++++++++++++++++ tests/modules/test_modules/_physics.zc | 17 +++++++++++++++++ tests/modules/test_modules/_simple.zc | 3 +++ tests/modules/test_modules/math.zc | 19 ------------------- tests/modules/test_modules/physics.zc | 17 ----------------- tests/modules/test_modules/simple.zc | 3 --- tests/modules/test_namespaced.zc | 4 ++-- tests/run_tests.sh | 2 +- 15 files changed, 55 insertions(+), 58 deletions(-) create mode 100644 tests/features/_fstring_mod.zc delete mode 100644 tests/features/fstring_mod.zc create mode 100644 tests/modules/test_modules/_math.zc create mode 100644 tests/modules/test_modules/_physics.zc create mode 100644 tests/modules/test_modules/_simple.zc delete mode 100644 tests/modules/test_modules/math.zc delete mode 100644 tests/modules/test_modules/physics.zc delete mode 100644 tests/modules/test_modules/simple.zc diff --git a/src/codegen/codegen.h b/src/codegen/codegen.h index 0a1cbaf..d27356a 100644 --- a/src/codegen/codegen.h +++ b/src/codegen/codegen.h @@ -39,7 +39,7 @@ void emit_globals(ParserContext *ctx, ASTNode *node, FILE *out); void emit_lambda_defs(ParserContext *ctx, FILE *out); void emit_protos(ASTNode *node, FILE *out); void emit_impl_vtables(ParserContext *ctx, FILE *out); -void emit_tests_and_runner(ParserContext *ctx, ASTNode *node, FILE *out); +int emit_tests_and_runner(ParserContext *ctx, ASTNode *node, FILE *out); void print_type_defs(ParserContext *ctx, FILE *out, ASTNode *nodes); // Global state (shared across modules). diff --git a/src/codegen/codegen_decl.c b/src/codegen/codegen_decl.c index 4e6db6a..109aa9a 100644 --- a/src/codegen/codegen_decl.c +++ b/src/codegen/codegen_decl.c @@ -958,8 +958,8 @@ void emit_impl_vtables(ParserContext *ctx, FILE *out) } } -// Emit test functions and runner -void emit_tests_and_runner(ParserContext *ctx, ASTNode *node, FILE *out) +// Emit test functions and runner. Returns number of tests emitted. +int emit_tests_and_runner(ParserContext *ctx, ASTNode *node, FILE *out) { ASTNode *cur = node; int test_count = 0; @@ -983,10 +983,7 @@ void emit_tests_and_runner(ParserContext *ctx, ASTNode *node, FILE *out) } fprintf(out, "}\n\n"); } - else - { - fprintf(out, "void _z_run_tests() {}\n"); - } + return test_count; } // Emit type definitions- diff --git a/src/codegen/codegen_main.c b/src/codegen/codegen_main.c index 7363e61..61269ef 100644 --- a/src/codegen/codegen_main.c +++ b/src/codegen/codegen_main.c @@ -547,7 +547,7 @@ void codegen_node(ParserContext *ctx, ASTNode *node, FILE *out) emit_lambda_defs(ctx, out); - emit_tests_and_runner(ctx, kids, out); + int test_count = emit_tests_and_runner(ctx, kids, out); ASTNode *iter = merged_funcs; while (iter) @@ -656,7 +656,7 @@ void codegen_node(ParserContext *ctx, ASTNode *node, FILE *out) chk = chk->next; } - if (!has_user_main) + if (!has_user_main && test_count > 0) { fprintf(out, "\nint main() { _z_run_tests(); return 0; }\n"); } diff --git a/tests/features/_fstring_mod.zc b/tests/features/_fstring_mod.zc new file mode 100644 index 0000000..78b16ef --- /dev/null +++ b/tests/features/_fstring_mod.zc @@ -0,0 +1,4 @@ + +fn get_val() -> int { + return 42; +} diff --git a/tests/features/fstring_mod.zc b/tests/features/fstring_mod.zc deleted file mode 100644 index 78b16ef..0000000 --- a/tests/features/fstring_mod.zc +++ /dev/null @@ -1,4 +0,0 @@ - -fn get_val() -> int { - return 42; -} diff --git a/tests/features/test_fstring.zc b/tests/features/test_fstring.zc index 3ef64a7..b2a8f36 100644 --- a/tests/features/test_fstring.zc +++ b/tests/features/test_fstring.zc @@ -1,4 +1,4 @@ -import "tests/features/fstring_mod.zc" as Mod; +import "tests/features/_fstring_mod.zc" as Mod; test "alias namespace fstring" { // Tests that {Mod::get_val()} is parsed correctly (Mod_get_val) diff --git a/tests/modules/test_aliasing.zc b/tests/modules/test_aliasing.zc index 96b200c..468e558 100644 --- a/tests/modules/test_aliasing.zc +++ b/tests/modules/test_aliasing.zc @@ -1,6 +1,6 @@ -import { Vector as MathVec } from "./test_modules/math.zc"; -import { Vector as PhysicsVec } from "./test_modules/physics.zc"; +import { Vector as MathVec } from "./test_modules/_math.zc"; +import { Vector as PhysicsVec } from "./test_modules/_physics.zc"; test "test_selective_aliasing" { var mv = MathVec::new(3, 4); diff --git a/tests/modules/test_modules/_math.zc b/tests/modules/test_modules/_math.zc new file mode 100644 index 0000000..be65a5c --- /dev/null +++ b/tests/modules/test_modules/_math.zc @@ -0,0 +1,19 @@ + +struct Vector { + x: int; + y: int; +} + +impl Vector { + fn new(x: int, y: int) -> Vector { + return Vector { x: x, y: y }; + } + + fn length(self) -> int { + // sqrt(3^2 + 4^2) = 5 + // Implementing simple integer fallback or mocked logic since we don't have sqrt in std (maybe?) + // For 3,4 it is 5. + if (self.x == 3 && self.y == 4) return 5; + return 0; + } +} diff --git a/tests/modules/test_modules/_physics.zc b/tests/modules/test_modules/_physics.zc new file mode 100644 index 0000000..ee56f7e --- /dev/null +++ b/tests/modules/test_modules/_physics.zc @@ -0,0 +1,17 @@ + +struct Vector { + vx: double; + vy: double; +} + +impl Vector { + fn new(vx: double, vy: double) -> Vector { + return Vector { vx: vx, vy: vy }; + } + + fn magnitude(self) -> double { + // sqrt(5^2 + 12^2) = 13 + if (self.vx == 5.0 && self.vy == 12.0) return 13.0; + return 0.0; + } +} diff --git a/tests/modules/test_modules/_simple.zc b/tests/modules/test_modules/_simple.zc new file mode 100644 index 0000000..ed002e2 --- /dev/null +++ b/tests/modules/test_modules/_simple.zc @@ -0,0 +1,3 @@ +fn foo() -> int { + return 100; +} diff --git a/tests/modules/test_modules/math.zc b/tests/modules/test_modules/math.zc deleted file mode 100644 index be65a5c..0000000 --- a/tests/modules/test_modules/math.zc +++ /dev/null @@ -1,19 +0,0 @@ - -struct Vector { - x: int; - y: int; -} - -impl Vector { - fn new(x: int, y: int) -> Vector { - return Vector { x: x, y: y }; - } - - fn length(self) -> int { - // sqrt(3^2 + 4^2) = 5 - // Implementing simple integer fallback or mocked logic since we don't have sqrt in std (maybe?) - // For 3,4 it is 5. - if (self.x == 3 && self.y == 4) return 5; - return 0; - } -} diff --git a/tests/modules/test_modules/physics.zc b/tests/modules/test_modules/physics.zc deleted file mode 100644 index ee56f7e..0000000 --- a/tests/modules/test_modules/physics.zc +++ /dev/null @@ -1,17 +0,0 @@ - -struct Vector { - vx: double; - vy: double; -} - -impl Vector { - fn new(vx: double, vy: double) -> Vector { - return Vector { vx: vx, vy: vy }; - } - - fn magnitude(self) -> double { - // sqrt(5^2 + 12^2) = 13 - if (self.vx == 5.0 && self.vy == 12.0) return 13.0; - return 0.0; - } -} diff --git a/tests/modules/test_modules/simple.zc b/tests/modules/test_modules/simple.zc deleted file mode 100644 index ed002e2..0000000 --- a/tests/modules/test_modules/simple.zc +++ /dev/null @@ -1,3 +0,0 @@ -fn foo() -> int { - return 100; -} diff --git a/tests/modules/test_namespaced.zc b/tests/modules/test_namespaced.zc index d2aa8cd..afc1d6c 100644 --- a/tests/modules/test_namespaced.zc +++ b/tests/modules/test_namespaced.zc @@ -1,6 +1,6 @@ -import "./test_modules/math.zc" as math; -import "./test_modules/physics.zc" as physics; +import "./test_modules/_math.zc" as math; +import "./test_modules/_physics.zc" as physics; test "test_namespaced_imports" { var mv = math::Vector::new(3, 4); diff --git a/tests/run_tests.sh b/tests/run_tests.sh index ab2601a..b01906a 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -49,7 +49,7 @@ while read -r test_file; do ((FAILED++)) FAILED_TESTS="$FAILED_TESTS\n- $test_file" fi -done < <(find "$TEST_DIR" -name "*.zc" | sort) +done < <(find "$TEST_DIR" -name "*.zc" -not -name "_*.zc" | sort) echo "----------------------------------------" echo "Summary:" -- cgit v1.2.3