]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/nameref4.sub
Bash-4.3 patch 7
[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
69unset foo bar
70bar=123
71foobar()
72{
73 typeset -n foo=bar
74 typeset -n foo=bar
75
76 ckval foo 123
77}
78
79typeset -n short=long
80short=( a b )
81echo "expect <a b>"
82echo ${long[@]}
83unset short long
84
85# assignment to a previously-unset variable
86typeset -n short=long
87short=foo
88echo "expect <foo>"
89echo ${long}
90unset short long
91
92unset foo bar
93
94# simple array references and assignments
95typeset -n foo=bar
96bar=( 1 3 5 7 9)
97echo ${foo[@]}
98echo ${foo[4]}
99foo[2]=42
100echo ${bar[@]}
101
102barfunc()
103{
104 typeset -n v=$1
105 echo ${v[@]}
106 echo ${v[4]}
107 v[2]=44
108 echo ${bar[@]}
109}
110barfunc bar
111
112unset -f foobar
113unset foo bar
114
115# should ref at global scope survive call to foobar()?
116unset ref x
117typeset -n ref
118x=42
119foobar()
120{
121 local xxx=3
122 ref=xxx
123 return 0
124}
125echo ${ref-unset}
126ref=x
127foobar
128ckval ref xxx
129ckval x xxx
130
131# assignment in a function to something possibly out of scope
132assignvar()
133{
134 typeset -n v=$1
135 shift
136 v="$@"
137}
138
139assignvar lex a b c d e
140echo "expect <a b c d e>"
141recho "${lex}"
142
143unset foo bar short long
144
145typeset -n foo='x[2]'
146
147x=(zero one two three four)
148foo=seven
149
150echo "expect <zero> <one> <seven> <three> <four>"
151recho "${x[@]}"
152
153unset ref x
154typeset -n ref
155ref=x
156# make sure nameref to a previously-unset variable creates the variable
157ref=42
158ckval x 42
159
160# make sure they work inside arithmetic expressions
161unset foo bar ref x xxx
162typeset -i ivar
163typeset -n iref=ivar
164
165ivar=4+3
166ckval ivar 7
167iref+=5
168ckval ivar 12
169echo $(( iref+4 ))
170(( iref=17 ))
171ckval ivar 17
172
173typeset +n iref
174unset iref ivar
175
176typeset +n foo bar
177unset foo bar
178
179# should the reference do immediate evaluation or deferred?
180set -- one two three four
181bar=4
182# XXX - what does foo get set to here?
183typeset -n foo='bar[0]'
184echo "expect <4>"
185echo ${bar[0]}
186echo "expect <4>"
187echo ${foo}
188echo "expect <4>"
189echo $foo
190ckval foo $bar
191
192# Need to add code and tests for nameref to array subscripts
193bar=(one two three four)
194
195typeset -n foo='bar[0]'
196typeset -n qux='bar[3]'
197echo "expect <one>"
198echo ${bar[0]}
199echo "expect <one>"
200echo ${foo}
201echo "expect <one>"
202echo $foo
203ckval foo $bar
204
205echo "expect <four>"
206echo $qux
207ckval qux ${bar[3]}
208
209# Need to add code and tests for `for' loop nameref variables
210
211echo errors = $errors
212exit $errors