node_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package node_test
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "git.red-green.com/david/node"
  6. )
  7. func TestNewNode(t *testing.T) {
  8. n := node.NewNode()
  9. if n.Name() != "" || n.Data() != nil || n.Parent() != nil || n.Len() != 0 {
  10. t.Fail()
  11. t.Logf("NewNode should return an empty struct, not %#v", n)
  12. }
  13. n = node.NewNodeWithName("taco")
  14. if n.Name() != "taco" || n.Data() != nil || n.Parent() != nil || n.Len() != 0 {
  15. if !t.Failed() {
  16. t.Fail()
  17. }
  18. t.Logf("NewNodeWithName should set name, not %#v", n)
  19. }
  20. n = node.NewNodeWithData("taco")
  21. if n.Name() != "" || n.Data() != "taco" || n.Parent() != nil || n.Len() != 0 {
  22. if !t.Failed() {
  23. t.Fail()
  24. }
  25. t.Logf("NewNodeWithData should set data, not %#v", n)
  26. }
  27. n = node.NewNodeWithNameAndData("taco", 2)
  28. if n.Name() != "taco" || n.Data() != 2 || n.Parent() != nil || n.Len() != 0 {
  29. if !t.Failed() {
  30. t.Fail()
  31. }
  32. t.Logf("NewNodeWithNameAndData should set name and data, not %#v", n)
  33. }
  34. n = node.NewNodeWithDataAndName(3, "taco")
  35. if n.Name() != "taco" || n.Data() != 3 || n.Parent() != nil || n.Len() != 0 {
  36. if !t.Failed() {
  37. t.Fail()
  38. }
  39. t.Logf("NewNodeWithDataAndName should set name and data, not %#v", n)
  40. }
  41. }
  42. func TestNameAndData(t *testing.T) {
  43. n := node.NewNodeWithNameAndData("taco", 1)
  44. if n.Name() != "taco" || n.Data() != 1 {
  45. t.Fail()
  46. t.Logf("NewNodeWithNameAndData should set name and data, not %#v", n)
  47. }
  48. n.Name("cat")
  49. if n.Name() != "cat" || n.Data() != 1 {
  50. if !t.Failed() {
  51. t.Fail()
  52. }
  53. t.Logf("Name should set name, not %#v", n)
  54. }
  55. n.Data(2)
  56. if n.Name() != "cat" || n.Data() != 2 {
  57. if !t.Failed() {
  58. t.Fail()
  59. }
  60. t.Logf("Data should set data, not %#v", n)
  61. }
  62. }
  63. func TestNodeTree1(t *testing.T) {
  64. n := node.NewNodeWithName("root")
  65. n.NewKidWithNameAndData("kid1", true)
  66. k2 := n.NewKidWithName("kid2")
  67. k2.NewKidWithNameAndData("kid2-sub1", 1)
  68. k2.NewKidWithNameAndData("kid2-sub2", 2.5)
  69. k3 := n.NewKidWithName("kid3")
  70. k3.NewKidWithNameAndData("kid3-sub1", '\u003f') // ?
  71. if n.Len() != 3 {
  72. t.Fail()
  73. t.Logf("Len should return 3, got %d from %#v", n.Len(), n)
  74. }
  75. if n.Kid(0).Len() != 0 {
  76. if !t.Failed() {
  77. t.Fail()
  78. }
  79. t.Logf("Kid1 should have no children, got %d from %#v", n.Kid(0).Len(), n.Kid(0))
  80. }
  81. if n.Kid(1).Len() != 2 {
  82. if !t.Failed() {
  83. t.Fail()
  84. }
  85. t.Logf("Kid2 should have 2 children, got %d from %#v", n.Kid(1).Len(), n.Kid(1))
  86. }
  87. if n.Kid(2).Len() != 1 {
  88. if !t.Failed() {
  89. t.Fail()
  90. }
  91. t.Logf("Kid3 should have 1 child, got %d from %#v", n.Kid(2).Len(), n.Kid(2))
  92. }
  93. if n.Kid(0).Data() != true {
  94. if !t.Failed() {
  95. t.Fail()
  96. }
  97. t.Logf("Kid1 should have data value true, got %#v from %#v", n.Kid(0).Data(), n.Kid(0))
  98. }
  99. if n.Kid(1).Data() != nil {
  100. if !t.Failed() {
  101. t.Fail()
  102. }
  103. t.Logf("Kid2 should have no data thus nil, got %#v from %#v", n.Kid(1).Data(), n.Kid(1))
  104. }
  105. if n.Kid(2).Data() != nil {
  106. if !t.Failed() {
  107. t.Fail()
  108. }
  109. t.Logf("Kid3 should have no data thus nil, got %#v from %#v", n.Kid(2).Data(), n.Kid(2))
  110. }
  111. if n.Kid(1).Kid(0).Name() != "kid2-sub1" {
  112. if !t.Failed() {
  113. t.Fail()
  114. }
  115. t.Logf("kid2-sub1 has invalid name, got '%s' from %#v", n.Kid(1).Kid(0).Name(), n.Kid(1).Kid(0))
  116. }
  117. if n.Kid(2).Kid(0).Name() != "kid3-sub1" {
  118. if !t.Failed() {
  119. t.Fail()
  120. }
  121. t.Logf("kid3-sub1 has invalid name, got '%s' from %#v", n.Kid(2).Kid(0).Name(), n.Kid(2).Kid(0))
  122. }
  123. if n.Kid(1).Kid(1).Name() != "kid2-sub2" {
  124. if !t.Failed() {
  125. t.Fail()
  126. }
  127. t.Logf("kid2-sub2 has invalid name, got '%s' from %#v", n.Kid(1).Kid(1).Name(), n.Kid(1).Kid(1))
  128. }
  129. }
  130. func TestJSON2NodeA1(t *testing.T) {
  131. pay := []byte(`
  132. {
  133. "test": {
  134. "kid1": 1,
  135. "kid2": 2.5,
  136. "kid3": [
  137. 1,
  138. 2,
  139. 3
  140. ],
  141. "kid4": [
  142. {
  143. "name": "Taco",
  144. "price": 3.75
  145. },
  146. {
  147. "name": "Soda",
  148. "price": 1.99
  149. }
  150. ]
  151. }
  152. }
  153. `)
  154. n := node.NewNode()
  155. err := json.Unmarshal(pay, &n)
  156. if err != nil {
  157. t.Fail()
  158. t.Logf("json.Unmarshal: %v", err)
  159. }
  160. if n.Name() != "test" {
  161. if !t.Failed() {
  162. t.Fail()
  163. }
  164. t.Logf("Root node name expected \"test\", but got \"%s\"", n.Name())
  165. }
  166. if n.Len() != 4 {
  167. if !t.Failed() {
  168. t.Fail()
  169. }
  170. t.Logf("Root node should have 4 children, has %d", n.Len())
  171. }
  172. if n.KidByName("kid1") == nil {
  173. if !t.Failed() {
  174. t.Fail()
  175. }
  176. t.Logf("Root node should have a child of \"kid1\", has %d:", n.Len())
  177. for _, k := range n.Kids() {
  178. t.Logf("\"%s\"", k.Name())
  179. }
  180. }
  181. k1 := n.KidByName("kid1")
  182. k3 := n.KidByName("kid3")
  183. k4 := n.KidByName("kid4")
  184. if k1.Data() != 1.0 { // JSON has Number, no Integer, thus Go marshals it into a Float
  185. if !t.Failed() {
  186. t.Fail()
  187. }
  188. t.Logf("Kid1 node should have data of 1, got %#v", k1.Data())
  189. }
  190. if k3.Len() != 3 {
  191. if !t.Failed() {
  192. t.Fail()
  193. }
  194. t.Logf("Kid3 node should have 3 children, has %d", k3.Len())
  195. }
  196. if k3.KidByName("2").Data() != 3.0 { // JSON has Number, no Integer, thus Go marshals it into a Float
  197. if !t.Failed() {
  198. t.Fail()
  199. }
  200. t.Logf("Kid3 sub-node \"2\" should have data of 3, got %#v from %#v", k3.KidByName("2").Data(), k3.KidByName("2"))
  201. }
  202. if k4.Len() != 2 {
  203. if !t.Failed() {
  204. t.Fail()
  205. }
  206. t.Logf("Kid4 node should have 2 children, has %d", k4.Len())
  207. }
  208. for idx, k := range k4.Kids() {
  209. if k.Len() != 2 {
  210. if !t.Failed() {
  211. t.Fail()
  212. }
  213. t.Logf("Kid4 sub-node \"%s\" should have 2 children, has %d", k.Name(), k.Len())
  214. continue
  215. }
  216. if k.KidByName("name") == nil {
  217. if !t.Failed() {
  218. t.Fail()
  219. }
  220. t.Logf("Kid4 sub-node \"%s\" should have a child of \"name\", has %d:", k.Name(), k.Len())
  221. for _, k0 := range k.Kids() {
  222. t.Logf("\"%s\"", k0.Name())
  223. }
  224. continue
  225. }
  226. if k.KidByName("price") == nil {
  227. if !t.Failed() {
  228. t.Fail()
  229. }
  230. t.Logf("Kid4 sub-node \"%s\" should have a child of \"price\", has %d:", k.Name(), k.Len())
  231. for _, k0 := range k.Kids() {
  232. t.Logf("\"%s\"", k0.Name())
  233. }
  234. continue
  235. }
  236. switch idx {
  237. case 0: // name="Taco", price=3.75
  238. if k.KidByName("name").Data().(string) != "Taco" {
  239. if !t.Failed() {
  240. t.Fail()
  241. }
  242. t.Logf("Kid4 sub-node \"%s\" failed name check \"Taco\", got \"%s\"", k.Name(), k.KidByName("name").Data().(string))
  243. }
  244. if k.KidByName("price").Data() != 3.75 {
  245. if !t.Failed() {
  246. t.Fail()
  247. }
  248. t.Logf("Kid4 sub-node \"%s\" failed price check 3.75, got %#v", k.Name(), k.KidByName("price").Data())
  249. }
  250. case 1: // name="Soda", price=1.99
  251. if k.KidByName("name").Data().(string) != "Soda" {
  252. if !t.Failed() {
  253. t.Fail()
  254. }
  255. t.Logf("Kid4 sub-node \"%s\" failed name check \"Soda\", got \"%s\"", k.Name(), k.KidByName("name").Data().(string))
  256. }
  257. if k.KidByName("price").Data() != 1.99 {
  258. if !t.Failed() {
  259. t.Fail()
  260. }
  261. t.Logf("Kid4 sub-node \"%s\" failed price check 1.99, got %#v", k.Name(), k.KidByName("price").Data())
  262. }
  263. default:
  264. if !t.Failed() {
  265. t.Fail()
  266. }
  267. t.Logf("Unexpected sub-node for Kid4 \"%s\"", k.Name())
  268. }
  269. }
  270. }