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

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