diff options
| author | Mustafa Quraish <[email protected]> | 2022-01-31 03:10:36 -0500 |
|---|---|---|
| committer | Mustafa Quraish <[email protected]> | 2022-01-31 03:21:09 -0500 |
| commit | 23666c777b98402ff539816e291a979d1b62a724 (patch) | |
| tree | 391aef0d42e18824f8f93773be64ee95d9f30d4f /tests/variables.sh | |
| parent | Add `std/math.cup` with some common math functions (diff) | |
| download | cup-23666c777b98402ff539816e291a979d1b62a724.tar.xz cup-23666c777b98402ff539816e291a979d1b62a724.zip | |
Move around tests in the categories; Add missing tests
We now also test for local variables in functions, and add a simple
test to see if imports work properly.
Diffstat (limited to 'tests/variables.sh')
| -rwxr-xr-x | tests/variables.sh | 146 |
1 files changed, 133 insertions, 13 deletions
diff --git a/tests/variables.sh b/tests/variables.sh index 7b44fe7..2d08c76 100755 --- a/tests/variables.sh +++ b/tests/variables.sh @@ -68,36 +68,156 @@ fn main() { EOF echo " OK" -echo -n "- Short-circuiting assignments: " -assert_exit_status_stdin 5 <<EOF +echo -n "- Nested Blocks: " +assert_exit_status_stdin 3 <<EOF fn main() { - let x: int = 5; - let y: int = (1 || (x = 10)); + let x: int = 1; + { + let y: int = 3; + x = y; + } return x; } EOF -assert_exit_status_stdin 10 <<EOF +assert_exit_status_stdin 15 <<EOF +fn main(): int { + let x: int; + { + let y: int = 10; + { + let z: int = 5; + x = y + z; + } + } + return x; +} +EOF + +assert_exit_status_stdin 8 <<EOF +fn main(): int { + let x: int = 0; + { + let y: int = 3; + x = x + y; + } + { + let y: int = 5; + x = x + y; + } + return x; +} +EOF + +assert_compile_failure_stdin <<EOF +fn main(): int { + let x: int; + { + let y: int = 10; + { + let z: int = 5; + x = y + z; + } + y = z; + } + return x; +} +EOF + +assert_compile_failure_stdin <<EOF +fn main(): int { + let x: int; + { + let y: int = 10; + { + let z: int = 5; + x = y + z; + } + } + x = z; + return x; +} +EOF + +assert_compile_failure_stdin <<EOF +fn main(): int { + let x: int; + { + let y: int = 10; + { + let z: int = 5; + x = y + z; + } + } + x = y; + return x; +} +EOF + +# We do not allow shadowing variables on purpose +assert_compile_failure_stdin <<EOF +fn main(): int { + let x: int; + { + let y: int = 10; + { + let z: int = 5; + x = y + z; + let x: int = 10; + } + } + return x; +} +EOF +echo " OK" + +echo -n "- Conditionals w/ blocks: " +assert_exit_status_stdin 3 <<EOF fn main() { - let x: int = 5; - let y: int = (0 || (x = 10)); + let x: int = 1; + if (x == 1) { + let y: int = 3; + x = y; + } + return x; +} +EOF + +assert_exit_status_stdin 1 <<EOF +fn main() { + let x: int = 1; + if (x != 1) { + let y: int = 3; + x = y; + } return x; } EOF assert_exit_status_stdin 5 <<EOF fn main() { - let x: int = 5; - let y: int = (0 && (x = 10)); + let x: int = 1; + if (x != 1) { + let y: int = 3; + x = y; + } else { + let y: int = 5; + x = y; + } return x; } EOF -assert_exit_status_stdin 10 <<EOF +assert_compile_failure_stdin <<EOF fn main() { - let x: int = 5; - let y: int = (1 && (x = 10)); + let x: int = 1; + if (x != 1) { + let y: int = 3; + x = y; + } + x = y; // Invalid return x; } EOF -echo " OK" + +echo " OK"
\ No newline at end of file |