| 1 | #!/bin/bash |
| 2 | # |
| 3 | # word-eval.test.sh: Test the word evaluation pipeline in order. |
| 4 | # |
| 5 | # Part evaluation, splitting, joining, elision, globbing. |
| 6 | |
| 7 | # TODO: Rename word-eval-smoke.test.sh? |
| 8 | # Word sequence evaluation. |
| 9 | # This is more like a vertical slice. For exhaustive tests, see: |
| 10 | # |
| 11 | # word-split.test.sh (perhaps rename word-reframe?) |
| 12 | # glob.test.sh |
| 13 | |
| 14 | ### Evaluation of constant parts |
| 15 | argv.py bare 'sq' |
| 16 | # stdout: ['bare', 'sq'] |
| 17 | |
| 18 | ### Evaluation of each part |
| 19 | #set -o noglob |
| 20 | HOME=/home/bob |
| 21 | str=s |
| 22 | array=(a1 a2) |
| 23 | argv.py bare 'sq' ~ $str "-${str}-" "${array[@]}" $((1+2)) $(echo c) `echo c` |
| 24 | # stdout: ['bare', 'sq', '/home/bob', 's', '-s-', 'a1', 'a2', '3', 'c', 'c'] |
| 25 | # N-I dash stdout-json: "" |
| 26 | # N-I dash status: 2 |
| 27 | |
| 28 | ### Word splitting |
| 29 | s1='1 2' |
| 30 | s2='3 4' |
| 31 | s3='5 6' |
| 32 | argv.py $s1$s2 "$s3" |
| 33 | # stdout: ['1', '23', '4', '5 6'] |
| 34 | |
| 35 | ### Word joining |
| 36 | set -- x y z |
| 37 | s1='1 2' |
| 38 | array=(a1 a2) |
| 39 | argv.py $s1"${array[@]}"_"$@" |
| 40 | # stdout: ['1', '2a1', 'a2_x', 'y', 'z'] |
| 41 | # N-I dash stdout-json: "" |
| 42 | # N-I dash status: 2 |
| 43 | |
| 44 | ### Word elision |
| 45 | s1='' |
| 46 | argv.py $s1 - "$s1" |
| 47 | # stdout: ['-', ''] |
| 48 | |
| 49 | ### Word elision with space |
| 50 | s1=' ' |
| 51 | argv.py $s1 |
| 52 | # stdout: [] |
| 53 | |
| 54 | ### Word elision with non-whitespace IFS |
| 55 | # Treated differently than the default IFS. What is the rule here? |
| 56 | IFS=_ |
| 57 | s1='_' |
| 58 | argv.py $s1 |
| 59 | # stdout: [''] |
| 60 | |
| 61 | ### Default values -- more cases |
| 62 | argv ${undef:-hi} ${undef:-'a b'} "${undef:-c d}" "${un:-"e f"}" "${un:-'g h'}" |
| 63 | # stdout: ['hi', 'a b', 'c d', 'e f', "'g h'"] |
| 64 | |
| 65 | ### Globbing after splitting |
| 66 | touch _tmp/foo.gg _tmp/bar.gg _tmp/foo.hh |
| 67 | pat='_tmp/*.hh _tmp/*.gg' |
| 68 | argv $pat |
| 69 | # stdout: ['_tmp/foo.hh', '_tmp/bar.gg', '_tmp/foo.gg'] |
| 70 | |
| 71 | ### Globbing escaping |
| 72 | touch '_tmp/[bc]ar.mm' # file that looks like a glob pattern |
| 73 | touch _tmp/bar.mm _tmp/car.mm |
| 74 | argv '_tmp/[bc]'*.mm - _tmp/?ar.mm |
| 75 | # stdout: ['_tmp/[bc]ar.mm', '-', '_tmp/bar.mm', '_tmp/car.mm'] |
| 76 | |
| 77 | ### Assignment Causes Array Decay |
| 78 | set -- x y z |
| 79 | #argv "[$@]" # NOT DECAYED here. |
| 80 | var="[$@]" |
| 81 | argv "$var" |
| 82 | # stdout: ['[x y z]'] |