8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libshell / common / tests / nameref.sh
blobfe4bc2998b0372694f3c8eb1ea3a78d9f795864f
1 ########################################################################
2 # #
3 # This software is part of the ast package #
4 # Copyright (c) 1982-2010 AT&T Intellectual Property #
5 # and is licensed under the #
6 # Common Public License, Version 1.0 #
7 # by AT&T Intellectual Property #
8 # #
9 # A copy of the License is available at #
10 # http://www.opensource.org/licenses/cpl1.0.txt #
11 # (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) #
12 # #
13 # Information and Software Systems Research #
14 # AT&T Research #
15 # Florham Park NJ #
16 # #
17 # David Korn <dgk@research.att.com> #
18 # #
19 ########################################################################
20 function err_exit
22 print -u2 -n "\t"
23 print -u2 -r ${Command}[$1]: "${@:2}"
24 let Errors+=1
26 alias err_exit='err_exit $LINENO'
28 Command=${0##*/}
29 integer Errors=0
31 tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32 trap "cd /; rm -rf $tmp" EXIT
34 function checkref
36 nameref foo=$1 bar=$2
37 if [[ $foo != $bar ]]
38 then err_exit "foo=$foo != bar=$bar"
40 foo=hello
41 if [[ $foo != $bar ]]
42 then err_exit "foo=$foo != bar=$bar"
44 foo.child=child
45 if [[ ${foo.child} != ${bar.child} ]]
46 then err_exit "foo.child=${foo.child} != bar=${bar.child}"
50 name=first
51 checkref name name
52 name.child=second
53 checkref name name
54 .foo=top
55 .foo.bar=next
56 checkref .foo.bar .foo.bar
57 if [[ ${.foo.bar} != hello ]]
58 then err_exit ".foo.bar=${.foo.bar} != hello"
60 if [[ ${.foo.bar.child} != child ]]
61 then err_exit ".foo.bar.child=${.foo.bar.child} != child"
63 function func1
65 nameref color=$1
66 func2 color
69 function func2
71 nameref color=$1
72 set -s -- ${!color[@]}
73 print -r -- "$@"
76 typeset -A color
77 color[apple]=red
78 color[grape]=purple
79 color[banana]=yellow
80 if [[ $(func1 color) != 'apple banana grape' ]]
81 then err_exit "nameref or nameref not working"
83 nameref x=.foo.bar
84 if [[ ${!x} != .foo.bar ]]
85 then err_exit "${!x} not working"
87 typeset +n x $(typeset +n)
88 unset x
89 nameref x=.foo.bar
90 function x.set
92 [[ ${.sh.value} ]] && print hello
94 if [[ $(.foo.bar.set) != $(x.set) ]]
95 then err_exit "function references not working"
97 if [[ $(typeset +n) != x ]]
98 then err_exit "typeset +n doesn't list names of reference variables"
100 if [[ $(typeset -n) != x=.foo.bar ]]
101 then err_exit "typeset +n doesn't list values of reference variables"
103 file=$tmp/test
104 typeset +n foo bar 2> /dev/null
105 unset foo bar
106 export bar=foo
107 nameref foo=bar
108 if [[ $foo != foo ]]
109 then err_exit "value of nameref foo != $foo"
111 cat > $file <<\!
112 print -r -- $foo
114 chmod +x "$file"
115 y=$( $file)
116 if [[ $y != '' ]]
117 then err_exit "reference variable not cleared"
120 command nameref xx=yy
121 command nameref yy=xx
122 } 2> /dev/null && err_exit "self reference not detected"
123 typeset +n foo bar
124 unset foo bar
125 set foo
126 nameref bar=$1
127 foo=hello
128 if [[ $bar != hello ]]
129 then err_exit 'nameref of positional paramters outside of function not working'
131 unset foo bar
132 bar=123
133 function foobar
135 typeset -n foo=bar
136 typeset -n foo=bar
138 foobar 2> /dev/null || err_exit 'nameref not unsetting previous reference'
140 nameref short=verylong
141 short=( A=a B=b )
142 if [[ ${verylong.A} != a ]]
143 then err_exit 'nameref short to longname compound assignment error'
145 ) 2> /dev/null|| err_exit 'nameref short to longname compound assignment error'
146 unset x
147 if [[ $(var1=1 var2=2
148 for i in var1 var2
149 do nameref x=$i
150 print $x
151 done) != $'1\n2' ]]
152 then err_exit 'for loop nameref optimization error'
154 if [[ $(typeset -A var1 var2
155 var1[sub1]=1 var2[sub2]=1
156 for i in var1 var2
158 typeset -n array=$i
159 print ${!array[*]}
160 done) != $'sub1\nsub2' ]]
161 then err_exit 'for loop nameref optimization test2 error'
164 unset -n x foo bar
165 if [[ $(nameref x=foo;for x in foo bar;do print ${!x};done) != $'foo\nbar' ]]
166 then err_exit 'for loop optimization with namerefs not working'
168 if [[ $(
169 p=(x=(r=3) y=(r=4))
170 for i in x y
171 do nameref x=p.$i
172 print ${x.r}
173 done
174 ) != $'3\n4' ]]
175 then err_exit 'nameref optimization error'
177 [[ $(
178 unset x y var
179 var=(foo=bar)
180 for i in y var
181 do typeset -n x=$i
182 if [[ ${!x.@} ]]
183 then print ok
185 typeset +n x
186 done) != ok ]] && err_exit 'invalid for loop optimization of name references'
187 function setval # name value
189 nameref arg=$1
190 nameref var=arg.bar
191 var=$2
193 foo=( integer bar=0)
194 setval foo 5
195 (( foo.bar == 5)) || err_exit 'nested nameref not working'
196 function selfref
198 typeset -n ps=$1
199 print -r -- "${ps}"
201 ps=(a=1 b=2)
202 [[ $(selfref ps) == *a=1* ]] || err_exit 'local nameref cannot reference global variable of the same name'
203 function subref
205 typeset -n foo=$1
206 print -r -- ${foo.a}
208 [[ $(subref ps) == 1 ]] || err_exit 'local nameref cannot reference global variable child'
210 function local
212 typeset ps=(typeset -i a=3 b=4)
213 [[ $(subref ps) == 3 ]] || err_exit 'local nameref cannot reference caller compound variable'
215 local
216 unset -f local
217 function local
219 qs=(integer a=3; integer b=4)
221 local 2> /dev/null || err_exit 'function local has non-zero exit status'
222 [[ ${qs.a} == 3 ]] || err_exit 'function cannot set compound global variable'
223 unset fun i
224 foo=(x=hi)
225 function fun
227 nameref i=$1
228 print -r -- "${i.x}"
230 i=foo
231 [[ $(fun $i) == hi ]] || err_exit 'nameref for compound variable with in function name of caller fails'
232 unset -n foo bar
233 typeset -A foo
234 foo[x.y]=(x=3 y=4)
235 nameref bar=foo[x.y]
236 [[ ${bar.x} == 3 ]] || err_exit 'nameref to subscript containing . fails'
237 [[ ${!bar} == 'foo[x.y]' ]] || err_exit '${!var} not correct for nameref to an array instance'
238 typeset +n bar
239 nameref bar=foo
240 [[ ${!bar} == foo ]] || err_exit '${!var} not correct for nameref to array variable'
241 $SHELL -c 'function bar { nameref x=foo[++];};typeset -A foo;bar' 2> /dev/null ||err_exit 'nameref of associative array tries to evaluate subscript'
242 i=$($SHELL -c 'nameref foo=bar; bar[2]=(x=3 y=4); nameref x=foo[2].y;print -r -- $x' 2> /dev/null)
243 [[ $i == 4 ]] || err_exit 'creating reference from subscripted variable whose name is a reference failed'
244 [[ $($SHELL 2> /dev/null <<- '+++EOF'
245 function bar
247 nameref x=$1
248 print -r -- "$x"
250 function foo
252 typeset var=( foo=hello)
253 bar var
256 +++EOF
257 ) == *foo=hello* ]] || err_exit 'unable to display compound variable from name reference of local variable'
258 #set -x
259 for c in '=' '[' ']' '\' "'" '"' '<' '=' '('
260 do [[ $($SHELL 2> /dev/null <<- ++EOF++
261 x;i=\\$c;typeset -A a; a[\$i]=foo;typeset -n x=a[\$i]; print "\$x"
262 ++EOF++
263 ) != foo ]] && err_exit 'nameref x=[$c] '"not working for c=$c"
264 done
265 unset -n foo x
266 unset foo x
267 typeset -A foo
268 nameref x=foo[xyz]
269 foo[xyz]=ok
270 [[ $x == ok ]] || err_exit 'nameref to unset subscript not working'
271 function function2
273 nameref v=$1
274 v.x=19 v.y=20
276 function function1
278 typeset compound_var=()
279 function2 compound_var
280 printf "x=%d, y=%d\n" compound_var.x compound_var.y
282 x="$(function1)"
283 [[ "$x" != 'x=19, y=20' ]] && err_exit "expected 'x=19, y=20', got '${x}'"
284 typeset +n bar
285 unset foo bar
286 [[ $(function a
288 for i in foo bar
289 do typeset -n v=$i
290 print $v
291 done | cat
293 foo=1 bar=2;a) == $'1\n2' ]] 2> /dev/null || err_exit 'nameref in pipeline broken'
294 function a
296 typeset -n v=vars.data._1
297 print "${v.a} ${v.b}"
299 vars=(data=())
300 vars.data._1.a=a.1
301 vars.data._1.b=b.1
302 [[ $(a) == 'a.1 b.1' ]] || err_exit 'nameref choosing wrong scope -- '
303 typeset +n bam zip foo
304 unset bam zip foo
305 typeset -A foo
306 foo[2]=bar
307 typeset -n bam=foo[2]
308 typeset -n zip=bam
309 [[ $zip == bar ]] || err_exit 'nameref to another nameref to array element fails'
310 [[ -R zip ]] || err_exit '[[ -R zip ]] should detect that zip is a reference'
311 [[ -R bam ]] || err_exit '[[ -R bam ]] should detect that bam is a reference'
312 [[ -R zip ]] || err_exit '[[ -v zip ]] should detect that zip is set'
313 [[ -v bam ]] || err_exit '[[ -v bam ]] should detect that bam is set'
314 [[ -R 123 ]] && err_exit '[[ -R 123 ]] should detect that 123 is not a reference'
315 [[ -v 123 ]] && err_exit '[[ -v 123 ]] should detect that 123 is not set'
317 unset ref x
318 typeset -n ref
320 function foobar
322 typeset xxx=3
323 ref=xxx
324 return 0
326 foobar 2> /dev/null && err_exit 'invalid reference should cause foobar to fail'
327 [[ -v ref ]] && err_exit '$ref should be unset'
328 ref=x
329 [[ $ref == 3 ]] || err_exit "\$ref is $ref, it should be 3"
330 function foobar
332 typeset fvar=()
333 typeset -n ref=fvar.foo
334 ref=ok
335 print -r $ref
337 [[ $(foobar) == ok ]] 2> /dev/null || err_exit 'nameref in function not creating variable in proper scope'
338 function foobar
340 nameref doc=docs
341 nameref bar=doc.num
342 [[ $bar == 2 ]] || err_exit 'nameref scoping error'
345 docs=(num=2)
346 foobar
348 typeset +n x y
349 unset x y
350 typeset -A x
351 x[a]=(b=c)
352 typeset -n y=x[a]
353 [[ ${!y.@} == 'x[a].b' ]] || err_exit 'reference to array element not expanded with ${!y.@}'
355 typeset +n v
356 v=()
357 k=a.b.c/d
358 command typeset -n n=v.${k//['./']/_} 2> /dev/null || err_exit 'patterns with quotes not handled correctly with name reference assignment'
360 typeset _n sp
361 nameref sp=addrsp
362 sp[14]=( size=1 )
363 [[ -v sp[19] ]] && err_exit '[[ -v sp[19] ]] where sp is a nameref should not be set'
365 exit $((Errors))