home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / TCL / ITCL / _ITCL.TAR / usr / lib / itcl / tests / basic.test < prev    next >
Encoding:
Text File  |  1994-04-25  |  9.5 KB  |  440 lines

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