commit 0c2cc9d2cffd783a4db159cb84027e4016f618ff (tree)
parent 5af4ef88acb68b66d38846b398e1e34845cccfbf
Author: Josh Wolfe <thejoshwolfe@gmail.com>
Date: Thu, 3 Dec 2015 11:06:05 -0700
tests for local variables
Diffstat:
2 files changed, 46 insertions(+), 2 deletions(-)
diff --git a/example/expressions/expressions.zig b/example/expressions/expressions.zig
@@ -9,6 +9,8 @@ export fn _start() -> unreachable {
let b = 2;
// let c : i32; // not yet support for const variables
// let d; // parse error
- puts("Hello, world!");
- exit(a + b);
+ if (a + b == 3) {
+ puts("OK");
+ }
+ exit(0);
}
diff --git a/test/run_tests.cpp b/test/run_tests.cpp
@@ -256,6 +256,23 @@ static void add_compiling_test_cases(void) {
exit(0);
}
)SOURCE", "loop\nloop\nloop\n");
+
+ add_simple_case("local variables", R"SOURCE(
+#link("c")
+extern {
+ fn puts(s: *const u8) -> i32;
+ fn exit(code: i32) -> unreachable;
+}
+
+export fn _start() -> unreachable {
+ let a : i32 = 1;
+ let b = 2;
+ if (a + b == 3) {
+ puts("OK");
+ }
+ exit(0);
+}
+ )SOURCE", "OK\n");
}
static void add_compile_failure_test_cases(void) {
@@ -340,6 +357,31 @@ done:
fn b() {}
)SOURCE", 1, ".tmp_source.zig:4:5: error: unreachable code");
+ add_compile_fail_case("parameter redeclaration", R"SOURCE(
+fn f(a : i32, a : i32) {
+}
+ )SOURCE", 1, ".tmp_source.zig:2:1: error: redeclaration of parameter 'a'.");
+
+ add_compile_fail_case("local variable redeclaration", R"SOURCE(
+fn f() {
+ let a : i32 = 0;
+ let a = 0;
+}
+ )SOURCE", 1, ".tmp_source.zig:4:5: error: redeclaration of variable 'a'.");
+
+ add_compile_fail_case("local variable redeclares parameter", R"SOURCE(
+fn f(a : i32) {
+ let a = 0;
+}
+ )SOURCE", 1, ".tmp_source.zig:3:5: error: redeclaration of variable 'a'.");
+
+ add_compile_fail_case("variable has wrong type", R"SOURCE(
+fn f() -> i32 {
+ let a = "a";
+ a
+}
+ )SOURCE", 1, ".tmp_source.zig:2:15: error: type mismatch. expected i32. got *const u8");
+
}
static void print_compiler_invokation(TestCase *test_case, Buf *zig_stderr) {