123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- package node_test
- import (
- "encoding/json"
- "testing"
- "git.red-green.com/david/node"
- )
- func TestNewNode(t *testing.T) {
- n := node.NewNode()
- if n.Name() != "" || n.Data() != nil || n.Parent() != nil || n.Len() != 0 {
- t.Fail()
- t.Logf("NewNode should return an empty struct, not %#v", n)
- }
- n = node.NewNodeWithName("taco")
- if n.Name() != "taco" || n.Data() != nil || n.Parent() != nil || n.Len() != 0 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("NewNodeWithName should set name, not %#v", n)
- }
- n = node.NewNodeWithData("taco")
- if n.Name() != "" || n.Data() != "taco" || n.Parent() != nil || n.Len() != 0 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("NewNodeWithData should set data, not %#v", n)
- }
- n = node.NewNodeWithNameAndData("taco", 2)
- if n.Name() != "taco" || n.Data() != 2 || n.Parent() != nil || n.Len() != 0 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("NewNodeWithNameAndData should set name and data, not %#v", n)
- }
- n = node.NewNodeWithDataAndName(3, "taco")
- if n.Name() != "taco" || n.Data() != 3 || n.Parent() != nil || n.Len() != 0 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("NewNodeWithDataAndName should set name and data, not %#v", n)
- }
- }
- func TestNameAndData(t *testing.T) {
- n := node.NewNodeWithNameAndData("taco", 1)
- if n.Name() != "taco" || n.Data() != 1 {
- t.Fail()
- t.Logf("NewNodeWithNameAndData should set name and data, not %#v", n)
- }
- n.Name("cat")
- if n.Name() != "cat" || n.Data() != 1 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Name should set name, not %#v", n)
- }
- n.Data(2)
- if n.Name() != "cat" || n.Data() != 2 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Data should set data, not %#v", n)
- }
- }
- func TestNodeTree1(t *testing.T) {
- n := node.NewNodeWithName("root")
- n.NewKidWithNameAndData("kid1", true)
- k2 := n.NewKidWithName("kid2")
- k2.NewKidWithNameAndData("kid2-sub1", 1)
- k2.NewKidWithNameAndData("kid2-sub2", 2.5)
- k3 := n.NewKidWithName("kid3")
- k3.NewKidWithNameAndData("kid3-sub1", '\u003f') // ?
- if n.Len() != 3 {
- t.Fail()
- t.Logf("Len should return 3, got %d from %#v", n.Len(), n)
- }
- if n.Kid(0).Len() != 0 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid1 should have no children, got %d from %#v", n.Kid(0).Len(), n.Kid(0))
- }
- if n.Kid(1).Len() != 2 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid2 should have 2 children, got %d from %#v", n.Kid(1).Len(), n.Kid(1))
- }
- if n.Kid(2).Len() != 1 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid3 should have 1 child, got %d from %#v", n.Kid(2).Len(), n.Kid(2))
- }
- if n.Kid(0).Data() != true {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid1 should have data value true, got %#v from %#v", n.Kid(0).Data(), n.Kid(0))
- }
- if n.Kid(1).Data() != nil {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid2 should have no data thus nil, got %#v from %#v", n.Kid(1).Data(), n.Kid(1))
- }
- if n.Kid(2).Data() != nil {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid3 should have no data thus nil, got %#v from %#v", n.Kid(2).Data(), n.Kid(2))
- }
- if n.Kid(1).Kid(0).Name() != "kid2-sub1" {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("kid2-sub1 has invalid name, got '%s' from %#v", n.Kid(1).Kid(0).Name(), n.Kid(1).Kid(0))
- }
- if n.Kid(2).Kid(0).Name() != "kid3-sub1" {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("kid3-sub1 has invalid name, got '%s' from %#v", n.Kid(2).Kid(0).Name(), n.Kid(2).Kid(0))
- }
- if n.Kid(1).Kid(1).Name() != "kid2-sub2" {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("kid2-sub2 has invalid name, got '%s' from %#v", n.Kid(1).Kid(1).Name(), n.Kid(1).Kid(1))
- }
- }
- func TestJSON2NodeA1(t *testing.T) {
- pay := []byte(`
- {
- "test": {
- "kid1": 1,
- "kid2": 2.5,
- "kid3": [
- 1,
- 2,
- 3
- ],
- "kid4": [
- {
- "name": "Taco",
- "price": 3.75
- },
- {
- "name": "Soda",
- "price": 1.99
- }
- ]
- }
- }
- `)
- n := node.NewNode()
- err := json.Unmarshal(pay, &n)
- if err != nil {
- t.Fail()
- t.Logf("json.Unmarshal: %v", err)
- }
- if n.Name() != "test" {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Root node name expected \"test\", but got \"%s\"", n.Name())
- }
- if n.Len() != 4 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Root node should have 4 children, has %d", n.Len())
- }
- if n.KidByName("kid1") == nil {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Root node should have a child of \"kid1\", has %d:", n.Len())
- for _, k := range n.Kids() {
- t.Logf("\"%s\"", k.Name())
- }
- }
- k1 := n.KidByName("kid1")
- k3 := n.KidByName("kid3")
- k4 := n.KidByName("kid4")
- if k1.Data() != 1.0 { // JSON has Number, no Integer, thus Go marshals it into a Float
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid1 node should have data of 1, got %#v", k1.Data())
- }
- if k3.Len() != 3 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid3 node should have 3 children, has %d", k3.Len())
- }
- if k3.KidByName("2").Data() != 3.0 { // JSON has Number, no Integer, thus Go marshals it into a Float
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid3 sub-node \"2\" should have data of 3, got %#v from %#v", k3.KidByName("2").Data(), k3.KidByName("2"))
- }
- if k4.Len() != 2 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid4 node should have 2 children, has %d", k4.Len())
- }
- for idx, k := range k4.Kids() {
- if k.Len() != 2 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid4 sub-node \"%s\" should have 2 children, has %d", k.Name(), k.Len())
- continue
- }
- if k.KidByName("name") == nil {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid4 sub-node \"%s\" should have a child of \"name\", has %d:", k.Name(), k.Len())
- for _, k0 := range k.Kids() {
- t.Logf("\"%s\"", k0.Name())
- }
- continue
- }
- if k.KidByName("price") == nil {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid4 sub-node \"%s\" should have a child of \"price\", has %d:", k.Name(), k.Len())
- for _, k0 := range k.Kids() {
- t.Logf("\"%s\"", k0.Name())
- }
- continue
- }
- switch idx {
- case 0: // name="Taco", price=3.75
- if k.KidByName("name").Data().(string) != "Taco" {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid4 sub-node \"%s\" failed name check \"Taco\", got \"%s\"", k.Name(), k.KidByName("name").Data().(string))
- }
- if k.KidByName("price").Data() != 3.75 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid4 sub-node \"%s\" failed price check 3.75, got %#v", k.Name(), k.KidByName("price").Data())
- }
- case 1: // name="Soda", price=1.99
- if k.KidByName("name").Data().(string) != "Soda" {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid4 sub-node \"%s\" failed name check \"Soda\", got \"%s\"", k.Name(), k.KidByName("name").Data().(string))
- }
- if k.KidByName("price").Data() != 1.99 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Kid4 sub-node \"%s\" failed price check 1.99, got %#v", k.Name(), k.KidByName("price").Data())
- }
- default:
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Unexpected sub-node for Kid4 \"%s\"", k.Name())
- }
- }
- }
- func TestNewNode2(t *testing.T) {
- n, err := node.NewNodeFromJSON([]byte(`{"test": [1, 2, 3]}`))
- if err != nil {
- t.Fail()
- t.Logf("Unexpected Error: %v", err)
- }
- if n.Name() != "test" {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Node Name expected \"test\", got \"%s\"", n.Name())
- }
- if n.Len() != 3 {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Node Length expected 3, got %d", n.Len())
- }
- n, err = node.NewNodeFromJSON([]byte(`{"test": [1, 2, 3}`))
- if err == nil {
- if !t.Failed() {
- t.Fail()
- }
- t.Logf("Error expected!")
- }
- if n != nil {
- if !t.Failed() {
- t.Fail()
- }
- pay, err := n.MarshalJSON()
- if err != nil {
- t.Errorf("Node.MarshalJSON: %v", err)
- }
- t.Logf("Node should be nil! %s", pay)
- }
- }
|