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 |
|
38 | # could this also be __defaults__.HOME or DEF.HOME?
|
39 | setglobal ENV.HOME = '/home/bob'
|
40 | no_dynamic_glob='*.py'
|
41 |
|
42 | var x = :| ~/src *.py {andy,bob}@example.com $no_dynamic_glob |
|
43 | argv.py @x
|
44 | ## STDOUT:
|
45 | ['/home/bob/src', 'bar.py', 'foo.py', 'andy@example.com', 'bob@example.com', '*.py']
|
46 | ## END
|
47 |
|
48 | #### Basic List, a[42] a['42'] allowed
|
49 |
|
50 | var x = :| 1 2 3 |
|
51 | write len=$[len(x)]
|
52 |
|
53 | pp test_ (x[1])
|
54 |
|
55 | # Can be int-looking string
|
56 | pp test_ (x['2'])
|
57 |
|
58 | # Not allowed
|
59 | pp test_ (x['zz'])
|
60 |
|
61 | ## status: 3
|
62 | ## STDOUT:
|
63 | len=3
|
64 | (Str) "2"
|
65 | (Str) "3"
|
66 | ## END
|
67 |
|
68 | #### Mutate List entries, a[42] a['42'] allowed
|
69 |
|
70 | var a = :| 2 3 4 |
|
71 |
|
72 | setvar a[1] = 1
|
73 | pp test_ (a)
|
74 |
|
75 | setvar a['2'] += 5
|
76 | pp test_ (a)
|
77 |
|
78 | # Can be int-looking string
|
79 | setvar a['2'] = 99
|
80 | pp test_ (a)
|
81 |
|
82 | # Not allowed
|
83 | setvar a['zz'] = 101
|
84 |
|
85 | ## status: 3
|
86 | ## STDOUT:
|
87 | (List) ["2",1,"4"]
|
88 | (List) ["2",1,9]
|
89 | (List) ["2",1,99]
|
90 | ## END
|
91 |
|
92 |
|
93 | #### string array with command sub, varsub, etc.
|
94 | shopt -s ysh:all
|
95 |
|
96 | var x = 1
|
97 | var a = :| $x $(write hi) 'sq' "dq $x" |
|
98 | write len=$[len(a)]
|
99 | write @a
|
100 | ## STDOUT:
|
101 | len=4
|
102 | 1
|
103 | hi
|
104 | sq
|
105 | dq 1
|
106 | ## END
|
107 |
|
108 | #### Can print type of List with pp
|
109 |
|
110 | var b = :|true| # this is a string
|
111 | pp test_ (b)
|
112 |
|
113 | # = b
|
114 |
|
115 | var empty = :||
|
116 | pp test_ (empty)
|
117 |
|
118 | # = empty
|
119 |
|
120 | ## STDOUT:
|
121 | (List) ["true"]
|
122 | (List) []
|
123 | ## END
|
124 |
|
125 | #### splice and stringify array
|
126 |
|
127 | shopt -s parse_at
|
128 |
|
129 | var x = :| 'a b' c |
|
130 |
|
131 | declare -a array=( @x )
|
132 |
|
133 | argv.py "${array[@]}" # should work
|
134 |
|
135 | echo -$array- # fails because of strict_arraywith type error
|
136 |
|
137 | echo -$x- # fails with type error
|
138 |
|
139 | ## status: 1
|
140 | ## STDOUT:
|
141 | ['a b', 'c']
|
142 | ## END
|
143 |
|
144 | #### List->extend()
|
145 | var l = list(1..<3)
|
146 | echo $[len(l)]
|
147 | call l->extend(list(3..<6))
|
148 | echo $[len(l)]
|
149 | ## STDOUT:
|
150 | 2
|
151 | 5
|
152 | ## END
|
153 |
|
154 | #### List append()/extend() should return null
|
155 | shopt -s ysh:all
|
156 | var l = list(1..<3)
|
157 |
|
158 | var result = l->extend(list(3..<6))
|
159 | assert [null === result]
|
160 |
|
161 | setvar result = l->append(6)
|
162 | assert [null === result]
|
163 |
|
164 | echo pass
|
165 | ## STDOUT:
|
166 | pass
|
167 | ## END
|
168 |
|
169 | #### List pop()
|
170 | shopt -s ysh:all
|
171 | var l = list(1..<5)
|
172 | assert [4 === l->pop()]
|
173 | assert [3 === l->pop()]
|
174 | assert [2 === l->pop()]
|
175 | assert [1 === l->pop()]
|
176 | echo pass
|
177 | ## STDOUT:
|
178 | pass
|
179 | ## END
|