Itcl - the [incr Tcl] extension

Artifact [a437c1d8c9]
Login

Artifact a437c1d8c98cbee62009afed6b101381878618088c27284c054be42a880d7282:


     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
   397
   398
   399
   400
   401
   402
   403
   404
   405
   406
   407
   408
   409
   410
   411
   412
   413
   414
   415
   416
   417
   418
   419
   420
   421
   422
   423
   424
   425
   426
   427
   428
   429
   430
   431
   432
   433
   434
   435
   436
   437
   438
   439
   440
   441
   442
   443
   444
   445
   446
   447
   448
   449
   450
   451
   452
   453
   454
   455
   456
   457
   458
   459
   460
   461
   462
   463
   464
   465
   466
   467
   468
   469
   470
   471
   472
   473
   474
   475
   476
   477
   478
   479
   480
   481
   482
   483
   484
   485
   486
   487
   488
   489
   490
   491
   492
   493
   494
   495
   496
   497
   498
   499
   500
   501
   502
   503
   504
   505
   506
   507
   508
   509
   510
   511
   512
   513
   514
   515
   516
   517
   518
   519
   520
   521
   522
   523
   524
   525
   526
   527
   528
   529
   530
   531
   532
   533
   534
   535
   536
   537
   538
   539
   540
   541
   542
   543
   544
   545
   546
   547
   548
   549
   550
   551
   552
   553
   554
   555
   556
   557
   558
   559
   560
   561
   562
   563
   564
   565
   566
   567
   568
   569
   570
   571
   572
   573
   574
   575
   576
   577
   578
   579
   580
   581
   582
   583
   584
   585
   586
   587
   588
   589
   590
   591
   592
   593
   594
   595
   596
   597
   598
   599
   600
   601
   602
   603
   604
   605
#
# Tests for SF bugs
# ----------------------------------------------------------------------
#   AUTHOR:  Arnulf Wiedemann
#            [email protected]
# ----------------------------------------------------------------------
#            Copyright (c) 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

global ::test_status

# ----------------------------------------------------------------------
#  Test bugs of the SourceForge bug tracker for incrtcl
# ----------------------------------------------------------------------

test sfbug-163 {upvar has to resolve instance variables in caller} -setup {
    itcl::class o1 {
        public method getValue {name} {
	    upvar $name val
	    set val 22
	}
    }
    itcl::class o2 {
	public variable command
	constructor {cls2} {
	    $cls2 getValue command
	}
	public method b {cls2} {
	    return $command
	}
    }
    o1 test1
    o2 test2 test1
} -body {
    test2 b test1
} -cleanup {
    itcl::delete class o2
    itcl::delete class o1
} -result 22

test sfbug-187 {upvar with this variable SF bug #187
} -body {
    ::itcl::class foo {
      method test {} {
          PopID
      }

      proc PopID {} {
        upvar 1 this me
        set me
      }
    }
    foo bar
    bar test
} -result {::bar} \
  -cleanup {::itcl::delete class foo}

test sfbug-234 {chain with no argument SF bug #234
} -body {
    set ::test_status ""
    itcl::class One {
	public method t1 {x} {
            lappend ::test_status "$this One.t1($x)"
	}
	public method t2 {} {
            lappend ::test_status "$this One.t2"
	}
    }

    itcl::class Two {
        inherit One

        public method t1 {x} {
            lappend ::test_status "$this Two.t1($x)"
            chain $x
        }

        public method t2 {} {
            lappend ::test_status "$this Two.t2"
            chain
        }
    }
    set y [Two #auto]
    $y t1 a
    $y t2
} -result {{::two0 Two.t1(a)} {::two0 One.t1(a)} {::two0 Two.t2} {::two0 One.t2}} \
  -cleanup {::itcl::delete class Two}

test sfbug-236 {problem with inheritance of methods SF bug #236
} -body {
    set ::test_status ""

    ::itcl::class c_mem {
        private method get_ports {}
        public method get_mem {}
    }

    ::itcl::class c_rom {
        inherit c_mem
        private method get_ports {}
    }

    ::itcl::body c_rom::get_ports {} {
        return "toto"
    }

    ::itcl::body c_mem::get_ports {} {
        return "tata"
    }

    ::itcl::body c_mem::get_mem {} {
        return [concat "titi" [get_ports]]
    }

    set ptr [c_rom #auto]
    lappend ::test_status [$ptr get_mem]

# expected output:
# titi toto
} -result {{titi toto}} \
  -cleanup {::itcl::delete class c_rom}

test sfbug-237 { problem with chain command SF bug #237
} -body {
    set ::test_status ""

    itcl::class main {
        constructor {} {
            lappend ::test_status "OK ITCL constructor"
	}

        public method init_OK1 { parm } {
            lappend ::test_status "OK1 MAIN $parm"
        }
        public method init_OK2 {} {
            lappend ::test_status "OK2 MAIN"
        }
        public method init_ERR1 {} {
            lappend ::test_status "ERR1 MAIN"
        }
    }

    itcl::class child {
        inherit main

        constructor {} {}

        public method init_OK1 {} {
            lappend ::test_status "OK1 CHILD"
            chain TEST
        }

        public method init_OK2 {} {
            lappend ::test_status "OK2 CHILD"
            next
        }

        public method init_ERR1 {} {
            lappend ::test_status "ERR1 CHILD"
            chain
        }
    }

    set obj [child #auto]
    $obj init_OK1
    $obj init_OK2
    $obj init_ERR1
} -result {{OK ITCL constructor} {OK1 CHILD} {OK1 MAIN TEST} {OK2 CHILD} {OK2 MAIN} {ERR1 CHILD} {ERR1 MAIN}} \
  -cleanup {::itcl::delete class child}

test sfbug-243 {faulty namespace behaviour SF bug #243
} -body {
    set ::test_status ""

    namespace eval ns {}

    itcl::class ns::A {
        method do {} {nsdo}

        method nsdo {} {
            lappend ::test_status "body do: [info function do -body]"
        }
    }

    [ns::A #auto] do

    itcl::body ns::A::do {} {A::nsdo}
    [ns::A #auto] do

    itcl::body ns::A::do {} {::ns::A::nsdo}
    [ns::A #auto] do

    itcl::body ns::A::do {} {ns::A::nsdo}
    [ns::A #auto] do
} -result {{body do: nsdo} {body do: A::nsdo} {body do: ::ns::A::nsdo} {body do: ns::A::nsdo}} \
  -cleanup {::itcl::delete class ns::A}

test sfbug-244 { SF bug 244
} -body {
    set ::test_status ""

    proc foo {body} {
        uplevel $body
    }

    itcl::class A {
      method do {body} {foo $body}
      method do2 {} {lappend ::test_status done}
    }

    set y [A #auto]
    $y  do {
        lappend ::test_status "I'm $this"
        do2
    }
} -result {{I'm ::a0} done} \
  -cleanup {::itcl::delete class A; rename foo {}}

test sfbug-250 { SF bug #250
} -body {
    set ::test_status ""

    ::itcl::class A {
        variable b

        constructor {} {
            set b [B #auto]
        }

        public method m1 {} {
            $b m3
        }

        public method m2 {} {
            lappend ::test_status m2
        }
    }

    ::itcl::class B {
        public method m3 {} {
          uplevel m2
         }
    }

    set a [A #auto]
    $a m1

} -result {m2} \
  -cleanup {::itcl::delete class A B}

test sfbug-Schelte {bug with onfo reported from Schelte SF bug xxx
} -body {
    set ::test_status ""

    itcl::class foo {
        method kerplunk {args} {
            lappend ::test_status [info level 0]
            lappend ::test_status [::info level 0]
            lappend ::test_status [[namespace which info] level 0]
       }
    }

    [foo #auto] kerplunk hello world
} -result {{foo0 kerplunk hello world} {foo0 kerplunk hello world} {foo0 kerplunk hello world}} \
  -cleanup {::itcl::delete class foo}

test sfbug-254 { SF bug #254
} -body {
    set interp [interp create]
    $interp eval {
      package require itcl

      set ::test_status ""
      oo::class destroy
      lappend ::test_status "::oo::class destroy worked"
    }
} -result {{::oo::class destroy worked}} \
  -cleanup { }

test sfbug-255 { SF bug #255
} -body {
    set ::test_status ""

    proc ::sfbug_255_do_uplevel { body } {
        uplevel 1 $body
    }

    proc ::sfbug_255_testclass { pathName args } {
        uplevel TestClass $pathName $args
    }

    ::itcl::class TestClass {
        public variable property "value"
        constructor {} {
        }

        private method internal-helper {} {
            return "TestClass::internal-helper"
        }

        public method api-call {} {
            lappend ::test_status "TestClass::api-call"
            lappend ::test_status [internal-helper]
            lappend ::test_status [sfbug_255_do_uplevel { internal-helper }]
            lappend ::test_status [cget -property]
            sfbug_255_do_uplevel { lappend ::test_status [cget -property] }
        }
    }

    [::sfbug_255_testclass tc] api-call
} -result {TestClass::api-call TestClass::internal-helper TestClass::internal-helper value value} \
  -cleanup {::itcl::delete class TestClass}

test fossilbug-8 { fossil bug 2cd667f270b68ef66d668338e09d144e20405e23
} -body {
    ::itcl::class ::Naughty {
        private method die {} {
        }
    }
    ::Naughty die
} -cleanup {
    ::itcl::delete class ::Naughty
} -result {die}

test sfbug-256 { SF bug #256
} -body {
    set ::test_status ""

    proc ::sfbug_256_do_uplevel { body } {
        uplevel 1 $body
    }

    proc ::sfbug_256_testclass { pathName args } {
        uplevel TestClass256 $pathName $args
    }

    ::itcl::class TestClass256 {
        public variable property "value"
        constructor {} {
        }

        private method internal-helper {} {
            lappend ::test_status "TestClass::internal-helper"
            sfbug_256_do_uplevel { lappend ::test_status [cget -property] }
        }

        public method api-call {} {
            lappend ::test_status "TestClass::api-call"
            lappend ::test_status [internal-helper]
            lappend ::test_status [sfbug_256_do_uplevel { internal-helper }]
            lappend ::test_status [cget -property]
            sfbug_256_do_uplevel { lappend ::test_status [cget -property] }
        }
    }

    [::sfbug_256_testclass tc] api-call
} -result {TestClass::api-call TestClass::internal-helper value {TestClass::api-call TestClass::internal-helper value} TestClass::internal-helper value {TestClass::api-call TestClass::internal-helper value {TestClass::api-call TestClass::internal-helper value} TestClass::internal-helper value} value value} \
  -cleanup {::itcl::delete class TestClass256}

test sfbug-257 { SF bug #257
} -body {
    set interp [interp create]
    $interp eval {
      package require itcl
      set ::test_status ""
      ::itcl::class ::cl1 {
        method m1 {} {
          ::oo::class destroy
          lappend ::test_status "method Hello World"
        }
        proc p1 {} {
          lappend ::test_status "proc Hello World"
        }
      }
      set obj1 [::cl1 #auto]
      ::cl1::p1
      $obj1 p1
      $obj1 m1
  
      catch {
      $obj1 m1
      ::cl1::p1
      } msg
      lappend ::test_status $msg
    }
} -result {{proc Hello World} {proc Hello World} {method Hello World} {invalid command name "cl10"}} \
  -cleanup {interp delete $interp}

test sfbug-259 { SF bug #257 } -setup {
    interp create slave
    load {} Itcl slave
} -cleanup {
    interp delete slave
} -body {
    slave eval {
proc do_uplevel { body } {
    uplevel 1 $body
}
proc ::testclass { pathName args } {
    uplevel TestClass $pathName $args
}
itcl::class TestClass {
    constructor {} {}
    public variable property "value"
    public method api-call {}
    protected method internal-helper {}
}
itcl::body TestClass::internal-helper {} {
}
itcl::configbody TestClass::property {
    internal-helper
}
itcl::body TestClass::api-call {} {
    do_uplevel {configure -property blah}
}
set tc [::testclass .]
$tc api-call
    }
}

test sfbug-261 { SF bug #261 } -setup {
    itcl::class A {
	public method a1 {} {a2}
	public method a2 {} {uplevel a3 hello}
	public method a3 {s} {return $s}
    }
    A x
} -body {
    x a1
} -cleanup {
    itcl::delete class A
} -result hello

test sfbug-265.1 { SF bug #265 } -setup {
    itcl::class C {}
} -body {
    namespace eval A {C c}
    namespace eval B {C c}
} -cleanup {
    itcl::delete class C
    namespace delete A B
} -result c
test sfbug-265.2 { SF bug #265 } -setup {
    itcl::class C {}
    itcl::class B::C {}
} -body {
    C ::A::B
    B::C ::A
} -cleanup {
    itcl::delete class B::C
    itcl::delete class C
    namespace delete A B
} -result ::A

test sfbug-268 { SF bug #268 } -setup {
    itcl::class C {
	private variable v
	destructor {error foo}
	public method demo {} {set v 0}
    }
    C c
} -body {
    catch {itcl::delete object c}
    c demo
} -cleanup {
    rename c {}
    itcl::delete class C
} -result 0

test sfbug-273 { SF bug #273 } -setup {
    itcl::class C {
	public proc call {m} {$m}
	public proc crash {} {
	    call null
	    info frame 2
	    return ok
	}
	public proc null {} {}
    }
} -body {
    C::call crash
} -cleanup {
    itcl::delete class C
} -result ok


test sfbug-276.0 { SF bug #276 } -setup {
  set ::answer {}
  itcl::class A {
    constructor {} {
      lappend ::answer [uplevel namespace current]
    }
  }
  itcl::class B {
    inherit A
    constructor {} {}
  }
} -body {
  B b
  set ::answer
} -cleanup {
  itcl::delete class A B
  unset -nocomplain ::answer
} -result ::B

test sfbug-276.1 { SF bug #276 } -setup {
  set ::answer {}
  itcl::class A {
    constructor {} {
      lappend ::answer [uplevel namespace current]
    }
  }
  itcl::class E {
    constructor {} {
      lappend ::answer [uplevel namespace current]
    }
  }
  itcl::class B {
    inherit A E
    constructor {} {}
  }
} -body {
  B b
  set ::answer
} -cleanup {
  itcl::delete class A B E
  unset -nocomplain ::answer
} -result {::B ::B}

test fossil-9.0 {d0126511d9} -setup {
    itcl::class N::B {}
} -body {
    itcl::class N {}
} -cleanup {
    itcl::delete class N::B N
} -result {}

test fossil-9.1 {d0126511d9} -setup {
    itcl::class N::B {}
    itcl::delete class N::B
    namespace delete N
} -body {
    itcl::class N {}
} -cleanup {
    itcl::delete class N
    catch {namespace delete N}
} -result {}

test fossil-9.2 {ec215db901} -setup {
    set ::answer {}
    itcl::class Object {
	constructor {} {set n 1} {set ::answer $n}
    }
} -body {
    Object foo
    set ::answer
} -cleanup {
    itcl::delete class Object
    unset -nocomplain ::answer
} -result 1

test fossil-9.3 {c45384364c} -setup {
    itcl::class A {
	method demo script {uplevel 1 $script}
    }
    A a
    itcl::class B {
	method demo script {eval $script; a demo $script}
    }
    B b
} -body {
    b demo {lappend result $this}
} -cleanup {
    itcl::delete class A B
} -result {::b ::b}

test sfbugs-281 {Resolve inherited common} -setup {
    itcl::class Parent {protected common x 0}
} -cleanup {
    itcl::delete class Parent
} -body {
    itcl::class Child {
	inherit Parent
	set Parent::x
    }
} -result {}



#test sfbug-xxx { SF bug xxx
#} -body {
#    set ::test_status ""
#
#} -result {::bar} \
#  -cleanup {::itcl::delete class yyy}

::tcltest::cleanupTests
return