You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.5 KiB

package tag
import (
"git.aiterp.net/rpdata2-take2/rpdata2/internal/genutils"
"github.com/stretchr/testify/assert"
"testing"
)
func printNode(t *testing.T, prefix string, node Node) {
t.Log(prefix + "[" + node.ID + "] " + node.Name)
for _, child := range node.Children {
printNode(t, prefix+" ", child)
}
}
func TestNode_Less(t *testing.T) {
n1 := Node{Tag: Tag{Kind: TKLocation | TKOrganization, Name: "Miner's Respite"}}
n2 := Node{Tag: Tag{Kind: TKLocation | TKOrganization, Name: "Litae's Grace"}}
n3 := Node{Tag: Tag{Kind: TKOrganization, Name: "Redrock Agency"}}
n4 := Node{Tag: Tag{Kind: TKLocation, Name: "Redrock HQ"}}
assert.True(t, n2.Less(n1))
assert.True(t, !n1.Less(n2))
assert.True(t, n3.Less(n4))
}
func TestNode_WithoutUnlisted(t *testing.T) {
before := Node{Tag{Name: "A", Listed: true}, []Node{
{Tag{Name: "B", Listed: true}, []Node{
{Tag{Name: "D", Listed: false}, []Node{
{Tag{Name: "F", Listed: true}, []Node{}},
}},
{Tag{Name: "E", Secret: true, Listed: true}, []Node{
{Tag{Name: "G", Listed: true}, []Node{}},
}},
}},
{Tag{Name: "C", Listed: true}, []Node{}},
}}
after := Node{Tag{Name: "A", Listed: true}, []Node{
{Tag{Name: "B", Listed: true}, []Node{
{Tag{Name: "E", Secret: true, Listed: true}, []Node{
{Tag{Name: "G", Listed: true}, []Node{}},
}},
}},
{Tag{Name: "C", Listed: true}, []Node{}},
}}
assert.Equal(t, &after, before.WithoutUnlisted())
}
func TestBuildForest(t *testing.T) {
tags := []Tag{
{ID: "T0", ParentID: genutils.Ptr("T7"), Name: "Root"},
{ID: "T1", ParentID: genutils.Ptr("T0"), Name: "Stuff"},
{ID: "T2", ParentID: genutils.Ptr("T0"), Name: "Things"},
{ID: "T3", Name: "Second Root"},
{ID: "T4", ParentID: genutils.Ptr("T2"), Name: "Items"},
{ID: "T5", ParentID: genutils.Ptr("T1"), Name: "Objects"},
{ID: "T6", ParentID: genutils.Ptr("T4"), Name: "Thingies"},
{ID: "T7", Name: "Real Root"},
{ID: "T8", ParentID: genutils.Ptr("T3"), Name: "Another Leaf"},
{ID: "T9", ParentID: genutils.Ptr("T5"), Name: "Branch"},
{ID: "TA", ParentID: genutils.Ptr("T9"), Name: "Leafy Leaf"},
}
nodes := BuildForest(tags)
for _, node := range nodes {
printNode(t, "", node)
}
expected := []Node{
{tags[7], []Node{
{tags[0], []Node{
{tags[1], []Node{
{tags[5], []Node{
{tags[9], []Node{
{tags[10], []Node{}},
}},
}},
}},
{tags[2], []Node{
{tags[4], []Node{
{tags[6], []Node{}},
}},
}},
}},
}},
{tags[3], []Node{
{tags[8], []Node{}},
}},
}
assert.Equal(t, expected, nodes)
}