...

Source file src/code.rocketnine.space/tslocum/cview/treeview_test.go

Documentation: code.rocketnine.space/tslocum/cview

     1  package cview
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  const (
     8  	treeViewTextA = "Hello, world!"
     9  	treeViewTextB = "Goodnight, moon!"
    10  )
    11  
    12  func TestTreeView(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	// Initialize
    16  
    17  	tr := NewTreeView()
    18  	if tr.GetRoot() != nil {
    19  		t.Errorf("failed to initialize TreeView: expected nil root node, got %v", tr.GetRoot())
    20  	} else if tr.GetCurrentNode() != nil {
    21  		t.Errorf("failed to initialize TreeView: expected nil current node, got %v", tr.GetCurrentNode())
    22  	} else if tr.GetRowCount() != 0 {
    23  		t.Errorf("failed to initialize TreeView: incorrect row count: expected 0, got %d", tr.GetRowCount())
    24  	}
    25  
    26  	app, err := newTestApp(tr)
    27  	if err != nil {
    28  		t.Errorf("failed to initialize Application: %s", err)
    29  	}
    30  
    31  	// Create root node
    32  
    33  	rootNode := NewTreeNode(treeViewTextA)
    34  	if rootNode.GetText() != treeViewTextA {
    35  		t.Errorf("failed to update TreeView: incorrect node text: expected %s, got %s", treeViewTextA, rootNode.GetText())
    36  	}
    37  
    38  	// Add root node
    39  
    40  	tr.SetRoot(rootNode)
    41  	tr.Draw(app.screen)
    42  	if tr.GetRoot() != rootNode {
    43  		t.Errorf("failed to initialize TreeView: expected root node A, got %v", tr.GetRoot())
    44  	} else if tr.GetRowCount() != 1 {
    45  		t.Errorf("failed to initialize TreeView: incorrect row count: expected 1, got %d", tr.GetRowCount())
    46  	}
    47  
    48  	// Set current node
    49  
    50  	tr.SetCurrentNode(rootNode)
    51  	if tr.GetCurrentNode() != rootNode {
    52  		t.Errorf("failed to initialize TreeView: expected current node A, got %v", tr.GetCurrentNode())
    53  	}
    54  
    55  	// Create child node
    56  
    57  	childNode := NewTreeNode(treeViewTextB)
    58  	if childNode.GetText() != treeViewTextB {
    59  		t.Errorf("failed to update TreeView: incorrect node text: expected %s, got %s", treeViewTextB, childNode.GetText())
    60  	}
    61  
    62  	// Add child node
    63  
    64  	rootNode.AddChild(childNode)
    65  	tr.Draw(app.screen)
    66  	if tr.GetRoot() != rootNode {
    67  		t.Errorf("failed to initialize TreeView: expected root node A, got %v", tr.GetRoot())
    68  	} else if tr.GetRowCount() != 2 {
    69  		t.Errorf("failed to initialize TreeView: incorrect row count: expected 1, got %d", tr.GetRowCount())
    70  	}
    71  }
    72  

View as plain text