1 | ## oils_failures_allowed: 1
|
2 |
|
3 | #### Type objects Bool, Int, Float, etc.
|
4 |
|
5 | pp test_ (Bool)
|
6 | pp test_ (Int)
|
7 | pp test_ (Float)
|
8 | pp test_ (Str)
|
9 |
|
10 | pp test_ (List)
|
11 | pp test_ (Dict)
|
12 | pp test_ (Obj)
|
13 | echo
|
14 |
|
15 | var b = Bool
|
16 |
|
17 | pp test_ (b is Bool)
|
18 |
|
19 | # Objects don't have equality, only identity
|
20 | #pp test_ (b === Bool)
|
21 |
|
22 | pp test_ (vm.id(b) === vm.id(Bool))
|
23 |
|
24 | ## STDOUT:
|
25 | (Obj) ("name":"Bool") --> ("__index__":<BuiltinFunc>)
|
26 | (Obj) ("name":"Int") --> ("__index__":<BuiltinFunc>)
|
27 | (Obj) ("name":"Float") --> ("__index__":<BuiltinFunc>)
|
28 | (Obj) ("name":"Str") --> ("__index__":<BuiltinFunc>)
|
29 | (Obj) ("name":"List") --> ("__index__":<BuiltinFunc>)
|
30 | (Obj) ("name":"Dict") --> ("__index__":<BuiltinFunc>)
|
31 | (Obj) ("name":"Obj","new":<BuiltinFunc>) --> ("__index__":<BuiltinFunc>)
|
32 |
|
33 | (Bool) true
|
34 | (Bool) true
|
35 | ## END
|
36 |
|
37 | #### Parameterized types - List[Int], Dict[Str, Int]
|
38 | shopt -s ysh:upgrade
|
39 |
|
40 | var li = List[Int]
|
41 | var dsi = Dict[Str, Int]
|
42 |
|
43 | pp test_ (li)
|
44 | pp test_ (dsi)
|
45 |
|
46 | # test identity
|
47 | for i in a b c {
|
48 | assert [li is List[Int]]
|
49 | assert [dsi is Dict[Str,Int]]
|
50 | }
|
51 |
|
52 | assert [li is not dsi]
|
53 |
|
54 | var lli = List[li]
|
55 | pp test_ (lli)
|
56 |
|
57 | pp test_ (Dict[Str, List[Int]])
|
58 |
|
59 | ## STDOUT:
|
60 | (Obj) ("unique_id":"List[Int]")
|
61 | (Obj) ("unique_id":"Dict[Str,Int]")
|
62 | (Obj) ("unique_id":"List[List[Int]]")
|
63 | (Obj) ("unique_id":"Dict[Str,List[Int]]")
|
64 | ## END
|
65 |
|
66 | #### Errors for parameterized types
|
67 | shopt -s ysh:upgrade
|
68 |
|
69 | # more in test/ysh-runtime-errors.sh test-obj-methods
|
70 | try {
|
71 | pp test_ (Bool[Str])
|
72 | }
|
73 | echo $[_error.code]
|
74 |
|
75 | # I think this means
|
76 | # TODO: need very low precedence operation
|
77 | #
|
78 | # Func[Int, Str : Int]
|
79 | # Func[Int, Str -> Int]
|
80 | # Func[Int, Str --> Int]
|
81 |
|
82 | ## STDOUT:
|
83 | 3
|
84 | ## END
|
85 |
|
86 | #### runproc
|
87 | shopt --set parse_proc parse_at
|
88 |
|
89 | f() {
|
90 | write -- f "$@"
|
91 | }
|
92 | proc p {
|
93 | write -- p @ARGV
|
94 | }
|
95 | runproc f 1 2
|
96 | echo status=$?
|
97 |
|
98 | runproc p 3 4
|
99 | echo status=$?
|
100 |
|
101 | runproc invalid 5 6
|
102 | echo status=$?
|
103 |
|
104 | runproc
|
105 | echo status=$?
|
106 |
|
107 | ## STDOUT:
|
108 | f
|
109 | 1
|
110 | 2
|
111 | status=0
|
112 | p
|
113 | 3
|
114 | 4
|
115 | status=0
|
116 | status=1
|
117 | status=2
|
118 | ## END
|
119 |
|
120 |
|
121 | #### runproc typed args
|
122 | shopt --set parse_brace parse_proc
|
123 |
|
124 | proc p {
|
125 | echo 'hi from p'
|
126 | }
|
127 |
|
128 | # The block is ignored for now
|
129 | runproc p {
|
130 | echo myblock
|
131 | }
|
132 | echo
|
133 |
|
134 | proc ty (w; t; n; block) {
|
135 | echo 'ty'
|
136 | pp test_ (w)
|
137 | pp test_ (t)
|
138 | pp test_ (n)
|
139 | echo $[type(block)]
|
140 | }
|
141 |
|
142 | ty a (42; n=99; ^(echo ty))
|
143 | echo
|
144 |
|
145 | runproc ty a (42; n=99; ^(echo ty))
|
146 | echo
|
147 |
|
148 | runproc ty a (42; n=99) {
|
149 | echo 'ty gets literal'
|
150 | }
|
151 |
|
152 | # TODO: Command vs. Block vs. Literal Block should be unified
|
153 |
|
154 | ## STDOUT:
|
155 | hi from p
|
156 |
|
157 | ty
|
158 | (Str) "a"
|
159 | (Int) 42
|
160 | (Int) 99
|
161 | Command
|
162 |
|
163 | ty
|
164 | (Str) "a"
|
165 | (Int) 42
|
166 | (Int) 99
|
167 | Command
|
168 |
|
169 | ty
|
170 | (Str) "a"
|
171 | (Int) 42
|
172 | (Int) 99
|
173 | Command
|
174 | ## END
|
175 |
|
176 |
|
177 | #### pp asdl_
|
178 |
|
179 | x=42
|
180 | setvar y = {foo: x}
|
181 |
|
182 | pp asdl_ (x)
|
183 | pp asdl_ (y)
|
184 |
|
185 | pp asdl_ (io.stdin)
|
186 |
|
187 | ## STDOUT:
|
188 | (value.Str s:42)
|
189 | (value.Dict d:{foo (value.Str s:42)})
|
190 | (value.Stdin)
|
191 | ## END
|
192 |
|
193 | #### pp asdl_ can handle an object cycle
|
194 |
|
195 | shopt -s ysh:upgrade
|
196 |
|
197 | var d = {}
|
198 | setvar d.cycle = d
|
199 |
|
200 | pp test_ (d) | fgrep -o '{"cycle":'
|
201 |
|
202 | pp asdl_ (d) | fgrep -o 'cycle ...'
|
203 |
|
204 | ## STDOUT:
|
205 | {"cycle":
|
206 | cycle ...
|
207 | ## END
|
208 |
|
209 |
|
210 | #### pp gc-stats_
|
211 |
|
212 | pp gc-stats_
|
213 |
|
214 | ## STDOUT:
|
215 | ## END
|
216 |
|
217 |
|
218 | #### pp cell_
|
219 | x=42
|
220 |
|
221 | pp cell_ x
|
222 | echo status=$?
|
223 |
|
224 | pp -- cell_ x
|
225 | echo status=$?
|
226 |
|
227 | pp cell_ nonexistent
|
228 | echo status=$?
|
229 | ## STDOUT:
|
230 | x = (Cell exported:F readonly:F nameref:F val:(value.Str s:42))
|
231 | status=0
|
232 | x = (Cell exported:F readonly:F nameref:F val:(value.Str s:42))
|
233 | status=0
|
234 | status=1
|
235 | ## END
|
236 |
|
237 | #### pp cell_ on indexed array with hole
|
238 | declare -a array
|
239 | array[3]=42
|
240 | array[5]=99
|
241 | pp cell_ array
|
242 |
|
243 | ## STDOUT:
|
244 | array = (Cell
|
245 | exported:F
|
246 | readonly:F
|
247 | nameref:F
|
248 | val:(value.BashArray strs:[_ _ _ 42 _ 99])
|
249 | )
|
250 | ## END
|
251 |
|
252 |
|
253 | #### pp proc
|
254 | shopt --set ysh:upgrade
|
255 |
|
256 | # This has to be a separate file because sh_spec.py strips comments!
|
257 | . $REPO_ROOT/spec/testdata/doc-comments.sh
|
258 |
|
259 | pp proc
|
260 | echo ---
|
261 |
|
262 | # print one
|
263 | pp proc f
|
264 |
|
265 | ## STDOUT:
|
266 | proc_name doc_comment
|
267 | f "doc ' comment with \" quotes"
|
268 | g ""
|
269 | myproc "YSH-style proc"
|
270 | "true" "Special quoting rule"
|
271 | ---
|
272 | proc_name doc_comment
|
273 | f "doc ' comment with \" quotes"
|
274 | ## END
|
275 |
|
276 | #### pp (x) and pp [x] quote code
|
277 |
|
278 | pp (42)
|
279 |
|
280 | shopt --set ysh:upgrade
|
281 |
|
282 | pp [42] | sed 's/0x[a-f0-9]\+/[replaced]/'
|
283 |
|
284 | ## STDOUT:
|
285 |
|
286 | pp (42)
|
287 | ^
|
288 | [ stdin ]:1: (Int) 42
|
289 |
|
290 | pp [42] | sed 's/0x[a-f0-9]\+/[replaced]/'
|
291 | ^
|
292 | [ stdin ]:5: <Expr [replaced]>
|
293 | ## END
|
294 |
|
295 | #### pp test_ supports BashArray, BashAssoc
|
296 |
|
297 | declare -a array=(a b c)
|
298 | pp test_ (array)
|
299 |
|
300 | array[5]=z
|
301 | pp test_ (array)
|
302 |
|
303 | declare -A assoc=([k]=v [k2]=v2)
|
304 | pp test_ (assoc)
|
305 |
|
306 | # I think assoc arrays can never null / unset
|
307 |
|
308 | assoc['k3']=
|
309 | pp test_ (assoc)
|
310 |
|
311 | ## STDOUT:
|
312 | {"type":"BashArray","data":{"0":"a","1":"b","2":"c"}}
|
313 | {"type":"BashArray","data":{"0":"a","1":"b","2":"c","5":"z"}}
|
314 | {"type":"BashAssoc","data":{"k":"v","k2":"v2"}}
|
315 | {"type":"BashAssoc","data":{"k":"v","k2":"v2","k3":""}}
|
316 | ## END
|
317 |
|
318 | #### pp value (x) is like = keyword
|
319 |
|
320 | shopt --set ysh:upgrade
|
321 | source $LIB_YSH/list.ysh
|
322 |
|
323 | # It can be piped!
|
324 |
|
325 | pp value ('foo') | cat
|
326 |
|
327 | pp value ("isn't this sq") | cat
|
328 |
|
329 | pp value ('"dq $myvar"') | cat
|
330 |
|
331 | pp value (r'\ backslash \\') | cat
|
332 |
|
333 | pp value (u'one \t two \n') | cat
|
334 |
|
335 | # Without a terminal, default width is 80
|
336 | pp value (repeat([123], 40)) | cat
|
337 |
|
338 | ## STDOUT:
|
339 | (Str) 'foo'
|
340 | (Str) b'isn\'t this sq'
|
341 | (Str) '"dq $myvar"'
|
342 | (Str) b'\\ backslash \\\\'
|
343 | (Str) b'one \t two \n'
|
344 | (List)
|
345 | [
|
346 | 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,
|
347 | 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,
|
348 | 123, 123, 123, 123, 123, 123, 123, 123, 123, 123
|
349 | ]
|
350 | ## END
|
351 |
|