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

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