OILS / spec / array-compat.test.sh View on Github | oilshell.org

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