1 | ## our_shell: ysh
|
2 | ## oils_failures_allowed: 0
|
3 |
|
4 | #### shell array :| a 'b c' |
|
5 | shopt -s parse_at
|
6 | var x = :| a 'b c' |
|
7 | var empty = %()
|
8 | argv.py / @x @empty /
|
9 |
|
10 | ## STDOUT:
|
11 | ['/', 'a', 'b c', '/']
|
12 | ## END
|
13 |
|
14 | #### empty array and simple_word_eval (regression test)
|
15 | shopt -s parse_at simple_word_eval
|
16 | var empty = :| |
|
17 | echo len=$[len(empty)]
|
18 | argv.py / @empty /
|
19 |
|
20 | ## STDOUT:
|
21 | len=0
|
22 | ['/', '/']
|
23 | ## END
|
24 |
|
25 | #### Empty array and assignment builtin (regression)
|
26 | # Bug happens with shell arrays too
|
27 | empty=()
|
28 | declare z=1 "${empty[@]}"
|
29 | echo z=$z
|
30 | ## STDOUT:
|
31 | z=1
|
32 | ## END
|
33 |
|
34 | #### Shell arrays support tilde detection, static globbing, brace detection
|
35 | shopt -s parse_at simple_word_eval
|
36 | touch {foo,bar}.py
|
37 | HOME=/home/bob
|
38 | no_dynamic_glob='*.py'
|
39 |
|
40 | var x = :| ~/src *.py {andy,bob}@example.com $no_dynamic_glob |
|
41 | argv.py @x
|
42 | ## STDOUT:
|
43 | ['/home/bob/src', 'bar.py', 'foo.py', 'andy@example.com', 'bob@example.com', '*.py']
|
44 | ## END
|
45 |
|
46 | #### Basic List, a[42] a['42'] allowed
|
47 |
|
48 | var x = :| 1 2 3 |
|
49 | write len=$[len(x)]
|
50 |
|
51 | pp test_ (x[1])
|
52 |
|
53 | # Can be int-looking string
|
54 | pp test_ (x['2'])
|
55 |
|
56 | # Not allowed
|
57 | pp test_ (x['zz'])
|
58 |
|
59 | ## status: 3
|
60 | ## STDOUT:
|
61 | len=3
|
62 | (Str) "2"
|
63 | (Str) "3"
|
64 | ## END
|
65 |
|
66 | #### Mutate List entries, a[42] a['42'] allowed
|
67 |
|
68 | var a = :| 2 3 4 |
|
69 |
|
70 | setvar a[1] = 1
|
71 | pp test_ (a)
|
72 |
|
73 | setvar a['2'] += 5
|
74 | pp test_ (a)
|
75 |
|
76 | # Can be int-looking string
|
77 | setvar a['2'] = 99
|
78 | pp test_ (a)
|
79 |
|
80 | # Not allowed
|
81 | setvar a['zz'] = 101
|
82 |
|
83 | ## status: 3
|
84 | ## STDOUT:
|
85 | (List) ["2",1,"4"]
|
86 | (List) ["2",1,9]
|
87 | (List) ["2",1,99]
|
88 | ## END
|
89 |
|
90 |
|
91 | #### string array with command sub, varsub, etc.
|
92 | shopt -s ysh:all
|
93 |
|
94 | var x = 1
|
95 | var a = :| $x $(write hi) 'sq' "dq $x" |
|
96 | write len=$[len(a)]
|
97 | write @a
|
98 | ## STDOUT:
|
99 | len=4
|
100 | 1
|
101 | hi
|
102 | sq
|
103 | dq 1
|
104 | ## END
|
105 |
|
106 | #### Can print type of List with pp
|
107 |
|
108 | var b = :|true| # this is a string
|
109 | pp test_ (b)
|
110 |
|
111 | # = b
|
112 |
|
113 | var empty = :||
|
114 | pp test_ (empty)
|
115 |
|
116 | # = empty
|
117 |
|
118 | ## STDOUT:
|
119 | (List) ["true"]
|
120 | (List) []
|
121 | ## END
|
122 |
|
123 | #### splice and stringify array
|
124 |
|
125 | shopt -s parse_at
|
126 |
|
127 | var x = :| 'a b' c |
|
128 |
|
129 | declare -a array=( @x )
|
130 |
|
131 | argv.py "${array[@]}" # should work
|
132 |
|
133 | echo -$array- # fails because of strict_arraywith type error
|
134 |
|
135 | echo -$x- # fails with type error
|
136 |
|
137 | ## status: 1
|
138 | ## STDOUT:
|
139 | ['a b', 'c']
|
140 | ## END
|
141 |
|
142 | #### List->extend()
|
143 | var l = list(1..3)
|
144 | echo $[len(l)]
|
145 | call l->extend(list(3..6))
|
146 | echo $[len(l)]
|
147 | ## STDOUT:
|
148 | 2
|
149 | 5
|
150 | ## END
|
151 |
|
152 | #### List append()/extend() should return null
|
153 | shopt -s ysh:all
|
154 | var l = list(1..3)
|
155 |
|
156 | var result = l->extend(list(3..6))
|
157 | assert [null === result]
|
158 |
|
159 | setvar result = l->append(6)
|
160 | assert [null === result]
|
161 |
|
162 | echo pass
|
163 | ## STDOUT:
|
164 | pass
|
165 | ## END
|
166 |
|
167 | #### List pop()
|
168 | shopt -s ysh:all
|
169 | var l = list(1..5)
|
170 | assert [4 === l->pop()]
|
171 | assert [3 === l->pop()]
|
172 | assert [2 === l->pop()]
|
173 | assert [1 === l->pop()]
|
174 | echo pass
|
175 | ## STDOUT:
|
176 | pass
|
177 | ## END
|