node_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package main
  2. import (
  3. "testing"
  4. "golang.org/x/exp/slices"
  5. )
  6. func TestNodeString(t *testing.T) {
  7. n := Node{
  8. Line: "Test",
  9. }
  10. n.AddTag("1")
  11. if n.String() != "Test" {
  12. t.Errorf("Expected 1 tab for depth=1, got '%s'", n.String())
  13. }
  14. n1 := n.NewChild()
  15. n1.Line = "Hello World"
  16. if n.StringAll() != "Test\n\tHello World\n" {
  17. t.Errorf("Expected 'Test\n\tHello World\n' got '%s'", n.StringAll())
  18. }
  19. }
  20. func TestNodeAddChild(t *testing.T) {
  21. n := Node{
  22. Line: "Test",
  23. }
  24. n.AddTag("1")
  25. n1 := Node{
  26. Line: "Test 2",
  27. }
  28. n1.AddTag("2")
  29. n.AddChild(&n1)
  30. if len(n.Children) != 1 {
  31. t.Errorf("Expected to add the child, but didn't (Children Count: %d)", len(n.Children))
  32. }
  33. if !slices.Equal(n1.Parent.Tags, n.Tags) {
  34. t.Errorf("Expected child's parent to have the same tag, expected '1', got '%s'", n1.Parent.Tags)
  35. }
  36. if !slices.Equal(n.Children[0].Tags, n1.Tags) {
  37. t.Errorf("Expected child's tag to be the same as other node's tag, expected '2', got '%s'", n.Children[0].Tags)
  38. }
  39. }
  40. func TestNodeNewChild(t *testing.T) {
  41. n := Node{
  42. Line: "Test",
  43. }
  44. n.AddTag("1")
  45. n1 := n.NewChild()
  46. n1.AddTag("2")
  47. n1.Line = "Test 2"
  48. if len(n.Children) != 1 {
  49. t.Errorf("Expected to add the child, but didn't (Children Count: %d)", len(n.Children))
  50. }
  51. if n.Children[0].Line != n1.Line {
  52. t.Errorf("Expected 'Test 2', got '%s' ('%s')", n.Children[0].Line, n1.Line)
  53. }
  54. if n1.Depth() != 1 {
  55. t.Errorf("Expected depth=1, got %d (%d)", n1.Depth(), n.Children[0].Depth())
  56. }
  57. }
  58. func TestNodeKey(t *testing.T) {
  59. n := Node{
  60. Line: "Test Value",
  61. }
  62. if n.Key() != "Test" {
  63. t.Errorf("Expected 'Test', got '%s' (%s)", n.Key(), n.Line)
  64. }
  65. n.Line = "\"Some Test\" Value"
  66. if n.Key() != "Some Test" {
  67. t.Errorf("Expected 'Some Test', got '%s' (%s)", n.Key(), n.Line)
  68. }
  69. n.Line = "Test \"Some Value\""
  70. if n.Key() != "Test" {
  71. t.Errorf("Expected 'Test', got '%s' (%s)", n.Key(), n.Line)
  72. }
  73. n.Line = "\"Some Test\" \"Some Value\""
  74. if n.Key() != "Some Test" {
  75. t.Errorf("Expected 'Some Test', got '%s', (%s)", n.Key(), n.Line)
  76. }
  77. }
  78. func TestNodeValue(t *testing.T) {
  79. n := Node{
  80. Line: "Test Value",
  81. }
  82. if n.Value() != "Value" {
  83. t.Errorf("Expected 'Value', got '%s', (%s)", n.Value(), n.Line)
  84. }
  85. n.Line = "\"Some Test\" Value"
  86. if n.Value() != "Value" {
  87. t.Errorf("Expected 'Value', got '%s', (%s)", n.Value(), n.Line)
  88. }
  89. n.Line = "Test \"Some Value\""
  90. if n.Value() != "Some Value" {
  91. t.Errorf("Expected 'Some Value', got '%s', (%s)", n.Value(), n.Line)
  92. }
  93. n.Line = "\"Some Test\" \"Some Value\""
  94. if n.Value() != "Some Value" {
  95. t.Errorf("Expected 'Some Value', got '%s', (%s)", n.Value(), n.Line)
  96. }
  97. }
  98. func TestNodeValueTypes(t *testing.T) {
  99. n := Node{
  100. Line: "\"Some Super Test\" 13",
  101. }
  102. if n.Key() != "Some Super Test" {
  103. t.Errorf("Expected 'Some Super Test', got '%s' (%s)", n.Key(), n.Line)
  104. }
  105. if n.Value() != "13" {
  106. t.Errorf("Expected '13', got '%s' (%s)", n.Value(), n.Line)
  107. }
  108. if n.ValueInt() != 13 {
  109. t.Errorf("Expected 13, got %d (%s)", n.ValueInt(), n.Line)
  110. }
  111. n.Line = "\"Some Super Duper Test\" 9.81"
  112. if n.Key() != "Some Super Duper Test" {
  113. t.Errorf("Expected 'Some Super Duper Test', got '%s' (%s)", n.Key(), n.Line)
  114. }
  115. if n.Value() != "9.81" {
  116. t.Errorf("Expected '9.81', got '%s', (%s)", n.Value(), n.Line)
  117. }
  118. if n.ValueFloat32() != 9.81 {
  119. t.Errorf("Expected 9.81, got %f, (%s)", n.ValueFloat32(), n.Line)
  120. }
  121. if n.ValueFloat64() != 9.81 {
  122. t.Errorf("Expected 'Value', got %f, (%s)", n.ValueFloat64(), n.Line)
  123. }
  124. }
  125. func TestNodeSetters(t *testing.T) {
  126. n := Node{}
  127. n.SetKey("Hello World")
  128. if n.Line != "\"Hello World\"" {
  129. t.Errorf("Expected '\"Hello World\"', got '%s'", n.Line)
  130. }
  131. n.SetValue("Meow")
  132. if n.Line != "\"Hello World\" Meow" {
  133. t.Errorf("Expected '\"Hello World\" Meow', got '%s'", n.Line)
  134. }
  135. n.SetValue(9.81)
  136. if n.Line != "\"Hello World\" 9.81" {
  137. t.Errorf("Expected '\"Hello World\" 9.81', got '%s'", n.Line)
  138. }
  139. n.Set("Taco", "Cat")
  140. if n.Line != "Taco Cat" {
  141. t.Errorf("Expected 'Taco Cat', got '%s'", n.Line)
  142. }
  143. n.Set("Meow", nil)
  144. if n.Line != "Meow" {
  145. t.Errorf("Expected 'Meow', got '%s'", n.Line)
  146. }
  147. n.Set("I am super man", 42)
  148. if n.Line != "\"I am super man\" 42" {
  149. t.Errorf("Expected '\"I am super man\" 42', got '%s'", n.Line)
  150. }
  151. }
  152. func TestNodeChildActions(t *testing.T) {
  153. n := Node{}
  154. n.AddTag("root")
  155. n.Set("I am root", nil)
  156. n1 := n.NewChild()
  157. n1.Set("I am a child", 1)
  158. n2 := n.NewChild()
  159. n2.Set("I am a child", 2)
  160. n2_1 := n2.NewChild()
  161. n2_1.Set("I am a child", 3)
  162. if n.Len() != 2 {
  163. t.Errorf("Expected 2 children, got %d", len(n.Children))
  164. }
  165. t1, err := n.Child(0)
  166. if err != nil {
  167. t.Errorf("Unexpected error: %v", err)
  168. }
  169. if t1.Line != "\"I am a child\" 1" {
  170. t.Errorf("Expected '\"I am a child\" 1', got '%s'", t1.Line)
  171. }
  172. t1, err = n.Child(1)
  173. if err != nil {
  174. t.Errorf("Unexpected error: %v", err)
  175. }
  176. if t1.Len() != 1 {
  177. t.Errorf("Expected 1 child, got %d", t1.Len())
  178. }
  179. t2, err := t1.Child(0)
  180. if err != nil {
  181. t.Errorf("Unexpected error: %v", err)
  182. }
  183. if t2.Line != "\"I am a child\" 3" {
  184. t.Errorf("Expected '\"I am a child\" 3', got '%s'", t2.Line)
  185. }
  186. // Split the child off
  187. n3, err := n.SplitChild(1)
  188. if err != nil {
  189. t.Errorf("Unexpected error: %v", err)
  190. }
  191. if n3.Line != "\"I am a child\" 2" {
  192. t.Errorf("Expected '\"I am a child\" 2', got '%s'", n3.Line)
  193. }
  194. if n3.Len() != 1 {
  195. t.Errorf("Expected 1 child, got %d", n3.Len())
  196. }
  197. // Add it back in
  198. n.AddChild(n3)
  199. // Now replace the first child with a new one
  200. n3 = &Node{}
  201. n3.AddTag("Kid 1")
  202. n3.Set("I am a child", 1.1)
  203. err = n.ReplaceChild(0, n3)
  204. if err != nil {
  205. t.Errorf("Unexpected error: %v", err)
  206. }
  207. n3_1 := n3.NewChild()
  208. n3_1.Set("I am a child", 1.2)
  209. if n.Len() != 2 {
  210. t.Errorf("Expected 2 children, got %d", n.Len())
  211. }
  212. t1, err = n.Child(0)
  213. if err != nil {
  214. t.Errorf("Unexpected error: %v", err)
  215. }
  216. if t1.Len() != 1 {
  217. t.Errorf("Expected 1 child, got %d", t1.Len())
  218. }
  219. t1, err = t1.DetachFromParent()
  220. if err != nil {
  221. t.Errorf("Unexpected error: %v", err)
  222. }
  223. if t1.Parent != nil {
  224. t.Errorf("Expected to detach from parent, should be root then")
  225. }
  226. n.RmAllChildren()
  227. if n.Len() != 0 {
  228. t.Errorf("Expected no children, got %d", n.Len())
  229. }
  230. }