xref: /titanic_41/usr/src/lib/libshell/common/tests/comvar.sh (revision ee5416c9d7e449233197d5d20bc6b81e4ff091b2)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#           Copyright (c) 1982-2007 AT&T Knowledge Ventures            #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                      by AT&T Knowledge Ventures                      #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
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
28#test for compound variables
29Command=${0##*/}
30integer Errors=0
31Point=(
32	float x=1. y=0.
33)
34eval p="$Point"
35if	(( (p.x*p.x + p.y*p.y) > 1.01 ))
36then	err_exit 'compound variable not working'
37fi
38nameref foo=p
39if	[[ ${foo.x} != ${Point.x} ]]
40then	err_exit 'reference to compound object not working'
41fi
42unset foo
43rec=(
44	name='Joe Blow'
45	born=(
46		month=jan
47		integer day=16
48		year=1980
49	)
50)
51eval newrec="$rec"
52if	[[ ${newrec.name} != "${rec.name}" ]]
53then	err_exit 'copying a compound object not working'
54fi
55if	(( newrec.born.day != 16 ))
56then	err_exit 'copying integer field of  compound object not working'
57fi
58p_t=(
59        integer z=0
60        typeset -A tokens
61)
62unset x
63typeset -A x
64x=( [foo]=bar )
65if	[[ ${x[@]} != bar ]]
66then	err_exit 'compound assignemnt of associative arrays not working'
67fi
68unset -n foo x
69unset foo x
70foo=( x=3)
71nameref x=foo
72if	[[ ${!x.@} != foo.x ]]
73then	err_exit 'name references not expanded on prefix matching'
74fi
75unset x
76(
77	x=()
78	x.foo.bar=7
79	[[ ${x.foo.bar} == 7 ]] || err_exit '[[ ${x.foo.bar} != 7 ]]'
80	(( x.foo.bar == 7  ))|| err_exit '(( x.foo.bar != 7 ))'
81	[[ ${x.foo} == *bar=7*  ]] || err_exit '[[ ${x.foo} != *bar=7* ]]'
82)
83foo=(integer x=3)
84if	[[ ${foo} != *x=3* ]]
85then	err_exit "compound variable with integer subvariable not working"
86fi
87$SHELL -c $'x=(foo=bar)\n[[ x == x ]]' 2> /dev/null ||
88	err_exit '[[ ... ]] not working after compound assignment'
89unset foo
90[[ ${!foo.@} ]] && err_exit 'unset compound variable leaves subvariables'
91suitable=(
92  label="Table Viewer"
93  langs="ksh"
94  uselang=ksh
95  launch=no
96  groups="default"
97  default=(
98    label="Table Viewer Preferences"
99    entrylist=" \
100      vieworigin viewsize viewcolor viewfontname viewfontsize \
101      showheader header showfooter footer showtitle title showlegends \
102      class_td_lg1_style class_tr_tr1_style \
103      class_th_th1_style class_td_td1_style \
104      fields fieldorder \
105    "
106    entries=(
107      vieworigin=(
108        type=coord var=vieworigin val="0 0" label="Window Position"
109      )
110      viewsize=(
111        type=coord var=viewsize val="400 400" label="Window Size"
112      )
113      viewcolor=(
114        type=2colors var=viewcolor val="gray black"
115        label="Window Colors"
116      )
117      viewfontname=(
118        type=fontname var=viewfontname val="Times-Roman"
119        label="Window Font Name"
120      )
121      viewfontsize=(
122        type=fontsize var=viewfontsize val=14 label="Window Font Size"
123      )
124
125      showheader=(
126        type=yesno var=showheader val=no label="Show Header"
127      )
128      header=(
129        type=text var=header val="" label="Header"
130      )
131
132      showfooter=(
133        type=yesno var=showfooter val=no label="Show Footer"
134      )
135      footer=(
136        type=text var=footer val="" label="Footer"
137      )
138
139      showtitle=(
140        type=yesno var=showtitle val=yes label="Show Title"
141      )
142      title=(
143        type=text var=title val="SWIFTUI - Table View" label="Title"
144      )
145
146      showlegends=(
147        type=yesno var=showlegends val=yes label="Show Legends"
148      )
149
150      class_td_lg1_style=(
151        type=style var=class_td_lg1_style
152        val="color: black; font-family: Times-Roman; font-size: 14pt"
153        label="Legend 1 Style"
154      )
155
156      class_tr_tr1_style=(
157        type=style var=class_tr_tr1_style val="background: black"
158        label="Table Row 1 Style"
159      )
160
161      class_th_th1_style=(
162        type=style var=class_th_th1_style
163        val="color: black; font-family: Times-Roman; font-size: 14pt; text-align: left"
164        label="Table Header 1 Style"
165      )
166
167      class_td_td1_style=(
168        type=style var=class_td_td1_style
169        val="color: black; font-family: Times-Roman; font-size: 14pt; text-align: left"
170        label="Table Cell 1 Style"
171      )
172
173      fields=(
174        type=text var=fields val= label="List of Fields"
175      )
176      fieldorder=(
177        type=text var=fieldorder val= label="Order of Fields"
178      )
179    )
180  )
181)
182[[ "${suitable}" == *entrylist=* ]] || err_exit 'compound variable expansion omitting fields'
183foo=( bar=foo  barbar=bar)
184[[ $foo == *bar=foo* ]] || err_exit 'no prefix elements in compound variable output'
185function localvar
186{
187	typeset point=(typeset -i x=3 y=4)
188	(( (point.x*point.x + point.y*point.y) == 25 )) || err_exit "local compound variable not working"
189}
190point=(integer x=6 y=8)
191localvar
192	(( (point.x*point.x + point.y*point.y) == 100 )) || err_exit "global compound variable not preserved"
193[[ $($SHELL -c 'foo=();foo.[x]=(y z); print ${foo.x[@]}') == 'y z' ]] 2> /dev/null || err_exit 'foo=( [x]=(y z)  not working'
194unset z
195( [[ ${z.foo.bar:-abc} == abc ]] 2> /dev/null) || err_exit ':- not working with compound variables'
196exit $((Errors))
197
198