2017-04-25 04:05:51 +00:00
|
|
|
# Tests for string builtin. Mostly taken from man page examples.
|
2016-04-08 02:18:58 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
# We don't want syntax errors to emit command usage help. This makes the
|
|
|
|
# stderr output considerably shorter and makes it easier to updates the tests
|
|
|
|
# and documentation without having to make pointless changes to the test
|
|
|
|
# output files.
|
|
|
|
function __fish_print_help
|
|
|
|
end
|
|
|
|
|
|
|
|
echo '# string match -r -v "c.*" dog can cat diz'
|
2016-04-08 02:18:58 +00:00
|
|
|
string match -r -v "c.*" dog can cat diz; and echo "exit 0"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -q -r -v "c.*" dog can cat diz'
|
2016-04-08 02:18:58 +00:00
|
|
|
string match -q -r -v "c.*" dog can cat diz; and echo "exit 0"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -v "c*" dog can cat diz'
|
2016-04-08 02:18:58 +00:00
|
|
|
string match -v "c*" dog can cat diz; and echo "exit 0"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -q -v "c*" dog can cat diz'
|
2016-04-08 02:18:58 +00:00
|
|
|
string match -q -v "c*" dog can cat diz; and echo "exit 0"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -v "d*" dog dan dat diz'
|
2016-04-08 02:18:58 +00:00
|
|
|
string match -v "d*" dog dan dat diz; or echo "exit 1"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -q -v "d*" dog dan dat diz'
|
2016-04-08 02:18:58 +00:00
|
|
|
string match -q -v "d*" dog dan dat diz; or echo "exit 1"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -r -v x y'
|
2016-04-08 02:18:58 +00:00
|
|
|
string match -r -v x y; and echo "exit 0"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -r -v x x'
|
2016-04-08 02:18:58 +00:00
|
|
|
string match -r -v x x; or echo "exit 1"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -q -r -v x y'
|
2016-04-08 02:18:58 +00:00
|
|
|
string match -q -r -v x y; and echo "exit 0"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -q -r -v x x'
|
2016-04-08 02:18:58 +00:00
|
|
|
string match -q -r -v x x; or echo "exit 1"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string length "hello, world"'
|
|
|
|
string length "hello, world"
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string length -q ""'
|
|
|
|
string length -q ""; and echo not zero length; or echo zero length
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string sub --length 2 abcde'
|
2015-10-09 03:27:18 +00:00
|
|
|
string sub --length 2 abcde
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string sub -s 2 -l 2 abcde'
|
2015-10-09 03:27:18 +00:00
|
|
|
string sub -s 2 -l 2 abcde
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string sub --start=-2 abcde'
|
2015-10-09 03:27:18 +00:00
|
|
|
string sub --start=-2 abcde
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string split . example.com'
|
2015-10-09 03:27:18 +00:00
|
|
|
string split . example.com
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string split -r -m1 / /usr/local/bin/fish'
|
2015-10-09 03:27:18 +00:00
|
|
|
string split -r -m1 / /usr/local/bin/fish
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string split "" abc'
|
|
|
|
string split "" abc
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# seq 3 | string join ...'
|
2015-10-09 03:27:18 +00:00
|
|
|
seq 3 | string join ...
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string trim " abc "'
|
|
|
|
string trim " abc "
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string trim --right --chars=yz xyzzy zany'
|
2015-10-09 03:27:18 +00:00
|
|
|
string trim --right --chars=yz xyzzy zany
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# echo \x07 | string escape'
|
2015-10-09 03:27:18 +00:00
|
|
|
echo \x07 | string escape
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match "?" a'
|
|
|
|
string match "?" a
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match "a*b" axxb'
|
|
|
|
string match "a*b" axxb
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -i "a??B" Axxb'
|
|
|
|
string match -i "a??B" Axxb
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# echo "ok?" | string match "*\?"'
|
|
|
|
echo "ok?" | string match "*\?"
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -r "cat|dog|fish" "nice dog"'
|
|
|
|
string match -r "cat|dog|fish" "nice dog"
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -r "(\d\d?):(\d\d):(\d\d)" 2:34:56'
|
|
|
|
string match -r "(\d\d?):(\d\d):(\d\d)" 2:34:56
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -r "^(\w{2,4})\g1\$" papa mud murmur'
|
|
|
|
string match -r "^(\w{2,4})\g1\$" papa mud murmur
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -r -a -n at ratatat'
|
2015-10-09 03:27:18 +00:00
|
|
|
string match -r -a -n at ratatat
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -r -i "0x[0-9a-f]{1,8}" "int magic = 0xBadC0de;"'
|
|
|
|
string match -r -i "0x[0-9a-f]{1,8}" "int magic = 0xBadC0de;"
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string replace is was "blue is my favorite"'
|
|
|
|
string replace is was "blue is my favorite"
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string replace 3rd last 1st 2nd 3rd'
|
2015-10-09 03:27:18 +00:00
|
|
|
string replace 3rd last 1st 2nd 3rd
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string replace -a " " _ "spaces to underscores"'
|
|
|
|
string replace -a " " _ "spaces to underscores"
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string replace -r -a "[^\d.]+" " " "0 one two 3.14 four 5x"'
|
|
|
|
string replace -r -a "[^\d.]+" " " "0 one two 3.14 four 5x"
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string replace -r "(\w+)\s+(\w+)" "\$2 \$1 \$\$" "left right"'
|
|
|
|
string replace -r "(\w+)\s+(\w+)" "\$2 \$1 \$\$" "left right"
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string replace -r "\s*newline\s*" "\n" "put a newline here"'
|
|
|
|
string replace -r "\s*newline\s*" "\n" "put a newline here"
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string replace -r -a "(\w)" "\$1\$1" ab'
|
|
|
|
string replace -r -a "(\w)" "\$1\$1" ab
|
2016-02-21 21:59:03 +00:00
|
|
|
|
2017-04-28 04:53:39 +00:00
|
|
|
echo
|
|
|
|
echo '# string replace --filter x X abc axc x def jkx'
|
|
|
|
string replace --filter x X abc axc x def jkx
|
|
|
|
or echo Unexpected exit status at line (status --current-line-number)
|
|
|
|
|
|
|
|
string replace --filter y Y abc axc x def jkx
|
|
|
|
and echo Unexpected exit status at line (status --current-line-number)
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo '# string replace --regex -f "\d" X 1bc axc 2 d3f jk4 xyz'
|
|
|
|
string replace --regex -f "\d" X 1bc axc 2 d3f jk4 xyz
|
|
|
|
or echo Unexpected exit status at line (status --current-line-number)
|
|
|
|
|
|
|
|
string replace --regex -f "Z" X 1bc axc 2 d3f jk4 xyz
|
|
|
|
and echo Unexpected exit status at line (status --current-line-number)
|
|
|
|
|
2015-10-09 03:27:18 +00:00
|
|
|
# test some failure cases
|
2017-04-25 04:05:51 +00:00
|
|
|
echo '# string match -r "[" "a[sd"' >&2
|
|
|
|
string match -r "[" "a[sd"; and echo "unexpected exit 0" >&2
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo >&2
|
|
|
|
echo '# string invalidarg' >&2
|
|
|
|
string invalidarg; and echo "unexpected exit 0" >&2
|
2015-10-09 03:27:18 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string length'
|
|
|
|
string length; or echo "missing argument returns 1"
|
2016-04-08 02:18:58 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -r -v "[dcantg].*" dog can cat diz'
|
2016-04-08 02:18:58 +00:00
|
|
|
string match -r -v "[dcantg].*" dog can cat diz; or echo "no regexp invert match"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -v "???" dog can cat diz'
|
2016-04-08 02:18:58 +00:00
|
|
|
string match -v "???" dog can cat diz; or echo "no glob invert match"
|
2016-05-31 21:14:03 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string match -rvn a bbb'
|
|
|
|
string match -rvn a bbb; or echo "exit 1"
|
2017-03-07 14:39:21 +00:00
|
|
|
|
|
|
|
# test repeat subcommand
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string repeat -n 2 "foo"'
|
|
|
|
string repeat -n 2 "foo"
|
2017-03-07 14:39:21 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string repeat --count 2 "foo"'
|
|
|
|
string repeat --count 2 "foo"
|
2017-03-07 14:39:21 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# echo foo | string repeat -n 2'
|
2017-03-07 14:39:21 +00:00
|
|
|
echo foo | string repeat -n 2
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string repeat -n2 -q "foo"'
|
|
|
|
string repeat -n2 -q "foo"; and echo "exit 0"
|
2017-03-07 14:39:21 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string repeat -n2 --quiet "foo"'
|
|
|
|
string repeat -n2 --quiet "foo"; and echo "exit 0"
|
2017-03-07 14:39:21 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string repeat -n0 "foo"'
|
|
|
|
string repeat -n0 "foo"; or echo "exit 1"
|
2017-03-07 14:39:21 +00:00
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string repeat -n0'
|
2017-03-07 14:39:21 +00:00
|
|
|
string repeat -n0; or echo "exit 1"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string repeat -m0'
|
2017-03-07 14:39:21 +00:00
|
|
|
string repeat -m0; or echo "exit 1"
|
|
|
|
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
|
|
|
echo '# string repeat -n1 -N "there is "'
|
|
|
|
string repeat -n1 -N "there is "; echo "no newline"
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo '# string repeat -n1 --no-newline "there is "'
|
|
|
|
string repeat -n1 --no-newline "there is "; echo "no newline"
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo '# string repeat -n10 -m4 "foo"'
|
|
|
|
string repeat -n10 -m4 "foo"
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo '# string repeat -n10 --max 5 "foo"'
|
|
|
|
string repeat -n10 --max 5 "foo"
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo '# string repeat -n3 -m20 "foo"'
|
|
|
|
string repeat -n3 -m20 "foo"
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo '# string repeat -m4 "foo"'
|
|
|
|
string repeat -m4 "foo"
|
|
|
|
|
|
|
|
echo >&2
|
|
|
|
echo '# string repeat -n-1 "foo"' >&2
|
|
|
|
string repeat -n-1 "foo"; and echo "exit 0" >&2
|
|
|
|
|
|
|
|
echo >&2
|
|
|
|
echo '# string repeat -m-1 "foo"' >&2
|
|
|
|
string repeat -m-1 "foo"; and echo "exit 0" >&2
|
|
|
|
|
|
|
|
echo >&2
|
|
|
|
echo '# string repeat -n notanumber "foo"' >&2
|
|
|
|
string repeat -n notanumber "foo"; and echo "exit 0" >&2
|
|
|
|
|
|
|
|
echo >&2
|
|
|
|
echo '# string repeat -m notanumber "foo"' >&2
|
|
|
|
string repeat -m notanumber "foo"; and echo "exit 0" >&2
|
|
|
|
|
|
|
|
echo >&2
|
|
|
|
echo '# echo "stdin" | string repeat -n1 "and arg"' >&2
|
|
|
|
echo "stdin" | string repeat -n1 "and arg"; and echo "exit 0" >&2
|
|
|
|
|
|
|
|
echo >&2
|
|
|
|
echo '# string repeat -n' >&2
|
|
|
|
string repeat -n; and echo "exit 0" >&2
|
|
|
|
|
|
|
|
echo >&2
|
|
|
|
echo '# string repeat -l fakearg 2>&1' >&2
|
|
|
|
string repeat -l fakearg
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo '# string repeat ""'
|
|
|
|
string repeat ""
|
|
|
|
or echo string repeat empty string failed
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo '# string repeat -n3 ""'
|
|
|
|
string repeat -n3 ""
|
|
|
|
or echo string repeat empty string failed
|
|
|
|
|
2017-05-02 05:19:58 +00:00
|
|
|
# Test equivalent matches with/without the --entire, --regex, and --invert flags.
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
2017-05-02 05:19:58 +00:00
|
|
|
echo '# string match -e x abc dxf xyz jkx x z'
|
|
|
|
string match -e x abc dxf xyz jkx x z
|
2017-04-25 04:05:51 +00:00
|
|
|
or echo exit 1
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo '# string match x abc dxf xyz jkx x z'
|
|
|
|
string match x abc dxf xyz jkx x z
|
|
|
|
|
|
|
|
echo
|
2017-05-02 05:19:58 +00:00
|
|
|
echo '# string match --entire -r "a*b[xy]+" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz'
|
|
|
|
string match --entire -r "a*b[xy]+" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz
|
2017-04-25 04:05:51 +00:00
|
|
|
or echo exit 1
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo '# string match -r "a*b[xy]+" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz'
|
|
|
|
string match -r "a*b[xy]+" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz
|
|
|
|
or echo exit 1
|
|
|
|
|
2017-05-02 05:19:58 +00:00
|
|
|
# Make sure that groups are handled correct with/without --entire.
|
2017-04-25 04:05:51 +00:00
|
|
|
echo
|
2017-05-02 05:19:58 +00:00
|
|
|
echo '# string match --entire -r "a*b([xy]+)" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz'
|
|
|
|
string match --entire -r "a*b([xy]+)" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz
|
2017-04-25 04:05:51 +00:00
|
|
|
or echo exit 1
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo '# string match -r "a*b([xy]+)" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz'
|
|
|
|
string match -r "a*b([xy]+)" abc abxc bye aaabyz kaabxz abbxy abcx caabxyxz
|
|
|
|
or echo exit 1
|
2017-03-15 20:55:53 +00:00
|
|
|
|
|
|
|
exit 0
|