summaryrefslogtreecommitdiff
path: root/Test/V10private.ztst
diff options
context:
space:
mode:
Diffstat (limited to 'Test/V10private.ztst')
-rw-r--r--Test/V10private.ztst262
1 files changed, 257 insertions, 5 deletions
diff --git a/Test/V10private.ztst b/Test/V10private.ztst
index 56ffbc5b4..26004a2dc 100644
--- a/Test/V10private.ztst
+++ b/Test/V10private.ztst
@@ -10,6 +10,8 @@
sed -e 's,# test_zsh_param_private,zmodload zsh/param/private,' < $ZTST_srcdir/B02typeset.ztst > private.TMP/B02
fi
+ setopt TYPESET_TO_UNSET
+
%test
(zmodload -u zsh/param/private && zmodload zsh/param/private)
@@ -26,7 +28,7 @@
print $scalar_test
0:basic scope hiding
>toplevel
->local scalar_test
+>local hide scalar_test
>0
>toplevel
@@ -52,7 +54,7 @@
print $+unset_test
0:variable defined only in scope
>0
->local unset_test
+>local hide unset_test
>setme
>0
@@ -68,7 +70,7 @@
}
print $array_test
0:nested scope with different type, correctly restored
->local array_test
+>local hide array_test
>in function
>top level
@@ -111,7 +113,7 @@
typeset -a hash_test=(top level)
typeset -p hash_test
inner () {
- private -p hash_test
+ typeset -p hash_test
print ${(t)hash_test} ${(kv)hash_test}
}
outer () {
@@ -246,7 +248,7 @@ F:note "typeset" rather than "private" in output from outer
1:privates are not visible in anonymous functions, part 3
>X top level
>array_test not set
-?(anon):4: array_test: can't change parameter attribute
+?(anon):4: array_test: can't modify read-only parameter
F:future revision will create a global with this assignment
typeset -a array_test
@@ -299,6 +301,256 @@ F:future revision will create a global with this assignment
*>*
*>*
+ typeset top=TOP
+ () {
+ local -P -n test=top
+ print $top
+ () { print UP: $test }
+ }
+0:nameref can be declared private
+>TOP
+>UP:
+
+ () {
+ typeset -a ary
+ local -P -n ref=ary
+ {
+ (){
+ ref=XX # Should be an error
+ typeset -p ary ref
+ }
+ } always {
+ TRY_BLOCK_ERROR=0
+ typeset -p ary ref
+ }
+ }
+ typeset -p ary
+1:assignment to private nameref in wrong scope, part 1
+>typeset -a ary
+>typeset -hn ref=ary
+*?*ref: can't modify read-only parameter
+*?*no such variable: ary
+
+ () {
+ typeset -a ary
+ local -P -n ref=ary
+ {
+ (){
+ typeset ref=XX # Should create a local
+ typeset -p ary ref
+ }
+ } always {
+ TRY_BLOCK_ERROR=0
+ typeset -p ary ref
+ }
+ }
+ typeset -p ary
+1:assignment to private nameref in wrong scope, part 2
+>typeset -g -a ary
+>typeset ref=XX
+>typeset -a ary
+>typeset -hn ref=ary
+*?*no such variable: ary
+
+ () {
+ typeset -n ptr1=ptr2
+ private -n ptr2 # TYPESET_TO_UNSET makes this not a "placeholder"
+ typeset -p ptr1 ptr2
+ typeset val=LOCAL
+ () {
+ ptr1=val # Test dies here as ptr2 is private and unset
+ typeset -n
+ printf "%s=%s\n" ptr1 "$ptr1" ptr2 "$ptr2"
+ }
+ typeset -p ptr1 ptr2
+ }
+ typeset -p ptr2
+1:up-reference for private namerefs, end unset and not in scope
+F:See K01nameref.ztst up-reference part 5
+F:Here ptr1 finds private ptr2 by scope mismatch
+>typeset -n ptr1=ptr2
+>typeset -hn ptr2
+*?*read-only variable: ptr2
+
+ () {
+ typeset -n ptr1=ptr2
+ private -n ptr2= # Assignment makes this a placeholder, not unset
+ typeset -p ptr1 ptr2
+ typeset val=LOCAL
+ () {
+ ptr1=val || print -u2 ptr1: assignment failed
+ typeset -n
+ printf "%s=%s\n" ptr1 "$ptr1" ptr2 "$ptr2"
+ }
+ typeset -p ptr1 ptr2
+ }
+ typeset -p ptr2
+1:up-reference for private namerefs, end not in scope
+F:See K01nameref.ztst up-reference part 5
+F:Here ptr1 finds private ptr2 by scope mismatch
+>typeset -n ptr1=ptr2
+>typeset -hn ptr2=''
+>ptr1=ptr2
+>ptr1=
+>ptr2=
+>typeset -n ptr1=ptr2
+>typeset -hn ptr2=''
+*?*ptr1: assignment failed
+*?*no such variable: ptr2
+
+ typeset ptr2
+ () {
+ typeset -n ptr1=ptr2
+ private -n ptr2 # Set/unset is irrelevant, not referenced
+ typeset -p ptr1 ptr2
+ typeset val=LOCAL
+ () {
+ ptr1=val
+ typeset -n
+ printf "%s=%s\n" ptr1 "$ptr1" ptr2 "$ptr2"
+ }
+ typeset -p ptr1 ptr2
+ }
+ typeset -p ptr2
+0:up-reference for private namerefs, end is in scope
+F:See K01typeset.ztst up-reference part 5
+F:Here ptr1 points to global ptr2 so assignment succeeds
+>typeset -n ptr1=ptr2
+>typeset -hn ptr2
+>ptr1=ptr2
+>ptr2=val
+>ptr1=val
+>ptr2=val
+>typeset -n ptr1=ptr2
+>typeset -hn ptr2
+>typeset ptr2=val
+
+ () {
+ setopt localoptions errreturn
+ private -n ptr2
+ typeset -n ptr1=ptr2
+ typeset -p ptr1 ptr2
+ typeset val=LOCAL
+ () {
+ ptr1=val
+ typeset -n
+ printf "v %s=%s\n" ptr1 "$ptr1" ptr2 "$ptr2"
+ }
+ typeset -p ptr1 ptr2
+ }
+ typeset -p ptr1 ptr2
+1:up-reference for private namerefs, end is in scope but private
+F:Should we allow "public" namerefs to private parameters?
+*?*ptr2: invalid reference
+*?*no such variable: ptr1
+*?*no such variable: ptr2
+
+ () {
+ private x=1
+ unset x
+ x=2
+ }
+0:regression test for unset private
+
+ () {
+ private x=1
+ unset x
+ private x=2
+ print $x
+ }
+0:private may be called twice
+>2
+
+ () {
+ private x=1
+ private -a x
+ print $x
+ }
+1:private may not change parameter type
+?(anon):private:2: can't change type of private param: x
+
+ () {
+ private fd1 fd2
+ exec {fd1}>&1
+ print OK
+ () { exec {fd2}>&2 }
+ print BAD $fd2
+ }
+1:redirection cannot assign private in wrong scope
+F:Better if caught in checkclobberparam() but exec.c doesn't know scope
+>OK
+?(anon): fd2: can't modify read-only parameter
+
+ () {
+ private z=outer
+ print ${(t)z} $z
+ print ${|
+ print ${(t)z} $z
+ REPLY=$z
+ }
+ }
+0:nofork may read private in calling function
+>scalar-local-hide-special outer
+>scalar-local-hide-special outer
+>outer
+
+ () {
+ private z=outer
+ print ${(t)z} $z
+ print ${| REPLY=${{z} z=nofork} }
+ print ${(t)z} $z
+ }
+0:nofork may write to private in calling function
+>scalar-local-hide-special outer
+>nofork
+>scalar-local-hide-special nofork
+
+ () {
+ local q=outer
+ print ${|
+ private q=nofork
+ REPLY=${| REPLY=$q}
+ }
+ }
+0:nofork cannot see private in surrounding nofork
+>outer
+
+ () {
+ private z=outer
+ print ${(t)z} $z
+ print ${{z}
+ private q
+ z=${{q} q=nofork}
+ }
+ print ${(t)z} $z
+ }
+1:nofork may not change private in surrounding nofork
+>scalar-local-hide-special outer
+*?*: q: can't modify read-only parameter
+
+ () {
+ private q=outer
+ print ${|
+ () { REPLY="{$q}" }
+ }
+ print ${{q}
+ () { q=nofork }
+ }
+ }
+1:function may not access private from inside nofork
+>{}
+*?*: q: can't modify read-only parameter
+
+ () {
+ print ${|
+ private q
+ () { q=nofork }
+ }
+ }
+1:function may not access private declared in nofork
+*?*: q: can't modify read-only parameter
+
%clean
+ unsetopt TYPESET_TO_UNSET
rm -r private.TMP