OSDN Git Service

Please enter the commit message for your changes. Lines starting
[eos/base.git] / util / src / TclTk / tcl8.6.12 / pkgs / itcl4.2.2 / tests / body.test
1 #
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 #            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.
13
14 package require tcltest 2.1
15 namespace import ::tcltest::test
16 ::tcltest::loadTestedCommands
17 package require itcl
18
19 # ----------------------------------------------------------------------
20 #  Test "body" command
21 # ----------------------------------------------------------------------
22 test body-1.1 {define a class with missing bodies and arg lists} {
23     itcl::class test_body {
24         constructor {args} {}
25         destructor {}
26
27         method any
28         method zero {}
29         method one {x}
30         method two {x y}
31         method defvals {x {y 0} {z 1}}
32         method varargs {x args}
33
34         method override {mesg} {
35             return "override: $mesg"
36         }
37     }
38 } ""
39
40 test body-1.2 {cannot use methods without a body} {
41     test_body #auto
42     list [catch "test_body0 any" msg] $msg
43 } {1 {member function "::test_body::any" is not defined and cannot be autoloaded}}
44
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"}}
48
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"}}
52
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
56 } {0 any}
57
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
61 } {0 {any: 1}}
62
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
66 } {0 {any: 1 2}}
67
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 ""}}
71
72 test body-1.6b {an empty argument list must stay empty} {
73     list [catch {itcl::body test_body::zero {} {return "zero"}} msg] $msg
74 } {0 {}}
75
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"}}
79
80 test body-1.7b {preserve argument list:  fixed arguments} {
81     list [catch {itcl::body test_body::one {a} {return "one: $a"}} msg] $msg
82 } {0 {}}
83
84 test body-1.7c {preserve argument list:  fixed arguments} {
85     list [catch "test_body0 one 1.0" msg] $msg
86 } {0 {one: 1.0}}
87
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"}}
91
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
94 } {0 {}}
95
96 test body-1.8c {preserve argument list:  fixed arguments} {
97     list [catch "test_body0 two 2.0 3.0" msg] $msg
98 } {0 {two: 2.0 3.0}}
99
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}"}}
103
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}"}}
107
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
110 } {0 {}}
111
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"}}
115
116 test body-1.10b {preserve argument list:  variable arguments} {
117     list [catch {itcl::body test_body::varargs {a} {}} msg] $msg
118 } {0 {}}
119
120 test body-1.10c {preserve argument list:  variable arguments} {
121     list [catch {itcl::body test_body::varargs {a b c} {}} msg] $msg
122 } {0 {}}
123
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}}
129
130
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 {
136         inherit test_body
137         method zero {}
138     }
139     test_ibody #auto
140 } {test_ibody0}
141
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"}}}
147
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"}}
151
152 ::itcl::delete class test_body
153
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
161
162         public variable option {} {
163             lappend messages "option: $option"
164         }
165         public variable nocode {}
166         public common messages
167     }
168 } ""
169
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"}}
173
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"}}
177
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}}
181
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}}
185
186 test body-3.6 {can use public variables without a body} {
187     test_cbody #auto
188     list [catch "test_cbody0 configure -nocode 1" msg] $msg
189 } {0 {}}
190
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}}}
197
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 {
203         inherit test_cbody
204         public variable option "icbody"
205     }
206     test_icbody #auto
207 } {test_icbody0}
208
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"} {}}}
214
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"}}
218
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}}
223 } -body {
224     D d1
225     itcl::body D::constructor {} {lappend ::answer D}
226     D d2
227     set ::answer
228 } -cleanup {
229     itcl::delete class B
230     unset -nocomplain answer
231 } -result {B A B D}
232
233 test body-6.1 {redefine class proc body} -setup {
234     unset -nocomplain ::answer
235     itcl::class C {
236         proc cheshire {} {
237             lappend ::answer x
238             itcl::body ::C::cheshire {} {}
239         }
240         constructor {args} {cheshire}
241     }
242 } -body {
243     itcl::delete object [C #auto]
244     itcl::delete object [C #auto]
245     itcl::delete object [C #auto]
246     set ::answer
247 } -cleanup {
248     itcl::delete class C
249     unset -nocomplain ::answer
250 } -result x
251
252 # ----------------------------------------------------------------------
253 #  Clean up
254 # ----------------------------------------------------------------------
255
256 itcl::delete class test_cbody
257
258 ::tcltest::cleanupTests
259 return