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