aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/read.in
diff options
context:
space:
mode:
authorGravatar Kevin Ballard <kevin@sb.org>2014-07-13 22:36:26 -0700
committerGravatar Kevin Ballard <kevin@sb.org>2014-07-14 00:46:38 -0700
commit973dd6ffbdc189f22b634de0d684e92a9c160c9d (patch)
tree242487f472926817ac2b805fad0f68b169aee76e /tests/read.in
parent72e8489d50d749c86d5b57609bb0c4d83a03b41a (diff)
read: Support arrays, character splitting
Enhance the `read` builtin to support creating an array with the --array flag. With --array, only a single variable name is allowed and the entire input is tokenized and placed into that variable as an array. Also add custom behavior if IFS is empty or unset. In that event, split the input on every character, instead of the previous behavior of doing no splitting at all.
Diffstat (limited to 'tests/read.in')
-rw-r--r--tests/read.in72
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/read.in b/tests/read.in
new file mode 100644
index 00000000..53f9873b
--- /dev/null
+++ b/tests/read.in
@@ -0,0 +1,72 @@
+#
+# Test read builtin and IFS
+#
+
+count (echo one\ntwo)
+set -l IFS \t
+count (echo one\ntwo)
+set -l IFS
+count (echo one\ntwo)
+set -le IFS
+
+function print_vars --no-scope-shadowing
+ set -l space
+ set -l IFS \n # ensure our command substitution works right
+ for var in $argv
+ echo -n $space (count $$var) \'$$var\'
+ set space ''
+ end
+ echo
+end
+
+echo
+echo 'hello there' | read -l one two
+print_vars one two
+echo 'hello there' | read -l one
+print_vars one
+echo '' | read -l one
+print_vars one
+echo '' | read -l one two
+print_vars one two
+echo 'test' | read -l one two three
+print_vars one two three
+
+echo
+set -l IFS
+echo 'hello' | read -l one
+print_vars one
+echo 'hello' | read -l one two
+print_vars one two
+echo 'hello' | read -l one two three
+print_vars one two three
+echo '' | read -l one
+print_vars one
+echo 't' | read -l one two
+print_vars one two
+echo 't' | read -l one two three
+print_vars one two three
+echo ' t' | read -l one two
+print_vars one two
+set -le IFS
+
+echo
+echo 'hello there' | read -la ary
+print_vars ary
+echo 'hello' | read -la ary
+print_vars ary
+echo 'this is a bunch of words' | read -la ary
+print_vars ary
+echo ' one two three' | read -la ary
+print_vars ary
+echo '' | read -la ary
+print_vars ary
+
+echo
+set -l IFS
+echo 'hello' | read -la ary
+print_vars ary
+echo 'h' | read -la ary
+print_vars ary
+echo '' | read -la ary
+print_vars ary
+set -le IFS