summaryrefslogtreecommitdiff
path: root/Shell
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-07-12 01:44:58 +0430
committerAndreas Kling <kling@serenityos.org>2020-07-16 16:01:10 +0200
commit2f731150a29bcfc4dd6db5c1c8d0effa57f7f144 (patch)
treeb9b87c0763741beba19bf0aff2ce1e629eb13dfa /Shell
parentb6066faa1fc2f7dc008bf3d80daee07b877688c2 (diff)
downloadserenity-2f731150a29bcfc4dd6db5c1c8d0effa57f7f144.zip
Shell: Add a test for loops
Diffstat (limited to 'Shell')
-rw-r--r--Shell/Tests/loop.sh50
1 files changed, 50 insertions, 0 deletions
diff --git a/Shell/Tests/loop.sh b/Shell/Tests/loop.sh
new file mode 100644
index 0000000000..dc7a25b130
--- /dev/null
+++ b/Shell/Tests/loop.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+singlecommand_ok=yes
+multicommand_ok=yes
+inlineexec_ok=yes
+implicit_ok=yes
+
+# Full form
+ # Empty
+for x in () { }
+
+ # Empty block but nonempty list
+for x in (1 2 3) { }
+
+ # Single command in block
+for cmd in ((test 1 = 1) (test 2 = 2)) {
+ $cmd || unset singlecommand_ok
+}
+
+ # Multiple commands in block
+for cmd in ((test 1 = 1) (test 2 = 2)) {
+ test -z "$cmd"
+ test -z "$cmd" && unset multicommand_ok
+
+}
+
+ # $(...) as iterable expression
+test_file=sh-test-1
+echo 1 > $test_file
+echo 2 >> $test_file
+echo 3 >> $test_file
+echo 4 >> $test_file
+lst=()
+for line in $(cat $test_file) {
+ lst=($lst $line)
+}
+test "$lst" = "1 2 3 4" || unset inlineexec_ok
+rm $test_file
+
+# Implicit var
+for ((test 1 = 1) (test 2 = 2)) {
+ $it || unset implicit_ok
+}
+
+test $singlecommand_ok || echo Fail: Single command inside for body
+test $multicommand_ok || echo Fail: Multiple commands inside for body
+test $inlineexec_ok || echo Fail: Inline Exec
+test $implicit_ok || echo Fail: implicit iter variable
+
+test "$singlecommand_ok $multicommand_ok $inlineexec_ok $implicit_ok" = "yes yes yes yes" || exit 1