summaryrefslogtreecommitdiffstats
path: root/tcl8.6/pkgs/itcl4.1.2/tests/body.test
blob: cf31f5022f614426ffda7c0364e9f9888e6b04d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#
# Tests for "body" and "configbody" commands
# ----------------------------------------------------------------------
#   AUTHOR:  Michael J. McLennan
#            Bell Labs Innovations for Lucent Technologies
#            mmclennan@lucent.com
#            http://www.tcltk.com/itcl
# ----------------------------------------------------------------------
#            Copyright (c) 1993-1998  Lucent Technologies, Inc.
# ======================================================================
# See the file "license.terms" for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.

package require tcltest 2.1
namespace import ::tcltest::test
::tcltest::loadTestedCommands
package require itcl

# ----------------------------------------------------------------------
#  Test "body" command
# ----------------------------------------------------------------------
test body-1.1 {define a class with missing bodies and arg lists} {
    itcl::class test_body {
        constructor {args} {}
        destructor {}

        method any
        method zero {}
        method one {x}
        method two {x y}
        method defvals {x {y 0} {z 1}}
        method varargs {x args}

        method override {mesg} {
            return "override: $mesg"
        }
    }
} ""

test body-1.2 {cannot use methods without a body} {
    test_body #auto
    list [catch "test_body0 any" msg] $msg
} {1 {member function "::test_body::any" is not defined and cannot be autoloaded}}

test body-1.3 {check syntax of "body" command} {
    list [catch "itcl::body test_body::any" msg] $msg
} {1 {wrong # args: should be "itcl::body class::func arglist body"}}

test body-1.4 {make sure members are found correctly} {
    list [catch "itcl::body test_body::xyzzyxyzzyxyzzy {} {}" msg] $msg
} {1 {function "xyzzyxyzzyxyzzy" is not defined in class "::test_body"}}

test body-1.5a {members without an argument list can have any args} {
    itcl::body test_body::any {} {return "any"}
    list [catch "test_body0 any" msg] $msg
} {0 any}

test body-1.5b {members without an argument list can have any args} {
    itcl::body test_body::any {x} {return "any: $x"}
    list [catch "test_body0 any 1" msg] $msg
} {0 {any: 1}}

test body-1.5c {members without an argument list can have any args} {
    itcl::body test_body::any {x {y 2}} {return "any: $x $y"}
    list [catch "test_body0 any 1" msg] $msg
} {0 {any: 1 2}}

test body-1.6a {an empty argument list must stay empty} {
    list [catch {itcl::body test_body::zero {x y} {return "zero: $x $y"}} msg] $msg
} {1 {argument list changed for function "::test_body::zero": should be ""}}

test body-1.6b {an empty argument list must stay empty} {
    list [catch {itcl::body test_body::zero {} {return "zero"}} msg] $msg
} {0 {}}

test body-1.7a {preserve argument list:  fixed arguments} {
    list [catch {itcl::body test_body::one {x y} {return "one: $x $y"}} msg] $msg
} {1 {argument list changed for function "::test_body::one": should be "x"}}

test body-1.7b {preserve argument list:  fixed arguments} {
    list [catch {itcl::body test_body::one {a} {return "one: $a"}} msg] $msg
} {0 {}}

test body-1.7c {preserve argument list:  fixed arguments} {
    list [catch "test_body0 one 1.0" msg] $msg
} {0 {one: 1.0}}

test body-1.8a {preserve argument list:  fixed arguments} {
    list [catch {itcl::body test_body::two {x} {return "two: $x"}} msg] $msg
} {1 {argument list changed for function "::test_body::two": should be "x y"}}

test body-1.8b {preserve argument list:  fixed arguments} {
    list [catch {itcl::body test_body::two {a b} {return "two: $a $b"}} msg] $msg
} {0 {}}

test body-1.8c {preserve argument list:  fixed arguments} {
    list [catch "test_body0 two 2.0 3.0" msg] $msg
} {0 {two: 2.0 3.0}}

test body-1.9a {preserve argument list:  default arguments} {
    list [catch {itcl::body test_body::defvals {x} {}} msg] $msg
} {1 {argument list changed for function "::test_body::defvals": should be "x {y 0} {z 1}"}}

test body-1.9b {preserve argument list:  default arguments} {
    list [catch {itcl::body test_body::defvals {a {b 0} {c 2}} {}} msg] $msg
} {1 {argument list changed for function "::test_body::defvals": should be "x {y 0} {z 1}"}}

test body-1.9c {preserve argument list:  default arguments} {
    list [catch {itcl::body test_body::defvals {a {b 0} {c 1}} {}} msg] $msg
} {0 {}}

test body-1.10a {preserve argument list:  variable arguments} {
    list [catch {itcl::body test_body::varargs {} {}} msg] $msg
} {1 {argument list changed for function "::test_body::varargs": should be "x args"}}

test body-1.10b {preserve argument list:  variable arguments} {
    list [catch {itcl::body test_body::varargs {a} {}} msg] $msg
} {0 {}}

test body-1.10c {preserve argument list:  variable arguments} {
    list [catch {itcl::body test_body::varargs {a b c} {}} msg] $msg
} {0 {}}

test body-1.11 {redefined body really does change} {
    list [test_body0 override "test #1"] \
         [itcl::body test_body::override {text} {return "new: $text"}] \
         [test_body0 override "test #2"]
} {{override: test #1} {} {new: test #2}}


# ----------------------------------------------------------------------
#  Test "body" command with inheritance
# ----------------------------------------------------------------------
test body-2.1 {inherit from a class with missing bodies} {
    itcl::class test_ibody {
        inherit test_body
        method zero {}
    }
    test_ibody #auto
} {test_ibody0}

test body-2.2 {redefine a method in a derived class} {
    itcl::body test_ibody::zero {} {return "ibody zero"}
    list [test_ibody0 info function zero] \
         [test_ibody0 info function test_body::zero]
} {{public method ::test_ibody::zero {} {return "ibody zero"}} {public method ::test_body::zero {} {return "zero"}}}

test body-2.3 {try to redefine a method that was not declared} {
    list [catch {itcl::body test_ibody::one {x} {return "new"}} msg] $msg
} {1 {function "one" is not defined in class "::test_ibody"}}

::itcl::delete class test_body

# ----------------------------------------------------------------------
#  Test "configbody" command
# ----------------------------------------------------------------------
test body-3.1 {define a class with public variables} {
    itcl::class test_cbody {
        private variable priv
        protected variable prot

        public variable option {} {
            lappend messages "option: $option"
        }
        public variable nocode {}
        public common messages
    }
} ""

test body-3.2 {check syntax of "configbody" command} {
    list [catch "itcl::configbody test_cbody::option" msg] $msg
} {1 {wrong # args: should be "itcl::configbody class::option body"}}

test body-3.3 {make sure that members are found correctly} {
    list [catch "itcl::configbody test_cbody::xyzzy {}" msg] $msg
} {1 {option "xyzzy" is not defined in class "::test_cbody"}}

test body-3.4 {private variables have no config code} {
    list [catch "itcl::configbody test_cbody::priv {bogus}" msg] $msg
} {1 {option "::test_cbody::priv" is not a public configuration option}}

test body-3.5 {protected variables have no config code} {
    list [catch "itcl::configbody test_cbody::prot {bogus}" msg] $msg
} {1 {option "::test_cbody::prot" is not a public configuration option}}

test body-3.6 {can use public variables without a body} {
    test_cbody #auto
    list [catch "test_cbody0 configure -nocode 1" msg] $msg
} {0 {}}

test body-3.7 {redefined body really does change} {
    list [test_cbody0 configure -option "hello"] \
         [itcl::configbody test_cbody::option {lappend messages "new: $option"}] \
         [test_cbody0 configure -option "goodbye"] \
         [set test_cbody::messages] \
} {{} {} {} {{option: hello} {new: goodbye}}}

# ----------------------------------------------------------------------
#  Test "configbody" command with inheritance
# ----------------------------------------------------------------------
test body-4.1 {inherit from a class with missing config bodies} {
    itcl::class test_icbody {
        inherit test_cbody
        public variable option "icbody"
    }
    test_icbody #auto
} {test_icbody0}

test body-4.2 {redefine a body in a derived class} {
    itcl::configbody test_icbody::option {lappend messages "test_icbody: $option"}
    list [test_icbody0 info variable option] \
         [test_icbody0 info variable test_cbody::option]
} {{public variable ::test_icbody::option icbody {lappend messages "test_icbody: $option"} icbody} {public variable ::test_cbody::option {} {lappend messages "new: $option"} {}}}

test body-4.3 {try to redefine a body for a variable that was not declared} {
    list [catch {itcl::configbody test_icbody::nocode {return "new"}} msg] $msg
} {1 {option "nocode" is not defined in class "::test_icbody"}}

test body-5.1 {redefine constructors} -setup {
    unset -nocomplain answer
    itcl::class B {constructor {} {lappend ::answer B}}
    itcl::class D {inherit B; constructor {} {lappend ::answer A}}
} -body {
    D d1
    itcl::body D::constructor {} {lappend ::answer D}
    D d2
    set ::answer
} -cleanup {
    itcl::delete class B 
    unset -nocomplain answer
} -result {B A B D}

test body-6.1 {redefine class proc body} -setup {
    unset -nocomplain ::answer
    itcl::class C {
	proc cheshire {} {
	    lappend ::answer x
	    itcl::body ::C::cheshire {} {}
	}
	constructor {args} {cheshire}
    }
} -body {
    itcl::delete object [C #auto]
    itcl::delete object [C #auto]
    itcl::delete object [C #auto]
    set ::answer
} -cleanup {
    itcl::delete class C
    unset -nocomplain ::answer
} -result x

# ----------------------------------------------------------------------
#  Clean up
# ----------------------------------------------------------------------

itcl::delete class test_cbody

::tcltest::cleanupTests
return