fish-shell/tests/string.in

144 lines
3.1 KiB
Fish
Raw Normal View History

# tests for string builtin
# mostly taken from examples
string match -r -v "c.*" dog can cat diz; and echo "exit 0"
string match -q -r -v "c.*" dog can cat diz; and echo "exit 0"
string match -v "c*" dog can cat diz; and echo "exit 0"
string match -q -v "c*" dog can cat diz; and echo "exit 0"
string match -v "d*" dog dan dat diz; or echo "exit 1"
string match -q -v "d*" dog dan dat diz; or echo "exit 1"
string match -r -v x y; and echo "exit 0"
string match -r -v x x; or echo "exit 1"
string match -q -r -v x y; and echo "exit 0"
string match -q -r -v x x; or echo "exit 1"
string length 'hello, world'
string length -q ""; and echo not zero length
string sub --length 2 abcde
string sub -s 2 -l 2 abcde
string sub --start=-2 abcde
string split . example.com
string split -r -m1 / /usr/local/bin/fish
string split '' abc
seq 3 | string join ...
echo ' abc '
string trim ' abc '
string trim --right --chars=yz xyzzy zany
echo \x07 | string escape
string match '?' a
string match 'a*b' axxb
string match -i 'a??B' Axxb
echo 'ok?' | string match '*\?'
string match -r 'cat|dog|fish' 'nice dog'
string match -r '(\d\d?):(\d\d):(\d\d)' 2:34:56
string match -r '^(\w{2,4})\g1$' papa mud murmur
string match -r -a -n at ratatat
string match -r -i '0x[0-9a-f]{1,8}' 'int magic = 0xBadC0de;'
string replace is was 'blue is my favorite'
string replace 3rd last 1st 2nd 3rd
string replace -a ' ' _ 'spaces to underscores'
string replace -r -a '[^\d.]+' ' ' '0 one two 3.14 four 5x'
string replace -r '(\w+)\s+(\w+)' '$2 $1 $$' 'left right'
string replace -r '\s*newline\s*' '\n' 'put a newline here'
string replace -r -a '(\w)' '$1$1' ab
# test some failure cases
string match -r '[' 'a[sd' 2>/dev/null; or echo "invalid expression error"
string invalidarg 2>/dev/null; or echo "invalid argument error"
string length 2>/dev/null; or echo "missing argument returns 0"
string match -r -v "[dcantg].*" dog can cat diz; or echo "no regexp invert match"
string match -v "???" dog can cat diz; or echo "no glob invert match"
string match -rvn a bbb
# test repeat subcommand
string repeat -n 2 'foo'
string repeat --count 2 'foo'
echo foo | string repeat -n 2
string repeat -n2 -q 'foo'; and echo "exit 0"
string repeat -n2 --quiet 'foo'; and echo "exit 0"
string repeat -n0 'foo'; or echo "exit 1"
string repeat -n0; or echo "exit 1"
string repeat -m0; or echo "exit 1"
string repeat -n1 -N 'there is '; echo "no newline"
string repeat -n1 --no-newline 'there is '; echo "no newline"
string repeat -n10 -m4 'foo'
string repeat -n10 --max 5 'foo'
string repeat -n3 -m20 'foo'
string repeat -m4 'foo'
string repeat -n-1 'foo'; or echo "exit 2"
string repeat -m-1 'foo'; or echo "exit 2"
string repeat -n notanumber 'foo'; or echo "exit 2"
string repeat -m notanumber 'foo'; or echo "exit 2"
echo 'stdin' | string repeat -n1 'and arg'; or echo "exit 2"
string repeat -n; or echo "exit 2"
string repeat -l fakearg 2>&1 | head -n1 1>&2
string repeat ''
and echo string repeat empty string did not fail
string repeat -n3 ''
and echo string repeat empty string did not fail
exit 0