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