package main import ( "os" "testing" ) func TestParseLoad(t *testing.T) { err := os.WriteFile( "_tmp1.txt", []byte("pilot Test Pilot\nship \"Little Fish\"\n\tname \"Hello World\"\n\tkind \"Space Fish\""), 0660, ) if err != nil { t.Errorf("Failed creating temp file") } defer func() { err = os.Remove("_tmp1.txt") if err != nil { t.Errorf("Failed cleaning up temp file") } }() p := Parser{} root, err := p.Load("_tmp1.txt") if err != nil { t.Fail() t.Logf("Unexpected error, %v", err) p.Debug = true p.Load("_tmp1.txt") p.Debug = false } if root.Len() != 2 { t.Fail() t.Logf("Incorrect child count for Root node, expected 2") t.Logf("%#v", root) for _, k := range root.Children { t.Logf("%#v", k) } } k, err := root.Child(1) if err != nil { t.Errorf("Unexpected error, %v", err) } if k.Line != "ship \"Little Fish\"" || k.Depth() != 0 { t.Errorf("Expected 'ship \"Little Fish\"' depth of 0, got '%s' (depth=%d)", k.Line, k.Depth()) } if k.Len() != 2 { t.Fail() t.Logf("Incorrect child count for Root.1 node, expected 2") t.Logf("%#v", k) for _, k0 := range k.Children { t.Logf("%#v", k0) } } k1, err := k.Child(0) if err != nil { t.Errorf("Unexpected error, %v", err) } if k1.Line != "name \"Hello World\"" || k1.Depth() != 1 { t.Errorf("Expected 'name \"Hello World\"' depth 1, got '%s' (depth=%d)", k1.Line, k1.Depth()) } } func TestParseSave(t *testing.T) { n := Node{} n1 := n.NewChild() n1.Line = "Hello World" n1 = n.NewChild() n1.Line = "Tacocat" n2 := n1.NewChild() n2.Line = "Meow?" p := Parser{} err := p.Save("_tmp2.txt", &n) if err != nil { t.Errorf("Unexpected error, %v", err) } defer func() { os.Remove("_tmp2.txt") }() }