blob: 3aa7a037937df493216c9a1a04392bf3d2837e60 (
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
|
#
# Tests for general class handling
# ----------------------------------------------------------------------
# AUTHOR: Wolfgang Großer, Arnulf Wiedemann
# wolfgang@grosser-erding.de, arnulf@wiedemann-pri.de
# ----------------------------------------------------------------------
# Copyright (c) Wolfgang Großer, Arnulf Wiedemann
# ======================================================================
# 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 protection with inheritance
# ----------------------------------------------------------------------
test general1-1.1 {define classes with different protection} {
variable ::test_cd_watch ""
itcl::class ClassA {
private variable priv privA
private variable privA privAA
protected variable prov provA
public variable pubv pubvA
constructor {args} {
lappend ::test_cd_watch constructorA
}
private method primA {} {
lappend ::test_cd_watch primA
set privA Hallo
lappend ::test_cd_watch [set priv]
}
protected method promA {} {
lappend ::test_cd_watch promA
lappend ::test_cd_watch [set prov]
}
public method pubmA {} {
lappend ::test_cd_watch pubmA
lappend ::test_cd_watch [set pubv]
}
public method doA {args} {eval $args}
}
itcl::class ClassB {
inherit ClassA
private variable priv privB
private variable privB privBB
protected variable prov provB
public variable pubv pubvB
constructor {args} {
lappend ::test_cd_watch [list constructorB $args]
}
destructor {
lappend ::test_cd_watch destructorB
}
private method primB {} {
lappend ::test_cd_watch primB
lappend ::test_cd_watch [set priv]
}
protected method promB {} {
lappend ::test_cd_watch promB
lappend ::test_cd_watch [set prov]
}
public method pubmB {} {
lappend ::test_cd_watch pubmB
lappend ::test_cd_watch [set pubv]
}
public method doB {args} {eval $args}
public method chkThis {} { set prov $this }
}
itcl::class ClassC {
inherit ClassB
private variable priv privC
protected variable prov provC
public variable pubv pubvC
constructor {args} {
eval ClassB::constructor $args
} {
lappend ::test_cd_watch [list "start constructorC" $args]
ClassA::constructor $args
lappend ::test_cd_watch [list "end constructorC"]
}
private method primC {} {
lappend ::test_cd_watch primC
lappend ::test_cd_watch [set priv]
}
protected method promC {} {
lappend ::test_cd_watch promC
lappend ::test_cd_watch [set prov]
}
public method pubmC {} {
lappend ::test_cd_watch pubmC
lappend ::test_cd_watch [set pubv]
$this primC
}
public method pubmC2 {arg1 {arg2 {}} {arg3 xxx}} {
lappend ::test_cd_watch "orig pubmC2"
}
public method doC {args} {
eval $args
}
}
} {}
test general1-1.2 {constructor of classA should be called twice} {
set ::test_cd_watch ""
list [ClassC #auto] [set ::test_cd_watch]
} {classC0 {constructorA {constructorB {}} {{start constructorC} {}} constructorA {{end constructorC}}}}
test general1-1.3 {body command should not produce error} {
set ::test_cd_watch ""
list [catch {
itcl::body ClassC::pubmC2 {aarg1 {aarg2 {}} {arg3 {xxx}}} {
lappend ::test_cd_watch "new body command for pubmC2 [list $aarg1 $aarg2 $arg3]"
}
} msg] $msg [classC0 pubmC2 Hallo]
} {0 {} {{new body command for pubmC2 Hallo {} xxx}}}
test general1-1.4 {call of configure} {
set ::test_cd_watch ""
list [lsort [classC0 configure]]
} {{{-ClassA::pubv pubvA pubvA} {-ClassB::pubv pubvB pubvB} {-pubv pubvC pubvC}}}
test general1-1.5 {call of configure with variable} {
set ::test_cd_watch ""
list [classC0 configure -pubv Arnulf]
} {{}}
test general1-1.6 {call of configure to check for changes} {
set ::test_cd_watch ""
list [lsort [classC0 configure]]
} {{{-ClassA::pubv pubvA pubvA} {-ClassB::pubv pubvB pubvB} {-pubv pubvC Arnulf}}}
test general1-1.7 {call of cget} {
set ::test_cd_watch ""
list [classC0 cget -pubv]
} {Arnulf}
test general1-1.8 {private method may not be called} {
set ::test_cd_watch ""
list [catch {classC0 primC} msg] $msg
} {1 {bad option "primC": should be one of...
classC0 cget -option
classC0 chkThis
classC0 configure ?-option? ?value -option value...?
classC0 doA ?arg arg ...?
classC0 doB ?arg arg ...?
classC0 doC ?arg arg ...?
classC0 isa className
classC0 pubmA
classC0 pubmB
classC0 pubmC
classC0 pubmC2 aarg1 ?aarg2? ?arg3?}}
test general1-1.9 {protected method may not be called} {
set ::test_cd_watch ""
list [catch {classC0 promC} msg] $msg
} {1 {bad option "promC": should be one of...
classC0 cget -option
classC0 chkThis
classC0 configure ?-option? ?value -option value...?
classC0 doA ?arg arg ...?
classC0 doB ?arg arg ...?
classC0 doC ?arg arg ...?
classC0 isa className
classC0 pubmA
classC0 pubmB
classC0 pubmC
classC0 pubmC2 aarg1 ?aarg2? ?arg3?}}
test general1-1.10 {can call private and protected methods from within the class} {
set ::test_cd_watch ""
list [catch {classC0 doC primC} msg] $msg [catch {classC0 doC promC} msg] $msg
} {0 {primC privC} 0 {primC privC promC provC}}
test general1-1.11 {*cannot* call private methods of inherited classes} {
set ::test_cd_watch ""
list [catch {classC0 doC primB} msg] $msg [catch {classC0 doC primA} msg] $msg
} {1 {invalid command name "primB"} 1 {invalid command name "primA"}}
test general1-1.12 {can call protected and public methods of inherited classes} {
set ::test_cd_watch ""
list [catch {classC0 doC promB} msg] $msg [catch {classC0 doC pubmC} msg] $msg [catch {classC0 doC promA} msg] $msg [catch {classC0 doC pubmA} msg] $msg
} {0 {promB provB} 0 {promB provB pubmC Arnulf primC privC} 0 {promB provB pubmC Arnulf primC privC promA provA} 0 {promB provB pubmC Arnulf primC privC promA provA pubmA pubvA}}
test general1-1.13 {"this" variable} {
set ::test_cd_watch ""
list [catch {classC0 doC doB set $this} msg] $msg
} {1 {can't read "this": no such variable}}
test general1-1.14 {can indirect calls through middle class} {
set ::test_cd_watch ""
list [catch {classC0 doC doB doA primA} msg] $msg [catch {classC0 doC doB doA promA} msg] $msg [catch {classC0 doC doB doA pubmA} msg] $msg
} {0 {primA privA} 0 {primA privA promA provA} 0 {primA privA promA provA pubmA pubvA}}
test general1-1.15 {*cannot* indirect private calls through middle class} {
set ::test_cd_watch ""
list [catch {classC0 doC doB primA} msg] $msg [catch {classC0 doC doB primC} msg] $msg
} {1 {invalid command name "primA"} 1 {invalid command name "primC"}}
test general1-1.16 {*cannot* indirect protected calls through middle class} {
set ::test_cd_watch ""
list [catch {classC0 doC doB promA} msg] $msg [catch {classC0 doC doB promC} msg] $msg
} {0 {promA provA} 1 {invalid command name "promC"}}
test general1-1.17 {access variables through calls through middle class} {
set ::test_cd_watch ""
list [catch {classC0 doC doB set privB} msg] $msg [catch {classC0 doC doB doA set pubv} msg] $msg
} {0 privBB 0 pubvA}
test general1-1.18 {"this" variable} {
set ::test_cd_watch ""
list [catch {classC0 doB set prov $this} msg] $msg \
[catch {classC0 chkThis} msg] $msg
} {1 {can't read "this": no such variable} 0 ::classC0}
test general1-1.20 {*cannot* read private variable from inherited class} {
set ::test_cd_watch ""
list [catch {classC0 doC set privA} msg] $msg [catch {classC0 doA set privA} msg] $msg [catch {classC0 doC set privB} msg] $msg [catch {classC0 doB set privB} msg] $msg
} {1 {can't read "privA": no such variable} 0 Hallo 1 {can't read "privB": no such variable} 0 privBB}
if {0} {
c publicC
}
::itcl::delete class ClassA
::tcltest::cleanupTests
return
|