OILS / spec / array-compat.test.sh View on Github | oils.pub

188 lines, 95 significant
1## compare_shells: bash mksh
2## oils_failures_allowed: 2
3
4# Arrays decay upon assignment (without splicing) and equality.
5
6#### Assignment Causes Array Decay
7set -- x y z
8argv.py "[$@]"
9var="[$@]"
10argv.py "$var"
11## STDOUT:
12['[x', 'y', 'z]']
13['[x y z]']
14## END
15
16#### Array Decay with IFS
17IFS=x
18set -- x y z
19var="[$@]"
20argv.py "$var"
21## stdout: ['[x y z]']
22
23#### User arrays decay
24declare -a a b
25a=(x y z)
26b="${a[@]}" # this collapses to a string
27c=("${a[@]}") # this preserves the array
28c[1]=YYY # mutate a copy -- doesn't affect the original
29argv.py "${a[@]}"
30argv.py "${b}"
31argv.py "${c[@]}"
32## STDOUT:
33['x', 'y', 'z']
34['x y z']
35['x', 'YYY', 'z']
36## END
37
38#### strict_array: $array is not valid in OSH, is ${array[0]} in ksh/bash
39shopt -s strict_array
40
41a=(1 '2 3')
42echo $a
43## STDOUT:
441
45## END
46## OK osh status: 1
47## OK osh stdout-json: ""
48
49#### strict_array: ${array} is not valid in OSH, is ${array[0]} in ksh/bash
50shopt -s strict_array
51
52a=(1 '2 3')
53echo ${a}
54## STDOUT:
551
56## END
57## OK osh status: 1
58## OK osh stdout-json: ""
59
60#### Assign to array index without initialization
61a[5]=5
62a[6]=6
63echo "${a[@]}" ${#a[@]}
64## stdout: 5 6 2
65
66#### a[40] grows array
67a=(1 2 3)
68a[1]=5
69a[40]=30 # out of order
70a[10]=20
71echo "${a[@]}" "${#a[@]}" # length is 1
72## stdout: 1 5 3 20 30 5
73
74#### array decays to string when comparing with [[ a = b ]]
75a=('1 2' '3 4')
76s='1 2 3 4' # length 2, length 4
77echo ${#a[@]} ${#s}
78[[ "${a[@]}" = "$s" ]] && echo EQUAL
79## STDOUT:
802 7
81EQUAL
82## END
83
84#### ++ on a whole array increments the first element (disallowed with strict_array)
85shopt -s strict_array
86
87a=(1 10)
88(( a++ )) # doesn't make sense
89echo "${a[@]}"
90## stdout: 2 10
91## OK osh status: 1
92## OK osh stdout-json: ""
93
94#### Apply vectorized operations on ${a[*]}
95a=('-x-' 'y-y' '-z-')
96
97# This does the prefix stripping FIRST, and then it joins.
98argv.py "${a[*]#-}"
99## STDOUT:
100['x- y-y z-']
101## END
102## N-I mksh status: 1
103## N-I mksh stdout-json: ""
104
105#### value.BashArray internal representation - Indexed
106
107case $SH in mksh) exit ;; esac
108
109z=()
110declare -a | grep z=
111
112z+=(b c)
113declare -a | grep z=
114
115# z[5]= finds the index, or puts it in SORTED order I think
116z[5]=d
117declare -a | grep z=
118
119z[1]=ZZZ
120declare -a | grep z=
121
122# Adds after last index
123z+=(f g)
124declare -a | grep z=
125
126# This is the equivalent of z[0]+=mystr
127z+=-mystr
128declare -a | grep z=
129
130z[1]+=-append
131declare -a | grep z=
132
133argv.py keys "${!z[@]}" # 0 1 5 6 7
134argv.py values "${z[@]}"
135
136# can't do this conversion
137declare -A z
138declare -A | grep z=
139
140echo status=$?
141
142## STDOUT:
143declare -a z=()
144declare -a z=([0]="b" [1]="c")
145declare -a z=([0]="b" [1]="c" [5]="d")
146declare -a z=([0]="b" [1]="ZZZ" [5]="d")
147declare -a z=([0]="b" [1]="ZZZ" [5]="d" [6]="f" [7]="g")
148declare -a z=([0]="b-mystr" [1]="ZZZ" [5]="d" [6]="f" [7]="g")
149declare -a z=([0]="b-mystr" [1]="ZZZ-append" [5]="d" [6]="f" [7]="g")
150['keys', '0', '1', '5', '6', '7']
151['values', 'b-mystr', 'ZZZ-append', 'd', 'f', 'g']
152status=1
153## END
154
155## N-I mksh STDOUT:
156## END
157
158#### value.BashArray internal representation - Assoc (ordering is a problem)
159
160case $SH in mksh) exit ;; esac
161
162declare -A A=([k]=v)
163declare -A | grep A=
164
165argv.py keys "${!A[@]}"
166argv.py values "${A[@]}"
167
168exit
169
170# Huh this actually works, we don't support it
171# Hm the order here is all messed up, in bash 5.2
172A+=([k2]=v2 [0]=foo [9]=9 [9999]=9999)
173declare -A | grep A=
174
175A+=-append
176declare -A | grep A=
177
178argv.py keys "${!A[@]}"
179argv.py values "${A[@]}"
180
181## STDOUT:
182declare -A A=([k]="v" )
183['keys', 'k']
184['values', 'v']
185## END
186
187## N-I mksh STDOUT:
188## END