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

179 lines, 100 significant
1## oils_failures_allowed: 0
2
3#### Simple Expr Closure
4shopt --set ysh:upgrade
5
6proc my-expr (; expr) {
7 echo $[io->evalExpr(expr)]
8}
9
10proc p {
11 var i = 42
12 my-expr [i + 1]
13}
14
15p
16
17## STDOUT:
1843
19## END
20
21#### Simple Block Closure
22shopt --set ysh:upgrade
23
24shopt --set ysh:upgrade
25
26proc my-expr (; ; ; block) {
27 call io->eval(block)
28}
29
30proc p {
31 var i = 42
32 my-expr {
33 echo $[i + 1]
34 }
35}
36
37p
38
39## STDOUT:
4043
41## END
42
43#### Expr Closures in a Loop !
44shopt --set ysh:upgrade
45
46proc task (; tasks, expr) {
47 call tasks->append(expr)
48}
49
50func makeTasks() {
51 var tasks = []
52 var x = 'x'
53 for __hack__ in (0 .. 3) {
54 var i = __hack__
55 var j = i + 2
56 task (tasks, ^"$x: i = $i, j = $j")
57 }
58 return (tasks)
59}
60
61var exprs = makeTasks()
62#= blocks
63
64for ex in (exprs) {
65 var s = io->evalExpr(ex)
66 echo $s
67}
68
69## STDOUT:
70x: i = 0, j = 2
71x: i = 1, j = 3
72x: i = 2, j = 4
73## END
74
75#### Block Closures in a Loop !
76shopt --set ysh:upgrade
77
78proc task (; tasks; ; b) {
79 call tasks->append(b)
80}
81
82func makeTasks() {
83 var tasks = []
84 var x = 'x'
85 for __hack__ in (0 .. 3) {
86 var i = __hack__
87 var j = i + 2
88 task (tasks) { echo "$x: i = $i, j = $j" }
89 }
90 return (tasks)
91}
92
93var blocks = makeTasks()
94#= blocks
95
96for b in (blocks) {
97 call io->eval(b)
98}
99
100## STDOUT:
101x: i = 0, j = 2
102x: i = 1, j = 3
103x: i = 2, j = 4
104## END
105
106
107#### Explicit __invoke__ for "objects in a loop", not closures in a loop
108shopt --set ysh:upgrade
109
110var procs = []
111for i in (0 .. 3) {
112 proc __invoke__ (; self) {
113 echo "i = $[self.i]"
114 }
115 var methods = Object(null, {__invoke__})
116 var obj = Object(methods, {i})
117 call procs->append(obj)
118}
119
120for p in (procs) {
121 p
122}
123
124# TODO: sugar
125# proc p (; self) capture {i} {
126# echo "i = $[self.i]"
127# }
128# call procs->append(p)
129
130## STDOUT:
131i = 0
132i = 1
133i = 2
134## END
135
136
137#### Expr Closures in a different module
138shopt --set ysh:upgrade
139
140use $REPO_ROOT/spec/testdata/module2/closure.ysh --pick {local,global}_expr
141
142echo $[io->evalExpr(global_expr)]
143
144echo $[io->evalExpr(local_expr())]
145
146## STDOUT:
147global!
148global local!
149## END
150
151
152#### Command Closures in a different module
153shopt --set ysh:upgrade
154
155use $REPO_ROOT/spec/testdata/module2/closure.ysh --pick {local,global}_block
156
157call io->eval(global_block)
158
159call io->eval(local_block())
160
161## STDOUT:
162global!
163global local!
164## END
165
166#### Closures as default argument
167shopt --set ysh:upgrade
168
169use $REPO_ROOT/spec/testdata/module2/closure.ysh --pick default_{expr,block}
170
171echo $[io->evalExpr(default_expr())]
172
173call io->eval(default_block())
174
175## STDOUT:
176global expr!
177global block!
178## END
179