package main import ( "testing" "golang.org/x/exp/slices" ) func TestNodeString(t *testing.T) { n := Node{ Line: "Test", } n.AddTag("1") if n.String() != "Test" { t.Errorf("Expected 1 tab for depth=1, got '%s'", n.String()) } n1 := n.NewChild() n1.Line = "Hello World" if n.StringAll() != "Test\n\tHello World\n" { t.Errorf("Expected 'Test\n\tHello World\n' got '%s'", n.StringAll()) } } func TestNodeAddChild(t *testing.T) { n := Node{ Line: "Test", } n.AddTag("1") n1 := Node{ Line: "Test 2", } n1.AddTag("2") n.AddChild(&n1) if len(n.Children) != 1 { t.Errorf("Expected to add the child, but didn't (Children Count: %d)", len(n.Children)) } if !slices.Equal(n1.Parent.Tags, n.Tags) { t.Errorf("Expected child's parent to have the same tag, expected '1', got '%s'", n1.Parent.Tags) } if !slices.Equal(n.Children[0].Tags, n1.Tags) { t.Errorf("Expected child's tag to be the same as other node's tag, expected '2', got '%s'", n.Children[0].Tags) } } func TestNodeNewChild(t *testing.T) { n := Node{ Line: "Test", } n.AddTag("1") n1 := n.NewChild() n1.AddTag("2") n1.Line = "Test 2" if len(n.Children) != 1 { t.Errorf("Expected to add the child, but didn't (Children Count: %d)", len(n.Children)) } if n.Children[0].Line != n1.Line { t.Errorf("Expected 'Test 2', got '%s' ('%s')", n.Children[0].Line, n1.Line) } if n1.Depth() != 1 { t.Errorf("Expected depth=1, got %d (%d)", n1.Depth(), n.Children[0].Depth()) } } func TestNodeKey(t *testing.T) { n := Node{ Line: "Test Value", } if n.Key() != "Test" { t.Errorf("Expected 'Test', got '%s' (%s)", n.Key(), n.Line) } n.Line = "\"Some Test\" Value" if n.Key() != "Some Test" { t.Errorf("Expected 'Some Test', got '%s' (%s)", n.Key(), n.Line) } n.Line = "Test \"Some Value\"" if n.Key() != "Test" { t.Errorf("Expected 'Test', got '%s' (%s)", n.Key(), n.Line) } n.Line = "\"Some Test\" \"Some Value\"" if n.Key() != "Some Test" { t.Errorf("Expected 'Some Test', got '%s', (%s)", n.Key(), n.Line) } } func TestNodeValue(t *testing.T) { n := Node{ Line: "Test Value", } if n.Value() != "Value" { t.Errorf("Expected 'Value', got '%s', (%s)", n.Value(), n.Line) } n.Line = "\"Some Test\" Value" if n.Value() != "Value" { t.Errorf("Expected 'Value', got '%s', (%s)", n.Value(), n.Line) } n.Line = "Test \"Some Value\"" if n.Value() != "Some Value" { t.Errorf("Expected 'Some Value', got '%s', (%s)", n.Value(), n.Line) } n.Line = "\"Some Test\" \"Some Value\"" if n.Value() != "Some Value" { t.Errorf("Expected 'Some Value', got '%s', (%s)", n.Value(), n.Line) } } func TestNodeValueTypes(t *testing.T) { n := Node{ Line: "\"Some Super Test\" 13", } if n.Key() != "Some Super Test" { t.Errorf("Expected 'Some Super Test', got '%s' (%s)", n.Key(), n.Line) } if n.Value() != "13" { t.Errorf("Expected '13', got '%s' (%s)", n.Value(), n.Line) } if n.ValueInt() != 13 { t.Errorf("Expected 13, got %d (%s)", n.ValueInt(), n.Line) } n.Line = "\"Some Super Duper Test\" 9.81" if n.Key() != "Some Super Duper Test" { t.Errorf("Expected 'Some Super Duper Test', got '%s' (%s)", n.Key(), n.Line) } if n.Value() != "9.81" { t.Errorf("Expected '9.81', got '%s', (%s)", n.Value(), n.Line) } if n.ValueFloat32() != 9.81 { t.Errorf("Expected 9.81, got %f, (%s)", n.ValueFloat32(), n.Line) } if n.ValueFloat64() != 9.81 { t.Errorf("Expected 'Value', got %f, (%s)", n.ValueFloat64(), n.Line) } } func TestNodeSetters(t *testing.T) { n := Node{} n.SetKey("Hello World") if n.Line != "\"Hello World\"" { t.Errorf("Expected '\"Hello World\"', got '%s'", n.Line) } n.SetValue("Meow") if n.Line != "\"Hello World\" Meow" { t.Errorf("Expected '\"Hello World\" Meow', got '%s'", n.Line) } n.SetValue(9.81) if n.Line != "\"Hello World\" 9.81" { t.Errorf("Expected '\"Hello World\" 9.81', got '%s'", n.Line) } n.Set("Taco", "Cat") if n.Line != "Taco Cat" { t.Errorf("Expected 'Taco Cat', got '%s'", n.Line) } n.Set("Meow", nil) if n.Line != "Meow" { t.Errorf("Expected 'Meow', got '%s'", n.Line) } n.Set("I am super man", 42) if n.Line != "\"I am super man\" 42" { t.Errorf("Expected '\"I am super man\" 42', got '%s'", n.Line) } } func TestNodeChildActions(t *testing.T) { n := Node{} n.AddTag("root") n.Set("I am root", nil) n1 := n.NewChild() n1.Set("I am a child", 1) n2 := n.NewChild() n2.Set("I am a child", 2) n2_1 := n2.NewChild() n2_1.Set("I am a child", 3) if n.Len() != 2 { t.Errorf("Expected 2 children, got %d", len(n.Children)) } t1, err := n.Child(0) if err != nil { t.Errorf("Unexpected error: %v", err) } if t1.Line != "\"I am a child\" 1" { t.Errorf("Expected '\"I am a child\" 1', got '%s'", t1.Line) } t1, err = n.Child(1) if err != nil { t.Errorf("Unexpected error: %v", err) } if t1.Len() != 1 { t.Errorf("Expected 1 child, got %d", t1.Len()) } t2, err := t1.Child(0) if err != nil { t.Errorf("Unexpected error: %v", err) } if t2.Line != "\"I am a child\" 3" { t.Errorf("Expected '\"I am a child\" 3', got '%s'", t2.Line) } // Split the child off n3, err := n.SplitChild(1) if err != nil { t.Errorf("Unexpected error: %v", err) } if n3.Line != "\"I am a child\" 2" { t.Errorf("Expected '\"I am a child\" 2', got '%s'", n3.Line) } if n3.Len() != 1 { t.Errorf("Expected 1 child, got %d", n3.Len()) } // Add it back in n.AddChild(n3) // Now replace the first child with a new one n3 = &Node{} n3.AddTag("Kid 1") n3.Set("I am a child", 1.1) err = n.ReplaceChild(0, n3) if err != nil { t.Errorf("Unexpected error: %v", err) } n3_1 := n3.NewChild() n3_1.Set("I am a child", 1.2) if n.Len() != 2 { t.Errorf("Expected 2 children, got %d", n.Len()) } t1, err = n.Child(0) if err != nil { t.Errorf("Unexpected error: %v", err) } if t1.Len() != 1 { t.Errorf("Expected 1 child, got %d", t1.Len()) } t1, err = t1.DetachFromParent() if err != nil { t.Errorf("Unexpected error: %v", err) } if t1.Parent != nil { t.Errorf("Expected to detach from parent, should be root then") } n.RmAllChildren() if n.Len() != 0 { t.Errorf("Expected no children, got %d", n.Len()) } }