OILS / spec / ysh-introspect.test.sh View on Github | oils.pub

238 lines, 131 significant
1## oils_failures_allowed: 3
2## our_shell: ysh
3
4#### getFrame()
5
6var fr = vm.getFrame(0)
7pp test_ (fr)
8var d = dict(fr)
9pp test_ (d.ARGV)
10echo
11
12proc p1 {
13 var p1_var = 'x'
14 p2
15}
16
17proc p2 {
18 echo 'p2 frame -1'
19 var fr = vm.getFrame(-1)
20 var d = dict(fr)
21
22 pp test_ (fr)
23 pp test_ (d)
24 pp test_ (keys(d))
25 echo
26
27 echo 'p2 frame -2'
28 setvar fr = vm.getFrame(-2)
29 setvar d = dict(fr)
30
31 pp test_ (fr)
32 pp test_ (keys(d))
33 echo
34}
35
36p1
37
38var fr = vm.getFrame(99) # fails
39
40## status: 3
41## STDOUT:
42<Frame>
43(List) []
44
45p2 frame -1
46<Frame>
47(Dict) {"ARGV":[],"fr":<Frame>}
48(List) ["ARGV","fr"]
49
50p2 frame -2
51<Frame>
52(List) ["ARGV","p1_var"]
53
54## END
55
56
57#### bindFrame()
58
59var frag = ^(echo $i)
60
61# TODO: should be fragment
62pp test_ (frag)
63
64var cmd = bindFrame(frag, vm.getFrame(0))
65
66pp test_ (cmd)
67
68## STDOUT:
69## END
70
71#### vm.getDebugStack()
72
73proc p {
74 echo $[len(vm.getDebugStack())]
75}
76
77proc p2 {
78 p
79}
80
81p
82p2
83
84## STDOUT:
851
862
87## END
88
89#### DebugFrame.toString() running file
90
91$[ENV.SH] $[ENV.REPO_ROOT]/spec/testdata/debug-frame-main.ysh |
92 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
93
94## STDOUT:
95 %1 MYROOT/spec/testdata/debug-frame-main.ysh:4
96 print-stack
97 ^~~~~~~~~~~
98
99 %1 MYROOT/spec/testdata/debug-frame-main.ysh:7
100 my-proc
101 ^~~~~~~
102 %2 MYROOT/spec/testdata/debug-frame-lib.ysh:15
103 print-stack
104 ^~~~~~~~~~~
105## END
106
107
108#### DebugFrame.toString() running stdin and -c
109
110# stdin
111echo 'source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; my-proc' |
112 $[ENV.SH] |
113 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
114echo
115
116# -c
117$[ENV.SH] -c 'source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; my-proc' |
118 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
119
120## STDOUT:
121 %1 [ stdin ]:1
122 source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; my-proc
123 ^~~~~~~
124 %2 MYROOT/spec/testdata/debug-frame-lib.ysh:15
125 print-stack
126 ^~~~~~~~~~~
127
128 %1 [ -c flag ]:1
129 source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; my-proc
130 ^~~~~~~
131 %2 MYROOT/spec/testdata/debug-frame-lib.ysh:15
132 print-stack
133 ^~~~~~~~~~~
134## END
135
136#### DebugFrame.toString() running eval
137
138# -c and eval
139$[ENV.SH] -c 'source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; eval "my-proc a b"' |
140 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
141echo
142
143# eval
144$[ENV.SH] -c 'source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-eval.ysh' |
145 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
146
147## STDOUT:
148## END
149
150#### DebugFrame.toString() running YSH functions
151
152# functions
153$[ENV.SH] -c 'source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh; call-func'
154echo
155
156## STDOUT:
157z
158## END
159
160#### DebugFrame.toString() with 'use' builtin
161
162# Work around spec test builtin limitation: line starting with # is treated as
163# a comment
164
165#echo $[ENV.REPO_ROOT]
166
167$[ENV.SH] -c 'use $[ENV.REPO_ROOT]/spec/testdata/debug-frame-use.ysh' |
168 sed -e "s;$[ENV.REPO_ROOT];MYROOT;g" -e 's;#;%;g'
169
170#write -- $[ENV.REPO_ROOT] | sed "s;$[ENV.REPO_ROOT];REPO_ROOT;g"
171
172## STDOUT:
173 %1 [ -c flag ]:1
174 use $[ENV.REPO_ROOT]/spec/testdata/debug-frame-use.ysh
175 ^~~
176 %2 MYROOT/spec/testdata/debug-frame-use.ysh:5
177 debug-frame-lib my-proc
178 ^~~~~~~~~~~~~~~
179 %3 MYROOT/spec/testdata/debug-frame-lib.ysh:15
180 print-stack
181 ^~~~~~~~~~~
182## END
183
184#### FUNCNAME BASH_LINENO BASH_SOURCE not available with YSH functions
185
186func g(x) {
187 echo ${FUNCNAME[@]}
188 echo ${BASH_LINENO[@]}
189 echo ${BASH_SOURCE[@]}
190}
191
192func f(x) {
193 return (g(x))
194}
195
196# We can allow it in procs -- there's no cost to doing so?
197
198proc p {
199 call f(42)
200}
201
202p
203
204## STDOUT:
205p
20616
207[ stdin ]
208## END
209
210#### DebugFrame.toString() with trap ERR
211
212source $[ENV.REPO_ROOT]/spec/testdata/debug-frame-lib.ysh
213
214trap 'print-stack (prefix=false)' ERR
215set -o errtrace # enable always
216
217proc f {
218 g
219}
220
221proc g {
222 false
223}
224
225f
226
227## status: 1
228## STDOUT:
229[ stdin ]:14
230 f
231 ^
232[ stdin ]:7
233 g
234 ^
235[ stdin ]:11
236 false
237 ^~~~~
238## END