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

1 year ago
  1. package tag
  2. import (
  3. "git.aiterp.net/rpdata2-take2/rpdata2/internal/genutils"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. )
  7. func printNode(t *testing.T, prefix string, node Node) {
  8. t.Log(prefix + "[" + node.ID + "] " + node.Name)
  9. for _, child := range node.Children {
  10. printNode(t, prefix+" ", child)
  11. }
  12. }
  13. func TestNode_Less(t *testing.T) {
  14. n1 := Node{Tag: Tag{Kind: TKLocation | TKOrganization, Name: "Miner's Respite"}}
  15. n2 := Node{Tag: Tag{Kind: TKLocation | TKOrganization, Name: "Litae's Grace"}}
  16. n3 := Node{Tag: Tag{Kind: TKOrganization, Name: "Redrock Agency"}}
  17. n4 := Node{Tag: Tag{Kind: TKLocation, Name: "Redrock HQ"}}
  18. assert.True(t, n2.Less(n1))
  19. assert.True(t, !n1.Less(n2))
  20. assert.True(t, n3.Less(n4))
  21. }
  22. func TestNode_WithoutUnlisted(t *testing.T) {
  23. before := Node{Tag{Name: "A", Listed: true}, []Node{
  24. {Tag{Name: "B", Listed: true}, []Node{
  25. {Tag{Name: "D", Listed: false}, []Node{
  26. {Tag{Name: "F", Listed: true}, []Node{}},
  27. }},
  28. {Tag{Name: "E", Secret: true, Listed: true}, []Node{
  29. {Tag{Name: "G", Listed: true}, []Node{}},
  30. }},
  31. }},
  32. {Tag{Name: "C", Listed: true}, []Node{}},
  33. }}
  34. after := Node{Tag{Name: "A", Listed: true}, []Node{
  35. {Tag{Name: "B", Listed: true}, []Node{
  36. {Tag{Name: "E", Secret: true, Listed: true}, []Node{
  37. {Tag{Name: "G", Listed: true}, []Node{}},
  38. }},
  39. }},
  40. {Tag{Name: "C", Listed: true}, []Node{}},
  41. }}
  42. assert.Equal(t, &after, before.WithoutUnlisted())
  43. }
  44. func TestBuildForest(t *testing.T) {
  45. tags := []Tag{
  46. {ID: "T0", ParentID: genutils.Ptr("T7"), Name: "Root"},
  47. {ID: "T1", ParentID: genutils.Ptr("T0"), Name: "Stuff"},
  48. {ID: "T2", ParentID: genutils.Ptr("T0"), Name: "Things"},
  49. {ID: "T3", Name: "Second Root"},
  50. {ID: "T4", ParentID: genutils.Ptr("T2"), Name: "Items"},
  51. {ID: "T5", ParentID: genutils.Ptr("T1"), Name: "Objects"},
  52. {ID: "T6", ParentID: genutils.Ptr("T4"), Name: "Thingies"},
  53. {ID: "T7", Name: "Real Root"},
  54. {ID: "T8", ParentID: genutils.Ptr("T3"), Name: "Another Leaf"},
  55. {ID: "T9", ParentID: genutils.Ptr("T5"), Name: "Branch"},
  56. {ID: "TA", ParentID: genutils.Ptr("T9"), Name: "Leafy Leaf"},
  57. }
  58. nodes := BuildForest(tags)
  59. for _, node := range nodes {
  60. printNode(t, "", node)
  61. }
  62. expected := []Node{
  63. {tags[7], []Node{
  64. {tags[0], []Node{
  65. {tags[1], []Node{
  66. {tags[5], []Node{
  67. {tags[9], []Node{
  68. {tags[10], []Node{}},
  69. }},
  70. }},
  71. }},
  72. {tags[2], []Node{
  73. {tags[4], []Node{
  74. {tags[6], []Node{}},
  75. }},
  76. }},
  77. }},
  78. }},
  79. {tags[3], []Node{
  80. {tags[8], []Node{}},
  81. }},
  82. }
  83. assert.Equal(t, expected, nodes)
  84. }