1 | ## our_shell: ysh
|
2 | ## oils_failures_allowed: 0
|
3 |
|
4 | #### Basic List, a[42] a['42'] allowed
|
5 |
|
6 | var x = :| 1 2 3 |
|
7 | write len=$[len(x)]
|
8 |
|
9 | pp test_ (x[1])
|
10 |
|
11 | # Can be int-looking string
|
12 | pp test_ (x['2'])
|
13 |
|
14 | # Not allowed
|
15 | pp test_ (x['zz'])
|
16 |
|
17 | ## status: 3
|
18 | ## STDOUT:
|
19 | len=3
|
20 | (Str) "2"
|
21 | (Str) "3"
|
22 | ## END
|
23 |
|
24 | #### Mutate List entries, a[42] a['42'] allowed
|
25 |
|
26 | var a = :| 2 3 4 |
|
27 |
|
28 | setvar a[1] = 1
|
29 | pp test_ (a)
|
30 |
|
31 | setvar a['2'] += 5
|
32 | pp test_ (a)
|
33 |
|
34 | # Can be int-looking string
|
35 | setvar a['2'] = 99
|
36 | pp test_ (a)
|
37 |
|
38 | # Not allowed
|
39 | setvar a['zz'] = 101
|
40 |
|
41 | ## status: 3
|
42 | ## STDOUT:
|
43 | (List) ["2",1,"4"]
|
44 | (List) ["2",1,9]
|
45 | (List) ["2",1,99]
|
46 | ## END
|
47 |
|
48 |
|
49 | #### string array with command sub, varsub, etc.
|
50 | shopt -s ysh:all
|
51 |
|
52 | var x = 1
|
53 | var a = :| $x $(write hi) 'sq' "dq $x" |
|
54 | write len=$[len(a)]
|
55 | write @a
|
56 | ## STDOUT:
|
57 | len=4
|
58 | 1
|
59 | hi
|
60 | sq
|
61 | dq 1
|
62 | ## END
|
63 |
|
64 | #### Can print type of List with pp
|
65 |
|
66 | var b = :|true| # this is a string
|
67 | pp test_ (b)
|
68 |
|
69 | # = b
|
70 |
|
71 | var empty = :||
|
72 | pp test_ (empty)
|
73 |
|
74 | # = empty
|
75 |
|
76 | ## STDOUT:
|
77 | (List) ["true"]
|
78 | (List) []
|
79 | ## END
|
80 |
|
81 | #### splice and stringify array
|
82 |
|
83 | shopt -s parse_at
|
84 |
|
85 | var x = :| 'a b' c |
|
86 |
|
87 | declare -a array=( @x )
|
88 |
|
89 | argv.py "${array[@]}" # should work
|
90 |
|
91 | echo -$array- # fails because of strict_arraywith type error
|
92 |
|
93 | echo -$x- # fails with type error
|
94 |
|
95 | ## status: 1
|
96 | ## STDOUT:
|
97 | ['a b', 'c']
|
98 | ## END
|
99 |
|
100 | #### List->extend()
|
101 | var l = list(1..3)
|
102 | echo $[len(l)]
|
103 | call l->extend(list(3..6))
|
104 | echo $[len(l)]
|
105 | ## STDOUT:
|
106 | 2
|
107 | 5
|
108 | ## END
|
109 |
|
110 | #### List append()/extend() should return null
|
111 | shopt -s ysh:all
|
112 | var l = list(1..3)
|
113 |
|
114 | var result = l->extend(list(3..6))
|
115 | assert [null === result]
|
116 |
|
117 | setvar result = l->append(6)
|
118 | assert [null === result]
|
119 |
|
120 | echo pass
|
121 | ## STDOUT:
|
122 | pass
|
123 | ## END
|
124 |
|
125 | #### List pop()
|
126 | shopt -s ysh:all
|
127 | var l = list(1..5)
|
128 | assert [4 === l->pop()]
|
129 | assert [3 === l->pop()]
|
130 | assert [2 === l->pop()]
|
131 | assert [1 === l->pop()]
|
132 | echo pass
|
133 | ## STDOUT:
|
134 | pass
|
135 | ## END
|