OILS / spec / hay-meta.test.sh View on Github | oilshell.org

257 lines, 92 significant
1## oils_failures_allowed: 1
2
3# Hay Metaprogramming
4
5#### Conditional Inside Blocks
6shopt --set ysh:all
7
8hay define Rule
9
10const DEBUG = true
11
12Rule one {
13 if (DEBUG) {
14 deps = 'foo'
15 } else {
16 deps = 'bar'
17 }
18}
19
20json write (_hay()) | jq '.children[0]' > actual.txt
21
22diff -u - actual.txt <<EOF
23{
24 "type": "Rule",
25 "args": [
26 "one"
27 ],
28 "children": [],
29 "attrs": {
30 "deps": "foo"
31 }
32}
33EOF
34
35## STDOUT:
36## END
37
38
39#### Conditional Outside Block
40shopt --set ysh:all
41
42hay define Rule
43
44const DEBUG = true
45
46if (DEBUG) {
47 Rule two {
48 deps = 'spam'
49 }
50} else {
51 Rule two {
52 deps = 'bar'
53 }
54}
55
56json write (_hay()) | jq '.children[0].attrs' > actual.txt
57
58diff -u - actual.txt <<EOF
59{
60 "deps": "spam"
61}
62EOF
63
64## STDOUT:
65## END
66
67
68#### Iteration Inside Block
69shopt --set ysh:all
70
71hay define Rule
72
73Rule foo {
74 var d = {}
75 # private var with _
76 for name_ in spam eggs ham {
77 setvar d[name_] = true
78 }
79}
80
81json write (_hay()) | jq '.children[0].attrs' > actual.txt
82
83# For loop name leaks! Might want to make it "name_" instead!
84
85#cat actual.txt
86
87diff -u - actual.txt <<EOF
88{
89 "d": {
90 "spam": true,
91 "eggs": true,
92 "ham": true
93 }
94}
95EOF
96
97
98## STDOUT:
99## END
100
101
102#### Iteration Outside Block
103shopt --set ysh:all
104
105hay define Rule
106
107for name in spam eggs ham {
108 Rule $name {
109 path = "/usr/bin/$name"
110 }
111}
112
113json write (_hay()) | jq '.children[].attrs' > actual.txt
114
115diff -u - actual.txt <<EOF
116{
117 "path": "/usr/bin/spam"
118}
119{
120 "path": "/usr/bin/eggs"
121}
122{
123 "path": "/usr/bin/ham"
124}
125EOF
126
127## STDOUT:
128## END
129
130
131#### Iteration outside Hay node - example from Samuel
132
133shopt --set ysh:all
134
135hay define task
136
137# BUG with hay eval!
138hay eval :result {
139 var all_hellos = [ "You", "lovely", "people", "Chuck Norris" ]
140 for hello in (all_hellos) {
141 task "Say $hello" {
142 var extend = "Say Hello"
143 var overrides = {
144 WORD: hello
145 }
146 }
147 }
148}
149
150json write (result) | jq '.children[].attrs' > actual.txt
151
152#json write (_hay()) | jq '.children[].attrs' > actual.txt
153
154diff -u - actual.txt <<EOF
155{
156 "extend": "Say Hello",
157 "overrides": {
158 "WORD": "You"
159 }
160}
161{
162 "extend": "Say Hello",
163 "overrides": {
164 "WORD": "lovely"
165 }
166}
167{
168 "extend": "Say Hello",
169 "overrides": {
170 "WORD": "people"
171 }
172}
173{
174 "extend": "Say Hello",
175 "overrides": {
176 "WORD": "Chuck Norris"
177 }
178}
179EOF
180echo status=$?
181
182## STDOUT:
183status=0
184## END
185
186#### Proc Inside Block
187shopt --set ysh:all
188
189hay define rule # lower case allowed
190
191proc p(name; out) {
192 echo 'p'
193 call out->setValue(name)
194}
195
196rule hello {
197 var eggs = ''
198 var bar = ''
199
200 p spam (&eggs)
201 p foo (&bar)
202}
203
204json write (_hay()) | jq '.children[0].attrs' > actual.txt
205
206diff -u - actual.txt <<EOF
207{
208 "eggs": "spam",
209 "bar": "foo"
210}
211EOF
212
213## STDOUT:
214p
215p
216## END
217
218
219
220#### Proc That Defines Block
221shopt --set ysh:all
222
223hay define Rule
224
225proc myrule(name) {
226
227 # Each haynode has its own scope. But then it can't see the args! Oops.
228 # Is there a better way to do this?
229
230 shopt --set dynamic_scope {
231 Rule $name {
232 path = "/usr/bin/$name"
233 }
234 }
235}
236
237myrule spam
238myrule eggs
239myrule ham
240
241json write (_hay()) | jq '.children[].attrs' > actual.txt
242
243diff -u - actual.txt <<EOF
244{
245 "path": "/usr/bin/spam"
246}
247{
248 "path": "/usr/bin/eggs"
249}
250{
251 "path": "/usr/bin/ham"
252}
253EOF
254
255## STDOUT:
256## END
257