1# Tests for the zsh/param/private module
2
3%prep
4
5 if ! zmodload zsh/param/private 2>/dev/null; then
6   ZTST_unimplemented="can't load the zsh/param/private module for testing"
7 else
8   # Do not use .tmp here, ztst.zsh will remove it too soon (see %cleanup)
9   mkdir private.TMP
10   sed -e 's,# test_zsh_param_private,zmodload zsh/param/private,' < $ZTST_srcdir/B02typeset.ztst > private.TMP/B02
11 fi
12
13%test
14
15 (zmodload -u zsh/param/private && zmodload zsh/param/private)
160:unload and reload the module without crashing
17
18 typeset scalar_test=toplevel
19 () {
20  print $scalar_test
21  private scalar_test
22  print $+scalar_test
23  unset scalar_test
24  print $+scalar_test
25 }
26 print $scalar_test
270:basic scope hiding
28>toplevel
29>1
30>0
31>toplevel
32
33 typeset scalar_test=toplevel
34 print $scalar_test
35 () {
36  private scalar_test=function
37  print $scalar_test
38 }
39 print $scalar_test
400:enter and exit a scope
41>toplevel
42>function
43>toplevel
44
45 print $+unset_test
46 () {
47  private unset_test
48  print $+unset_test
49  unset_test=setme
50  print $unset_test
51 }
52 print $+unset_test
530:variable defined only in scope
54>0
55>1
56>setme
57>0
58
59 # Depends on zsh-5.0.9 typeset keyword
60 typeset -a array_test=(top level)
61 () {
62  local -Pa array_test=(in function)
63  () {
64   private array_test
65   print $+array_test
66  }
67  print $array_test
68 }
69 print $array_test
700:nested scope with different type, correctly restored
71>1
72>in function
73>top level
74
75 typeset -a array_test=(top level)
76 () {
77  private array_test
78  array_test=(in function)
79 }
801:type of private may not be changed by assignment
81?(anon):2: array_test: attempt to assign array value to non-array
82
83 typeset -A hash_test=(top level)
84 () {
85  setopt localoptions noglob
86  private hash_test[top]
87 }
881:associative array fields may not be private
89?(anon):private:2: hash_test[top]: can't create local array elements
90
91 () {
92  private path
93 }
941:tied params may not be private, part 1
95?(anon):private:1: can't change scope of existing param: path
96
97 () {
98  private PATH
99 }
1001:tied params may not be private, part 2
101?(anon):private:1: can't change scope of existing param: PATH
102
103 () {
104  private -h path
105  print X$path
106 }
1070:privates may hide tied parameters
108>X
109
110 # Deliberate type mismatch here
111 typeset -a hash_test=(top level)
112 typeset -p hash_test
113 inner () {
114  private -p hash_test
115  print ${(t)hash_test} ${(kv)hash_test}
116 }
117 outer () {
118  local -PA hash_test=(in function)
119  typeset -p hash_test
120  inner
121 }
122 outer
123 print ${(kv)hash_test}
1240:private hides value from surrounding scope in nested scope
125>typeset -a hash_test=( top level )
126>typeset -A hash_test=( [in]=function )
127>typeset -g -a hash_test=( top level )
128>array-local top level
129>top level
130F:note "typeset" rather than "private" in output from outer
131
132 () {
133  private -a array_test
134  local array_test=scalar
135 }
1361:private cannot be re-declared as local
137?(anon):local:2: array_test: inconsistent type for assignment
138
139 () {
140  local hash_test=scalar
141  private -A hash_test
142 }
1431:local cannot be re-declared as private
144?(anon):private:2: can't change scope of existing param: hash_test
145
146 inner () {
147  print $+scalar_test
148  $ZTST_testdir/../Src/zsh -fc 'print X $scalar_test'
149 }
150 () {
151  private -x scalar_test=whaat
152  $ZTST_testdir/../Src/zsh -fc 'print X $scalar_test'
153  inner
154  print Y $scalar_test
155 }
1560:exported private behaves like a local, part 1
157>X whaat
158>0
159>X whaat
160>Y whaat
161
162 inner () {
163  typeset -p array_test
164  $ZTST_testdir/../Src/zsh -fc 'print X $array_test'
165 }
166 () {
167  local -Pax array_test=(whaat)
168  print Y $array_test
169  $ZTST_testdir/../Src/zsh -fc 'print X $array_test'
170  inner
171 }
1720:exported private behaves like a local, part 2 (arrays do not export)
173?inner:typeset:1: no such variable: array_test
174>Y whaat
175>X
176>X
177
178 inner () {
179  print $+scalar_test
180  $ZTST_testdir/../Src/zsh -fc 'print X $scalar_test'
181 }
182 () {
183  private scalar_test=whaat
184  export scalar_test
185  $ZTST_testdir/../Src/zsh -fc 'print X $scalar_test'
186  inner
187  () {
188   print $+scalar_test
189   $ZTST_testdir/../Src/zsh -fc 'print X $scalar_test'
190  }
191  print Y $scalar_test
192 }
1930:exported private behaves like a local, part 3 (export does not change scope)
194>X whaat
195>0
196>X whaat
197>0
198>X whaat
199>Y whaat
200
201 typeset -A hash_test=(top level)
202 () {
203  local -PA hash_test=(in function)
204  () {
205   print X ${(kv)hash_test}
206  }
207  print Y ${(kv)hash_test}
208 }
209 print ${(kv)hash_test}
2100:privates are not visible in anonymous functions, part 1
211>X top level
212>Y in function
213>top level
214
215 typeset -A hash_test=(top level)
216 () {
217  local -PA hash_test=(in function)
218  () {
219   print X ${(kv)hash_test}
220   hash_test[in]=deeper
221  }
222  print Y ${(kv)hash_test}
223 }
224 print ${(okv)hash_test}
2250:privates are not visible in anonymous functions, part 2
226>X top level
227>Y in function
228>deeper in level top
229
230 typeset -A hash_test=(top level)
231 () {
232  local -Pa array_test=(in function)
233  local -PA hash_test=($array_test)
234  () {
235   print X ${(kv)hash_test}
236   hash_test=(even deeper)
237   {
238     array_test+=(${(kv)hash_test})
239   } always {
240     print ${array_test-array_test not set} ${(t)array_test}
241   }
242  }
243  print Y ${(kv)hash_test} Z $array_test
244 }
245 print ${(kv)hash_test} ${(t)array_test}
2461:privates are not visible in anonymous functions, part 3
247>X top level
248>array_test not set
249?(anon):4: array_test: attempt to assign private in nested scope
250F:future revision will create a global with this assignment
251
252 typeset -a array_test
253 typeset -A hash_test=(top level)
254 () {
255  local -Pa array_test=(in function)
256  local -PA hash_test=($array_test)
257  () {
258   print X ${(kv)hash_test}
259   hash_test=(even deeper)
260   array_test+=(${(kv)hash_test})
261  }
262  print Y ${(kv)hash_test} Z $array_test
263 }
264 print ${(kv)hash_test} $array_test
2650:privates are not visible in anonymous functions, part 4
266>X top level
267>Y in function Z in function
268>even deeper even deeper
269
270 typeset -A hash_test=(top level)
271 () {
272  local -PA hash_test=(in function)
273  () {
274   print X ${(kv)hash_test}
275   unset hash_test
276  }
277  print Y ${(kv)hash_test}
278 }
279 print ${(t)hash_test} ${(kv)hash_test}
2800:privates are not visible in anonymous functions, part 5
281>X top level
282>Y in function
283>
284
285 # Subshell because otherwise this silently dumps core when broken
286 ( () { private SECONDS } )
2871:special parameters cannot be made private
288?(anon):private: can't change scope of existing param: SECONDS
289
290 () { private -h SECONDS }
2910:private parameter may hide a special parameter
292
293 if (( UID )); then
294   ZTST_verbose=0 $ZTST_exe +Z -f $ZTST_srcdir/ztst.zsh private.TMP/B02
295 else
296   ZTST_skip="cannot re-run typeset tests when tests run as superuser"
297 fi
2980:typeset still works with zsh/param/private module loaded
299*>*
300*>*
301
302%clean
303
304  rm -r private.TMP
305