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
|
# This file contains a collection of tests for functionality originally
# sourced from the ooutil package in Tcllib. Sourcing this file into Tcl runs
# the tests and generates output for errors. No output means no errors were
# found.
#
# Copyright © 2014-2016 Andreas Kupries
# Copyright © 2018 Donal K. Fellows
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.
package require tcl::oo 1.3.0
if {"::tcltest" ni [namespace children]} {
package require tcltest 2.5
namespace import -force ::tcltest::*
}
test ooUtil-1.1 {TIP 478: classmethod} -setup {
oo::class create parent
} -body {
oo::class create ActiveRecord {
superclass parent
classmethod find args {
return "[self] called with arguments: $args"
}
}
oo::class create Table {
superclass ActiveRecord
}
Table find foo bar
} -cleanup {
parent destroy
} -result {::Table called with arguments: foo bar}
test ooUtil-1.2 {TIP 478: classmethod in namespace} -setup {
namespace eval ::testns {}
} -body {
namespace eval ::testns {
oo::class create ActiveRecord {
classmethod find args {
return "[self] called with arguments: $args"
}
}
oo::class create Table {
superclass ActiveRecord
}
}
testns::Table find foo bar
} -cleanup {
namespace delete ::testns
} -result {::testns::Table called with arguments: foo bar}
test ooUtil-1.3 {TIP 478: classmethod must not interfere with constructor signatures} -setup {
oo::class create parent
} -body {
oo::class create TestClass {
superclass oo::class parent
self method create {name ignore body} {
next $name $body
}
}
TestClass create okay {} {}
} -cleanup {
parent destroy
} -result {::okay}
test ooUtil-1.4 {TIP 478: classmethod with several inheritance levels} -setup {
oo::class create parent
} -body {
oo::class create ActiveRecord {
superclass parent
classmethod find args {
return "[self] called with arguments: $args"
}
}
oo::class create Table {
superclass ActiveRecord
}
oo::class create SubTable {
superclass Table
}
SubTable find foo bar
} -cleanup {
parent destroy
} -result {::SubTable called with arguments: foo bar}
test ooUtil-1.5 {TIP 478: classmethod and instances} -setup {
oo::class create parent
} -body {
oo::class create ActiveRecord {
superclass parent
classmethod find args {
return "[self] called with arguments: $args"
}
}
oo::class create Table {
superclass ActiveRecord
}
set t [Table new]
$t find 1 2 3
} -cleanup {
parent destroy
} -result {::Table called with arguments: 1 2 3}
test ooUtil-1.6 {TIP 478: classmethod and instances} -setup {
oo::class create parent
} -body {
oo::class create ActiveRecord {
superclass parent
classmethod find args {
return "[self] called with arguments: $args"
}
}
oo::class create Table {
superclass ActiveRecord
unexport find
}
set t [Table new]
$t find 1 2 3
} -returnCodes error -cleanup {
parent destroy
} -match glob -result {unknown method "find": must be *}
test ooUtil-1.7 {} -setup {
oo::class create parent
} -body {
oo::class create Foo {
superclass parent
classmethod bar {} {
puts "This is in the class; self is [self]"
my meee
}
classmethod meee {} {
puts "This is meee"
}
}
oo::class create Grill {
superclass Foo
classmethod meee {} {
puts "This is meee 2"
}
}
list [Foo bar] [Grill bar] [[Foo new] bar] [[Grill new] bar]
} -cleanup {
parent destroy
} -result {{} {} {} {}} -output "This is in the class; self is ::Foo\nThis is meee\nThis is in the class; self is ::Grill\nThis is meee 2\nThis is in the class; self is ::Foo\nThis is meee\nThis is in the class; self is ::Grill\nThis is meee 2\n"
# Two tests to confirm that we correctly initialise the scripted part of TclOO
# in child interpreters. This is slightly tricky at the implementation level
# because we cannot count on either [source] or [open] being available.
test ooUtil-1.8 {TIP 478: classmethod in child interp} -setup {
set childinterp [interp create]
} -body {
$childinterp eval {
oo::class create ActiveRecord {
classmethod find args {
return "[self] called with arguments: $args"
}
}
oo::class create Table {
superclass ActiveRecord
}
# This is confirming that this is not the parent interpreter
list [Table find foo bar] [info globals childinterp]
}
} -cleanup {
interp delete $childinterp
} -result {{::Table called with arguments: foo bar} {}}
test ooUtil-1.9 {TIP 478: classmethod in safe child interp} -setup {
set safeinterp [interp create -safe]
} -body {
$safeinterp eval {
oo::class create ActiveRecord {
classmethod find args {
return "[self] called with arguments: $args"
}
}
oo::class create Table {
superclass ActiveRecord
}
# This is confirming that this is a (basic) safe interpreter
list [Table find foo bar] [info commands source]
}
} -cleanup {
interp delete $safeinterp
} -result {{::Table called with arguments: foo bar} {}}
test ooUtil-1.10.1 {Bug 680503: classmethod shouldn't require create body} -body {
oo::class create C
oo::define C {classmethod cm {} {}}
} -cleanup {
catch {C destroy}
} -result {}
test ooUtil-1.10.2 {Bug 680503: case that worked} -body {
oo::class create C {}
oo::define C {classmethod cm {} {}}
} -cleanup {
catch {C destroy}
} -result {}
test ooUtil-2.1 {TIP 478: callback generation} -setup {
oo::class create parent
} -body {
oo::class create c {
superclass parent
method CallMe {} { return ok,[self] }
method makeCall {} {
return [callback CallMe]
}
}
c create ::context
set cb [context makeCall]
{*}$cb
} -cleanup {
parent destroy
} -result {ok,::context}
test ooUtil-2.2 {TIP 478: callback generation} -setup {
oo::class create parent
} -body {
oo::class create c {
superclass parent
method CallMe {a b c} { return ok,[self],$a,$b,$c }
method makeCall {b} {
return [callback CallMe 123 $b]
}
}
c create ::context
set cb [context makeCall "a b c"]
{*}$cb PQR
} -cleanup {
parent destroy
} -result {ok,::context,123,a b c,PQR}
test ooUtil-2.3 {TIP 478: callback generation, alternate name} -setup {
oo::class create parent
} -body {
oo::class create c {
superclass parent
method CallMe {} { return ok,[self] }
method makeCall {} {
return [mymethod CallMe]
}
}
c create ::context
set cb [context makeCall]
{*}$cb
} -cleanup {
parent destroy
} -result {ok,::context}
test ooUtil-2.4 {TIP 478: callback generation, alternate name} -setup {
oo::class create parent
} -body {
oo::class create c {
superclass parent
method CallMe {a b c} { return ok,[self],$a,$b,$c }
method makeCall {b} {
return [mymethod CallMe 123 $b]
}
}
c create ::context
set cb [context makeCall "a b c"]
{*}$cb PQR
} -cleanup {
parent destroy
} -result {ok,::context,123,a b c,PQR}
test ooUtil-2.5 {TIP 478: callbacks and method lifetime} -setup {
oo::class create parent
} -body {
oo::class create c {
superclass parent
method makeCall {b} {
return [callback CallMe 123 $b]
}
}
c create ::context
set cb [context makeCall "a b c"]
set result [list [catch {{*}$cb PQR} msg] $msg]
oo::objdefine context {
method CallMe {a b c} { return ok,[self],$a,$b,$c }
}
lappend result [{*}$cb PQR]
} -cleanup {
parent destroy
} -result {1 {unknown method "CallMe": must be <cloned>, destroy, eval, makeCall, unknown, variable or varname} {ok,::context,123,a b c,PQR}}
test ooUtil-2.6 {TIP 478: callback use case} -setup {
oo::class create parent
unset -nocomplain x
} -body {
oo::class create c {
superclass parent
variable count
constructor {var} {
set count 0
upvar 1 $var v
trace add variable v write [callback TraceCallback]
}
method count {} {return $count}
method TraceCallback {name1 name2 op} {
incr count
}
}
set o [c new x]
for {set x 0} {$x < 5} {incr x} {}
$o count
} -cleanup {
unset -nocomplain x
parent destroy
} -result 6
test ooUtil-3.1 {TIP 478: class initialisation} -setup {
oo::class create parent
catch {rename ::foobar-3.1 {}}
} -body {
oo::class create ::cls {
superclass parent
initialise {
proc foobar-3.1 {} {return ok}
}
method calls {} {
list [catch foobar-3.1 msg] $msg \
[namespace eval [info object namespace [self class]] foobar-3.1]
}
}
[cls new] calls
} -cleanup {
parent destroy
} -result {1 {invalid command name "foobar-3.1"} ok}
test ooUtil-3.2 {TIP 478: class variables} -setup {
oo::class create parent
catch {rename ::foobar-3.1 {}}
} -body {
oo::class create ::cls {
superclass parent
initialise {
variable x 123
}
method call {} {
classvariable x
incr x
}
}
cls create a
cls create b
cls create c
list [a call] [b call] [c call] [a call] [b call] [c call]
} -cleanup {
parent destroy
} -result {124 125 126 127 128 129}
test ooUtil-3.3 {TIP 478: class initialisation} -setup {
oo::class create parent
catch {rename ::foobar-3.3 {}}
} -body {
oo::class create ::cls {
superclass parent
initialize {
proc foobar-3.3 {} {return ok}
}
method calls {} {
list [catch foobar-3.3 msg] $msg \
[namespace eval [info object namespace [self class]] foobar-3.3]
}
}
[cls new] calls
} -cleanup {
parent destroy
} -result {1 {invalid command name "foobar-3.3"} ok}
test ooUtil-3.4 {TIP 478: class initialisation} -setup {
oo::class create parent
catch {rename ::appendToResultVar {}}
proc ::appendToResultVar args {
lappend ::result {*}$args
}
set result {}
} -body {
trace add execution oo::define::initialise enter appendToResultVar
oo::class create ::cls {
superclass parent
initialize {proc xyzzy {} {}}
}
return $result
} -cleanup {
catch {
trace remove execution oo::define::initialise enter appendToResultVar
}
rename ::appendToResultVar {}
parent destroy
} -result {{initialize {proc xyzzy {} {}}} enter}
test ooUtil-3.5 {TIP 478: class initialisation} -body {
oo::define oo::object {
::list [::namespace which initialise] [::namespace which initialize] \
[::namespace origin initialise] [::namespace origin initialize]
}
} -result {::oo::define::initialise ::oo::define::initialize ::oo::define::initialise ::oo::define::initialise}
test ooUtil-4.1 {TIP 478: singleton} -setup {
oo::class create parent
} -body {
oo::singleton create xyz {
superclass parent
}
set x [xyz new]
set y [xyz new]
set z [xyz new]
set code [catch {$x destroy} msg]
set p [xyz new]
lappend code [catch {rename $x ""}]
set q [xyz new]
string map [list $x ONE $q TWO] [list {*}$code $x $y $z $p $q [xyz new]]
} -cleanup {
parent destroy
} -result {1 0 ONE ONE ONE ONE TWO TWO}
test ooUtil-4.2 {TIP 478: singleton errors} -setup {
oo::class create parent
} -body {
oo::singleton create xyz {
superclass parent
}
[xyz new] destroy
} -returnCodes error -cleanup {
parent destroy
} -result {may not destroy a singleton object}
test ooUtil-4.3 {TIP 478: singleton errors} -setup {
oo::class create parent
} -body {
oo::singleton create xyz {
superclass parent
}
oo::copy [xyz new]
} -returnCodes error -cleanup {
parent destroy
} -result {may not clone a singleton object}
test ooUtil-5.1 {TIP 478: abstract} -setup {
oo::class create parent
} -body {
oo::abstract create xyz {
superclass parent
method foo {} {return 123}
}
oo::class create pqr {
superclass xyz
method bar {} {return 456}
}
set codes [list [catch {xyz new}] [catch {xyz create x}] [catch {xyz createWithNamespace x y}]]
set x [pqr new]
set y [pqr create ::y]
lappend codes [$x foo] [$x bar] $y
} -cleanup {
parent destroy
} -result {1 1 1 123 456 ::y}
test ooUtil-6.1 {TIP 478: classvariable} -setup {
oo::class create parent
} -body {
oo::class create xyz {
superclass parent
initialise {
variable x 1 y 2
}
method a {} {
classvariable x
incr x
}
method b {} {
classvariable y
incr y
}
method c {} {
classvariable x y
list $x $y
}
}
set p [xyz new]
set q [xyz new]
set result [list [$p c] [$q c]]
$p a
$q b
lappend result [[xyz new] c]
} -cleanup {
parent destroy
} -result {{1 2} {1 2} {2 3}}
test ooUtil-6.2 {TIP 478: classvariable error case} -setup {
oo::class create parent
} -body {
oo::class create xyz {
superclass parent
method a {} {
classvariable x(1)
incr x(1)
}
}
set p [xyz new]
set q [xyz new]
list [$p a] [$q a]
} -returnCodes error -cleanup {
parent destroy
} -result {bad variable name "x(1)": can't create a scalar variable that looks like an array element}
test ooUtil-6.3 {TIP 478: classvariable error case} -setup {
oo::class create parent
} -body {
oo::class create xyz {
superclass parent
method a {} {
classvariable ::x
incr x
}
}
set p [xyz new]
set q [xyz new]
list [$p a] [$q a]
} -returnCodes error -cleanup {
parent destroy
} -result {bad variable name "::x": can't create a local variable with a namespace separator in it}
test ooUtil-7.1 {TIP 478: link calling pattern} -setup {
oo::class create parent
} -body {
oo::class create cls {
superclass parent
method foo {} {return "in foo of [self]"}
method Bar {} {return "in bar of [self]"}
method Grill {} {return "in grill of [self]"}
export eval
constructor {} {
link foo
link {bar Bar} {grill Grill}
}
}
cls create o
o eval {list [foo] [bar] [grill]}
} -cleanup {
parent destroy
} -result {{in foo of ::o} {in bar of ::o} {in grill of ::o}}
test ooUtil-7.2 {TIP 478: link removed when [my] disappears} -setup {
oo::class create parent
} -body {
oo::class create cls {
superclass parent
method foo {} {return "in foo of [self]"}
constructor {cmd} {
link [list ::$cmd foo]
}
}
cls create o pqr
list [o foo] [pqr] [rename [info object namespace o]::my {}] [catch pqr msg] $msg
} -cleanup {
parent destroy
} -result {{in foo of ::o} {in foo of ::o} {} 1 {invalid command name "pqr"}}
# Tests a very weird combination of things (with a key problem locus in
# MixinClassDelegates) that TIP 567 fixes
test ooUtil-8.1 {TIP 567: call oo::define twice from metaclass constructor} -setup {
oo::class create parent
} -body {
::oo::class create A {
superclass parent
}
::oo::class create B {
superclass ::oo::class parent
constructor {{definitionScript ""}} {
next $definitionScript
next {superclass ::A}
}
}
B create C {
superclass A
}
C create instance
} -cleanup {
parent destroy
} -result ::instance
# Tests that verify issues detected with the tcllib version of the code
test ooUtil-tcllib-ticket-b3577ed586 {test scoping of delegation in oo::class.Delegate } -setup {
oo::class create animal {}
namespace eval ::ooutiltest {
oo::class create pet { superclass animal }
}
} -body {
namespace eval ::ooutiltest {
oo::class create dog { superclass pet }
}
} -cleanup {
namespace delete ooutiltest
rename animal {}
} -result {::ooutiltest::dog}
test ooUtil-tcllib-ticket-fe7a0e0a3a {classmethod must not interfere with constructor signatures} -setup {
oo::class create TestClass {
superclass oo::class
self method create {name ignore body} {
next $name $body
}
}
} -body {
TestClass create okay {} {}
} -cleanup {
rename TestClass {}
} -result {::okay}
cleanupTests
return
# Local Variables:
# fill-column: 78
# mode: tcl
# End:
|