OILS / spec / ysh-with-sh.test.sh View on Github | oilshell.org

221 lines, 87 significant
1## our_shell: osh
2# compare_shells: bash
3
4#### Can't use x+= on YSH Int (issue #840)
5
6sh_str=2
7echo sh_str=$sh_str
8
9sh_str+=1
10echo sh_str=$sh_str
11
12sh_str+=1
13echo sh_str=$sh_str
14
15echo
16
17var ysh_int = 2
18echo ysh_int=$ysh_int
19
20# What should happen here?
21
22ysh_int+=1
23echo ysh_int=$ysh_int
24
25ysh_int+=1
26echo ysh_int=$ysh_int
27
28## status: 1
29## STDOUT:
30sh_str=2
31sh_str=21
32sh_str=211
33
34ysh_int=2
35## END
36
37#### Can't x+= on other YSH types
38
39$SH -c '
40var x = /d+/
41x+=1
42'
43echo eggex $?
44
45$SH -c '
46var d = {}
47d+=1
48'
49echo Dict $?
50
51# This is unspecified for now, could try to match bash
52$SH -c '
53declare -A A=()
54A+=1
55declare -A
56'
57#echo AssocArray $?
58
59## STDOUT:
60eggex 1
61Dict 1
62## END
63
64#### Shell ${x:-default} with YSH List (issue #954)
65
66var mylist = [1, 2, 3]
67
68echo mylist ${mylist:-default}
69
70var myint = 42
71
72echo myint ${myint:-default}
73
74## status: 3
75## STDOUT:
76## END
77
78
79#### Shell ${a[0]} with YSH List (issue #1092)
80
81var a = [1, 2, 3]
82echo first ${a[0]}
83
84## status: 3
85## STDOUT:
86## END
87
88
89#### Can't splice nested List
90
91shopt --set parse_at
92
93var mylist = ["ls", {name: 42}]
94
95echo @mylist
96
97## status: 3
98## STDOUT:
99## END
100
101#### Can't splice nested Dict
102
103declare -A A=([k]=v [k2]=v2)
104echo ${A[@]}
105
106var d ={name: [1, 2, 3]}
107echo ${d[@]}
108
109## status: 3
110## STDOUT:
111v v2
112## END
113
114#### ${#x} on List and Dict
115
116var L = [1,2,3]
117
118echo List ${#L[@]}
119echo List ${#L}
120# Not supported. TODO: could be a problem
121#echo List ${#L[0]}
122
123declare -a a=(abc d)
124
125echo array ${#a[@]}
126echo array ${#a}
127echo array ${#a[0]}
128
129var d = {k: 'v', '0': 'abc'}
130
131echo Dict ${#d[@]}
132echo Dict ${#d}
133# Not supported. TODO: could be a problem
134#echo Dict ${#d[0]}
135
136declare -A d=([k]=v [0]=abc)
137
138echo Assoc ${#d[@]}
139echo Assoc ${#d}
140echo Assoc ${#d[0]}
141
142## status: 3
143## STDOUT:
144## END
145
146#### Can't use $x on List and Dict
147
148declare -a a=(abc d)
149echo array $a
150echo array ${a[0]}
151
152var L = [1,2,3]
153echo List $L
154
155declare -A A=([k]=v [0]=abc)
156echo Assoc $A
157echo Assoc ${A[0]}
158
159var d = {k: 'v', '0': 'abc'}
160#echo Dict $d
161
162## status: 3
163## STDOUT:
164array abc
165array abc
166## END
167
168#### Iterate over array with holes (bug fix)
169
170declare -a array=(a b)
171array[5]=c
172argv.py "${array[@]}"
173
174# TODO: Should print this like this bash, with value.BashArray
175declare -a
176
177for i, item in (array) {
178 echo "$i $item"
179}
180
181## status: 3
182## STDOUT:
183['a', 'b', 'c']
184array=(); array[0]=a array[1]=b array[5]=c
185## END
186
187#### Slice bash array isn't allowed
188
189shopt --set parse_at
190
191var ysh = :| ysh 'c d' e f |
192var yslice = ysh[1:3]
193argv.py @yslice
194
195declare -a bash=(bash 'c d' e f)
196
197# You can copy it and then slice it
198var ysh2 = :| copy "${bash[@]}" |
199var yslice = ysh2[0:2]
200argv.py @yslice
201
202# Note this
203var sh_slice = bash[1:3]
204argv.py @sh_slice
205
206## status: 3
207## STDOUT:
208['c d', 'e']
209['copy', 'bash']
210## END
211
212#### Concat ++ not defined on shell array
213
214declare -a a=(x y)
215declare -a b=(x y)
216
217= a ++ b
218
219## status: 3
220## STDOUT:
221## END