pass tests including pretty tree

This commit is contained in:
Bel LaPointe
2022-02-08 15:22:33 -07:00
parent 6f27327ac6
commit d0cea24d88
5 changed files with 351 additions and 9 deletions

View File

@@ -1,9 +1,13 @@
package main
import (
"bytes"
"encoding/json"
"path"
"testing"
"time"
"go.mongodb.org/mongo-driver/bson"
)
func TestTree(t *testing.T) {
@@ -52,3 +56,89 @@ func TestTree(t *testing.T) {
t.Fatal(branch)
}
}
func TestTreePretty(t *testing.T) {
tree := NewTree(path.Join(t.TempDir(), "tree_pretty.yaml"))
tree.Put("A", Branch{Title: "A", PID: ""})
tree.Put("AA", Branch{Title: "AA", PID: "A", Deleted: true})
tree.Put("B", Branch{Title: "B", PID: ""})
tree.Put("BA", Branch{Title: "BA", PID: "B"})
tree.Put("BAA", Branch{Title: "BAA", PID: "BA", Deleted: true})
tree.Put("BB", Branch{Title: "BB", PID: "B"})
tree.Put("BBA", Branch{Title: "BBA", PID: "BB"})
tree.Put("BBB", Branch{Title: "BBB", PID: "BB"})
tree.Put("BBBC", Branch{Title: "BBBC", PID: "BBB"})
tree.Put("C", Branch{Title: "C", PID: "", Deleted: true})
tree.Put("D", Branch{Title: "D", PID: ""})
tree.Put("DA", Branch{Title: "DA", PID: "D"})
tree.Put("DAA", Branch{Title: "DAA", PID: "DA", Deleted: true})
tree.Put("DAAA", Branch{Title: "DAAA", PID: "DAA"})
got, err := tree.GetPretty()
if err != nil {
t.Fatal(err)
}
gotb, _ := json.MarshalIndent(got, "", " ")
t.Logf("%s", gotb)
/*
A
-AA
B
BA
-BAA
BB
BBA
BBB
BBBC
-C
D
DA
-DAA
DAAA
*/
want := bson.M{
"A": bson.M{
"Title": "A",
"Children": bson.M{},
},
"B": bson.M{
"Title": "B",
"Children": bson.M{
"BA": bson.M{
"Title": "BA",
"Children": bson.M{},
},
"BB": bson.M{
"Title": "BB",
"Children": bson.M{
"BBA": bson.M{
"Title": "BBA",
"Children": bson.M{},
},
"BBB": bson.M{
"Title": "BBB",
"Children": bson.M{
"BBBC": bson.M{
"Title": "BBBC",
"Children": bson.M{},
},
},
},
},
},
},
},
"D": bson.M{
"Title": "D",
"Children": bson.M{
"DA": bson.M{
"Title": "DA",
"Children": bson.M{},
},
},
},
}
wantb, _ := json.MarshalIndent(want, "", " ")
if !bytes.Equal(gotb, wantb) {
t.Fatalf("want:\n\t%s, got\n\t%s", wantb, gotb)
}
}