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

245 lines, 128 significant
1#### Augmented assignment doesn't work on List
2
3# I suppose the logic is that string and array concat is ++
4#
5# I wonder if a ++= operator makes sense?
6
7shopt -s parse_at simple_word_eval
8var x = :| a 'b c' |
9argv.py @x
10
11setvar x += :| d e | # fatal error
12argv.py @x
13## status: 3
14## STDOUT:
15['a', 'b c']
16## END
17
18#### Augmented assignment respects command_sub_errexit
19
20
21var x = '42'
22setvar x += $(echo 3)
23echo x=$x
24
25setvar x += $(echo 3; false)
26echo x=$x
27
28## status: 1
29## STDOUT:
30x=45
31## END
32
33
34#### Augmented assignment with integers
35var x = 1 + 2 * 3
36echo x=$x
37
38setvar x += 4 * 1
39echo x=$x
40## STDOUT:
41x=7
42x=11
43## END
44
45#### Augmented assignment on string changes to Int Float
46
47var x = '42'
48pp test_ (x)
49
50setvar x += 4 * 1
51pp test_ (x)
52
53setvar x += '9'
54pp test_ (x)
55
56setvar x = '42'
57setvar x /= 4
58pp test_ (x)
59
60## STDOUT:
61(Str) "42"
62(Int) 46
63(Int) 55
64(Float) 10.5
65## END
66
67#### Augmented assignment with floats
68
69var x = 42
70
71setvar x += 1.5
72echo $x
73
74setvar x += '1.5'
75echo $x
76
77setvar x += '3'
78echo $x
79## STDOUT:
8043.5
8145.0
8248.0
83## END
84
85#### Int/Float augmented += -= *= /=
86
87var x = 0
88
89setvar x = 10
90setvar x -= 3
91echo x=$x
92
93setvar x = 10
94setvar x *= 3
95echo x=$x
96
97var x = 0
98setvar x = 10
99setvar x /= 2
100echo x=$x
101
102## STDOUT:
103x=7
104x=30
105x=5.0
106## END
107
108#### Int Augmented //= %= **= and bitwise ops
109
110var x = 0
111
112setvar x = 10
113setvar x //= 3
114echo x=$x
115
116setvar x = 10
117setvar x %= 3
118echo x=$x
119
120setvar x = 10
121setvar x **= 3
122echo x=$x
123
124echo
125echo bitwise
126
127setvar x = 0b1111
128setvar x &= 0b0101
129echo x=$x
130
131setvar x = 0b1000
132setvar x |= 0b0111
133echo x=$x
134
135setvar x = 0b1010
136setvar x ^= 0b1001
137echo x=$x
138
139echo
140echo shift
141
142setvar x = 0b1000
143setvar x <<= 1
144echo x=$x
145
146setvar x = 0b1000
147setvar x >>= 1
148echo x=$x
149
150## STDOUT:
151x=3
152x=1
153x=1000
154
155bitwise
156x=5
157x=15
158x=3
159
160shift
161x=16
162x=4
163## END
164
165#### Augmented assignment of Dict
166
167var d = {x: 42}
168
169setvar d['x'] += 1.5
170echo $[d.x]
171
172setvar d.x += '1.5'
173echo $[d.x]
174
175setvar d.x += '3'
176echo $[d.x]
177
178## STDOUT:
17943.5
18045.0
18148.0
182## END
183
184#### Augmented assignment of List
185
186shopt -s parse_at
187
188var mylist = :| 32 42 |
189
190setvar mylist[0] -= 1
191echo @mylist
192
193setvar mylist[1] //= 2
194echo @mylist
195
196setvar mylist[1] /= 2
197echo @mylist
198
199
200## STDOUT:
20131 42
20231 21
20331 10.5
204## END
205
206#### Dict with nonexistent key (bug fix)
207
208var d = {x: 42}
209
210setvar d.x += 1
211setglobal d.x += 1
212echo $[d.x]
213
214setvar d.missing -= 1
215setglobal d.missing -= 1
216
217
218## status: 3
219## STDOUT:
22044
221## END
222
223
224#### List with nonexistent elemnt
225var L = [42]
226setvar L[99] += 1
227
228## status: 3
229## STDOUT:
230## END
231
232
233#### Augmented assignment doesn't work with multiple LHS
234
235var x = 3
236var y = 4
237setvar x, y += 2
238echo $x $y
239
240## status: 2
241## STDOUT:
242## END
243
244
245