package node_test import ( "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)) } }