/
ysh-builtin-meta.test.sh
220 lines (161 loc) · 2.74 KB
/
ysh-builtin-meta.test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
## oils_failures_allowed: 1
#### runproc
shopt --set parse_proc parse_at
f() {
write -- f "$@"
}
proc p {
write -- p @ARGV
}
runproc f 1 2
echo status=$?
runproc p 3 4
echo status=$?
runproc invalid 5 6
echo status=$?
runproc
echo status=$?
## STDOUT:
f
1
2
status=0
p
3
4
status=0
status=1
status=2
## END
#### runproc typed args
shopt --set parse_brace parse_proc
proc p {
echo 'hi from p'
}
# The block is ignored for now
runproc p {
echo myblock
}
echo
proc ty (w; t; n; block) {
echo 'ty'
pp line (w)
pp line (t)
pp line (n)
echo $[type(block)]
}
ty a (42; n=99; ^(echo ty))
echo
runproc ty a (42; n=99; ^(echo ty))
echo
runproc ty a (42; n=99) {
echo 'ty gets literal'
}
# TODO: Command vs. Block vs. Literal Block should be unified
## STDOUT:
hi from p
ty
(Str) "a"
(Int) 42
(Int) 99
Command
ty
(Str) "a"
(Int) 42
(Int) 99
Command
ty
(Str) "a"
(Int) 42
(Int) 99
Block
## END
#### pp asdl
shopt -s ysh:upgrade
fopen >out.txt {
x=42
setvar y = {foo: x}
pp asdl (x)
pp asdl (y)
# TODO, this might be nice?
# pp asdl (x, y)
}
# Two lines with value.Str
grep -n -o value.Str out.txt
# Dict should have an address
#grep -n -o 'Dict 0x' out.txt
#cat out.txt
## STDOUT:
1:value.Str
2:value.Str
## END
#### pp asdl can handle an object cycle
shopt -s ysh:upgrade
var d = {}
setvar d.cycle = d
pp line (d) | fgrep -o '{"cycle":'
pp asdl (d) | fgrep -o 'cycle ...'
## STDOUT:
{"cycle":
cycle ...
## END
#### pp line supports BashArray, BashAssoc
declare -a array=(a b c)
pp line (array)
array[5]=z
pp line (array)
declare -A assoc=([k]=v [k2]=v2)
pp line (assoc)
# I think assoc arrays can never null / unset
assoc['k3']=
pp line (assoc)
## STDOUT:
(BashArray) ["a","b","c"]
(BashArray) ["a","b","c",null,null,"z"]
(BashAssoc) {"k":"v","k2":"v2"}
(BashAssoc) {"k":"v","k2":"v2","k3":""}
## END
#### pp gc-stats
pp gc-stats
## STDOUT:
## END
#### pp cell
x=42
pp cell x
echo status=$?
pp -- cell :x
echo status=$?
pp cell nonexistent
echo status=$?
## STDOUT:
x = (Cell exported:F readonly:F nameref:F val:(value.Str s:42))
status=0
x = (Cell exported:F readonly:F nameref:F val:(value.Str s:42))
status=0
status=1
## END
#### pp cell on indexed array with hole
declare -a array
array[3]=42
pp cell array
## STDOUT:
array = (Cell exported:F readonly:F nameref:F val:(value.BashArray strs:[_ _ _ 42]))
## END
#### pp proc
shopt --set ysh:upgrade
# This has to be a separate file because sh_spec.py strips comments!
. $REPO_ROOT/spec/testdata/doc-comments.sh
pp proc
echo ---
# print one
pp proc f
## STDOUT:
proc_name doc_comment
f "doc ' comment with \" quotes"
g ""
myproc "YSH-style proc"
"true" "Special quoting rule"
---
proc_name doc_comment
f "doc ' comment with \" quotes"
## END