summaryrefslogtreecommitdiff
path: root/itcl/itcl/tests/info.test
blob: 628d890a2848a12c008426bb5be2ecab7d546277 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#
# Tests for information accessed by the "info" command
# ----------------------------------------------------------------------
#   AUTHOR:  Michael J. McLennan
#            Bell Labs Innovations for Lucent Technologies
#            mmclennan@lucent.com
#            http://www.tcltk.com/itcl
#
#      RCS:  $Id$
# ----------------------------------------------------------------------
#            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
namespace import -force ::tcltest::*

if {[string compare test [info procs test]] == 1} then {source defs}

package require Itcl

# ----------------------------------------------------------------------
#  Class definition with one of everything
# ----------------------------------------------------------------------
test info-1.1 {define a simple class} {
    itcl::class test_info_base {
        method base {} {return "default"}
        variable base {}

        method do {args} {eval $args}
    }
    itcl::class test_info {
        inherit test_info_base

        constructor {args} {
            foreach v [info variable] {
                catch {set $v "new-[set $v]"}
            }
        }
        destructor {}

        method defm {} {return "default method"}
        public method pubm {x} {return "public method"}
        protected method prom {x y} {return "protected method"}
        private method prim {x y z} {return "private method"}

        proc defp {} {return "default proc"}
        public proc pubp {x} {return "public proc"}
        protected proc prop {x y} {return "protected proc"}
        private proc prip {x y z} {return "private proc"}

        variable defv "default"
        public variable pubv "public" {set pubv "public: $pubv"}
        protected variable prov "protected"
        private variable priv "private"

        common defc "default"
        public common pubc "public"
        protected common proc "protected"
        private common pric "private"

        method uninitm
        proc uninitp {x y}
        variable uninitv
        common uninitc
        set uninitc(0) zero
        set uninitc(1) one
    }
} ""

test info-1.2 {info: errors trigger usage info} {
    list [catch {namespace eval test_info {info}} msg] $msg
} {1 {wrong # args: should be one of...
  info args procname
  info body procname
  info class
  info function ?name? ?-protection? ?-type? ?-name? ?-args? ?-body?
  info heritage
  info inherit
  info variable ?name? ?-protection? ?-type? ?-name? ?-init? ?-value? ?-config?
...and others described on the man page}}

test basic-1.3 {info: errors trigger usage info} {
    test_info ti
    list [catch {ti info} msg] $msg
} {1 {wrong # args: should be one of...
  info args procname
  info body procname
  info class
  info function ?name? ?-protection? ?-type? ?-name? ?-args? ?-body?
  info heritage
  info inherit
  info variable ?name? ?-protection? ?-type? ?-name? ?-init? ?-value? ?-config?
...and others described on the man page}}

test info-1.4 {info: info class works on class itself} {
    namespace eval test_info { info class }
} {::test_info}

# ----------------------------------------------------------------------
#  Data members
# ----------------------------------------------------------------------
test info-2.1 {info: all variables} {
    lsort [ti info variable]
} {::test_info::defc ::test_info::defv ::test_info::pric ::test_info::priv ::test_info::proc ::test_info::prov ::test_info::pubc ::test_info::pubv ::test_info::this ::test_info::uninitc ::test_info::uninitv ::test_info_base::base}

test info-2.2a {info: public variables} {
    ti info variable pubv
} {public variable ::test_info::pubv public {set pubv "public: $pubv"} new-public}

test info-2.2b {info: public variables} {
    list [ti info variable pubv -protection] \
         [ti info variable pubv -type] \
         [ti info variable pubv -name] \
         [ti info variable pubv -init] \
         [ti info variable pubv -config] \
         [ti info variable pubv -value] \
} {public variable ::test_info::pubv public {set pubv "public: $pubv"} new-public}

test info-2.3a {info: protected variables} {
    ti info variable prov
} {protected variable ::test_info::prov protected new-protected}

test info-2.3b {info: protected variables} {
    list [ti info variable prov -protection] \
         [ti info variable prov -type] \
         [ti info variable prov -name] \
         [ti info variable prov -init] \
         [ti info variable prov -value] \
} {protected variable ::test_info::prov protected new-protected}

test info-2.4a {info: private variables} {
    ti info variable priv
} {private variable ::test_info::priv private new-private}

test info-2.4b {info: private variables} {
    list [ti info variable priv -protection] \
         [ti info variable priv -type] \
         [ti info variable priv -name] \
         [ti info variable priv -init] \
         [ti info variable priv -value] \
} {private variable ::test_info::priv private new-private}

test info-2.5 {"this" variable is built in} {
    ti info variable this
} {protected variable ::test_info::this ::ti ::ti}

test info-2.6 {info: protected/private variables have no "config" code} {
    list [ti info variable prov -config] [ti info variable priv -config]
} {{} {}}

test info-2.7 {by default, variables are "protected"} {
    ti info variable defv
} {protected variable ::test_info::defv default new-default}

test info-2.8 {data members may be uninitialized} {
    ti info variable uninitv
} {protected variable ::test_info::uninitv <undefined> <undefined>}

test info-2.9a {info: public common variables} {
    ti info variable pubc
} {public common ::test_info::pubc public new-public}

test info-2.9b {info: public common variables} {
    list [ti info variable pubc -protection] \
         [ti info variable pubc -type] \
         [ti info variable pubc -name] \
         [ti info variable pubc -init] \
         [ti info variable pubc -value] \
} {public common ::test_info::pubc public new-public}

test info-2.10a {info: protected common variables} {
    ti info variable proc
} {protected common ::test_info::proc protected new-protected}

test info-2.10b {info: protected common variables} {
    list [ti info variable proc -protection] \
         [ti info variable proc -type] \
         [ti info variable proc -name] \
         [ti info variable proc -init] \
         [ti info variable proc -value] \
} {protected common ::test_info::proc protected new-protected}

test info-2.11a {info: private common variables} {
    ti info variable pric
} {private common ::test_info::pric private new-private}

test info-2.11b {info: private common variables} {
    list [ti info variable pric -protection] \
         [ti info variable pric -type] \
         [ti info variable pric -name] \
         [ti info variable pric -init] \
         [ti info variable pric -value] \
} {private common ::test_info::pric private new-private}

test info-2.12 {info: public/protected/private vars have no "config" code} {
    list [ti info variable pubc -config] \
         [ti info variable proc -config] \
         [ti info variable pric -config]
} {{} {} {}}

test info-2.13 {by default, variables are "protected"} {
    ti info variable defc
} {protected common ::test_info::defc default new-default}

test info-2.14 {data members may be uninitialized} {
    ti info variable uninitc
} {protected common ::test_info::uninitc <undefined> <undefined>}

test info-2.15 {common vars can be initialized within class definition} {
    list [namespace eval test_info {lsort [array names uninitc]}] \
         [namespace eval test_info {set uninitc(0)}] \
         [namespace eval test_info {set uninitc(1)}]
} {{0 1} zero one}

test info-2.16 {flag syntax errors} {
    list [catch {ti info variable defv -xyzzy} msg] $msg
} {1 {bad option "-xyzzy": must be -config, -init, -name, -protection, -type, or -value}}

# ----------------------------------------------------------------------
#  Member functions
# ----------------------------------------------------------------------
test basic-3.1 {info: all functions} {
    lsort [ti info function]
} {::test_info::constructor ::test_info::defm ::test_info::defp ::test_info::destructor ::test_info::prim ::test_info::prip ::test_info::prom ::test_info::prop ::test_info::pubm ::test_info::pubp ::test_info::uninitm ::test_info::uninitp ::test_info_base::base ::test_info_base::cget ::test_info_base::configure ::test_info_base::do ::test_info_base::isa}

test info-3.2a {info: public methods} {
    ti info function pubm
} {public method ::test_info::pubm x {return "public method"}}

test info-3.2b {info: public methods} {
    list [ti info function pubm -protection] \
         [ti info function pubm -type] \
         [ti info function pubm -name] \
         [ti info function pubm -args] \
         [ti info function pubm -body]
} {public method ::test_info::pubm x {return "public method"}}

test info-3.3a {info: protected methods} {
    ti info function prom
} {protected method ::test_info::prom {x y} {return "protected method"}}

test info-3.3b {info: protected methods} {
    list [ti info function prom -protection] \
         [ti info function prom -type] \
         [ti info function prom -name] \
         [ti info function prom -args] \
         [ti info function prom -body]
} {protected method ::test_info::prom {x y} {return "protected method"}}

test info-3.4a {info: private methods} {
    ti info function prim
} {private method ::test_info::prim {x y z} {return "private method"}}

test info-3.4b {info: private methods} {
    list [ti info function prim -protection] \
         [ti info function prim -type] \
         [ti info function prim -name] \
         [ti info function prim -args] \
         [ti info function prim -body]
} {private method ::test_info::prim {x y z} {return "private method"}}

test info-3.5 {"configure" function is built in} {
    ti info function configure
} {public method ::test_info_base::configure {?-option? ?value -option value...?} @itcl-builtin-configure}

test info-3.6 {by default, methods are "public"} {
    ti info function defm
} {public method ::test_info::defm {} {return "default method"}}

test info-3.7 {methods may not have arg lists or bodies defined} {
    ti info function uninitm
} {public method ::test_info::uninitm <undefined> <undefined>}

test info-3.8a {info: public procs} {
    ti info function pubp
} {public proc ::test_info::pubp x {return "public proc"}}

test info-3.8b {info: public procs} {
    list [ti info function pubp -protection] \
         [ti info function pubp -type] \
         [ti info function pubp -name] \
         [ti info function pubp -args] \
         [ti info function pubp -body]
} {public proc ::test_info::pubp x {return "public proc"}}

test info-3.9a {info: protected procs} {
    ti info function prop
} {protected proc ::test_info::prop {x y} {return "protected proc"}}

test info-3.9b {info: protected procs} {
    list [ti info function prop -protection] \
         [ti info function prop -type] \
         [ti info function prop -name] \
         [ti info function prop -args] \
         [ti info function prop -body]
} {protected proc ::test_info::prop {x y} {return "protected proc"}}

test info-3.10a {info: private procs} {
    ti info function prip
} {private proc ::test_info::prip {x y z} {return "private proc"}}

test info-3.10b {info: private procs} {
    list [ti info function prip -protection] \
         [ti info function prip -type] \
         [ti info function prip -name] \
         [ti info function prip -args] \
         [ti info function prip -body]
} {private proc ::test_info::prip {x y z} {return "private proc"}}

test info-3.11 {by default, procs are "public"} {
    ti info function defp
} {public proc ::test_info::defp {} {return "default proc"}}

test info-3.12 {procs may not have arg lists or bodies defined} {
    ti info function uninitp
} {public proc ::test_info::uninitp {x y} <undefined>}

test info-3.13 {flag syntax errors} {
    list [catch {ti info function defm -xyzzy} msg] $msg
} {1 {bad option "-xyzzy": must be -args, -body, -name, -protection, or -type}}

# ----------------------------------------------------------------------
#  Other object-related queries
# ----------------------------------------------------------------------

test info-4.1a {query class (wrong # args)} {
    list [catch {ti info class x} result] $result
} {1 {wrong # args: should be "info class"}}

test info-4.1b {query most-specific class} {
    list [ti info class] [ti do info class]
} {::test_info ::test_info}

test info-4.2a {query inheritance info (wrong # args)} {
    list [catch {ti info inherit x} result] $result
} {1 {wrong # args: should be "info inherit"}}

test info-4.2b {query inheritance info} {
    list [ti info inherit] [ti do info inherit]
} {::test_info_base {}}

test info-4.3a {query heritage info (wrong # args)} {
    list [catch {ti info heritage x} result] $result
} {1 {wrong # args: should be "info heritage"}}

test info-4.3b {query heritage info} {
    list [ti info heritage] [ti do info heritage]
} {{::test_info ::test_info_base} ::test_info_base}

test info-4.4a {query argument list (wrong # args)} {
    list [catch {ti info args} result] $result \
         [catch {ti info args x y} result] $result
} {1 {wrong # args: should be "info args function"} 1 {wrong # args: should be "info args function"}}

test info-4.4b {query argument list} {
    ti info args prim
} {x y z}

test info-4.4c {query argument list (undefined)} {
    ti info args uninitm
} {<undefined>}

test info-4.5a {query body (wrong # args)} {
    list [catch {ti info body} result] $result \
         [catch {ti info body x y} result] $result
} {1 {wrong # args: should be "info body function"} 1 {wrong # args: should be "info body function"}}

test info-4.5b {query body} {
    ti info body prim
} {return "private method"}

test info-4.5c {query body (undefined)} {
    ti info body uninitm
} {<undefined>}

# ----------------------------------------------------------------------
#  Other parts of the usual "info" command
# ----------------------------------------------------------------------

test info-5.1 {info vars} {
    ti do info vars
} {args}

test info-5.2 {info exists} {
    list [ti do info exists args] [ti do info exists xyzzy]
} {1 0}

# ----------------------------------------------------------------------
#  Clean up
# ----------------------------------------------------------------------
itcl::delete class test_info test_info_base

::tcltest::cleanupTests
return