aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-01 04:08:59 -0500
committerMustafa Quraish <[email protected]>2022-02-01 04:08:59 -0500
commitd471a41369690a9d2d9b8862ea5ff0ae9cbe40fc (patch)
tree9399d12700c3d975a95b0946833dc419b3268419 /tests
parentTesting: Add support for testing stdout results, check builtins (diff)
downloadcup-d471a41369690a9d2d9b8862ea5ff0ae9cbe40fc.tar.xz
cup-d471a41369690a9d2d9b8862ea5ff0ae9cbe40fc.zip
Add basic `defer` implementation.
We don't have any closures yet, so it's essentially the same as just moving the statement after the `defer` keyword to the end of the block/ right before returning from the function.
Diffstat (limited to 'tests')
-rw-r--r--tests/common.sh2
-rwxr-xr-xtests/core.sh (renamed from tests/basics.sh)63
2 files changed, 64 insertions, 1 deletions
diff --git a/tests/common.sh b/tests/common.sh
index 1047708..965975c 100644
--- a/tests/common.sh
+++ b/tests/common.sh
@@ -57,7 +57,7 @@ function assert_stdout_text() {
echo "----------------------------------------------"
echo "Test failed: executable returned non-0 code"
echo "----------------------------------------------"
- echo "$code"
+ echo "$1"
exit 1
fi
if [[ "$output" != $2 ]]
diff --git a/tests/basics.sh b/tests/core.sh
index fc43847..71c539d 100755
--- a/tests/basics.sh
+++ b/tests/core.sh
@@ -123,4 +123,67 @@ fn main() {
return x + y - 1;
}
EOF
+echo " OK"
+
+echo -n "- Defer: "
+assert_stdout_text \
+"fn main() {
+ defer print(5);
+ print(4);
+}" \
+"4
+5"
+
+assert_stdout_text \
+"fn main() {
+ defer print(1);
+ {
+ defer print(2);
+ {
+ defer print(3);
+ print(4);
+ }
+ print(5);
+ return 0;
+ }
+ print(10);
+}" \
+"4
+3
+5
+2
+1"
+
+assert_stdout_text \
+"fn test() {
+ defer {
+ defer print(1);
+ print(2);
+ }
+ print(3);
+}
+fn main() {
+ defer print(4);
+ defer test();
+ print(10);
+}" \
+"10
+3
+2
+1
+4"
+
+assert_stdout_text \
+"let g: int;
+fn test(): int {
+ g = 5;
+ defer g = 10;
+ return g;
+}
+fn main() {
+ print(test());
+ print(g);
+}" \
+"5
+10"
echo " OK" \ No newline at end of file