]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/nameref4.sub
Bash-4.4 patch 19
[thirdparty/bash.git] / tests / nameref4.sub
CommitLineData
ac50fbac
CR
1# test suite cribbed from ksh93 nameref tests
2typeset -i errors=0
3ckval()
4{
5 typeset -n one=$1
6
7 if [[ $one != $2 ]]; then
8 echo "one=$one != 2=$2"
9 (( errors++ ))
10 fi
11}
12
13ckref()
14{
15 typeset -n one=$1 two=$2
16
17 if [[ $one != $two ]]; then
18 echo "one=$one != two=$two"
19 (( errors++ ))
20 fi
21}
22
23name=first
24
25ckref name name
26
27func1()
28{
29 typeset -n color=$1
30 func2 color
31}
32
33func2()
34{
35 typeset color=$1
36 set -- ${color[@]}
37 printf "<%s>" "$@"
38 echo
39}
40
41typeset -A color
42color[apple]=red
43color[grape]=purple
44color[banana]=yellow
45
46# XXX
47#func1 color
48
49unset foo bar
50export bar=foo
51typeset -n foo=bar
52ckval foo foo
53
54# XXX - need to see if we can do checks for self-referencing at assignment
55# time
56command typeset -n xx=yy
57command typeset -n yy=xx
58echo $?
59
60unset foo bar
61unset -n foo bar
62set foo
63typeset -n bar=$1
64foo=hello
65ckval bar hello
66
67# XXX -- another self-referencing error?
68# ksh93 makes this another invalid self-reference
a0c0a00f
CR
69unset foo
70unset -n bar
71
ac50fbac
CR
72bar=123
73foobar()
74{
75 typeset -n foo=bar
76 typeset -n foo=bar
77
78 ckval foo 123
79}
80
81typeset -n short=long
82short=( a b )
83echo "expect <a b>"
84echo ${long[@]}
a0c0a00f
CR
85unset long
86unset -n short
ac50fbac
CR
87
88# assignment to a previously-unset variable
89typeset -n short=long
90short=foo
91echo "expect <foo>"
92echo ${long}
a0c0a00f
CR
93unset long
94unset -n short
ac50fbac
CR
95
96unset foo bar
97
98# simple array references and assignments
99typeset -n foo=bar
100bar=( 1 3 5 7 9)
101echo ${foo[@]}
102echo ${foo[4]}
103foo[2]=42
104echo ${bar[@]}
105
106barfunc()
107{
108 typeset -n v=$1
109 echo ${v[@]}
110 echo ${v[4]}
111 v[2]=44
112 echo ${bar[@]}
113}
114barfunc bar
115
116unset -f foobar
a0c0a00f
CR
117unset bar
118unset -n foo
ac50fbac
CR
119
120# should ref at global scope survive call to foobar()?
121unset ref x
122typeset -n ref
123x=42
124foobar()
125{
126 local xxx=3
127 ref=xxx
128 return 0
129}
130echo ${ref-unset}
131ref=x
132foobar
133ckval ref xxx
134ckval x xxx
135
136# assignment in a function to something possibly out of scope
137assignvar()
138{
139 typeset -n v=$1
140 shift
141 v="$@"
142}
143
144assignvar lex a b c d e
145echo "expect <a b c d e>"
146recho "${lex}"
147
148unset foo bar short long
149
150typeset -n foo='x[2]'
151
152x=(zero one two three four)
153foo=seven
154
155echo "expect <zero> <one> <seven> <three> <four>"
156recho "${x[@]}"
157
158unset ref x
a0c0a00f
CR
159unset -n ref
160
ac50fbac
CR
161typeset -n ref
162ref=x
163# make sure nameref to a previously-unset variable creates the variable
164ref=42
165ckval x 42
166
167# make sure they work inside arithmetic expressions
168unset foo bar ref x xxx
a0c0a00f
CR
169unset -n ref
170
ac50fbac
CR
171typeset -i ivar
172typeset -n iref=ivar
173
174ivar=4+3
175ckval ivar 7
176iref+=5
177ckval ivar 12
178echo $(( iref+4 ))
179(( iref=17 ))
180ckval ivar 17
181
182typeset +n iref
183unset iref ivar
184
185typeset +n foo bar
186unset foo bar
187
188# should the reference do immediate evaluation or deferred?
189set -- one two three four
190bar=4
191# XXX - what does foo get set to here?
192typeset -n foo='bar[0]'
193echo "expect <4>"
194echo ${bar[0]}
195echo "expect <4>"
196echo ${foo}
197echo "expect <4>"
198echo $foo
199ckval foo $bar
200
201# Need to add code and tests for nameref to array subscripts
202bar=(one two three four)
203
204typeset -n foo='bar[0]'
205typeset -n qux='bar[3]'
206echo "expect <one>"
207echo ${bar[0]}
208echo "expect <one>"
209echo ${foo}
210echo "expect <one>"
211echo $foo
212ckval foo $bar
213
214echo "expect <four>"
215echo $qux
216ckval qux ${bar[3]}
217
218# Need to add code and tests for `for' loop nameref variables
219
220echo errors = $errors
221exit $errors