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

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