1 |
# spec/ysh-methods
|
2 |
|
3 |
## our_shell: ysh
|
4 |
## oils_failures_allowed: 2
|
5 |
|
6 |
|
7 |
|
8 |
# later we may make it mandatory
|
9 |
|
10 |
if ("abc" => startsWith("a")) {
|
11 |
echo yes
|
12 |
}
|
13 |
|
14 |
var mylist = [1, 2, 3]
|
15 |
|
16 |
# This one should be ->
|
17 |
call mylist->pop()
|
18 |
echo 'ok'
|
19 |
|
20 |
## STDOUT:
|
21 |
yes
|
22 |
ok
|
23 |
## END
|
24 |
|
25 |
|
26 |
|
27 |
func dictfunc() {
|
28 |
return ({k1: 'spam', k2: 'eggs'})
|
29 |
}
|
30 |
|
31 |
echo $[list(dictfunc()) => join('/') => upper()]
|
32 |
|
33 |
# This is nicer and more consistent
|
34 |
echo $[dictfunc() => list() => join('/') => upper()]
|
35 |
|
36 |
## STDOUT:
|
37 |
K1/K2
|
38 |
K1/K2
|
39 |
## END
|
40 |
|
41 |
|
42 |
func test(s, p) { echo $[s => startsWith(p)] $[s => endsWith(p)] }
|
43 |
|
44 |
call test('', '')
|
45 |
call test('abc', '')
|
46 |
call test('abc', 'a')
|
47 |
call test('abc', 'b')
|
48 |
call test('abc', 'c')
|
49 |
call test('abc', 'z')
|
50 |
call test('', 'abc')
|
51 |
## status: 0
|
52 |
## STDOUT:
|
53 |
true true
|
54 |
true true
|
55 |
true false
|
56 |
false false
|
57 |
false true
|
58 |
false false
|
59 |
false false
|
60 |
## END
|
61 |
|
62 |
|
63 |
func test(s, p) { echo $[s => startsWith(p)] $[s => endsWith(p)] }
|
64 |
|
65 |
call test(b'\yce\ya3', u'\u{03a3}')
|
66 |
call test(b'\yce\ya3', b'\yce')
|
67 |
call test(b'\yce\ya3', b'\ya3')
|
68 |
call test(b'\yce', b'\yce')
|
69 |
## status: 0
|
70 |
## STDOUT:
|
71 |
true true
|
72 |
true false
|
73 |
false true
|
74 |
true true
|
75 |
## END
|
76 |
|
77 |
|
78 |
func test(s, p) { echo $[s => startsWith(p)] $[s => endsWith(p)] }
|
79 |
|
80 |
call test('abc', / d+ /)
|
81 |
call test('abc', / [ a b c ] /)
|
82 |
call test('abc', / 'abc' /)
|
83 |
call test('cba', / d+ /)
|
84 |
call test('cba', / [ a b c ] /)
|
85 |
call test('cba', / 'abc' /)
|
86 |
## status: 0
|
87 |
## STDOUT:
|
88 |
false false
|
89 |
true true
|
90 |
true true
|
91 |
false false
|
92 |
true true
|
93 |
false false
|
94 |
## END
|
95 |
|
96 |
|
97 |
func test(s, p) { echo $[s => startsWith(p)] $[s => endsWith(p)] }
|
98 |
|
99 |
call test('ab', / %start 'a' /)
|
100 |
call test('ab', / 'a' %end /)
|
101 |
call test('ab', / %start 'a' %end /)
|
102 |
call test('ab', / %start 'b' /)
|
103 |
call test('ab', / 'b' %end /)
|
104 |
call test('ab', / %start 'b' %end /)
|
105 |
## status: 0
|
106 |
## STDOUT:
|
107 |
true false
|
108 |
false false
|
109 |
false false
|
110 |
false false
|
111 |
false true
|
112 |
false false
|
113 |
## END
|
114 |
|
115 |
|
116 |
func test(s, p) { echo $[s => startsWith(p)] $[s => endsWith(p)] }
|
117 |
|
118 |
call test(u'\u{03a3}', / dot /)
|
119 |
call test(u'\u{03a3}', / ![z] /)
|
120 |
call test(b'\yce', / dot /) # Fails: eggex does not match bytes
|
121 |
call test(b'\yce', / ![z] /) # Fails: eggex does not match bytes
|
122 |
## status: 0
|
123 |
## STDOUT:
|
124 |
true true
|
125 |
true true
|
126 |
true true
|
127 |
true true
|
128 |
## END
|
129 |
|
130 |
|
131 |
= 'abc' => startsWith()
|
132 |
## status: 3
|
133 |
|
134 |
|
135 |
= 'abc' => startsWith('extra', 'arg')
|
136 |
## status: 3
|
137 |
|
138 |
|
139 |
= 'abc' => endsWith()
|
140 |
## status: 3
|
141 |
|
142 |
|
143 |
= 'abc' => endsWith('extra', 'arg')
|
144 |
## status: 3
|
145 |
|
146 |
|
147 |
func test(s) { write --sep ', ' --j8 $[s => trimStart()] $[s => trimEnd()] $[s => trim()] }
|
148 |
|
149 |
call test("")
|
150 |
call test(" ")
|
151 |
call test("mystr")
|
152 |
call test(" mystr")
|
153 |
call test("mystr ")
|
154 |
call test(" mystr ")
|
155 |
call test(" my str ")
|
156 |
## status: 0
|
157 |
## STDOUT:
|
158 |
"", "", ""
|
159 |
"", "", ""
|
160 |
"mystr", "mystr", "mystr"
|
161 |
"mystr", " mystr", "mystr"
|
162 |
"mystr ", "mystr", "mystr"
|
163 |
"mystr ", " mystr", "mystr"
|
164 |
"my str ", " my str", "my str"
|
165 |
## END
|
166 |
|
167 |
|
168 |
func test(s, p) { write --sep ', ' --j8 $[s => trimStart(p)] $[s => trimEnd(p)] $[s => trim(p)] }
|
169 |
|
170 |
call test('' , 'xyz')
|
171 |
call test(' ' , 'xyz')
|
172 |
call test('xy' , 'xyz')
|
173 |
call test('yz' , 'xyz')
|
174 |
call test('xyz' , 'xyz')
|
175 |
call test('xyzxyz' , 'xyz')
|
176 |
call test('xyzxyzxyz', 'xyz')
|
177 |
## status: 0
|
178 |
## STDOUT:
|
179 |
"", "", ""
|
180 |
" ", " ", " "
|
181 |
"xy", "xy", "xy"
|
182 |
"yz", "yz", "yz"
|
183 |
"", "", ""
|
184 |
"xyz", "xyz", ""
|
185 |
"xyzxyz", "xyzxyz", "xyz"
|
186 |
## END
|
187 |
|
188 |
|
189 |
func test(s, p) { write --sep ', ' --j8 $[s => trimStart(p)] $[s => trimEnd(p)] $[s => trim(p)] }
|
190 |
|
191 |
call test(b'\yce\ya3', u'\u{03a3}')
|
192 |
call test(b'\yce\ya3', b'\yce')
|
193 |
call test(b'\yce\ya3', b'\ya3')
|
194 |
## status: 0
|
195 |
## STDOUT:
|
196 |
"", "", ""
|
197 |
b'\ya3', "Σ", b'\ya3'
|
198 |
"Σ", b'\yce', b'\yce'
|
199 |
## END
|
200 |
|
201 |
|
202 |
func test(s, p) { write --sep ', ' --j8 $[s => trimStart(p)] $[s => trimEnd(p)] $[s => trim(p)] }
|
203 |
|
204 |
call test('' , / 'xyz' /)
|
205 |
call test(' ' , / 'xyz' /)
|
206 |
call test('xy' , / 'xyz' /)
|
207 |
call test('yz' , / 'xyz' /)
|
208 |
call test('xyz' , / 'xyz' /)
|
209 |
call test('xyzxyz' , / 'xyz' /)
|
210 |
call test('xyzxyzxyz', / 'xyz' /)
|
211 |
call test('xyzabcxyz', / 'xyz' /)
|
212 |
call test('xyzabcxyz', / %start 'xyz' /)
|
213 |
call test('xyzabcxyz', / 'xyz' %end /)
|
214 |
call test('123abc123', / d+ /)
|
215 |
## status: 0
|
216 |
## STDOUT:
|
217 |
"", "", ""
|
218 |
" ", " ", " "
|
219 |
"xy", "xy", "xy"
|
220 |
"yz", "yz", "yz"
|
221 |
"", "", ""
|
222 |
"xyz", "xyz", ""
|
223 |
"xyzxyz", "xyzxyz", "xyz"
|
224 |
"abcxyz", "xyzabc", "abc"
|
225 |
"abcxyz", "xyzabcxyz", "abcxyz"
|
226 |
"xyzabcxyz", "xyzabc", "xyzabc"
|
227 |
"abc123", "123abc", "abc"
|
228 |
## END
|
229 |
|
230 |
|
231 |
func test(s, p) { write --sep ', ' --j8 $[s => trimStart(p)] $[s => trimEnd(p)] $[s => trim(p)] }
|
232 |
|
233 |
call test(u'\u{03a3}', / dot /) # Fails: eggex does not match bytes, so entire rune is trimmed.
|
234 |
call test(u'\u{03a3}', / ![z] /) # Fails: eggex does not match bytes, so entire rune is trimmed.
|
235 |
call test(b'\yce', / dot /) # Fails: eggex does not match bytes, so nothing is trimmed.
|
236 |
call test(b'\yce', / ![z] /) # Fails: eggex does not match bytes, so nothing is trimmed.
|
237 |
## status: 0
|
238 |
## STDOUT:
|
239 |
b'\ya3', b'\yce', ""
|
240 |
b'\ya3', b'\yce', ""
|
241 |
"", "", ""
|
242 |
"", "", ""
|
243 |
## END
|
244 |
|
245 |
|
246 |
= 'mystr' => trim('extra', 'args')
|
247 |
## status: 3
|
248 |
|
249 |
|
250 |
= 'mystr' => trimStart('extra', 'args')
|
251 |
## status: 3
|
252 |
|
253 |
|
254 |
= 'mystr' => trimEnd('extra', 'args')
|
255 |
## status: 3
|
256 |
|
257 |
|
258 |
|
259 |
# Supported set of whitespace characters. The full set of Unicode whitespace
|
260 |
# characters is not supported. See comments in the implementation.
|
261 |
var spaces = [
|
262 |
b'\u{0009}', # Horizontal tab (\t)
|
263 |
b'\u{000A}', # Newline (\n)
|
264 |
b'\u{000B}', # Vertical tab (\v)
|
265 |
b'\u{000C}', # Form feed (\f)
|
266 |
b'\u{000D}', # Carriage return (\r)
|
267 |
b'\u{0020}', # Normal space
|
268 |
b'\u{00A0}', # No-break space <NBSP>
|
269 |
b'\u{FEFF}', # Zero-width no-break space <ZWNBSP>
|
270 |
] => join('')
|
271 |
|
272 |
echo $["$spaces YSH $spaces" => trim()]
|
273 |
## status: 0
|
274 |
## STDOUT:
|
275 |
YSH
|
276 |
## END
|
277 |
|
278 |
|
279 |
var badUtf = b'\yF9'
|
280 |
|
281 |
echo trim
|
282 |
|
283 |
# We only decode UTF until the first non-space char. So the bad UTF-8 is
|
284 |
# missed.
|
285 |
try { call " a$[badUtf]b " => trim() }
|
286 |
echo status=$_status
|
287 |
|
288 |
# These require trim to decode the badUtf, so an error is raised
|
289 |
try { call "$[badUtf]b " => trim() }
|
290 |
echo status=$_status
|
291 |
try { call " a$[badUtf]" => trim() }
|
292 |
echo status=$_status
|
293 |
|
294 |
# Similarly, trim{Left,Right} will assume correct encoding until shown
|
295 |
# otherwise.
|
296 |
echo trimStart
|
297 |
try { call " a$[badUtf]" => trimStart() }
|
298 |
echo status=$_status
|
299 |
try { call "$[badUtf]b " => trimStart() }
|
300 |
echo status=$_status
|
301 |
|
302 |
echo trimEnd
|
303 |
try { call "$[badUtf]b " => trimEnd() }
|
304 |
echo status=$_status
|
305 |
try { call " a$[badUtf]" => trimEnd() }
|
306 |
echo status=$_status
|
307 |
|
308 |
## STDOUT:
|
309 |
trim
|
310 |
status=0
|
311 |
status=3
|
312 |
status=3
|
313 |
trimStart
|
314 |
status=0
|
315 |
status=3
|
316 |
trimEnd
|
317 |
status=0
|
318 |
status=3
|
319 |
## END
|
320 |
|
321 |
|
322 |
var badStrs = [
|
323 |
b'\yF4\yA2\yA4\yB0', # Too large of a codepoint
|
324 |
b'\yED\yBF\y80', # Surrogate
|
325 |
b'\yC1\y81', # Overlong
|
326 |
b'\y80', b'\yFF', # Does not match UTF8 bit pattern
|
327 |
]
|
328 |
|
329 |
for badStr in (badStrs) {
|
330 |
try { call badStr => trimStart() }
|
331 |
echo status=$_status
|
332 |
}
|
333 |
|
334 |
## STDOUT:
|
335 |
status=3
|
336 |
status=3
|
337 |
status=3
|
338 |
status=3
|
339 |
status=3
|
340 |
## END
|
341 |
|
342 |
|
343 |
# Tests the backwards UTF-8 decoder
|
344 |
var badStrs = [
|
345 |
b'\yF4\yA2\yA4\yB0', # Too large of a codepoint
|
346 |
b'\yED\yBF\y80', # Surrogate
|
347 |
b'\yC1\y81', # Overlong
|
348 |
b'\y80', b'\yFF', # Does not match UTF8 bit pattern
|
349 |
]
|
350 |
|
351 |
for badStr in (badStrs) {
|
352 |
try { call badStr => trimEnd() }
|
353 |
echo status=$_status
|
354 |
}
|
355 |
|
356 |
## STDOUT:
|
357 |
status=3
|
358 |
status=3
|
359 |
status=3
|
360 |
status=3
|
361 |
status=3
|
362 |
## END
|
363 |
|
364 |
|
365 |
json write (b' \y00 ' => trim())
|
366 |
json write (b' \y00 ' => trimStart())
|
367 |
json write (b' \y00 ' => trimEnd())
|
368 |
## STDOUT:
|
369 |
"\u0000"
|
370 |
"\u0000 "
|
371 |
" \u0000"
|
372 |
## END
|
373 |
|
374 |
|
375 |
var en2fr = {}
|
376 |
setvar en2fr["hello"] = "bonjour"
|
377 |
setvar en2fr["friend"] = "ami"
|
378 |
setvar en2fr["cat"] = "chat"
|
379 |
pp test_ (en2fr => keys())
|
380 |
## status: 0
|
381 |
## STDOUT:
|
382 |
(List) ["hello","friend","cat"]
|
383 |
## END
|
384 |
|
385 |
|
386 |
pp test_ ('a,b,c'.split(','))
|
387 |
pp test_ ('aa'.split('a'))
|
388 |
pp test_ ('a<>b<>c<d'.split('<>'))
|
389 |
pp test_ ('a;b;;c'.split(';'))
|
390 |
pp test_ (''.split('foo'))
|
391 |
## STDOUT:
|
392 |
(List) ["a","b","c"]
|
393 |
(List) ["","",""]
|
394 |
(List) ["a","b","c<d"]
|
395 |
(List) ["a","b","","c"]
|
396 |
(List) []
|
397 |
## END
|
398 |
|
399 |
|
400 |
pp test_ ('a,b,c'.split(',', count=-1))
|
401 |
pp test_ ('a,b,c'.split(',', count=-2)) # Any negative count means "ignore count"
|
402 |
pp test_ ('aa'.split('a', count=1))
|
403 |
pp test_ ('a<>b<>c<d'.split('<>', count=10))
|
404 |
pp test_ ('a;b;;c'.split(';', count=2))
|
405 |
pp test_ (''.split('foo', count=3))
|
406 |
## STDOUT:
|
407 |
(List) ["a","b","c"]
|
408 |
(List) ["a","b","c"]
|
409 |
(List) ["","a"]
|
410 |
(List) ["a","b","c<d"]
|
411 |
(List) ["a","b",";c"]
|
412 |
(List) []
|
413 |
## END
|
414 |
|
415 |
|
416 |
try { pp test_ ('abc'.split('')) } # Sep cannot be ""
|
417 |
echo status=$[_error.code]
|
418 |
try { pp test_ ('abc'.split()) } # Sep must be present
|
419 |
echo status=$[_error.code]
|
420 |
try { pp test_ ('abc'.split('b', count=0)) } # Count cannot be 0
|
421 |
echo status=$[_error.code]
|
422 |
## STDOUT:
|
423 |
status=3
|
424 |
status=3
|
425 |
status=3
|
426 |
## END
|
427 |
|
428 |
|
429 |
var en2fr = {}
|
430 |
setvar en2fr["hello"] = "bonjour"
|
431 |
setvar en2fr["friend"] = "ami"
|
432 |
setvar en2fr["cat"] = "chat"
|
433 |
pp test_ (en2fr => values())
|
434 |
## status: 0
|
435 |
## STDOUT:
|
436 |
(List) ["bonjour","ami","chat"]
|
437 |
## END
|
438 |
|
439 |
|
440 |
var book = {title: "The Histories", author: "Herodotus"}
|
441 |
call book->erase("author")
|
442 |
pp test_ (book)
|
443 |
# confirm method is idempotent
|
444 |
call book->erase("author")
|
445 |
pp test_ (book)
|
446 |
## status: 0
|
447 |
## STDOUT:
|
448 |
(Dict) {"title":"The Histories"}
|
449 |
(Dict) {"title":"The Histories"}
|
450 |
## END
|
451 |
|
452 |
|
453 |
var book = {title: "Hitchhiker's Guide", published: 1979}
|
454 |
pp test_ (book => get("title", ""))
|
455 |
pp test_ (book => get("published", 0))
|
456 |
pp test_ (book => get("author", ""))
|
457 |
## status: 0
|
458 |
## STDOUT:
|
459 |
(Str) "Hitchhiker's Guide"
|
460 |
(Int) 1979
|
461 |
(Str) ""
|
462 |
## END
|
463 |
|
464 |
|
465 |
const check = "abc" => startsWith
|
466 |
pp test_ (check("a"))
|
467 |
## status: 0
|
468 |
## STDOUT:
|
469 |
(Bool) true
|
470 |
## END
|
471 |
|
472 |
|
473 |
var is_a_ref = { "foo": "bar" }
|
474 |
const f = is_a_ref => keys
|
475 |
pp test_ (f())
|
476 |
setvar is_a_ref["baz"] = 42
|
477 |
pp test_ (f())
|
478 |
|
479 |
var is_a_val = "abc"
|
480 |
const g = is_a_val => startsWith
|
481 |
pp test_ (g("a"))
|
482 |
setvar is_a_val = "xyz"
|
483 |
pp test_ (g("a"))
|
484 |
## status: 0
|
485 |
## STDOUT:
|
486 |
(List) ["foo"]
|
487 |
(List) ["foo","baz"]
|
488 |
(Bool) true
|
489 |
(Bool) true
|
490 |
## END
|
491 |
|
492 |
|
493 |
var items = [1, '2', 3, { 'a': 5 }]
|
494 |
|
495 |
json write (items => indexOf('a'))
|
496 |
json write (items => indexOf(1))
|
497 |
json write (items => indexOf('2'))
|
498 |
json write (items => indexOf({'a': 5}))
|
499 |
## STDOUT:
|
500 |
-1
|
501 |
0
|
502 |
1
|
503 |
3
|
504 |
## END
|
505 |
|
506 |
|
507 |
var items = [1, 2, 3]
|
508 |
|
509 |
json write (items => join()) # default separator is ''
|
510 |
json write (items => join(" ")) # explicit separator (can be any number or chars)
|
511 |
json write (items => join(", ")) # separator can be any number of chars
|
512 |
|
513 |
try {
|
514 |
json write (items => join(1)) # separator must be a string
|
515 |
}
|
516 |
echo "failed with status $_status"
|
517 |
## STDOUT:
|
518 |
"123"
|
519 |
"1 2 3"
|
520 |
"1, 2, 3"
|
521 |
failed with status 3
|
522 |
## END
|
523 |
|
524 |
|
525 |
|
526 |
var empty = []
|
527 |
|
528 |
var a = [0]
|
529 |
var b = [2, 1, 3]
|
530 |
var c = :| hello world |
|
531 |
|
532 |
call empty->reverse()
|
533 |
call a->reverse()
|
534 |
call b->reverse()
|
535 |
call c->reverse()
|
536 |
|
537 |
pp test_ (empty)
|
538 |
pp test_ (a)
|
539 |
pp test_ (b)
|
540 |
pp test_ (c)
|
541 |
|
542 |
## STDOUT:
|
543 |
(List) []
|
544 |
(List) [0]
|
545 |
(List) [3,1,2]
|
546 |
(List) ["world","hello"]
|
547 |
## END
|
548 |
|
549 |
|
550 |
var x = list(0 .. 3)
|
551 |
call x->reverse()
|
552 |
write @x
|
553 |
## STDOUT:
|
554 |
2
|
555 |
1
|
556 |
0
|
557 |
## END
|
558 |
|