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