cview/treeview_test.go

72 lines
2.0 KiB
Go
Raw Normal View History

2020-04-28 05:00:18 +00:00
package cview
import (
"testing"
)
const (
treeViewTextA = "Hello, world!"
treeViewTextB = "Goodnight, moon!"
)
func TestTreeView(t *testing.T) {
t.Parallel()
2020-04-28 21:58:37 +00:00
// Initialize
2020-04-28 14:57:37 +00:00
tr := NewTreeView()
2020-04-28 05:00:18 +00:00
if tr.GetRoot() != nil {
2020-04-28 14:57:37 +00:00
t.Errorf("failed to initialize TreeView: expected nil root node, got %v", tr.GetRoot())
2020-04-28 05:00:18 +00:00
} else if tr.GetCurrentNode() != nil {
2020-04-28 14:57:37 +00:00
t.Errorf("failed to initialize TreeView: expected nil current node, got %v", tr.GetCurrentNode())
2020-04-28 05:00:18 +00:00
} else if tr.GetRowCount() != 0 {
2020-04-28 14:57:37 +00:00
t.Errorf("failed to initialize TreeView: incorrect row count: expected 0, got %d", tr.GetRowCount())
}
app, err := newTestApp(tr)
if err != nil {
t.Errorf("failed to initialize Application: %s", err)
2020-04-28 05:00:18 +00:00
}
2020-04-28 21:58:37 +00:00
// Create root node
2020-04-28 05:00:18 +00:00
rootNode := NewTreeNode(treeViewTextA)
if rootNode.GetText() != treeViewTextA {
t.Errorf("failed to update TreeView: incorrect node text: expected %s, got %s", treeViewTextA, rootNode.GetText())
}
2020-04-28 21:58:37 +00:00
// Add root node
2020-04-28 05:00:18 +00:00
tr.SetRoot(rootNode)
2020-04-28 14:57:37 +00:00
tr.Draw(app.screen)
2020-04-28 05:00:18 +00:00
if tr.GetRoot() != rootNode {
2020-04-28 14:57:37 +00:00
t.Errorf("failed to initialize TreeView: expected root node A, got %v", tr.GetRoot())
2020-04-28 05:00:18 +00:00
} else if tr.GetRowCount() != 1 {
2020-04-28 14:57:37 +00:00
t.Errorf("failed to initialize TreeView: incorrect row count: expected 1, got %d", tr.GetRowCount())
2020-04-28 05:00:18 +00:00
}
2020-04-28 21:58:37 +00:00
// Set current node
2020-04-28 05:00:18 +00:00
tr.SetCurrentNode(rootNode)
if tr.GetCurrentNode() != rootNode {
2020-04-28 14:57:37 +00:00
t.Errorf("failed to initialize TreeView: expected current node A, got %v", tr.GetCurrentNode())
2020-04-28 05:00:18 +00:00
}
2020-04-28 21:58:37 +00:00
// Create child node
2020-04-28 05:00:18 +00:00
childNode := NewTreeNode(treeViewTextB)
if childNode.GetText() != treeViewTextB {
t.Errorf("failed to update TreeView: incorrect node text: expected %s, got %s", treeViewTextB, childNode.GetText())
}
2020-04-28 21:58:37 +00:00
// Add child node
2020-04-28 05:00:18 +00:00
rootNode.AddChild(childNode)
2020-04-28 14:57:37 +00:00
tr.Draw(app.screen)
2020-04-28 05:00:18 +00:00
if tr.GetRoot() != rootNode {
2020-04-28 14:57:37 +00:00
t.Errorf("failed to initialize TreeView: expected root node A, got %v", tr.GetRoot())
2020-04-28 05:00:18 +00:00
} else if tr.GetRowCount() != 2 {
2020-04-28 14:57:37 +00:00
t.Errorf("failed to initialize TreeView: incorrect row count: expected 1, got %d", tr.GetRowCount())
2020-04-28 05:00:18 +00:00
}
}