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