home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / devel / tcl / itcl1_31.z / itcl1_31 / tcldev / itcl-1.3 / tests / basic.test < prev    next >
Encoding:
Text File  |  1993-09-23  |  8.0 KB  |  401 lines

  1. #
  2. # Basic tests for class definition and method/proc access
  3. # ----------------------------------------------------------------------
  4. #   AUTHOR:  Michael J. McLennan       Phone: (215)770-2842
  5. #            AT&T Bell Laboratories   E-mail: aluxpo!mmc@att.com
  6. #
  7. #     SCCS:  @(#)basic.test    1.3 (9/9/93)
  8. # ----------------------------------------------------------------------
  9. #            Copyright (c) 1993  AT&T  All Rights Reserved
  10. # ======================================================================
  11.  
  12. # ----------------------------------------------------------------------
  13. #  CLEAN THE SLATE
  14. # ----------------------------------------------------------------------
  15. foreach obj [itcl_info objects -class Foo] {
  16.     $obj delete
  17. }
  18.  
  19. # ----------------------------------------------------------------------
  20. #  CREATING OBJECTS
  21. # ----------------------------------------------------------------------
  22. test {Create a simple object} {
  23.     Foo x
  24. } {
  25.     $result == "x"
  26. }
  27.  
  28. test {Make sure that object names cannot be duplicated} {
  29.     catch "Foo x" errmsg
  30. } {
  31.     $result == 1
  32. }
  33.  
  34. test {Create another object} {
  35.     Foo xx
  36. } {
  37.     $result == "xx"
  38. }
  39.  
  40. test {Create an object with an automatic name} {
  41.     Foo #auto
  42. } {
  43.     [string match Foo* $result]
  44. }
  45.  
  46. test {Get list of objects in a class} {
  47.     itcl_info objects -class Foo
  48. } {
  49.     [llength $result] == 3
  50. }
  51.  
  52. # ----------------------------------------------------------------------
  53. #  PUBLIC VARIABLES
  54. # ----------------------------------------------------------------------
  55. test {Info: all public variables} {
  56.     x info public
  57. } {
  58.     [test_cmp_lists $result {Foo::blit Foo::blat Foo::blot}]
  59. }
  60.  
  61. test {Info: public variable initial value} {
  62.     x info public blit -init
  63. } {
  64.     $result == ""
  65. }
  66.  
  67. test {Info: public variable initial value (undefined)} {
  68.     x info public blit -value
  69. } {
  70.     $result == "<undefined>"
  71. }
  72.  
  73. test {Info: public variable initial value} {
  74.     x info public blat -init
  75. } {
  76.     $result == 0
  77. }
  78.  
  79. test {Info: public variable current value} {
  80.     x info public blot -value
  81. } {
  82.     $result == 1
  83. }
  84.  
  85. test {Info: public variable config statement} {
  86.     x info public blit -config
  87. } {
  88.     $result == ""
  89. }
  90.  
  91. test {Info: public variable config statement} {
  92.     x info public blot -config
  93. } {
  94.     $result == {global WATCH; set WATCH "blot=$blot"}
  95. }
  96.  
  97. # ----------------------------------------------------------------------
  98. #  CONFIG-ING PUBLIC VARIABLES
  99. # ----------------------------------------------------------------------
  100. test {Setting public variables via "config"} {
  101.     x config -blit 27 -blat xyz
  102. } {
  103.     $result == "Foo::blit Foo::blat"
  104. }
  105.  
  106. test {Info: public variable init/current value} {
  107.     x info public blit -init -value
  108. } {
  109.     $result == {{} 27}
  110. }
  111.  
  112. test {Info: public variable init/current value} {
  113.     x info public blat -init -value
  114. } {
  115.     $result == {0 xyz}
  116. }
  117.  
  118. test {"config" is ordinary arg if it is not last arg} {
  119.     x configx -blit pdq
  120. } {
  121.     $result == {-blit|pdq}
  122. }
  123.  
  124. test {Public variables with "config" code} {
  125.     set WATCH ""
  126.     concat [x config -blot abc] / $WATCH
  127. } {
  128.     $result == "Foo::blot / blot=abc"
  129. }
  130.  
  131. test {Make sure object data is local to objects} {
  132.     x config -blit abc
  133.     xx config -blit xyz
  134.     concat [x info public blit -value] / [xx info public blit -value]
  135. } {
  136.     $result == "abc / xyz"
  137. }
  138.  
  139. # ----------------------------------------------------------------------
  140. #  PROTECTED VARIABLES
  141. # ----------------------------------------------------------------------
  142. test {Info: all protected variables} {
  143.     x info protected
  144. } {
  145.     [test_cmp_lists $result {Foo::_blit Foo::_blat Foo::this}]
  146. }
  147.  
  148. test {Info: protected "this" variable} {
  149.     x info protected this -value
  150. } {
  151.     $result == "x"
  152. }
  153.  
  154. test {Info: protected "this" variable} {
  155.     xx info protected this -value
  156. } {
  157.     $result == "xx"
  158. }
  159.  
  160. test {Info: protected variable initial value} {
  161.     x info protected _blit -init
  162. } {
  163.     $result == ""
  164. }
  165.  
  166. test {Info: protected variable access/value} {
  167.     x do {set _blit rst}
  168. } {
  169.     $result == "Foo says 'rst'" &&
  170.     [x info protected _blit -value] == "rst"
  171. }
  172.  
  173. # ----------------------------------------------------------------------
  174. #  COMMON VARIABLES
  175. # ----------------------------------------------------------------------
  176. test {Info: all protected variables} {
  177.     x info common
  178. } {
  179.     [test_cmp_lists $result {Foo::foos Foo::nfoo}]
  180. }
  181.  
  182. test {Info: common variable initial value} {
  183.     x info common foos -init
  184. } {
  185.     $result == ""
  186. }
  187.  
  188. test {Info: common variable initial value} {
  189.     x info common nfoo -init
  190. } {
  191.     $result == 0
  192. }
  193.  
  194. test {Info: common variable access/value} {
  195.     x do {set nfoo 999}
  196.     x info common nfoo -value
  197. } {
  198.     $result == 999
  199. }
  200.  
  201. test {Make sure common data is really common} {
  202.     x do {set nfoo 0}
  203.     x info common nfoo -value
  204. } {
  205.     $result == [xx info common nfoo -value]
  206. }
  207.  
  208. test {Access common data in proc} {
  209.     x do {set nfoo 10}
  210.     Foo :: nfoos
  211. } {
  212.     $result == 10
  213. }
  214.  
  215. test {Common variables can be initialized within class definition} {
  216.     x do {if {[info exists foos(_ignore_)]} {set foos(_ignore_)}}
  217. } {
  218.     $result == "Foo says 'foos-is-now-an-array'"
  219. }
  220.  
  221. test {Arrays as common data} {
  222.     Foo :: foos
  223. } {
  224.     [test_cmp_lists $result [itcl_info objects -class Foo]]
  225. }
  226.  
  227. # ----------------------------------------------------------------------
  228. #  METHODS
  229. # ----------------------------------------------------------------------
  230. test {Info: all methods} {
  231.     x info method
  232. } {
  233.     [test_cmp_lists $result {
  234.         Foo::isa Foo::delete
  235.         Foo::constructor Foo::destructor
  236.         Foo::nothing Foo::do Foo::xecho
  237.         Foo::config Foo::xconfig Foo::configx
  238.     }]
  239. }
  240.  
  241. test {Info: method args} {
  242.     x info method nothing -args
  243. } {
  244.     $result == ""
  245. }
  246.  
  247. test {Info: method args} {
  248.     x info method xconfig -args
  249. } {
  250.     $result == "x config"
  251. }
  252.  
  253. test {Info: method body} {
  254.     x info method nothing -body
  255. } {
  256.     $result == ""
  257. }
  258.  
  259. test {Info: method body} {
  260.     x info method xconfig -body
  261. } {
  262.     $result == {
  263.         return "$x|$config"
  264.     }
  265. }
  266.  
  267. test {Info: built-in methods} {
  268.     x info method delete
  269. } {
  270.     $result == "Foo::delete <built-in>"
  271. }
  272.  
  273. test {Info: built-in methods (args only)} {
  274.     x info method delete -args
  275. } {
  276.     $result == "<built-in>"
  277. }
  278.  
  279. test {Info: built-in methods (body only)} {
  280.     x info method delete -body
  281. } {
  282.     $result == "<built-in>"
  283. }
  284.  
  285. # ----------------------------------------------------------------------
  286. #  PROCS
  287. # ----------------------------------------------------------------------
  288. test {Info: all procs} {
  289.     x info proc
  290. } {
  291.     [test_cmp_lists $result {Foo::info Foo::echo Foo::foos Foo::nfoos}]
  292. }
  293.  
  294. test {Info: proc args} {
  295.     x info proc nfoos -args
  296. } {
  297.     $result == ""
  298. }
  299.  
  300. test {Info: proc args} {
  301.     x info proc foos -args
  302. } {
  303.     $result == "{pattern *}"
  304. }
  305.  
  306. test {Info: proc body} {
  307.     x info proc nfoos -body
  308. } {
  309.     $result == {
  310.         return $nfoo
  311.     }
  312. }
  313.  
  314. test {Info: proc body} {
  315.     x info body nfoos
  316. } {
  317.     $result == {
  318.         return $nfoo
  319.     }
  320. }
  321.  
  322. # ----------------------------------------------------------------------
  323. #  ARGUMENT LISTS
  324. # ----------------------------------------------------------------------
  325. test {Default arguments can get assigned a proper value} {
  326.     Foo :: foos x*
  327. } {
  328.     [test_cmp_lists $result {x xx}]
  329. }
  330.  
  331. test {Default value for "config" argument} {
  332.     x config
  333. } {
  334.     $result == "Foo::blit Foo::blat" &&
  335.     [x info public blit -value] == "auto" &&
  336.     [x info public blat -value] == "matic"
  337. }
  338.  
  339. test {"args" formal argument absorbs extra arguments} {
  340.     Foo :: echo abc 1 2 3
  341. } {
  342.     $result == "abc | 3: 1 2 3"
  343. }
  344.  
  345. test {"args" formal argument absorbs extra arguments} {
  346.     Foo :: echo def
  347. } {
  348.     $result == "def | 0: "
  349. }
  350.  
  351. test {"args" formal argument absorbs extra arguments} {
  352.     x xecho abc 1 2 3
  353. } {
  354.     $result == "abc | 3: 1 2 3"
  355. }
  356.  
  357. test {"args" formal argument absorbs extra arguments} {
  358.     x xecho def
  359. } {
  360.     $result == "def | 0: "
  361. }
  362.  
  363. test {Extra args cause an error} {
  364.     catch "x configx arg arg error"
  365. } {
  366.     $result != 0
  367. }
  368.  
  369. test {Extra args cause an error} {
  370.     catch "x nothing error"
  371. } {
  372.     $result != 0
  373. }
  374.  
  375. # ----------------------------------------------------------------------
  376. #  DELETING OBJECTS
  377. # ----------------------------------------------------------------------
  378. test {Delete an object} {
  379.     x delete
  380. } {
  381.     $result == ""
  382. }
  383.  
  384. test {Delete an object} {
  385.     xx delete
  386. } {
  387.     $result == ""
  388. }
  389.  
  390. test {Destructor is properly invoked} {
  391.     Foo :: foos
  392. } {
  393.     [test_cmp_lists $result [itcl_info objects -class Foo]]
  394. }
  395.  
  396. test {Object names are removed as commands} {
  397.     expr {[info commands x] == "" && [info commands xx] == ""}
  398. } {
  399.     $result == 1
  400. }
  401.