1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2012 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                 Eclipse Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#          http://www.eclipse.org/org/documents/epl-v10.html           #
11#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0
30
31tmp=$(mktemp -dt) || { err_exit mktemp -dt failed; exit 1; }
32trap "cd /; rm -rf $tmp" EXIT
33
34foo=abc
35typeset -C bar=(x=3 y=4 t=7)
36typeset -A z=([abc]=qqq)
37integer r=9
38function fn
39{
40	print global fn $foo
41}
42function fun
43{
44	print global fun $foo
45}
46mkdir -p $tmp/global/bin $tmp/local/bin
47cat > $tmp/global/xfun <<- \EOF
48	function xfun
49	{
50		print xfun global $foo
51	}
52EOF
53cat > $tmp/local/xfun <<- \EOF
54	function xfun
55	{
56		print xfun local $foo
57	}
58EOF
59chmod +x "$tmp/global/xfun" "$tmp/local/xfun"
60print 'print local prog $1' >  $tmp/local/bin/run
61print 'print global prog $1' >  $tmp/global/bin/run
62chmod +x "$tmp/local/bin/run" "$tmp/global/bin/run"
63PATH=$tmp/global/bin:$PATH
64FPATH=$tmp/global
65
66namespace x
67{
68	foo=bar
69	typeset -C bar=(x=1 y=2 z=3)
70	typeset -A z=([qqq]=abc)
71	function fn
72	{
73		print local fn $foo
74	}
75	[[ $(fn) == 'local fn bar' ]] || err_exit 'fn inside namespace should run local function'
76	[[ $(fun) == 'global fun abc' ]] || err_exit 'global fun run from namespace not working'
77	(( r == 9 )) || err_exit 'global variable r not set in namespace'
78false
79	[[ ${z[qqq]} == abc ]] || err_exit 'local array element not correct'
80	[[ ${z[abc]} == '' ]] || err_exit 'global array element should not be visible when local element exists'
81	[[ ${bar.y} == 2 ]] || err_exit 'local variable bar.y not found'
82	[[ ${bar.t} == '' ]] || err_exit 'global bar.t should not be visible'
83	function runxrun
84	{
85		xfun
86	}
87	function runrun
88	{
89		run $1
90	}
91	PATH=$tmp/local/bin:/bin
92	FPATH=$tmp/local
93	[[ $(runxrun) ==  'xfun local bar' ]] || err_exit 'local function on FPATH failed'
94	[[ $(runrun $foo) ==  'local prog bar' ]] || err_exit 'local binary on PATH failed'
95}
96[[ $(fn) == 'global fn abc' ]] || err_exit 'fn outside namespace should run global function'
97[[ $(.x.fn) == 'local fn bar' ]] || err_exit 'namespace function called from global failed'
98[[  ${z[abc]} == qqq ]] || err_exit 'global associative array should not be affected by definiton in namespace'
99[[  ${bar.y} == 4 ]] || err_exit 'global compound variable should not be affected by definiton in namespace'
100[[  ${bar.z} == ''  ]] || err_exit 'global compound variable should not see elements in namespace'
101[[ $(xfun) ==  'xfun global abc' ]] || err_exit 'global function on FPATH failed'
102[[ $(run $foo) ==  'global prog abc' ]] || err_exit 'global binary on PATH failed'
103false
104[[ $(.x.runxrun) ==  'xfun local bar' ]] || err_exit 'namespace function on FPATH failed'
105
106exit $((Errors<125?Errors:125))
107