1# There are certain usages of typeset and its synonyms that it is not
2# possible to test here, because they must appear at the top level and
3# everything that follows is processed by an "eval" within a function.
4
5# Equivalences:
6#  declare	typeset
7#  export	typeset -x		and typeset -x implies -g
8#  float	typeset -E
9#  functions	typeset -f
10#  integer	typeset -i
11#  local	typeset +g -m		approximately
12#  readonly	typeset -r
13
14# Tested elsewhere:
15#  Equivalence of autoload and typeset -fu 	A05execution
16#  Associative array creation & assignment	D04parameter, D06subscript
17#  Effects of GLOBAL_EXPORT			E01options
18#  Function tracing (typeset -ft)		E02xtrace
19
20# Not yet tested:
21#  Assorted illegal flag combinations
22
23%prep
24
25  setopt noglob
26
27  scalar=scalar
28  array=(a r r a y)
29
30  scope00() {
31    typeset scalar
32    scalar=local
33    typeset -a array
34    array=(l o c a l)
35    print $scalar $array
36  }
37  scope01() {
38    local scalar
39    scalar=local
40    local -a array
41    array=(l o c a l)
42    print $scalar $array
43  }
44  scope02() {
45    declare scalar
46    scalar=local
47    declare -a array
48    array=(l o c a l)
49    print $scalar $array
50  }
51  scope10() {
52    export outer=outer
53    /bin/sh -fc 'echo $outer'
54  }
55  scope11() {
56    typeset -x outer=outer
57    /bin/sh -fc 'echo $outer'
58  }
59  scope12() {
60    local -x outer=inner
61    /bin/sh -fc 'echo $outer'
62  }
63  scope13() {
64    local -xT OUTER outer
65    outer=(i n n e r)
66    /bin/sh -fc 'echo $OUTER'
67  }
68
69  # Bug?  `typeset -h' complains that ! # $ * - ? @ are not identifiers.
70  stress00() {
71    typeset -h +g -m [[:alpha:]_]*
72    unset -m [[:alpha:]_]*
73    typeset +m [[:alpha:]_]*
74  }
75
76%test
77
78 typeset +m scalar array
790:Report types of parameters with typeset +m
80>scalar
81>array array
82
83 scope00
84 print $scalar $array
850:Simple local declarations
86>local l o c a l
87>scalar a r r a y
88
89 scope01
90 print $scalar $array
910:Equivalence of local and typeset in functions
92>local l o c a l
93>scalar a r r a y
94
95 scope02
96 print $scalar $array
970:Basic equivalence of declare and typeset
98>local l o c a l
99>scalar a r r a y
100
101 declare +m scalar
1020:declare previously lacked -m/+m options
103>scalar
104
105 scope10
106 print $outer
1070:Global export
108>outer
109>outer
110
111 scope11
112 print $outer
1130:Equivalence of export and typeset -x
114>outer
115>outer
116
117 scope12
118 print $outer
1190:Local export
120>inner
121>outer
122
123 float f=3.14159
124 typeset +m f
125 float -E3 f
126 print $f
127 float -F f
128 print $f
1290:Floating point, adding a precision, and fixed point
130>float local f
131>3.14e+00
132>3.142
133
134 integer i=3.141
135 typeset +m i
136 integer -i2 i
137 print $i
1380:Integer and changing the base
139>integer local i
140>2#11
141
142 float -E3 f=3.141
143 typeset +m f
144 integer -i2 f
145 typeset +m f
146 print $f
1470:Conversion of floating point to integer
148>float local f
149>integer 2 local f
150>2#11
151
152 typeset -f
1530q:Equivalence of functions and typeset -f
154>$(functions)
155
156 readonly r=success
157 print $r
158 r=failure
1591:Readonly declaration
160>success
161?(eval):3: read-only variable: r
162
163 typeset r=success
164 readonly r
165 print $r
166 r=failure
1671:Convert to readonly
168>success
169?(eval):4: read-only variable: r
170
171 typeset -gU array
172 print $array
1730:Uniquified arrays and non-local scope
174>a r y
175
176 typeset -T SCALAR=l:o:c:a:l array
177 print $array
178 typeset -U SCALAR
179 print $SCALAR $array
1800:Tied parameters and uniquified colon-arrays
181>l o c a l
182>l:o:c:a l o c a
183
184 (setopt NO_multibyte cbases
185 LC_ALL=C 2>/dev/null
186 typeset -T SCALAR=$'l\x83o\x83c\x83a\x83l' array $'\x83'
187 print $array
188 typeset -U SCALAR
189 for (( i = 1; i <= ${#SCALAR}; i++ )); do
190   char=$SCALAR[i]
191   print $(( [#16] #char ))
192 done
193 print $array)
1940:Tied parameters and uniquified arrays with meta-character as separator
195>l o c a l
196>0x6C
197>0x83
198>0x6F
199>0x83
200>0x63
201>0x83
202>0x61
203>l o c a
204
205 typeset -T SCALAR=$'l\000o\000c\000a\000l' array $'\000'
206 typeset -U SCALAR
207 print $array
208 [[ $SCALAR == $'l\000o\000c\000a' ]]
2090:Tied parameters and uniquified arrays with NUL-character as separator
210>l o c a
211
212 typeset -T SCALAR array
213 typeset +T SCALAR
2141:Untying is prohibited
215?(eval):typeset:2: use unset to remove tied variables
216
217 OUTER=outer
218 scope13
219 print $OUTER
2200:Export of tied parameters
221>i:n:n:e:r
222>outer
223
224 typeset -TU MORESTUFF=here-we-go-go-again morestuff '-'
225 print -l $morestuff
2260:Tied arrays with separator specified
227>here
228>we
229>go
230>again
231
232 typeset -T THIS will not work
2331:Tied array syntax
234?(eval):typeset:1: -T requires names of scalar and array
235
236 local array[2]=x
2371:Illegal local array element assignment
238?(eval):local:1: array[2]: can't create local array elements
239
240 local -a array
241 typeset array[1]=a array[2]=b array[3]=c
242 print $array
2430:Legal local array element assignment
244>a b c
245
246 local -A assoc
247 local b=1 ;: to stomp assoc[1] if assoc[b] is broken
248 typeset assoc[1]=a assoc[b]=2 assoc[3]=c
249 print $assoc[1] $assoc[b] $assoc[3]
2500:Legal local associative array element assignment
251>a 2 c
252
253 local scalar scalar[1]=a scalar[2]=b scalar[3]=c
254 print $scalar
2550:Local scalar subscript assignment
256>abc
257
258 typeset -L 10 fools
259 for fools in "   once" "twice"  "      thrice" "   oops too long here"; do
260   print "'$fools'"
261 done
2620:Left justification of scalars
263>'once      '
264>'twice     '
265>'thrice    '
266>'oops too l'
267
268 typeset -L 10 -F 3 foolf
269 for foolf in 1.3 4.6 -2.987 -4.91031; do
270   print "'$foolf'"
271 done
2720:Left justification of floating point
273>'1.300     '
274>'4.600     '
275>'-2.987    '
276>'-4.910    '
277
278 typeset -L 10 -Z foolzs
279 for foolzs in 001.3 04.6 -2.987 -04.91231; do
280   print "'$foolzs'"
281 done
2820:Left justification of scalars with zero suppression
283>'1.3       '
284>'4.6       '
285>'-2.987    '
286>'-04.91231 '
287
288 typeset -R 10 foors
289 for foors in short longer even-longer; do
290   print "'$foors'"
291 done
2920:Right justification of scalars
293>'     short'
294>'    longer'
295>'ven-longer'
296
297 typeset -Z 10 foozs
298 for foozs in 42 -42 " 43" " -43"; do
299   print "'$foozs'"
300 done
3010:Right justification of scalars with zeroes
302>'0000000042'
303>'       -42'
304>' 000000043'
305>'       -43'
306
307 integer -Z 10 foozi
308 for foozi in 42 -42 " 43" " -43"; do
309   print "'$foozi'"
310 done
3110:Right justification of integers with zero, no initial base
312>'0000000042'
313>'-000000042'
314>'0000000043'
315>'-000000043'
316# In case you hadn't twigged, the spaces are absorbed in the initial
317# math evaluation, so don't get through.
318
319 unsetopt cbases
320 integer -Z 10 -i 16 foozi16
321 for foozi16 in 42 -42 " 43" " -43"; do
322   print "'$foozi16'"
323 done
3240:Right justification of integers with zero, base 16, C_BASES off
325>'16#000002A'
326>'-16#00002A'
327>'16#000002B'
328>'-16#00002B'
329
330 setopt cbases
331 integer -Z 10 -i 16 foozi16c
332 for foozi16c in 42 -42 " 43" " -43"; do
333   print "'$foozi16c'"
334 done
3350:Right justification of integers with zero, base 16, C_BASES on
336>'0x0000002A'
337>'-0x000002A'
338>'0x0000002B'
339>'-0x000002B'
340
341 setopt cbases
342 integer -Z 10 -i 16 foozi16c
343 for foozi16c in 0x1234 -0x1234; do
344   for (( i = 1; i <= 5; i++ )); do
345       print "'${foozi16c[i,11-i]}'"
346   done
347   print "'${foozi16c[-2]}'"
348 done
3490:Extracting substrings from padded integers
350>'0x00001234'
351>'x0000123'
352>'000012'
353>'0001'
354>'00'
355>'3'
356>'-0x0001234'
357>'0x000123'
358>'x00012'
359>'0001'
360>'00'
361>'3'
362
363 typeset -F 3 -Z 10 foozf
364 for foozf in 3.14159 -3.14159 4 -4; do
365   print "'$foozf'"
366 done
3670:Right justification of fixed point numbers with zero
368>'000003.142'
369>'-00003.142'
370>'000004.000'
371>'-00004.000'
372
373 stress00
374 print $scalar $array
3750q:Stress test: all parameters are local and unset, using -m
376>scalar a r y
377
378 local parentenv=preserved
379 fn() {
380  typeset -h +g -m \*
381  unset -m \*
382  integer i=9
383  float -H f=9
384  declare -t scalar
385  declare -H -a array
386  typeset
387  typeset +
388 }
389 fn
390 echo $parentenv
3910:Parameter hiding and tagging, printing types and values
392>array local array
393>float local f
394>integer local i=9
395>local tagged scalar=''
396>array local array
397>float local f
398>integer local i
399>local tagged scalar
400>preserved
401
402 export ENVFOO=bar
403 print ENVFOO in environment
404 env | grep '^ENVFOO'
405 print Changing ENVFOO
406 ENVFOO="not bar any more"
407 env | grep '^ENVFOO'
408 unset ENVFOO
409 print ENVFOO no longer in environment
410 env | grep '^ENVFOO'
4111:Adding and removing values to and from the environment
412>ENVFOO in environment
413>ENVFOO=bar
414>Changing ENVFOO
415>ENVFOO=not bar any more
416>ENVFOO no longer in environment
417
418 (export FOOENV=BAR
419 env | grep '^FOOENV'
420 print Exec
421 exec $ZTST_testdir/../Src/zsh -fc '
422 print Unset
423 unset FOOENV
424 env | grep "^FOOENV"')
4251:Can unset environment variables after exec
426>FOOENV=BAR
427>Exec
428>Unset
429
430 local case1=upper
431 typeset -u case1
432 print $case1
433 upper="VALUE OF \$UPPER"
434 print ${(P)case1}
4350:Upper case conversion, does not apply to values used internally
436>UPPER
437>VALUE OF $UPPER
438
439 local case2=LOWER
440 typeset -l case2
441 print $case2
442 LOWER="value of \$lower"
443 print ${(P)case2}
4440:Lower case conversion, does not apply to values used internally
445>lower
446>value of $lower
447
448 typeset -a array
449 array=(foo bar)
450 fn() { typeset -p array nonexistent; }
451 fn
4521:declare -p shouldn't create scoped values
453>typeset -a array
454>array=(foo bar)
455?fn:typeset: no such variable: nonexistent
456
457 unsetopt typesetsilent
458 silent1(){ typeset -g silence; }
459 silent2(){ local silence; silent1; }
460 silent2
4610:typeset -g should be silent even without TYPESET_SILENT
462
463 typeset -T TIED_SCALAR tied_array
464 TIED_SCALAR=foo:bar
465 print $tied_array
466 typeset -T TIED_SCALAR=goo:car tied_array
467 print $tied_array
4680:retying arrays to same array works
469>foo bar
470>goo car
471