1 ########################################################################
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 #
9 # A copy of the License is available at #
10 # http://www.opensource.org/licenses/cpl1.0.txt #
11 # (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) #
13 # Information and Software Systems Research #
17 # David Korn <dgk@research.att.com> #
19 ########################################################################
23 print
-u2 -r ${Command}[$1]: "${@:2}"
26 alias err_exit
='err_exit $LINENO'
31 tmp
=$
(mktemp
-dt) ||
{ err_exit mktemp
-dt failed
; exit 1; }
32 trap "cd /; rm -rf $tmp" EXIT
38 then err_exit
"foo=$foo != bar=$bar"
42 then err_exit
"foo=$foo != bar=$bar"
45 if [[ ${foo.child} != ${bar.child} ]]
46 then err_exit
"foo.child=${foo.child} != bar=${bar.child}"
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"
72 set -s -- ${!color[@]}
80 if [[ $
(func1 color
) != 'apple banana grape' ]]
81 then err_exit
"nameref or nameref not working"
84 if [[ ${!x} != .foo.bar
]]
85 then err_exit
"${!x} not working"
87 typeset
+n x $
(typeset
+n
)
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"
104 typeset
+n foo bar
2> /dev
/null
109 then err_exit
"value of nameref foo != $foo"
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"
128 if [[ $bar != hello
]]
129 then err_exit
'nameref of positional paramters outside of function not working'
138 foobar
2> /dev
/null || err_exit
'nameref not unsetting previous reference'
140 nameref short
=verylong
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'
147 if [[ $
(var1
=1 var2
=2
152 then err_exit
'for loop nameref optimization error'
154 if [[ $
(typeset
-A var1 var2
155 var1
[sub1
]=1 var2
[sub2
]=1
160 done) != $
'sub1\nsub2' ]]
161 then err_exit
'for loop nameref optimization test2 error'
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'
175 then err_exit
'nameref optimization error'
186 done) != ok
]] && err_exit
'invalid for loop optimization of name references'
187 function setval
# name value
195 (( foo.bar
== 5)) || err_exit
'nested nameref not working'
202 [[ $
(selfref ps
) == *a
=1* ]] || err_exit
'local nameref cannot reference global variable of the same name'
208 [[ $
(subref ps
) == 1 ]] || err_exit
'local nameref cannot reference global variable child'
212 typeset ps
=(typeset
-i a
=3 b
=4)
213 [[ $
(subref ps
) == 3 ]] || err_exit
'local nameref cannot reference caller compound variable'
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'
231 [[ $
(fun
$i) == hi
]] || err_exit
'nameref for compound variable with in function name of caller fails'
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'
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'
252 typeset var=( foo=hello)
257 ) == *foo=hello* ]] || err_exit 'unable to display compound variable from name reference of local variable'
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"
263 ) != foo ]] && err_exit 'nameref x=[$c] '"not working for c=$c"
270 [[ $x == ok ]] || err_exit 'nameref to unset subscript not working'
278 typeset compound_var=()
279 function2 compound_var
280 printf "x=%d, y=%d\n" compound_var.x compound_var.y
283 [[ "$x" != 'x=19, y=20' ]] && err_exit "expected 'x=19, y=20', got '${x}'"
293 foo=1 bar=2;a) == $'1\n2' ]] 2> /dev/null || err_exit 'nameref in pipeline broken'
296 typeset -n v=vars.data._1
297 print "${v.a} ${v.b}"
302 [[ $(a) == 'a.1 b.1' ]] || err_exit 'nameref choosing wrong scope -- '
303 typeset +n bam zip foo
307 typeset -n bam=foo[2]
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'
326 foobar 2> /dev/null && err_exit 'invalid reference should cause foobar to fail'
327 [[ -v ref ]] && err_exit '$ref should be unset'
329 [[ $ref == 3 ]] || err_exit "\$ref is $ref, it should be 3"
333 typeset -n ref=fvar.foo
337 [[ $(foobar) == ok ]] 2> /dev/null || err_exit 'nameref in function not creating variable in proper scope'
342 [[ $bar == 2 ]] || err_exit 'nameref scoping error'
353 [[ ${!y.@} == 'x[a].b' ]] || err_exit 'reference to array element not expanded with ${!y.@}'
358 command typeset -n n=v.${k//['./']/_} 2> /dev/null || err_exit 'patterns with quotes not handled correctly with name reference assignment'
363 [[ -v sp[19] ]] && err_exit '[[ -v sp[19] ]] where sp is a nameref should not be set'