Bash · 3325 bytes Raw Blame History
1 #!/bin/sh
2 TEST_PREFIX="[var-ops]"
3 . "$(cd "$(dirname "$0")/.." && pwd)/test_harness.sh"
4
5 section "1. default value operators"
6 compare_output '${var:-default} with unset var' 'unset V; echo ${V:-fallback}'
7 compare_output '${var:-default} with set var' 'V=real; echo ${V:-fallback}'
8 compare_output '${var:-default} with empty var' 'V=""; echo ${V:-fallback}'
9 compare_output '${var-default} with unset (no colon)' 'unset V; echo ${V-fallback}'
10 compare_output '${var-default} with empty (no colon)' 'V=""; echo "${V-fallback}"'
11 compare_output '${var:=default} assigns default' 'unset V; echo ${V:=assigned}; echo $V'
12 compare_output '${var:+alternate} with set var' 'V=yes; echo ${V:+alt}'
13 compare_output '${var:+alternate} with unset var' 'unset V; echo ${V:+alt}'
14 compare_output '${var:+alternate} with empty var' 'V=""; echo ${V:+alt}'
15 compare_exit '${var:?error} with unset var fails' 'unset V; : ${V:?oops} 2>/dev/null'
16 compare_output '${var:?error} with set var' 'V=ok; echo ${V:?oops}'
17
18 section "2. string length"
19 compare_output '${#var} string length' 'V=hello; echo ${#V}'
20 compare_output '${#var} empty string' 'V=""; echo ${#V}'
21 compare_output '${#var} with spaces' 'V="hello world"; echo ${#V}'
22 compare_output '${#var} unset is zero' 'unset V; echo ${#V}'
23
24 section "3. prefix removal"
25 compare_output '${var#pattern} shortest prefix' 'V="hello.world.txt"; echo ${V#*.}'
26 compare_output '${var##pattern} longest prefix' 'V="hello.world.txt"; echo ${V##*.}'
27 compare_output '${var#prefix} literal prefix' 'V="/usr/local/bin"; echo ${V#/usr}'
28 compare_output '${var##*/} basename equivalent' 'V="/path/to/file.txt"; echo ${V##*/}'
29
30 section "4. suffix removal"
31 compare_output '${var%pattern} shortest suffix' 'V="hello.world.txt"; echo ${V%.*}'
32 compare_output '${var%%pattern} longest suffix' 'V="hello.world.txt"; echo ${V%%.*}'
33 compare_output '${var%suffix} literal suffix' 'V="file.tar.gz"; echo ${V%.gz}'
34 compare_output '${var%%/*} dirname-like' 'V="path/to/file"; echo ${V%%/*}'
35
36 section "5. replacement"
37 compare_output '${var/pattern/repl} first match' 'V="hello world hello"; echo ${V/hello/hi}'
38 compare_output '${var//pattern/repl} all matches' 'V="aabaa"; echo ${V//a/X}'
39 compare_output '${var/pattern/} deletion' 'V="hello world"; echo ${V/world/}'
40 compare_output '${var/#pattern/repl} anchor start' 'V="hello world"; echo ${V/#hello/hi}'
41 compare_output '${var/%pattern/repl} anchor end' 'V="hello world"; echo ${V/%world/earth}'
42 compare_output '${var//pattern/} delete all' 'V="a1b2c3"; echo ${V//[0-9]/}'
43
44 section "6. case conversion"
45 compare_output '${var^} capitalize first' 'V="hello"; echo ${V^}'
46 compare_output '${var^^} uppercase all' 'V="hello"; echo ${V^^}'
47 compare_output '${var,} lowercase first' 'V="HELLO"; echo ${V,}'
48 compare_output '${var,,} lowercase all' 'V="HELLO"; echo ${V,,}'
49 compare_output '${var^^} mixed case' 'V="hElLo"; echo ${V^^}'
50 compare_output '${var,,} mixed case' 'V="HeLLo"; echo ${V,,}'
51
52 section "7. substring"
53 compare_output '${var:offset:length}' 'V="hello world"; echo ${V:6:5}'
54 compare_output '${var:offset} to end' 'V="hello world"; echo ${V:6}'
55 compare_output '${var:0:5} from start' 'V="hello world"; echo ${V:0:5}'
56 compare_output '${var: -3} negative offset' 'V="hello"; echo ${V: -3}'
57 compare_output '${var:0:0} empty substring' 'V="hello"; echo "${V:0:0}"'
58
59 print_summary