| 1 | ## compare_shells: bash
|
| 2 |
|
| 3 | #### [k1]=v1 (BashArray)
|
| 4 | # Note: This and next tests have originally been in "spec/assign.test.sh" and
|
| 5 | # compared the behavior of OSH's BashAssoc and Bash's indexed array. After
|
| 6 | # supporting "arr=([index]=value)" for indexed arrays, the test was adjusted
|
| 7 | # and copied here. See also the corresponding tests in "spec/assign.test.sh"
|
| 8 | a=([k1]=v1 [k2]=v2)
|
| 9 | echo ${a["k1"]}
|
| 10 | echo ${a["k2"]}
|
| 11 | ## STDOUT:
|
| 12 | v2
|
| 13 | v2
|
| 14 | ## END
|
| 15 |
|
| 16 | #### [k1]=v1 (BashAssoc)
|
| 17 | declare -A a
|
| 18 | a=([k1]=v1 [k2]=v2)
|
| 19 | echo ${a["k1"]}
|
| 20 | echo ${a["k2"]}
|
| 21 | ## STDOUT:
|
| 22 | v1
|
| 23 | v2
|
| 24 | ## END
|
| 25 |
|
| 26 | #### [k1]=v1 looking like brace expansions (BashArray)
|
| 27 | declare -A a
|
| 28 | a=([k2]=-{a,b}-)
|
| 29 | echo ${a["k2"]}
|
| 30 | ## STDOUT:
|
| 31 | -{a,b}-
|
| 32 | ## END
|
| 33 |
|
| 34 | #### [k1]=v1 looking like brace expansions (BashAssoc)
|
| 35 | a=([k2]=-{a,b}-)
|
| 36 | echo ${a["k2"]}
|
| 37 | ## STDOUT:
|
| 38 | -{a,b}-
|
| 39 | ## END
|
| 40 | ## BUG bash STDOUT:
|
| 41 | [k2]=-a-
|
| 42 | ## END
|
| 43 |
|
| 44 | #### BashArray cannot be changed to BashAssoc and vice versa
|
| 45 | declare -a a=(1 2 3 4)
|
| 46 | eval 'declare -A a=([a]=x [b]=y [c]=z)'
|
| 47 | echo status=$?
|
| 48 | argv.py "${a[@]}"
|
| 49 |
|
| 50 | declare -A A=([a]=x [b]=y [c]=z)
|
| 51 | eval 'declare -a A=(1 2 3 4)'
|
| 52 | echo status=$?
|
| 53 | argv.py $(printf '%s\n' "${A[@]}" | sort)
|
| 54 | ## STDOUT:
|
| 55 | status=1
|
| 56 | ['1', '2', '3', '4']
|
| 57 | status=1
|
| 58 | ['x', 'y', 'z']
|
| 59 | ## END
|
| 60 |
|
| 61 | #### s+=() with strict_array
|
| 62 | case $SH in bash) ;; *) shopt --set strict_array;; esac
|
| 63 |
|
| 64 | s1=hello
|
| 65 | s2=world
|
| 66 |
|
| 67 | # Overwriting Str with a new BashArray is allowed
|
| 68 | eval 's1=(1 2 3 4)'
|
| 69 | echo status=$?
|
| 70 | declare -p s1
|
| 71 | # Promoting Str to a BashArray is disallowed
|
| 72 | eval 's2+=(1 2 3 4)'
|
| 73 | echo status=$?
|
| 74 | declare -p s2
|
| 75 | ## STDOUT:
|
| 76 | status=0
|
| 77 | declare -a s1=(1 2 3 4)
|
| 78 | status=1
|
| 79 | declare -- s2=world
|
| 80 | ## END
|
| 81 | ## N-I bash STDOUT:
|
| 82 | status=0
|
| 83 | declare -a s1=([0]="1" [1]="2" [2]="3" [3]="4")
|
| 84 | status=0
|
| 85 | declare -a s2=([0]="world" [1]="1" [2]="2" [3]="3" [4]="4")
|
| 86 | ## END
|
| 87 |
|
| 88 | #### declare -A s+=() with strict_array
|
| 89 | case $SH in bash) ;; *) shopt --set strict_array;; esac
|
| 90 |
|
| 91 | s1=hello
|
| 92 | s2=world
|
| 93 |
|
| 94 | # Overwriting Str with a new BashAssoc is allowed
|
| 95 | eval 'declare -A s1=([a]=x [b]=y)'
|
| 96 | echo status=$?
|
| 97 | declare -p s1
|
| 98 | # Promoting Str to a BashAssoc is disallowed
|
| 99 | eval 'declare -A s2+=([a]=x [b]=y)'
|
| 100 | echo status=$?
|
| 101 | declare -p s2
|
| 102 | ## STDOUT:
|
| 103 | status=0
|
| 104 | declare -A s1=(['a']=x ['b']=y)
|
| 105 | status=1
|
| 106 | declare -- s2=world
|
| 107 | ## END
|
| 108 | ## N-I bash STDOUT:
|
| 109 | status=0
|
| 110 | declare -A s1=([b]="y" [a]="x" )
|
| 111 | status=0
|
| 112 | declare -A s2=([0]="world" [b]="y" [a]="x" )
|
| 113 | ## END
|
| 114 |
|
| 115 | #### assoc=(key value ...) is not allowed in the strict_array mode
|
| 116 | case $SH in bash) ;; *) shopt --set strict_array;; esac
|
| 117 |
|
| 118 | declare -A a=([a]=b)
|
| 119 | eval "a=(1 2 3 4)"
|
| 120 | declare -p a
|
| 121 | ## STDOUT:
|
| 122 | declare -A a=()
|
| 123 | ## END
|
| 124 | ## N-I bash STDOUT:
|
| 125 | declare -A a=([3]="4" [1]="2" )
|
| 126 | ## END
|