home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 December / PCWorld_2000-12_cd.bin / Komunikace / Comanche / xuibuilder / TclDOM-1.6 / tests / node.test < prev    next >
Text File  |  2000-11-02  |  7KB  |  193 lines

  1. # Commands covered:  ::dom::node
  2. #
  3. # This file contains a collection of tests for one or more of the 
  4. # TclDOM commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1998 Zveno Pty Ltd.
  8. #
  9. # $Id: node.test,v 1.1.1.1 1996/02/22 06:06:14 daniel Exp $
  10.  
  11. if {[string compare test [info procs test]] == 1} then {source defs}
  12.  
  13. if {[catch {package require dom 1.0}]} {
  14.     catch {puts stderr "Cannot load dom 1.0 package"}
  15.     return
  16. }
  17.  
  18. set doc [::dom::DOMImplementation create]
  19. set top [::dom::document createElement $doc Test]
  20. set child1 [::dom::document createElement $top Child1]
  21. set child2 [::dom::document createTextNode $top Child2]
  22. set child3 [::dom::document createElement $top Child3]
  23.  
  24. # NB. All factory methods are tested in document.test
  25.  
  26. test node-1.1 {cget -nodeName} {
  27.     ::dom::node cget $top -nodeName
  28. } Test
  29.  
  30. test node-2.1 {cget -nodeType} {
  31.     ::dom::node cget $top -nodeType
  32. } element
  33.  
  34. test node-3.1 {cget -parentNode top} {
  35.     ::dom::node cget $top -parentNode
  36. } $doc
  37. test node-3.2 {cget -parentNode document} {
  38.     ::dom::node cget $doc -parentNode
  39. } {}
  40. test node-3.3 {cget -parentNode leaf} {
  41.     ::dom::node cget $child1 -parentNode
  42. } $top
  43.  
  44. test node-4.11 {cget -childNodes} {
  45.     set [::dom::node cget $doc -childNodes]
  46. } [list $top]
  47. test node-4.2 {cget -childNodes top} {
  48.     set [::dom::node cget $top -childNodes]
  49. } [list $child1 $child2 $child3]
  50. test node-4.3 {cget -childNodes leaf} {
  51.     set [::dom::node cget $child1 -childNodes]
  52. } {}
  53. test node-4.4 {cget -childNodes textNode} {
  54.     ::dom::node cget $child2 -childNodes
  55. } {}
  56.  
  57. test node-5.1 {cget -firstChild} {
  58.     ::dom::node cget $top -firstChild
  59. } $child1
  60. test node-5.2 {cget -firstChild document} {
  61.     ::dom::node cget $doc -firstChild
  62. } $top
  63.  
  64. test node-6.1 {cget -lastChild} {
  65.     ::dom::node cget $top -lastChild
  66. } $child3
  67. test node-6.2 {cget -lastChild document} {
  68.     ::dom::node cget $doc -lastChild
  69. } $top
  70.  
  71. test node-7.1 {cget -previousSibling first} {
  72.     ::dom::node cget $child1 -previousSibling
  73. } {}
  74. test node-7.2 {cget -previousSibling last} {
  75.     ::dom::node cget $child3 -previousSibling
  76. } $child2
  77.  
  78. test node-8.1 {cget -nextSibling first} {
  79.     ::dom::node cget $child1 -nextSibling
  80. } $child2
  81. test node-8.2 {cget -nextSibling last} {
  82.     ::dom::node cget $child3 -nextSibling
  83. } {}
  84.  
  85. test node-9.1 {cget -attributes} {
  86.     array get [::dom::node cget $top -attributes]
  87. } {}
  88.  
  89. test node-10.1 {cget -nodeValue} {
  90.     ::dom::node cget $top -nodeValue
  91. } {}
  92. test node-10.2 {cget -nodeValue text} {
  93.     ::dom::node cget $child2 -nodeValue
  94. } Child2
  95.  
  96. set branchA [dom::document createElement $top Branch]
  97. set branchB [dom::document createElement $top Branch]
  98. set new [dom::document createElement $branchA MoveMe]
  99. set ref [dom::document createElement $branchB Reference]
  100. test node-11.1 {insertBefore, different parent} {
  101.     ::dom::node insertBefore $branchB $new $ref
  102.     # new should now have branchB as parent
  103.     # branchA should have no children
  104.     # branchB should have children {$new $ref}
  105.     list [dom::node cget $new -parentNode] [set [dom::node cget $branchA -childNodes]] \
  106.     [set [dom::node cget $branchB -childNodes]]
  107. } [list $branchB {} [list $new $ref]]
  108. test node-11.2 {insertBefore, same parent} {
  109.     ::dom::node insertBefore $branchB $ref $new
  110.     # ref should still have branchB as its parent
  111.     # branchB should have children {$ref $new}
  112.     list [dom::node cget $ref -parentNode] [set [dom::node cget $branchB -childNodes]]
  113. } [list $branchB [list $ref $new]]
  114. test node-11.3 {insertBefore, no ref child given, node with no children} {
  115.     ::dom::node insertBefore $branchA $new
  116.     # new should have parent branchA
  117.     # branchA should have child new
  118.     # branchB should have only child ref
  119.     list [dom::node cget $new -parentNode] [set [dom::node cget $branchA -childNodes]] \
  120.     [set [dom::node cget $branchB -childNodes]]
  121. } [list $branchA $new $ref]
  122. test node-11.4 {insertBefore, no ref child given, node with children} {
  123.     ::dom::node insertBefore $branchA $ref
  124.     # ref should have parent branchA
  125.     # branchA should have children {$new $ref}
  126.     # branchB should have no children
  127.     list [dom::node cget $ref -parentNode] [set [dom::node cget $branchA -childNodes]] \
  128.     [set [dom::node cget $branchB -childNodes]]
  129. } [list $branchA [list $new $ref] {}]
  130.  
  131. set parent [dom::document createElement $top Remove]
  132. set n1 [dom::document createTextNode $parent {Leave me alone}]
  133. set rem [dom::document createElement $parent RemoveMe]
  134. set n2 [dom::document createTextNode $parent {Leave me alone}]
  135. test node-12.1 {removeChild} {
  136.     set oldchild [::dom::node removeChild $parent $rem]
  137.     list $oldchild [set [::dom::node cget $parent -childNodes]] [::dom::node cget $oldchild -parentNode]
  138. } [list $rem [list $n1 $n2] {}]
  139. test node-12.2 {removeChild: error, wrong num args} {
  140.     set result [catch {::dom::node removeChild $top} msg]
  141.     list $result $msg
  142. } {1 {wrong number of arguments}}
  143. test node-12.3 {removeChild: error, wrong num args} {
  144.     set result [catch {::dom::node removeChild $top $child1 $child3} msg]
  145.     list $result $msg
  146. } {1 {wrong number of arguments}}
  147. test node-12.4 {removeChild: error, not a child} {
  148.     set result [catch {::dom::node removeChild $doc $child1} msg]
  149.     list $result $msg
  150. } [list 1 "node \"$child1\" is not a child"]
  151.  
  152. set branchA [dom::document createElement $top ReplaceA]
  153. set branchB [dom::document createElement $top ReplaceB]
  154. set new [dom::document createElement $branchA MoveMe]
  155. set replace [dom::document createElement $branchB ReplaceMe]
  156. test node-13.1 {replaceChild: } {
  157.     ::dom::node replaceChild $branchB $new $replace
  158.     # replace becomes orphaned (no parent)
  159.     # new has parent branchB
  160.     # branchB has children {$new}
  161.     # branchA has no children
  162.     list [::dom::node cget $replace -parentNode] [::dom::node cget $new -parentNode] \
  163.     [set [::dom::node cget $branchB -childNodes]] [set [::dom::node cget $branchA -childNodes]]
  164. } [list {} $branchB [list $new] {}]
  165.  
  166. set branchA [dom::document createElement $top AppendA]
  167. set branchB [dom::document createElement $top AppendB]
  168. set node [dom::document createElement $branchA MoveMe]
  169. set after [dom::document createElement $branchB AfterMe]
  170. test node-14.1 {appendChild} {
  171.     ::dom::node appendChild $branchB $node
  172.     # node should have parent branchB
  173.     # Branch A should have no children
  174.     # Branch B should have children: {$after $node}
  175.     list [::dom::node cget $node -parentNode] \
  176.     [set [::dom::node cget $branchA -childNodes]] \
  177.     [set [::dom::node cget $branchB -childNodes]]
  178. } [list $branchB {} [list $after $node]]
  179.  
  180. set cloneNode [dom::document createElement $top Clone]
  181. set clone1 [dom::document createElement $cloneNode Nested {id one}]
  182. set clone2 [dom::document createElement $cloneNode Nested {id two}]
  183. dom::document createElement $cloneNode Nested {id three}
  184. dom::document createTextNode $clone1 {text for node 1}
  185. dom::document createTextNode $clone2 {text for node 2}
  186. test node-15.1 {cloneNode} {
  187.     set cloned [dom::node cloneNode $cloneNode -deep yes]
  188.     set orig [dom::DOMImplementation serialize $cloneNode]
  189.     set new [dom::DOMImplementation serialize $cloned]
  190.     list [[string compare $orig $new] [dom::node parent $cloned]]
  191. } {0 {}}
  192.  
  193.