2 # Tests for "body" and "configbody" commands
3 # ----------------------------------------------------------------------
4 # AUTHOR: Michael J. McLennan
5 # Bell Labs Innovations for Lucent Technologies
7 # http://www.tcltk.com/itcl
8 # ----------------------------------------------------------------------
9 # Copyright (c) 1993-1998 Lucent Technologies, Inc.
10 # ======================================================================
11 # See the file "license.terms" for information on usage and
12 # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 package require tcltest 2.1
15 namespace import ::tcltest::test
16 ::tcltest::loadTestedCommands
19 # ----------------------------------------------------------------------
21 # ----------------------------------------------------------------------
22 test body-1.1 {define a class with missing bodies and arg lists} {
23 itcl::class test_body {
31 method defvals {x {y 0} {z 1}}
32 method varargs {x args}
34 method override {mesg} {
35 return "override: $mesg"
40 test body-1.2 {cannot use methods without a body} {
42 list [catch "test_body0 any" msg] $msg
43 } {1 {member function "::test_body::any" is not defined and cannot be autoloaded}}
45 test body-1.3 {check syntax of "body" command} {
46 list [catch "itcl::body test_body::any" msg] $msg
47 } {1 {wrong # args: should be "itcl::body class::func arglist body"}}
49 test body-1.4 {make sure members are found correctly} {
50 list [catch "itcl::body test_body::xyzzyxyzzyxyzzy {} {}" msg] $msg
51 } {1 {function "xyzzyxyzzyxyzzy" is not defined in class "::test_body"}}
53 test body-1.5a {members without an argument list can have any args} {
54 itcl::body test_body::any {} {return "any"}
55 list [catch "test_body0 any" msg] $msg
58 test body-1.5b {members without an argument list can have any args} {
59 itcl::body test_body::any {x} {return "any: $x"}
60 list [catch "test_body0 any 1" msg] $msg
63 test body-1.5c {members without an argument list can have any args} {
64 itcl::body test_body::any {x {y 2}} {return "any: $x $y"}
65 list [catch "test_body0 any 1" msg] $msg
68 test body-1.6a {an empty argument list must stay empty} {
69 list [catch {itcl::body test_body::zero {x y} {return "zero: $x $y"}} msg] $msg
70 } {1 {argument list changed for function "::test_body::zero": should be ""}}
72 test body-1.6b {an empty argument list must stay empty} {
73 list [catch {itcl::body test_body::zero {} {return "zero"}} msg] $msg
76 test body-1.7a {preserve argument list: fixed arguments} {
77 list [catch {itcl::body test_body::one {x y} {return "one: $x $y"}} msg] $msg
78 } {1 {argument list changed for function "::test_body::one": should be "x"}}
80 test body-1.7b {preserve argument list: fixed arguments} {
81 list [catch {itcl::body test_body::one {a} {return "one: $a"}} msg] $msg
84 test body-1.7c {preserve argument list: fixed arguments} {
85 list [catch "test_body0 one 1.0" msg] $msg
88 test body-1.8a {preserve argument list: fixed arguments} {
89 list [catch {itcl::body test_body::two {x} {return "two: $x"}} msg] $msg
90 } {1 {argument list changed for function "::test_body::two": should be "x y"}}
92 test body-1.8b {preserve argument list: fixed arguments} {
93 list [catch {itcl::body test_body::two {a b} {return "two: $a $b"}} msg] $msg
96 test body-1.8c {preserve argument list: fixed arguments} {
97 list [catch "test_body0 two 2.0 3.0" msg] $msg
100 test body-1.9a {preserve argument list: default arguments} {
101 list [catch {itcl::body test_body::defvals {x} {}} msg] $msg
102 } {1 {argument list changed for function "::test_body::defvals": should be "x {y 0} {z 1}"}}
104 test body-1.9b {preserve argument list: default arguments} {
105 list [catch {itcl::body test_body::defvals {a {b 0} {c 2}} {}} msg] $msg
106 } {1 {argument list changed for function "::test_body::defvals": should be "x {y 0} {z 1}"}}
108 test body-1.9c {preserve argument list: default arguments} {
109 list [catch {itcl::body test_body::defvals {a {b 0} {c 1}} {}} msg] $msg
112 test body-1.10a {preserve argument list: variable arguments} {
113 list [catch {itcl::body test_body::varargs {} {}} msg] $msg
114 } {1 {argument list changed for function "::test_body::varargs": should be "x args"}}
116 test body-1.10b {preserve argument list: variable arguments} {
117 list [catch {itcl::body test_body::varargs {a} {}} msg] $msg
120 test body-1.10c {preserve argument list: variable arguments} {
121 list [catch {itcl::body test_body::varargs {a b c} {}} msg] $msg
124 test body-1.11 {redefined body really does change} {
125 list [test_body0 override "test #1"] \
126 [itcl::body test_body::override {text} {return "new: $text"}] \
127 [test_body0 override "test #2"]
128 } {{override: test #1} {} {new: test #2}}
131 # ----------------------------------------------------------------------
132 # Test "body" command with inheritance
133 # ----------------------------------------------------------------------
134 test body-2.1 {inherit from a class with missing bodies} {
135 itcl::class test_ibody {
142 test body-2.2 {redefine a method in a derived class} {
143 itcl::body test_ibody::zero {} {return "ibody zero"}
144 list [test_ibody0 info function zero] \
145 [test_ibody0 info function test_body::zero]
146 } {{public method ::test_ibody::zero {} {return "ibody zero"}} {public method ::test_body::zero {} {return "zero"}}}
148 test body-2.3 {try to redefine a method that was not declared} {
149 list [catch {itcl::body test_ibody::one {x} {return "new"}} msg] $msg
150 } {1 {function "one" is not defined in class "::test_ibody"}}
152 ::itcl::delete class test_body
154 # ----------------------------------------------------------------------
155 # Test "configbody" command
156 # ----------------------------------------------------------------------
157 test body-3.1 {define a class with public variables} {
158 itcl::class test_cbody {
159 private variable priv
160 protected variable prot
162 public variable option {} {
163 lappend messages "option: $option"
165 public variable nocode {}
166 public common messages
170 test body-3.2 {check syntax of "configbody" command} {
171 list [catch "itcl::configbody test_cbody::option" msg] $msg
172 } {1 {wrong # args: should be "itcl::configbody class::option body"}}
174 test body-3.3 {make sure that members are found correctly} {
175 list [catch "itcl::configbody test_cbody::xyzzy {}" msg] $msg
176 } {1 {option "xyzzy" is not defined in class "::test_cbody"}}
178 test body-3.4 {private variables have no config code} {
179 list [catch "itcl::configbody test_cbody::priv {bogus}" msg] $msg
180 } {1 {option "::test_cbody::priv" is not a public configuration option}}
182 test body-3.5 {protected variables have no config code} {
183 list [catch "itcl::configbody test_cbody::prot {bogus}" msg] $msg
184 } {1 {option "::test_cbody::prot" is not a public configuration option}}
186 test body-3.6 {can use public variables without a body} {
188 list [catch "test_cbody0 configure -nocode 1" msg] $msg
191 test body-3.7 {redefined body really does change} {
192 list [test_cbody0 configure -option "hello"] \
193 [itcl::configbody test_cbody::option {lappend messages "new: $option"}] \
194 [test_cbody0 configure -option "goodbye"] \
195 [set test_cbody::messages] \
196 } {{} {} {} {{option: hello} {new: goodbye}}}
198 # ----------------------------------------------------------------------
199 # Test "configbody" command with inheritance
200 # ----------------------------------------------------------------------
201 test body-4.1 {inherit from a class with missing config bodies} {
202 itcl::class test_icbody {
204 public variable option "icbody"
209 test body-4.2 {redefine a body in a derived class} {
210 itcl::configbody test_icbody::option {lappend messages "test_icbody: $option"}
211 list [test_icbody0 info variable option] \
212 [test_icbody0 info variable test_cbody::option]
213 } {{public variable ::test_icbody::option icbody {lappend messages "test_icbody: $option"} icbody} {public variable ::test_cbody::option {} {lappend messages "new: $option"} {}}}
215 test body-4.3 {try to redefine a body for a variable that was not declared} {
216 list [catch {itcl::configbody test_icbody::nocode {return "new"}} msg] $msg
217 } {1 {option "nocode" is not defined in class "::test_icbody"}}
219 test body-5.1 {redefine constructors} -setup {
220 unset -nocomplain answer
221 itcl::class B {constructor {} {lappend ::answer B}}
222 itcl::class D {inherit B; constructor {} {lappend ::answer A}}
225 itcl::body D::constructor {} {lappend ::answer D}
230 unset -nocomplain answer
233 test body-6.1 {redefine class proc body} -setup {
234 unset -nocomplain ::answer
238 itcl::body ::C::cheshire {} {}
240 constructor {args} {cheshire}
243 itcl::delete object [C #auto]
244 itcl::delete object [C #auto]
245 itcl::delete object [C #auto]
249 unset -nocomplain ::answer
252 # ----------------------------------------------------------------------
254 # ----------------------------------------------------------------------
256 itcl::delete class test_cbody
258 ::tcltest::cleanupTests