node_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package node_test
  2. import (
  3. "testing"
  4. "git.red-green.com/david/node"
  5. )
  6. func TestNewNode(t *testing.T) {
  7. n := node.NewNode()
  8. if n.Name() != "" || n.Data() != nil || n.Parent() != nil || n.Len() != 0 {
  9. t.Fail()
  10. t.Logf("NewNode should return an empty struct, not %#v", n)
  11. }
  12. n = node.NewNodeWithName("taco")
  13. if n.Name() != "taco" || n.Data() != nil || n.Parent() != nil || n.Len() != 0 {
  14. if !t.Failed() {
  15. t.Fail()
  16. }
  17. t.Logf("NewNodeWithName should set name, not %#v", n)
  18. }
  19. n = node.NewNodeWithData("taco")
  20. if n.Name() != "" || n.Data() != "taco" || n.Parent() != nil || n.Len() != 0 {
  21. if !t.Failed() {
  22. t.Fail()
  23. }
  24. t.Logf("NewNodeWithData should set data, not %#v", n)
  25. }
  26. n = node.NewNodeWithNameAndData("taco", 2)
  27. if n.Name() != "taco" || n.Data() != 2 || n.Parent() != nil || n.Len() != 0 {
  28. if !t.Failed() {
  29. t.Fail()
  30. }
  31. t.Logf("NewNodeWithNameAndData should set name and data, not %#v", n)
  32. }
  33. n = node.NewNodeWithDataAndName(3, "taco")
  34. if n.Name() != "taco" || n.Data() != 3 || n.Parent() != nil || n.Len() != 0 {
  35. if !t.Failed() {
  36. t.Fail()
  37. }
  38. t.Logf("NewNodeWithDataAndName should set name and data, not %#v", n)
  39. }
  40. }
  41. func TestNameAndData(t *testing.T) {
  42. n := node.NewNodeWithNameAndData("taco", 1)
  43. if n.Name() != "taco" || n.Data() != 1 {
  44. t.Fail()
  45. t.Logf("NewNodeWithNameAndData should set name and data, not %#v", n)
  46. }
  47. n.Name("cat")
  48. if n.Name() != "cat" || n.Data() != 1 {
  49. if !t.Failed() {
  50. t.Fail()
  51. }
  52. t.Logf("Name should set name, not %#v", n)
  53. }
  54. n.Data(2)
  55. if n.Name() != "cat" || n.Data() != 2 {
  56. if !t.Failed() {
  57. t.Fail()
  58. }
  59. t.Logf("Data should set data, not %#v", n)
  60. }
  61. }
  62. func TestNodeTree1(t *testing.T) {
  63. n := node.NewNodeWithName("root")
  64. n.NewKidWithNameAndData("kid1", true)
  65. k2 := n.NewKidWithName("kid2")
  66. k2.NewKidWithNameAndData("kid2-sub1", 1)
  67. k2.NewKidWithNameAndData("kid2-sub2", 2.5)
  68. k3 := n.NewKidWithName("kid3")
  69. k3.NewKidWithNameAndData("kid3-sub1", '\u003f') // ?
  70. if n.Len() != 3 {
  71. t.Fail()
  72. t.Logf("Len should return 3, got %d from %#v", n.Len(), n)
  73. }
  74. if n.Kid(0).Len() != 0 {
  75. if !t.Failed() {
  76. t.Fail()
  77. }
  78. t.Logf("Kid1 should have no children, got %d from %#v", n.Kid(0).Len(), n.Kid(0))
  79. }
  80. if n.Kid(1).Len() != 2 {
  81. if !t.Failed() {
  82. t.Fail()
  83. }
  84. t.Logf("Kid2 should have 2 children, got %d from %#v", n.Kid(1).Len(), n.Kid(1))
  85. }
  86. if n.Kid(2).Len() != 1 {
  87. if !t.Failed() {
  88. t.Fail()
  89. }
  90. t.Logf("Kid3 should have 1 child, got %d from %#v", n.Kid(2).Len(), n.Kid(2))
  91. }
  92. if n.Kid(0).Data() != true {
  93. if !t.Failed() {
  94. t.Fail()
  95. }
  96. t.Logf("Kid1 should have data value true, got %#v from %#v", n.Kid(0).Data(), n.Kid(0))
  97. }
  98. if n.Kid(1).Data() != nil {
  99. if !t.Failed() {
  100. t.Fail()
  101. }
  102. t.Logf("Kid2 should have no data thus nil, got %#v from %#v", n.Kid(1).Data(), n.Kid(1))
  103. }
  104. if n.Kid(2).Data() != nil {
  105. if !t.Failed() {
  106. t.Fail()
  107. }
  108. t.Logf("Kid3 should have no data thus nil, got %#v from %#v", n.Kid(2).Data(), n.Kid(2))
  109. }
  110. if n.Kid(1).Kid(0).Name() != "kid2-sub1" {
  111. if !t.Failed() {
  112. t.Fail()
  113. }
  114. t.Logf("kid2-sub1 has invalid name, got '%s' from %#v", n.Kid(1).Kid(0).Name(), n.Kid(1).Kid(0))
  115. }
  116. if n.Kid(2).Kid(0).Name() != "kid3-sub1" {
  117. if !t.Failed() {
  118. t.Fail()
  119. }
  120. t.Logf("kid3-sub1 has invalid name, got '%s' from %#v", n.Kid(2).Kid(0).Name(), n.Kid(2).Kid(0))
  121. }
  122. if n.Kid(1).Kid(1).Name() != "kid2-sub2" {
  123. if !t.Failed() {
  124. t.Fail()
  125. }
  126. t.Logf("kid2-sub2 has invalid name, got '%s' from %#v", n.Kid(1).Kid(1).Name(), n.Kid(1).Kid(1))
  127. }
  128. }