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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
| | # Tests for "nofork" command substitution.
%prep
mkdir nofork.tmp
touch nofork.tmp/file{1,2}.txt
purr() { print -r -- "$@" }
purl() { print -rl -- "$@" }
%test
REPLY=OUTER
purr ${| REPLY=INNER } $REPLY
0:Basic substitution and REPLY scoping
>INNER OUTER
purr ${| REPLY=first}:${| REPLY=second}:$REPLY
0:re-scoping of REPLY in one statement
>first:second:OUTER
purr BEGIN${| printf -v REPLY '%s\n' one two three ; }END
0:Adjacent words
>BEGINone
>two
>three
>END
purr "BEGIN${| printf -v REPLY '%s\n' one two three }END"
0:Adjacent words and quoting, part 1
>BEGINone
>two
>three
>END
purr BEGIN"${| printf -v REPLY '%s\n' one two three }"END
0:Adjacent words and quoting, part 2
>BEGINone
>two
>three
>END
purr BEGIN"${|
printf -v REPLY '%s\n'\
one two three
}"END
0:Embedded newlines
>BEGINone
>two
>three
>END
purr BEGIN"${|
printf -v REPLY $'%s\n' one two three
}"END
0:Embedded newlines and $'...'
>BEGINone
>two
>three
>END
purl ${| print -v REPLY one word here; setopt shwordsplit }
purl ${| print -v REPLY three words here }
purl "and ${| print -v REPLY one word here }"
unsetopt shwordsplit
0:test word splitting on result
F:setting option inside is too late for that substitution
>one word here
>three
>words
>here
>and one word here
(
cd nofork.tmp
setopt globsubst
purr ${| REPLY=f* }
purr ${| REPLY=f? }*
unsetopt globsubst
purr ${| REPLY=f* }
purr ${| REPLY=f? }*
)
1:globsubst on result
>file1.txt file2.txt
>file1.txt file2.txt
>f*
?(eval):8: no matches found: f?*
purr ${| REPLY=$'trailing newlines remain\n\n' }
0:newline removal should not occur
>trailing newlines remain
>
>
() {
purr ${| REPLY=$* ; shift 2 }
purr $*
} these are arguments
0:access to context $argv
>these are arguments
>arguments
purr ${:-${| REPLY=${REPLY:-buried}}}
purr ${:-"${| REPLY=${REPLY:-more buried}}"}
0:nofork inside parameter scope
>buried
>more buried
: ${(e):-'${| REPLY=oops'}
1:unclosed braces are sometimes a bad substitution
F:This seems silly, but see A01grammar ${(e):-'${'} test
?(eval):1: bad substitution
purr ${| REPLY=oops
1:other times lack of closing brace is merely unexpected
F:Why not use this error in the previous case as well?
?(eval):1: closing brace expected
# Next tests check that the PS2 stack is properly managed on error
purr ${| REPLY=${REPLY:-buried}}}
1:unbalanced braces, part 0
?(eval):1: parse error near `}'
purr ${:-${| REPLY=${REPLY:-buried}}
1:unbalanced braces, part 1
?(eval):1: closing brace expected
purr ${:-"${| REPLY=${REPLY:-more buried}"}
1:unbalanced braces, part 2
?(eval):1: unmatched "
purr ${:-"${| REPLY=${REPLY:-more buried"}}}
1:unbalanced braces, part 3
?(eval):1: unmatched "
purr ${:-"${| REPLY=${REPLY:-more buried}}}"
1:unbalanced braces, part 4
?(eval):1: closing brace expected
# Same tests with leading space (future-proofing)
purr ${ purr ${REPLY:-buried}}}
1:unbalanced braces, part 0+
?(eval):1: parse error near `}'
purr ${:-${ purr ${REPLY:-buried}}
1:unbalanced braces, part 1+
?(eval):1: closing brace expected
purr ${:-"${ purr ${REPLY:-more buried}"}
1:unbalanced braces, part 2+
?(eval):1: unmatched "
purr ${:-"${ purr ${REPLY:-more buried"}}}
1:unbalanced braces, part 3+
?(eval):1: unmatched "
purr ${:-"${ purr ${REPLY:-more buried}}}"
1:unbalanced braces, part 4+
?(eval):1: closing brace expected
purr ${ purr STDOUT }
0f:capture stdout
>STDOUT
# end PS2 stack tests
purr $(purr outside ${| REPLY=inside })
purr BEGIN$(purr outside ${| REPLY=inside })END
purr "BEGIN$(purr outside ${| REPLY=inside })END"
purr outside ${| REPLY=$(purr inside)}
purr "outside ${| REPLY=$(purr inside)}"
0:mixing with forking cmdsubst
>outside inside
>BEGINoutside insideEND
>BEGINoutside insideEND
>outside inside
>outside inside
purr `purr outside ${| REPLY=inside }`
purr "outside `purr ${| REPLY=inside }`"
purr outside ${| REPLY=`purr inside`}
purr "outside ${| REPLY=`purr inside`}"
purr outside "`purr ${| REPLY="${:-inside}"}`"
purr "outside ${| REPLY=`purr ${:-inside}`}"
0:mixing with backticks
>outside inside
>outside inside
>outside inside
>outside inside
>outside inside
>outside inside
purr ${| REPLY=$(( 9 + 17 )) }
purr $(( 9 + ${| REPLY=17 } ))
0:mixing with arithemetic
>26
>26
unset reply
purl ${|reply| reply=(1 2 ${| REPLY=3 } 4) }
typeset -p reply
0:array behavior with global assignment
>1
>2
>3
>4
>typeset -g -a reply=( 1 2 3 4 )
unset outer
purr "${|
outer=OUTER
REPLY=INNER
return 7
OUTER=NOTREACHED
} $outer $?"
0:return statement inside, part 1
F:status of "print" should hide return
>INNER OUTER 7
unset outer
outer=${| REPLY=${| return 7}}
7:return status propages in assignment like $(...)
unset outer
purr "${|
outer=OUTER
REPLY=INNER
return 7
OUTER=NOTREACHED
} $outer $?"
print REACHED $OUTER
0:return statement inside, part 2
>INNER OUTER 7
>REACHED
unset outer
purr "${|
# Localoptions needed to avoid breaking test harness?
# The setopt command affects surrounding context
setopt localoptions errreturn
outer=OUTER
REPLY=INNER
false
OUTER=NOTREACHED
} $outer $?"
print REACHED $OUTER ${options[errreturn]}
0:errreturn works inside and remains outside
>INNER OUTER 1
>REACHED on
(
unset outer
purr "${|
outer=OUTER
REPLY=INNER
exit 7
OUTER=NOTREACHED
} $outer $OUTER $?"
print NOT REACHED
)
7:exit statement inside
(
unset outer
purr "${|
setopt errexit
outer=OUTER
REPLY=INNER
false
OUTER=NOTREACHED
} $outer $?"
print NOT REACHED
)
1:errexit inside
outer=GLOBAL
purr "${|
local outer=LOCAL
REPLY=INNER
} $outer $?"
0:local declaration inside
>INNER GLOBAL 0
unset zz
outer=GLOBAL
purr "${|zz|
local outer=LOCAL
zz=NONLOCAL
} $outer $?"
print $zz
0:local declaration, global assignment, part 1
>NONLOCAL GLOBAL 0
>NONLOCAL
unset zz
outer=GLOBAL
purr "${${|
local outer=LOCAL
zz=NONLOCAL
}:-$zz} $outer $?"
0:local declaration, global assignment, part 2 (evaluation order)
>NONLOCAL GLOBAL 0
: ${| fn1() { () { print -v REPLY $'Defined Function' } } }
print "IN${| fn2() { () { print "${:-Second }${|fn1}" } } }OUT"
fn2
0:function definition, brace nesting, quote nesting
>INOUT
>Second Defined Function
<<-EOF
${| REPLY=$'in a here document\n' }
EOF
0:here-document behavior
F:Fiddly here to get EOF past the test syntax
>in a here document
>
<<<${| REPLY="in a here string" }
0:here-string behavior
>in a here string
print -u $ZTST_fd ${ZTST_testname}: TEST COMPLETE
0:make sure we got to the end
F:some tests might silently break the test harness
%clean
unfunction purr purl
|