OILS / spec / ysh-list.test.sh View on Github | oilshell.org

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