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 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
|
# -*- Tcl -*-
package prefer latest
package req nx
package require nx::test
#
# test cases for disposition "alias" and "forward"
#
nx::test case basics {
Class create C {
:object property {inst "::__%&singleton"}
:method foo {x} {
#puts stderr [current method]
set :[current method] $x
}
:method bar {} {;}
:protected method baz {y} {
#puts stderr [current method]
set :my[current method] $y
}
#
# some testing helpers
#
:public object method setObjectParams {spec} {
:protected method __object_configureparameter {} [list return $spec]
::nsf::parameter::cache::classinvalidate [current]
}
:setObjectParams ""
:public object method new args {
return [:create ${:inst} {*}$args]
}
}
foreach paramType {forward alias} {
#
# Restricted to object parameters only?
#
set msg "parameter option '$paramType' not allowed"
? [list C method m1 -foo:$paramType {;}] $msg
? [list C method m1 foo:$paramType {;}] $msg
#
# Not applicable in parametercheck
#
? [list ::nsf::is $paramType $msg] "invalid value constraints \"$paramType\": parameter option '$paramType' not allowed"
}
#
# Do aliases and forwarder set instance variables? They should not.
#
C setObjectParams -baz:alias
? {[C new -baz BAZ] eval {info exists :baz}} 0
C setObjectParams {{{-baz:forward,method=%self %method}}}
? {[C new -baz BAZ] eval {info exists :baz}} 0
#
# Note, currently alias/forward disposition operate on public and
# protected target methods alike. Is this intended? For example,
# providing access through the parameter interface to protected
# methods etc. (at the instantiation site only) ? Or, are they
# expected to be public ...
#
### objectparameter are from the intentions public: the typical
### use-case is that someone wants to configure an object to be
### created, before the object exists....
#
# 1) Positional object parameters + alias/forward disposition?
#
#
# Passing a single argument to a positional alias
#
C setObjectParams foo:alias
? {C new FOO} "::__%&singleton"
? {C new {FOO FAA}} "::__%&singleton"
###
### Whenever a value is provided (default value or actual value) the
### parameter is evaluated.
###
C setObjectParams {{foo:alias ""}}
? {C new} "::__%&singleton"
C setObjectParams {{-foo:alias "fooDefault"}}
? {[C new] eval {set :foo}} "fooDefault"
#
# What about multi-argument vectors?
#
C eval {
:method multi-2 {x y} {
set :[current method] [current args]
}
:method multi-escape {x} {
set :[current method] $x
}
:method multi-args {args} {
set :[current method] $args
}
}
#
# Parameters are limited to a single value by the object parameter.
#
C setObjectParams {{-multi-2:alias}}
? {[C new -multi-2 {X Y}] eval {set :multi-2}} \
"wrong # args: should be \"multi-2 x y\""
#
# Passing multiple arguments as a list
#
C setObjectParams {{-multi-escape:alias}}
? {[C new -multi-escape [list X Y]] eval {set :multi-escape}} \
[list X Y]
#
# Passing multiple arguments as a list, passed to a args argument
# list.
#
C setObjectParams {{-multi-args:alias}}
? {[C new -multi-args [list X Y]] eval {set :multi-args}} \
[list [list X Y]]
#
# As used, all parameters receive currently 0 or 1
# argument. The same is true for disposition "alias" an
# "forward". One could consider to unbox a parameter list via a
# parameter option "expand" (like {*}) for alias|forward parameter
# specs, e.g.:
# {-multi-2:alias,expand}
# {-multi-2:forward,method=...,expand}
#
# Without the sketched extension, one could use eval in a forwarder.
#
C setObjectParams {{{-multi-2:forward,method=eval %self %method}}}
? {[C new -multi-2 {X Y}] eval {set :multi-2}} \
"X Y"
#
# In the positional case, why is FOO not passed on as arg value to
# the target method?
#
C setObjectParams {{{foo:forward,method=%self %method}}}
? {C new FOO} "::__%&singleton"
? {[C new FOO] eval {set :foo}} "FOO"
#
# Naming of the parameter spec element "method": It fits the alias
# disposition, but is a little irritating in the context of a
# forward. One would expect forwardspec or simply "spec" (as this is
# used in the docs, the error messages etc.), e.g.:
#
# {foo:forward,spec=%self %method}
#
# 'spec' would also work for 'alias' as it is more general (the spec
# of an alias is the method name ...)
#
#### well, "spec" is not nice for alias, and potentially confusing
#### with the parameter spec (the full parameter definition).
#
# Passing non-positional arguments to target methods (at least
# forwarder ones)?
#
C method multi-mix {-x y args} {
set :[current method] --x-$x--y-$y--args-$args
}
C setObjectParams {{{-multi-mix:forward,method=eval %self %method}}}
? {[C new -multi-mix [list -x X Y Z 1 2]] eval {set :multi-mix}} \
"--x-X--y-Y--args-Z 1 2"
#
# Aliased methods with nonpos arguments are rendered entirely
# useless by the single-value limitation (see also above):
#
C method single-np {-x:required} {
set :[current method] --x-$x
}
C setObjectParams {{-single-np:alias}}
? {[C new -single-np [list -x]] eval {set :single-np}} \
"value for parameter '-x' expected"
? {[C new -single-np [list -x X]] eval {set :single-np}} \
"invalid non-positional argument '-x X', valid are: -x;
should be \"::__%&singleton single-np -x /value/\""
#
# INTERACTIONS with other parameter types
#
# There are two validation points:
# 1) the object parameter validation on the initial argument set
# 2) the target method validation on the (mangled) argument set
#
# ... they can deviate from each other, to a point of direct
# conflict
#
#
# Allowed built-in value types (according to feature matrix in
# parameters.test)
#
set msg {expected $mtype but got \"$testvalue\" for parameter \"x\"}
dict set types boolean [list testvalue f mtype object msg $msg]
dict set types integer [list testvalue 81 mtype punct msg $msg]
dict set types object [list testvalue ::C mtype integer msg $msg ]
dict set types class [list testvalue ::C mtype boolean msg $msg]
dict set types object,type=::nx::Class \
[list testvalue ::C mtype object,type=::C \
msg "expected object of type ::C but got \"::C\"\
for parameter \"x\""]
# for aliases ...
dict for {t tdict} $types {
dict with tdict {
::C public method foo [list x:$t] {
set :[current method] $x
}
::C setObjectParams [list [list -foo:alias,$t]]
? "::nsf::is $t \[\[::C new -foo $testvalue\] eval {set :foo}\]" 1 "check: -foo:alias,$t"
}
}
::C public method baz {x} {
return $x
}
dict for {t tdict} $types {
dict with tdict {
::C public method foo [list x:$mtype] {
set :[current method] $x
}
::C setObjectParams [list [list -foo:alias,$t]]
? "::nsf::is $t \[\[::C new -foo $testvalue\] eval {set :foo}\]" \
[subst $msg]
}
}
#
# TODO: update the matrix in parameters.test (feature matrix)
#
###
### The question is, what happens with the matrix. The matrix is in
### some respects not complete (no disposition) and contains old
### namings (e.g. allowempty, multiple) and contains types removed
### some time ago (such as e.g. "relation").
###
#
# define a user defined parameter type
#
::nx::methodParameterSlot object method type=mytype {name value} {
if {$value < 1 || $value > 3} {
error "Value '$value' of parameter $name is not between 1 and 3"
}
}
array set script {alias "method=baz" forward "method=%self %method"}
foreach disposition [list alias forward] {
C setObjectParams [list [list -foo:$disposition,switch]]
? {C new} "parameter invocation types cannot be used with option 'switch'" \
"switch not allowed for $disposition"
C setObjectParams [list [list -baz:$disposition,mytype,$script($disposition)]]
? {C new -baz 1} "::__%&singleton" \
"disposition $disposition, user defined type, valid value"
C setObjectParams [list [list -baz:$disposition,mytype,$script($disposition)]]
? {C new -baz 0} "Value '0' of parameter baz is not between 1 and 3" \
"disposition $disposition, user defined type, invalid value"
C setObjectParams [list [list -foo:$disposition,xxx]]
? {C new} "::__%&singleton" \
"disposition $disposition, unknown user defined type - just a warning"
C setObjectParams [list [list -foo:$disposition,type=::C]]
? {C new} "parameter option 'type=' only allowed for parameter types 'object' and 'class'"
#
# The 'arg=...' option should not be used, consider using 'method=...'
#
C setObjectParams [list [list -foo:$disposition,arg=BOOM]]
? {C new} "parameter option 'arg=' only allowed for user-defined converter"
}
#
# The option 'method=...' applies to disposition types only
#
C setObjectParams [list [list -foo:initcmd,method=BOOM]]
? {C new} "parameter option 'method=' only allowed for parameter types 'alias', 'forward' and 'slotset'"
C setObjectParams [list [list -foo:alias,forward]]
? {C new} "parameter option 'forward' not valid in this option combination"
C setObjectParams [list [list -foo:forward,alias]]
? {C new} "parameter option 'alias' not valid in this option combination"
C setObjectParams [list [list -foo:alias,initcmd]]
? {C new} "parameter option 'initcmd' not valid in this option combination"
C setObjectParams [list [list -foo:forward,initcmd]]
? {C new} "parameter option 'initcmd' not valid in this option combination"
}
nx::test case dispo-multiplicities {
Class create S {
:public object method setObjectParams {spec} {
:protected method __object_configureparameter {} [list return $spec]
::nsf::parameter::cache::classinvalidate [current]
}
#:object method __object_configureparameter {} {
# return ${:objectparams}
#}
:public method foo {args} {
set :foo $args
return $args
}
}
#
# On multiplicity classes ...
#
# ... implying a Tcl list value: 1..*, 0..*
# ... implying a Tcl word value: 1..1, 0..1
#
S setObjectParams {-foo:alias,1..*,boolean}
S method foo {x:0..1,boolean} {
set :foo $x
}
? {[S new -foo [list f f]] eval {info exists :foo}} \
"expected boolean but got \"f f\" for parameter \"x\""
S setObjectParams {-foo:alias,1..*,integer}
S method foo {x:1..1,integer} {
set :foo $x
}
? {[S new -foo [list a 1]] eval {info exists :foo}} \
"invalid value in \"a 1\": expected integer but got \"a\" for parameter \"-foo\""
? {[S new -foo [list 0 1]] eval {info exists :foo}} \
"expected integer but got \"0 1\" for parameter \"x\""
? {[S new -foo [list]] eval {info exists :foo}} \
"invalid value for parameter '-foo': list is not allowed to be empty"
? {[S new -foo 5] eval {info exists :foo}} 1
? {[S new -foo f] eval {info exists :foo}} \
"invalid value in \"f\": expected integer but got \"f\" for parameter \"-foo\""
S setObjectParams {-foo:alias,0..*,false}
S method foo {x:0..1,false} {
set :foo $x
}
? {[S new -foo [list a 1]] eval {info exists :foo}} \
"invalid value in \"a 1\": expected false but got \"a\" for parameter \"-foo\""
? {[S new -foo [list f 0]] eval {info exists :foo}} \
"expected false but got \"f 0\" for parameter \"x\""
? {[S new -foo [list t]] eval {info exists :foo}} \
"invalid value in \"t\": expected false but got \"t\" for parameter \"-foo\""
? {[S new -foo [list f]] eval {info exists :foo}} 1
? {[S new -foo [list]] eval {info exists :foo}} 1
}
nx::test case dispo-returns {
Class create R {
:public object method setObjectParams {spec} {
:protected method __object_configureparameter {} [list return $spec]
::nsf::parameter::cache::classinvalidate [current]
}
}
#
# Alias/forward dispositions are unavailable as parameter types of return checkers
#
set methods(raz) [R public object method raz {} {;}]
foreach dispoSpec {
alias,noarg
alias,method=xxx
{forward,method=%self xxx}
initcmd
} {
::nsf::method::property R $methods(raz) returns $dispoSpec
set trueSpec [lindex $dispoSpec 0]
? {R raz} "invalid value constraints \"$dispoSpec\": parameter option '$trueSpec' not allowed"
}
#
# Interactions between disposition types and the return value checkers
#
::nsf::configure checkresults true
# --
R setObjectParams -foo:alias,true
set methods(foo) [R public method foo {x:true} -returns false {
set :foo $x
}]
? {[R new] foo t} "expected false but got \"t\" as return value"
R setObjectParams [list -foo:alias,true bar:alias,false]
::nsf::method::property R $methods(foo) returns boolean
set methods(bar) [R public method bar {y:false} -returns true {
set :bar $y
}]
? {[R new -foo t f] eval {info exists :bar}} "expected true but got \"f\" as return value"
R setObjectParams [list -foo:alias,true bar:alias,false \
[list baz:alias,wideinteger,substdefault {[expr {2 ** 63 - 1}]}]]
::nsf::method::property R $methods(bar) returns boolean
set methods(baz) [R public method baz {z:wideinteger} -returns int32 {
set :baz $z
}]
? {[R new -foo t f [expr {2 ** 31}]] eval {info exists :foo}} 1
? {[R new -foo t f] eval {info exists :baz}} "expected int32 but got \"[expr {2 ** 63 - 1}]\" as return value"
? {[R new -foo t f] eval {info exists :baz}} "expected int32 but got \"[expr {2 ** 63 - 1}]\" as return value"
::nsf::method::property R $methods(baz) returns wideinteger
? {string is wideinteger [[R new -foo t f] eval {set :baz}]} 1
}
nx::test case dispo-callstack {
Class create Callee {
:public object method setObjectParams {spec} {
:protected method __object_configureparameter {} [list return $spec]
::nsf::parameter::cache::classinvalidate [current]
}
}
#
# uplevel, upvar (with alias and forward)
#
Callee public method call {{-level 2} x} {
#
# The callstack positioning corresponds to the one of
# alias/forward target methods in general:
# Level -1 -> C-level frame
# Level -2 -> Actual caller frame
#
# Note: Like any aliased methods, target methods of alias
# parameters do not have full callstack transparency (e.g., in a
# direct call to the target method, level -1 would resolve to the
# caller frame)
#
# ::nsf::__db_show_stack
uplevel $level [list set ix $x]
upvar $level $x _
incr _
}
foreach dispoSpec {
{-ah:alias,method=call {call:alias X}}
{{{-ah:forward,method=%self call}} {{call:forward,method=%self %method} X}}
{{{-ah:forward,method=uplevel %self call -level 1}} {{call:forward,method=uplevel %self %method -level 1} X}}
} {
Callee setObjectParams $dispoSpec
namespace eval __ {
? {info exists X} 0
? {info exists ix} 0
? {Callee new; info exists ix} 1
? {set X} 1
? {Callee new; info exists X} 1
? {Callee new; set X} 3
? {Callee new; set ix} X
? {Callee new -ah X X; set ix} X
? {set X} 6
? {info exists Y} 0
? {Callee new -ah X Y; set Y} 1
? {set X} 7
? {set ix} Y
}
namespace delete __
}
#
# TODO: Test missing elements for method declarations:
# /cls/ public class {} {} ...
#
# / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
# Test the ACTIVE/INACTIVE transparency for the method-variants of
# uplevel|upvar
#
Callee public object method run {} {
set self [self]
set objparams [:__object_configureparameter]
#
# The ? helper by default performs a [namespace eval] in the ::
# namespace, so the uplevel|upvar would happen in a different,
# non-testable callstack branch. Therefore, we have to build the
# tests around this limitation (for now)
#
? [list set _ [info exists X]] 0
? [list set _ [info exists ix]] 0
$self new
? [list set _ [info exists ix]] 1 "after 1. uplevel/upvar calls ('$objparams')"
? [list set _ [set X]] 1 "after 1. uplevel/upvar calls ('$objparams')"
$self new
? [list set _ [info exists X]] 1 "after 2. uplevel/upvar calls ('$objparams')"
$self new
? [list set _ [set X]] 3 "after 3. uplevel/upvar calls ('$objparams')"
$self new
? [list set _ [set ix]] X "after 4. uplevel/upvar calls ('$objparams')"
$self new -ah X X;
? [list set _ [set ix]] X "after 5. uplevel/upvar calls ('$objparams')"
? [list set _ [set X]] 6 "after 5. uplevel/upvar calls ('$objparams')"
? [list set _ [info exists Y]] 0
$self new -ah X Y;
? [list set _ [set Y]] 1 "after 6. uplevel/upvar calls ('$objparams')"
? [list set _ [set X]] 7 "after 6. uplevel/upvar calls ('$objparams')"
? [list set _ [set ix]] Y
}
# {{{-ah:forward,method=uplevel %self call -level 1}} {{call:forward,method=uplevel %self %method -level 1} X}}
#
# a) NSF/Nx methods upvar() and uplevel()
#
Callee public method call {x} {
:uplevel [list set ix $x]
:upvar $x _
incr _
}
foreach dispoSpec {
{-ah:alias,method=call {call:alias X}}
{{{-ah:forward,method=%self call}} {{call:forward,method=%self %method} X}}
} {
Callee setObjectParams $dispoSpec
Callee run
}
#
# b) [current callinglevel]
#
# ... with [uplevel [current callinglevel]] being equivalent to
# using NSF/Nx methods upvar() and uplevel() directly.
#
Callee public method call {x} {
# ::nsf::__db_show_stack
uplevel [current callinglevel] [list set ix $x]
upvar [current callinglevel] $x _
incr _
}
foreach dispoSpec {
{-ah:alias,method=call {call:alias X}}
{{{-ah:forward,method=%self call}} {{call:forward,method=%self %method} X}}
} {
Callee setObjectParams $dispoSpec
Callee run
}
#
# c) [current activelevel]
#
# ... Currently, in the current testing scenario, there is no
# effective difference between #activelevel and #callinglevel, both
# skip INACTIVE frames.
Callee mixins set [Class new {:public method call args { next }}]
foreach dispoSpec {
{-ah:alias,method=call {call:alias X}}
{{{-ah:forward,method=%self call}} {{call:forward,method=%self %method} X}}
} {
Callee setObjectParams $dispoSpec
Callee run
}
Callee public method call {x} {
uplevel [current activelevel] [list set ix $x]
upvar [current activelevel] $x _
incr _
}
foreach dispoSpec {
{-ah:alias,method=call {call:alias X}}
{{{-ah:forward,method=%self call}} {{call:forward,method=%self %method} X}}
} {
Callee setObjectParams $dispoSpec
Callee run
}
}
nx::test case alias-noarg {
Class create C {
:public object method setObjectParams {spec} {
:protected method __object_configureparameter {} [list return $spec]
::nsf::parameter::cache::classinvalidate [current]
}
:public method foo {args} {
set :foo $args
return $args
}
:public method bar {args} {
set :bar $args
return $args
}
}
#
# nopos arg with noargs, given
#
C setObjectParams {-bar:alias,noarg}
C create c1 -bar
? {c1 eval {info exists :bar}} 1
? {c1 eval {info exists :x}} 0
#
# nopos arg with noargs, not given
#
C setObjectParams {-bar:alias,noarg}
C create c1
? {c1 eval {info exists :bar}} 0
#
# pos arg with noargs
#
C setObjectParams {foo:alias,noarg}
C create c1
? {c1 eval {info exists :foo}} 1
#
# initcmd with default
#
C setObjectParams {{__init:cmd :foo}}
C create c1
? {c1 eval {info exists :foo}} 1
#
# pos arg with noargs and nonposarg with noargs, given
#
C setObjectParams {foo:alias,noarg -bar:alias,noarg}
C create c1 -bar
? {c1 eval {info exists :bar}} 1
? {c1 eval {info exists :foo}} 1
? {c1 eval {info exists :x}} 0
#
# optional initcmd, like in nx
#
C setObjectParams {initcmd:cmd,optional}
C create c1 {set :x 1}
? {c1 eval {info exists :x}} 1
#
# using a default value for initcmd
#
C setObjectParams {{initcmd:cmd ""}}
C create c1 {set :x 1}
C create c2
? {c1 eval {info exists :x}} 1
? {c2 eval {info exists :x}} 0
#
# optional initcmd + non-consuming (nrargs==0) posarg, provided
# initcmd
#
C setObjectParams {foo:alias,noarg initcmd:cmd,optional}
C create c1 {set :x 1}
? {c1 eval {info exists :x}} 1
? {c1 eval {info exists :foo}} 1
? {c1 eval {info exists :bar}} 0
#
# optional initcmd + non-consuming (nrargs==0) posarg, no value for
# initcmd
#
C setObjectParams {foo:alias,noarg initcmd:cmd,optional}
C create c1
? {c1 eval {info exists :x}} 0
? {c1 eval {info exists :foo}} 1
? {c1 eval {info exists :bar}} 0
#
# initcmd with default + non-consuming (nrargs==0) posarg, no value
# for initcmd
#
C setObjectParams {foo:alias,noarg {initcmd:cmd ""}}
C create c1
? {c1 eval {info exists :x}} 0
? {c1 eval {info exists :foo}} 1
? {c1 eval {info exists :bar}} 0
#
# non-consuming alias, nonpos alias with noarg, initcmd provided
#
C setObjectParams {foo:alias,noarg -bar:alias,noarg initcmd:cmd,optional}
C create c1 {set :x 1}
? {c1 eval {info exists :foo}} 1
? {c1 eval {info exists :bar}} 0
? {c1 eval {info exists :x}} 1
#
# non-consuming alias, nonpos alias with noarg, nonpos called, initcmd provided
#
C setObjectParams {foo:alias,noarg -bar:alias,noarg initcmd:cmd,optional}
C create c1 -bar {set :x 1}
? {c1 eval {info exists :foo}} 1
? {c1 eval {info exists :bar}} 1
? {c1 eval {info exists :x}} 1
#
# non-consuming alias, nonpos alias with noarg, no initcmd provided
#
C setObjectParams {foo:alias,noarg -bar:alias,noarg initcmd:cmd,optional}
C create c1
? {c1 eval {info exists :foo}} 1
? {c1 eval {info exists :bar}} 0
? {c1 eval {info exists :x}} 0
#
# non-consuming alias, nonpos alias with noarg, nonpos called, no
# initcmd provided
#
C setObjectParams {foo:alias,noarg -bar:alias,noarg initcmd:cmd,optional}
C create c1 -bar
? {c1 eval {info exists :foo}} 1
? {c1 eval {info exists :bar}} 1
? {c1 eval {info exists :x}} 0
}
#
# check inticmd + noarg (should not be allowed)
#
nx::test case alias-noarg {
Class create C {
:public object method setObjectParams {spec} {
:protected method __object_configureparameter {} [list return $spec]
::nsf::parameter::cache::classinvalidate [current]
}
}
C setObjectParams {initcmd:cmd,noarg}
? {C create c1} {parameter option "noarg" only allowed for parameter type "alias"}
}
#
# check alias + args
#
nx::test case alias-args {
Class create C {
:public object method setObjectParams {spec} {
:protected method __object_configureparameter {} [list return $spec]
::nsf::parameter::cache::classinvalidate [current]
}
:public method Residualargs args {
#puts stderr "aliased RESIDUALARGS <[llength $args]>"
#puts stderr "....... <$args>"
set :args $args
}
:public method residualargs args {
#puts stderr "residualargs <$args>"
}
}
C copy D
# TODO: check the meaning of these
C setObjectParams {args}
D setObjectParams {-a args}
# Configure object parameters to call method Residualargs with
# option args when args is used
C setObjectParams {args:alias,method=Residualargs,args}
D setObjectParams {-a args:alias,method=Residualargs,args}
# If no residual args are provided, the method residualargs is not
# called. This is the same rule as for all other consuming object
# parameter dispatches
? {C create c1} {::c1}
? {c1 eval {info exists :args}} 0
? {D create c1} {::c1}
? {c1 eval {info exists :args}} 0
# Residual args are provided, the method residualargs is
# called.
? {C create c1 1 2 3} {::c1}
? {c1 eval {info exists :args}} 1
? {c1 eval {set :args}} {1 2 3}
? {D create c1 1 2 3} {::c1}
? {c1 eval {info exists :args}} 1
? {c1 eval {set :args}} {1 2 3}
#
# Provide a default for args.
#
C setObjectParams {{args:alias,method=Residualargs,args {hello world}}}
# use the default
? {C create c1} {::c1}
? {c1 eval {info exists :args}} 1
? {c1 eval {set :args}} {hello world}
# override the default
? {C create c1 a b c} {::c1}
? {c1 eval {info exists :args}} 1
? {c1 eval {set :args}} {a b c}
#
# don't allow other types for parameter option "args"
#
C setObjectParams {{args:alias,int,method=Residualargs,args {hello world}}}
? {C create c1} {refuse to redefine parameter type of 'args' from type 'integer' to type 'args'}
? {nsf::is object c1} 0
C setObjectParams {{args:int,alias,method=Residualargs,args {hello world}}}
? {C create c1} {refuse to redefine parameter type of 'args' from type 'integer' to type 'args'}
? {nsf::is object c1} 0
#
# don't allow multiplicity settings for parameter option "args"
#
C setObjectParams {{args:alias,method=Residualargs,0..n,args {hello world}}}
? {C create c1} {multiplicity settings for variable argument parameter "args" not allowed}
? {nsf::is object c1} 0
C setObjectParams {args:alias,method=Residualargs,args,1..n}
? {C create c1} {multiplicity settings for variable argument parameter "args" not allowed}
? {nsf::is object c1} 0
#
# make sure, parameter with parameter option "args" is used in last parameter
#
C setObjectParams {a:alias,method=Residualargs,args -b:integer}
? {C create c1 hello world} {parameter option "args" invalid for parameter "a"; only allowed for last parameter}
? {nsf::is object c1} 0
}
nx::test case alias-init {
Class create C {
:public object method setObjectParams {spec} {
:protected method __object_configureparameter {} [list return $spec]
::nsf::parameter::cache::classinvalidate [current]
}
:method init {} {
incr :y
}
}
# call init between -a and -b
C setObjectParams {-a init:alias,noarg -b:integer}
? {C create c1} {::c1}
# "init" should be called only once
? {c1 eval {set :y}} 1
}
nx::test case submethods-via-aliasparams {
#
# Could move to submethods.test?
#
Class create C {
:public object method setObjectParams {spec} {
:protected method __object_configureparameter {} [list return $spec]
::nsf::parameter::cache::classinvalidate [current]
}
}
# A depth-1 submethod ...
C public method "FOO foo" {} {
# next
append :msg "[::nsf::current]--[::nsf::current methodpath]--[::nsf::current method]"
}
# A depth-2 submethod ...
C public method "BAR BOO buu" {} {
append :msg "[::nsf::current]--[::nsf::current methodpath]--[::nsf::current method]"
}
# // Ordinary dispatch //
# The message send below expands into the following callstack
# structure (when viewed at from within foo(), N is the anonymous
# call site)
#
# N+3 |:CscFrame @Type(ENSEMBLE) | <-- foo (leaf)
# N+2 |:CscFrame @Call(ENSEMBLE) | <-- FOO (root)
# N+1 |:TclFrame| e.g. cmd, [namespace eval], [apply]
? {
[C create c1] FOO foo; # N
c1 eval {set :msg}
} "::c1--FOO foo--foo"
#
# Submethod levels greater than 1 turn into intermittent frames:
# N+4 |:CscFrame @Type(ENSEMBLE) | <-- buu (leaf)
# N+3 |:CscFrame @Type(ENSEMBLE) @Call(ENSEMBLE)| <-- BOO (intermittent)
# N+2 |:CscFrame @Call(ENSEMBLE) | <-- BAR (root)
# N+1 |:TclFrame|
#
? {
[C create c3] BAR BOO buu; # N
c3 eval {set :msg}
} "::c3--BAR BOO buu--buu"
# // Parameter (alias) dispatch //
#
# In contrast to an ordinary dispatch, a parameter dispatch results
# in a different callstack structure, due to the interfering
# configure():
#
# N+5 |:CscFrame @Type(ENSEMBLE)| <-- foo (leaf)
# N+4 |:CscFrame @Call(ENSEMBLE)| <-- FOO (root)
# N+3 |:CscFrame @INACTIVE| <-- (INNER configure() frame)
# N+2 |:ObjFrame| <-- ::c2 (OUTER configure() frame)
# N+1 |:TclFrame|
C setObjectParams [list FOO:alias]
? {
[C create c2 foo] eval {set :msg}; # N
} "::c2--FOO foo--foo"
#
# 1) Interleaving @Type(INACTIVE) frames through indirection
#
# a) Ahead of the ensemble "root" frame (i.e., indirection at the
# level the receiver object)
#
Class create M1 {
:public method FOO args {
next
}
}
C mixins set M1
# N+4 |:CscFrame @Type(ENSEMBLE) | <-- foo (leaf)
# N+3 |:CscFrame @Call(ENSEMBLE) | <-- FOO (root)
# N+2 |:CscFrame @INACTIVE| <-- M1.FOO
# N+1 |:TclFrame|
C setObjectParams [list]
? {
[C create c1] FOO foo; # N
c1 eval {set :msg}
} "::c1--FOO foo--foo"
# N+6 |:CscFrame @Type(ENSEMBLE)| <-- foo (leaf)
# N+5 |:CscFrame @Call(ENSEMBLE)| <-- FOO (root)
# N+4 |:CscFrame @INACTIVE| <-- M1.FOO
# N+3 |:CscFrame @INACTIVE| <-- (INNER configure() frame)
# N+2 |:ObjFrame| <-- ::c2 (OUTER configure() frame)
# N+1 |:TclFrame|
C setObjectParams [list FOO:alias]
? {
[C create c2 foo] eval {set :msg}; # N
} "::c2--FOO foo--foo"
# ... the filter variant ...
C mixins set {}
C public method intercept args {
next
}
C filters set intercept
# N+4 |:CscFrame @Type(ENSEMBLE) | <-- foo (leaf)
# N+3 |:CscFrame @Call(ENSEMBLE) | <-- FOO (root)
# N+2 |:CscFrame @INACTIVE| <-- intercept
# N+1 |:TclFrame|
C setObjectParams [list]
? {
[C create c1] FOO foo; # N
c1 eval {set :msg}
} "::c1--FOO foo--foo"
# N+6 |:CscFrame @Type(ENSEMBLE)| <-- foo (leaf)
# N+5 |:CscFrame @Call(ENSEMBLE)| <-- FOO (root)
# N+4 |:CscFrame @INACTIVE| <-- intercept
# N+3 |:CscFrame @INACTIVE| <-- (INNER configure() frame)
# N+2 |:ObjFrame| <-- ::c2 (OUTER configure() frame)
# N+1 |:TclFrame|
C setObjectParams [list FOO:alias]
? {
[C create c2 foo] eval {set :msg}; # N
} "::c2--FOO foo--foo"
C filters set ""
# / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
# b) Between root and intermittent or in-between the set of
# intermittent frames (i.e., indirection at the level of
# container/ensemble objects)
# NOTE: Filters and mixins registered for the container object do
# not interleave in ensemble dispatches ... the dispatch lookup
# (along the next path) always starts at the top-level
# (calling) object. As a result, there are no intermediate frames to
# be expected ...
Class create M2 {
:public method foo args {
return "[current class]--[next]"
}
}
C::slot::__FOO object mixins set M2
? {C::slot::__FOO foo} "::M2--::C::slot::__FOO--foo--foo"
C::slot::__FOO eval {unset :msg}
C setObjectParams [list]
? {
[C create c1] FOO foo; # N
c1 eval {set :msg}
} "::c1--FOO foo--foo"
C::slot::__FOO object mixins set {}
C::slot::__FOO public object method intercept {} {
return "[current]--[next]"
}
C::slot::__FOO object filters set intercept
? {C::slot::__FOO foo} "::C::slot::__FOO--::C::slot::__FOO--foo--foo"
C setObjectParams [list]
? {
[C create c1] FOO foo; # N
c1 eval {set :msg}
} "::c1--FOO foo--foo"
# --
Class create M2 {
:public method "FOO foo" args {
append :msg "(1)--[current nextmethod]"
next
#puts stderr ++++++++++++++++++
append :msg "--(3)--[current class]--[current methodpath]--[current]"
#puts stderr ++++++++++++++++++
}
}
C mixins set M2
# N+4 |:CscFrame @Type(ENSEMBLE) | <-- C.FOO.foo (leaf)
# N+2 |:CscFrame @Call(ENSEMBLE) | <-- C.FOO (root)
# N+3 |:CscFrame @INACTIVE @Type(ENSEMBLE)| <-- M2.FOO.foo
# N+2 |:CscFrame @INACTIVE @Call(ENSEMBLE) | <-- M2.FOO
# N+1 |:TclFrame|
C setObjectParams [list]
? {
#puts stderr "/ / / / / / / / / / / "
[C create c1] FOO foo; # N
#puts stderr "/ / / / / / / / / / / "
c1 eval {set :msg}
} "(1)--::c1--FOO foo--foo--(3)--::M2--FOO foo--::c1"
C mixins set {}
}
nx::test case dispo-configure-transparency {
Class create C {
:public object method setObjectParams {spec} {
:protected method __object_configureparameter {} [list return $spec]
::nsf::parameter::cache::classinvalidate [current]
}
}
::proc foo {} {
error [::nsf::current]-[::nsf::current methodpath]-[::nsf::current method]
}
#
::nsf::method::alias C FOO ::foo
? {[C create c] FOO} "::c-FOO-FOO"
C setObjectParams [list [list FOO:alias,noarg ""]]
? {C create c} "::c-FOO-FOO"
C public method "show me" {} {
set :msg [::nsf::current]-[::nsf::current methodpath]
}
C setObjectParams [list -show:alias]
? {[C create c -show me] eval {info exists :msg}} 1
? {[C create c -show me] eval {set :msg}} "::c-show me"
#
# ... with mixin indirection
#
# ... at the calling object level / configure() ...
Class create M {
:public method configure args {
next;
}
:public method foo args {
next;
}
:public method FOO args {
error [::nsf::current]-[::nsf::current methodpath]
}
}
C setObjectParams [list [list FOO:alias,noarg ""]]
C mixins add M
? {C create c} "::c-FOO"
C mixins set {}
# ... at the called object level
Object create ::callee {
::nsf::object::property [self] perobjectdispatch true
:public object method foo {} {
error [::nsf::current]-[::nsf::current methodpath]
}
}
::nsf::method::alias C FOO ::callee
C setObjectParams [list [list FOO:alias,noarg ""]]
? {C create c} "::c" "Defaultmethod of callee is invoked ..."
C setObjectParams [list [list FOO:alias "foo"]]
? {C create c} "::callee-FOO foo" "foo leaf method is selected ..."
::callee object mixins add M
? {C create c} "::callee-FOO foo" "With mixin ..."
#
# ... at the calling object level / ensemble path
#
# This scenario effectively stacks additional call frames to be
# traversed by CallStackMethodPath(). However, these frames precede
# the first ensemble frame, that's why they are skipped by
# CallStackMethodPath().
M eval {
:public method FOO args {
puts stderr "!!!!! FOO MIXIN ...."
next;
}
}
? {C create c} "::callee-FOO foo" "With mixin ..."
#
# ... with filter indirection: tbd
#
}
nx::test case dispo-object-targets {
Object create obj
::nsf::object::property obj perobjectdispatch true
Class create C
Class create T {
:public object method setObjectParams {spec} {
:protected method __object_configureparameter {} [list return $spec]
::nsf::parameter::cache::classinvalidate [current]
}
}
#
# 1. Behavioural baseline: An alias method binding an object
#
set methods(z) [::nsf::method::alias T z ::obj]
? {[T new] z} ::obj "Aliased dispatch to defaultmethod"
? {[T new] z uff} "::obj: unable to dispatch method 'uff'" \
"Aliased dispatch to unknown method (default unknown handler)"
Class create UnknownHandler {
:method unknown {callInfo args} {
#
# callInfo is a Tcl list. For ensemble dispatches, it contains
# the complete message: delegator <ensemble ...> <unknown method>; for
# ordinary dispatches, the list has a single element: <unknown
# method>
#
# methodpath [current methodpath]
# puts stderr "CALLINFO='$callInfo' args=$args"
switch [llength $callInfo] {
1 {
error "UNKNOWNMETHOD-$callInfo"
}
default {
set delegator [lindex $callInfo 0]
set unknownMethod [lindex $callInfo end]
set path [lrange $callInfo 1 end-1]
error "CURRENT-[current]-DELEGATOR-$delegator-UNKNOWNMETHOD-$unknownMethod-PATH-$path"
}
}
}
}
::obj object mixins set UnknownHandler
? {[T create t] z uff} "CURRENT-::obj-DELEGATOR-::t-UNKNOWNMETHOD-uff-PATH-z" \
"Aliased dispatch to unknown method (custom unknown handler)"
set x [UnknownHandler create handledObj]
::nsf::object::property handledObj perobjectdispatch true
set methods(ix) [::nsf::method::alias ::obj ix $x]
? {[T create t] z ix baff} "CURRENT-$x-DELEGATOR-::obj-UNKNOWNMETHOD-baff-PATH-z ix" \
"Aliased dispatch to unknown method (custom unknown handler)"
#
# 2. Obj targets via alias disposition parameters
#
#
# a) direct dispatch (non-aliased) with fully qualified selector (::*)
#
::obj object mixins set {}
T setObjectParams x:alias,method=::obj
? {T create t XXX} "::t: unable to dispatch method '::obj'" "FQ dispatch with default unknown handler"
::T mixins set UnknownHandler
? {T create t XXX} "UNKNOWNMETHOD-::obj" "FQ dispatch with custom unknown handler"
#
# b) calls to the defaultmethod of the aliased object
#
UnknownHandler method defaultmethod {} {
set :defaultmethod 1
}
::obj object mixins set UnknownHandler
T setObjectParams [list [list z:alias,noarg ""]]
? {T create t; ::obj eval {info exists :defaultmethod}} 1 \
"Calling defaultmethod via alias+noarg combo with empty default"
T setObjectParams [list [list z:alias,noarg "XXX"]]
? {T create t; ::obj eval {info exists :defaultmethod}} 1 \
"Calling defaultmethod via alias+noarg nonempty with \
default combo (default is not passed)"
#
# b) intermediary object aliases, non-fully qualified selector
#
T setObjectParams [list [list z:alias,noarg ""]]
? {T create tt} ::tt "sending the msg: tt->z()"
#
# ISSUE: positional objparam + alias + noarg -> what's the point?
# noarg & ?z? are irritating, ?z? should not be printed!
#
? {T create t XXX} "invalid argument 'XXX', maybe too many arguments; should be \"::t configure ?/z/?\""
::obj object mixins set {}
T setObjectParams [list z:alias]
? {T create tt YYY} "::obj: unable to dispatch method 'YYY'" "sending the msg: tt->z(::obj)->YYY()"
::obj object mixins set UnknownHandler
? {T create tt YYY} "CURRENT-::obj-DELEGATOR-::tt-UNKNOWNMETHOD-YYY-PATH-z" \
"sending the msg: tt->z(::obj)->YYY()"
::obj object mixins set {}
T setObjectParams [list -z:alias]
? {T create tt -z YYY} "::obj: unable to dispatch method 'YYY'" "sending the msg: tt->z(::obj)->YYY()"
::obj object mixins set UnknownHandler
? {T create tt -z YYY} "CURRENT-::obj-DELEGATOR-::tt-UNKNOWNMETHOD-YYY-PATH-z" \
"sending the msg: tt->z(::obj)->YYY()"
#
# [current methodpath] & empty selector strings:
#
::obj object mixins set {}
T setObjectParams [list z:alias]
? {T create tt ""} "::obj: unable to dispatch method ''" "sending the msg: tt->z->{}()"
::obj object mixins set UnknownHandler
? {T create tt ""} "CURRENT-::obj-DELEGATOR-::tt-UNKNOWNMETHOD--PATH-z" "sending the msg: tt->z->{}()"
T setObjectParams [list -z:alias]
? {T create tt -z ""} "CURRENT-::obj-DELEGATOR-::tt-UNKNOWNMETHOD--PATH-z" "sending the msg: tt->z()"
#
# Dispatch with a method handle
#
::T mixins set {}
? [list [T create t] $methods(z) XXX] \
"CURRENT-::obj-DELEGATOR-::t-UNKNOWNMETHOD-XXX-PATH-::nsf::classes::T::z"
T setObjectParams x:alias,method=$methods(z)
? {T create t XXX} "CURRENT-::obj-DELEGATOR-::t-UNKNOWNMETHOD-XXX-PATH-::nsf::classes::T::z" \
"Non-object FQ selector with default unknown handler"
::T mixins set UnknownHandler
? {T create t XXX} "CURRENT-::obj-DELEGATOR-::t-UNKNOWNMETHOD-XXX-PATH-::nsf::classes::T::z" \
"Non-object FQ selector with custom unknown handler"
#
# A Tcl proc is allowed?!
#
proc ::baz {x} {
set :baz $x
}
T setObjectParams x:alias,method=::baz
? {[T create t XXX] eval {info exists :baz}} 1
? {[T create t XXX] eval {set :baz}} XXX
#
# TBD: nested objects
#
#
# TBD: object-system methods
#
}
#
# check xotcl with residual args
#
nx::test case xotcl-residualargs {
package prefer latest
puts stderr "XOTcl loaded: [package req XOTcl 2.0]"
? {::xotcl::Class create XD -set x 1} "::XD"
#? {c1 eval {info exists :args}} 0
? {XD __object_configureparameter} "-instfilter:filterreg,alias,0..n -superclass:alias,0..n -instmixin:mixinreg,alias,0..n {-__default_metaclass ::xotcl::Class} {-__default_superclass ::xotcl::Object} -mixin:mixinreg,alias,0..n -filter:filterreg,alias,0..n -class:class,alias args:alias,method=residualargs,args"
#
# test passing arguments to init
#
::XD instproc init args {
set :args $args
}
::XD create x1 1 2 3 -set x 1
? {x1 exists x} 1
? {x1 exists args} 1
? {x1 set args} {1 2 3}
}
nx::test configure -count 1000
nx::test case xotcl-residualargs2 {
::xotcl::Class create XC -parameter {a b c}
::XC instproc init args {set :x $args; incr :y}
? {XC create xc1 -a 1} ::xc1
? {XC create xc2 x y -a 1} ::xc2
::nx::Class create C {
:property a
:property b
:property c
:method init args {set :x $args; incr :y}
}
? {C create c1 -a 1} ::c1
? {xc2 eval {info exists :a}} 1
? {xc2 eval {set :x}} {x y}
? {xc2 eval {set :y}} 1
? {c1 eval {info exists :a}} 1
? {c1 eval {set :y}} 1
}
nx::test case xotcl-residualargs-upleveling {
#
# Test callstack resolution for upvar/uplevel in
# parameter-dispatched methods under residualargs() ...
#
package prefer latest
package req XOTcl 2.0
xotcl::Class C -proc onTheFly {name args} {
? [list set _ [info exists ix]] 0
? [list set _ [info exists Y]] 0
set c [[self] $name {*}$args]
? [list set _ [info exists ix]] 1
? [list set _ [set ix]] Y
? [list set _ [info exists Y]] 1
? [list set _ [set Y]] 1
return $c
} -instproc call {x} {
# ::nsf::__db_show_stack
my uplevel [list set ix $x]
my upvar $x _
incr _
} -instproc call2 {x} {
# ::nsf::__db_show_stack
uplevel [self callinglevel] [list set ix $x]
upvar [self callinglevel] $x _
incr _
}
C onTheFly c1 -call Y
C onTheFly c1 -call2 Y
}
# TODO: what todo with object parameter inspection for names with
# alias, forward... "names" do not always correspond with vars set.
nx::test case class-configure-default {
# Background: when class is created, it is created with a "default"
# superclass of "::nx::Object". This is defined in the slot for
# superclass in nx.tcl
nx::Class create P
? {P info superclasses} ::nx::Object
#
# When we pass the superclass a different value, this is certainly used.
#
nx::Class create Q -superclass P
? {Q info superclasses} ::P
#
# When we call configure on the superclass, we do not want the
# default to be used to reset it to ::nx::Object. Therefore, the
# configure uses the default for parameters with METHOD_INVOCATION
# only, when the object is not yet initialized.
#
Q configure
? {Q info superclasses} ::P
}
#
# Local variables:
# mode: tcl
# tcl-indent-level: 2
# indent-tabs-mode: nil
# End:
|