1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package main
- import (
- "os"
- "testing"
- "github.com/beanzilla/gonode"
- )
- 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.Iter() {
- t.Logf("%#v", k)
- }
- }
- k := root.Child(1)
- if k == nil {
- t.Errorf("Unexpected nil value")
- }
- if k.Data() != "ship \"Little Fish\"" || k.Depth() != 0 {
- t.Errorf("Expected 'ship \"Little Fish\"' depth of 0, got '%s' (depth=%d)", k.Data(), 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.Iter() {
- t.Logf("%#v", k0)
- }
- }
- k1 := k.Child(0)
- if k1 == nil {
- t.Errorf("Unexpected nil value")
- }
- if k1.Data() != "name \"Hello World\"" || k1.Depth() != 1 {
- t.Errorf("Expected 'name \"Hello World\"' depth 1, got '%s' (depth=%d)", k1.Data(), k1.Depth())
- }
- }
- func TestParseSave(t *testing.T) {
- n := gonode.NewNode()
- n.NewChildWithData("Hello World")
- n1 := n.NewChildWithData("Tacocat")
- n1.NewChildWithData("Meow?")
- p := Parser{Debug: true}
- err := p.Save("_tmp2.txt", n)
- if err != nil {
- t.Errorf("Unexpected error, %v", err)
- }
- defer func() {
- os.Remove("_tmp2.txt")
- }()
- }
|