1 |
578 |
markom |
#
|
2 |
|
|
# Tests for "body" and "configbody" commands
|
3 |
|
|
# ----------------------------------------------------------------------
|
4 |
|
|
# AUTHOR: Michael J. McLennan
|
5 |
|
|
# Bell Labs Innovations for Lucent Technologies
|
6 |
|
|
# mmclennan@lucent.com
|
7 |
|
|
# http://www.tcltk.com/itcl
|
8 |
|
|
#
|
9 |
|
|
# RCS: $Id: body.test,v 1.1.1.1 2002-01-16 10:24:47 markom Exp $
|
10 |
|
|
# ----------------------------------------------------------------------
|
11 |
|
|
# Copyright (c) 1993-1998 Lucent Technologies, Inc.
|
12 |
|
|
# ======================================================================
|
13 |
|
|
# See the file "license.terms" for information on usage and
|
14 |
|
|
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
15 |
|
|
|
16 |
|
|
if {[string compare test [info procs test]] == 1} then {source defs}
|
17 |
|
|
|
18 |
|
|
# ----------------------------------------------------------------------
|
19 |
|
|
# Test "body" command
|
20 |
|
|
# ----------------------------------------------------------------------
|
21 |
|
|
test body-1.1 {define a class with missing bodies and arg lists} {
|
22 |
|
|
class test_body {
|
23 |
|
|
constructor {args} {}
|
24 |
|
|
destructor {}
|
25 |
|
|
|
26 |
|
|
method any
|
27 |
|
|
method zero {}
|
28 |
|
|
method one {x}
|
29 |
|
|
method two {x y}
|
30 |
|
|
method defvals {x {y 0} {z 1}}
|
31 |
|
|
method varargs {x args}
|
32 |
|
|
|
33 |
|
|
method override {mesg} {
|
34 |
|
|
return "override: $mesg"
|
35 |
|
|
}
|
36 |
|
|
}
|
37 |
|
|
} ""
|
38 |
|
|
|
39 |
|
|
test body-1.2 {cannot use methods without a body} {
|
40 |
|
|
test_body #auto
|
41 |
|
|
list [catch "test_body0 any" msg] $msg
|
42 |
|
|
} {1 {member function "::test_body::any" is not defined and cannot be autoloaded}}
|
43 |
|
|
|
44 |
|
|
test body-1.3 {check syntax of "body" command} {
|
45 |
|
|
list [catch "body test_body::any" msg] $msg
|
46 |
|
|
} {1 {wrong # args: should be "body class::func arglist body"}}
|
47 |
|
|
|
48 |
|
|
test body-1.4 {make sure members are found correctly} {
|
49 |
|
|
list [catch "body test_body::xyzzyxyzzyxyzzy {} {}" msg] $msg
|
50 |
|
|
} {1 {function "xyzzyxyzzyxyzzy" is not defined in class "::test_body"}}
|
51 |
|
|
|
52 |
|
|
test body-1.5a {members without an argument list can have any args} {
|
53 |
|
|
body test_body::any {} {return "any"}
|
54 |
|
|
list [catch "test_body0 any" msg] $msg
|
55 |
|
|
} {0 any}
|
56 |
|
|
|
57 |
|
|
test body-1.5b {members without an argument list can have any args} {
|
58 |
|
|
body test_body::any {x} {return "any: $x"}
|
59 |
|
|
list [catch "test_body0 any 1" msg] $msg
|
60 |
|
|
} {0 {any: 1}}
|
61 |
|
|
|
62 |
|
|
test body-1.5c {members without an argument list can have any args} {
|
63 |
|
|
body test_body::any {x {y 2}} {return "any: $x $y"}
|
64 |
|
|
list [catch "test_body0 any 1" msg] $msg
|
65 |
|
|
} {0 {any: 1 2}}
|
66 |
|
|
|
67 |
|
|
test body-1.6a {an empty argument list must stay empty} {
|
68 |
|
|
list [catch {body test_body::zero {x y} {return "zero: $x $y"}} msg] $msg
|
69 |
|
|
} {1 {argument list changed for function "::test_body::zero": should be ""}}
|
70 |
|
|
|
71 |
|
|
test body-1.6b {an empty argument list must stay empty} {
|
72 |
|
|
list [catch {body test_body::zero {} {return "zero"}} msg] $msg
|
73 |
|
|
} {0 {}}
|
74 |
|
|
|
75 |
|
|
test body-1.7a {preserve argument list: fixed arguments} {
|
76 |
|
|
list [catch {body test_body::one {x y} {return "one: $x $y"}} msg] $msg
|
77 |
|
|
} {1 {argument list changed for function "::test_body::one": should be "x"}}
|
78 |
|
|
|
79 |
|
|
test body-1.7b {preserve argument list: fixed arguments} {
|
80 |
|
|
list [catch {body test_body::one {a} {return "one: $a"}} msg] $msg
|
81 |
|
|
} {0 {}}
|
82 |
|
|
|
83 |
|
|
test body-1.7c {preserve argument list: fixed arguments} {
|
84 |
|
|
list [catch "test_body0 one 1.0" msg] $msg
|
85 |
|
|
} {0 {one: 1.0}}
|
86 |
|
|
|
87 |
|
|
test body-1.8a {preserve argument list: fixed arguments} {
|
88 |
|
|
list [catch {body test_body::two {x} {return "two: $x"}} msg] $msg
|
89 |
|
|
} {1 {argument list changed for function "::test_body::two": should be "x y"}}
|
90 |
|
|
|
91 |
|
|
test body-1.8b {preserve argument list: fixed arguments} {
|
92 |
|
|
list [catch {body test_body::two {a b} {return "two: $a $b"}} msg] $msg
|
93 |
|
|
} {0 {}}
|
94 |
|
|
|
95 |
|
|
test body-1.8c {preserve argument list: fixed arguments} {
|
96 |
|
|
list [catch "test_body0 two 2.0 3.0" msg] $msg
|
97 |
|
|
} {0 {two: 2.0 3.0}}
|
98 |
|
|
|
99 |
|
|
test body-1.9a {preserve argument list: default arguments} {
|
100 |
|
|
list [catch {body test_body::defvals {x} {}} msg] $msg
|
101 |
|
|
} {1 {argument list changed for function "::test_body::defvals": should be "x {y 0} {z 1}"}}
|
102 |
|
|
|
103 |
|
|
test body-1.9b {preserve argument list: default arguments} {
|
104 |
|
|
list [catch {body test_body::defvals {a {b 0} {c 2}} {}} msg] $msg
|
105 |
|
|
} {1 {argument list changed for function "::test_body::defvals": should be "x {y 0} {z 1}"}}
|
106 |
|
|
|
107 |
|
|
test body-1.9c {preserve argument list: default arguments} {
|
108 |
|
|
list [catch {body test_body::defvals {a {b 0} {c 1}} {}} msg] $msg
|
109 |
|
|
} {0 {}}
|
110 |
|
|
|
111 |
|
|
test body-1.10a {preserve argument list: variable arguments} {
|
112 |
|
|
list [catch {body test_body::varargs {} {}} msg] $msg
|
113 |
|
|
} {1 {argument list changed for function "::test_body::varargs": should be "x args"}}
|
114 |
|
|
|
115 |
|
|
test body-1.10b {preserve argument list: variable arguments} {
|
116 |
|
|
list [catch {body test_body::varargs {a} {}} msg] $msg
|
117 |
|
|
} {0 {}}
|
118 |
|
|
|
119 |
|
|
test body-1.10c {preserve argument list: variable arguments} {
|
120 |
|
|
list [catch {body test_body::varargs {a b c} {}} msg] $msg
|
121 |
|
|
} {0 {}}
|
122 |
|
|
|
123 |
|
|
test body-1.11 {redefined body really does change} {
|
124 |
|
|
list [test_body0 override "test #1"] \
|
125 |
|
|
[body test_body::override {text} {return "new: $text"}] \
|
126 |
|
|
[test_body0 override "test #2"]
|
127 |
|
|
} {{override: test #1} {} {new: test #2}}
|
128 |
|
|
|
129 |
|
|
# ----------------------------------------------------------------------
|
130 |
|
|
# Test "body" command with inheritance
|
131 |
|
|
# ----------------------------------------------------------------------
|
132 |
|
|
test body-2.1 {inherit from a class with missing bodies} {
|
133 |
|
|
class test_ibody {
|
134 |
|
|
inherit test_body
|
135 |
|
|
method zero {}
|
136 |
|
|
}
|
137 |
|
|
test_ibody #auto
|
138 |
|
|
} {test_ibody0}
|
139 |
|
|
|
140 |
|
|
test body-2.2 {redefine a method in a derived class} {
|
141 |
|
|
body test_ibody::zero {} {return "ibody zero"}
|
142 |
|
|
list [test_ibody0 info function zero] \
|
143 |
|
|
[test_ibody0 info function test_body::zero]
|
144 |
|
|
} {{public method ::test_ibody::zero {} {return "ibody zero"}} {public method ::test_body::zero {} {return "zero"}}}
|
145 |
|
|
|
146 |
|
|
test body-2.3 {try to redefine a method that was not declared} {
|
147 |
|
|
list [catch {body test_ibody::one {x} {return "new"}} msg] $msg
|
148 |
|
|
} {1 {function "one" is not defined in class "::test_ibody"}}
|
149 |
|
|
|
150 |
|
|
# ----------------------------------------------------------------------
|
151 |
|
|
# Test "configbody" command
|
152 |
|
|
# ----------------------------------------------------------------------
|
153 |
|
|
test body-3.1 {define a class with public variables} {
|
154 |
|
|
class test_cbody {
|
155 |
|
|
private variable priv
|
156 |
|
|
protected variable prot
|
157 |
|
|
|
158 |
|
|
public variable option {} {
|
159 |
|
|
lappend messages "option: $option"
|
160 |
|
|
}
|
161 |
|
|
public variable nocode {}
|
162 |
|
|
public common messages
|
163 |
|
|
}
|
164 |
|
|
} ""
|
165 |
|
|
|
166 |
|
|
test body-3.2 {check syntax of "configbody" command} {
|
167 |
|
|
list [catch "configbody test_cbody::option" msg] $msg
|
168 |
|
|
} {1 {wrong # args: should be "configbody class::option body"}}
|
169 |
|
|
|
170 |
|
|
test body-3.3 {make sure that members are found correctly} {
|
171 |
|
|
list [catch "configbody test_cbody::xyzzy {}" msg] $msg
|
172 |
|
|
} {1 {option "xyzzy" is not defined in class "::test_cbody"}}
|
173 |
|
|
|
174 |
|
|
test body-3.4 {private variables have no config code} {
|
175 |
|
|
list [catch "configbody test_cbody::priv {bogus}" msg] $msg
|
176 |
|
|
} {1 {option "::test_cbody::priv" is not a public configuration option}}
|
177 |
|
|
|
178 |
|
|
test body-3.5 {protected variables have no config code} {
|
179 |
|
|
list [catch "configbody test_cbody::prot {bogus}" msg] $msg
|
180 |
|
|
} {1 {option "::test_cbody::prot" is not a public configuration option}}
|
181 |
|
|
|
182 |
|
|
test body-3.6 {can use public variables without a body} {
|
183 |
|
|
test_cbody #auto
|
184 |
|
|
list [catch "test_cbody0 configure -nocode 1" msg] $msg
|
185 |
|
|
} {0 {}}
|
186 |
|
|
|
187 |
|
|
test body-3.7 {redefined body really does change} {
|
188 |
|
|
list [test_cbody0 configure -option "hello"] \
|
189 |
|
|
[configbody test_cbody::option {lappend messages "new: $option"}] \
|
190 |
|
|
[test_cbody0 configure -option "goodbye"] \
|
191 |
|
|
[set test_cbody::messages] \
|
192 |
|
|
} {{} {} {} {{option: hello} {new: goodbye}}}
|
193 |
|
|
|
194 |
|
|
# ----------------------------------------------------------------------
|
195 |
|
|
# Test "configbody" command with inheritance
|
196 |
|
|
# ----------------------------------------------------------------------
|
197 |
|
|
test body-4.1 {inherit from a class with missing config bodies} {
|
198 |
|
|
class test_icbody {
|
199 |
|
|
inherit test_cbody
|
200 |
|
|
public variable option "icbody"
|
201 |
|
|
}
|
202 |
|
|
test_icbody #auto
|
203 |
|
|
} {test_icbody0}
|
204 |
|
|
|
205 |
|
|
test body-4.2 {redefine a body in a derived class} {
|
206 |
|
|
configbody test_icbody::option {lappend messages "test_icbody: $option"}
|
207 |
|
|
list [test_icbody0 info variable option] \
|
208 |
|
|
[test_icbody0 info variable test_cbody::option]
|
209 |
|
|
} {{public variable ::test_icbody::option icbody {lappend messages "test_icbody: $option"} icbody} {public variable ::test_cbody::option {} {lappend messages "new: $option"} {}}}
|
210 |
|
|
|
211 |
|
|
test body-4.3 {try to redefine a body for a variable that was not declared} {
|
212 |
|
|
list [catch {configbody test_icbody::nocode {return "new"}} msg] $msg
|
213 |
|
|
} {1 {option "nocode" is not defined in class "::test_icbody"}}
|
214 |
|
|
|
215 |
|
|
# ----------------------------------------------------------------------
|
216 |
|
|
# Clean up
|
217 |
|
|
# ----------------------------------------------------------------------
|
218 |
|
|
delete class test_body test_cbody
|