...

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

Documentation: code.rocketnine.space/tslocum/cview

     1  package cview
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  const (
     8  	listTextA = "Hello, world!"
     9  	listTextB = "Goodnight, moon!"
    10  	listTextC = "Hello, Dolly!"
    11  )
    12  
    13  func TestList(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	// Initialize
    17  
    18  	l := NewList()
    19  	if l.GetItemCount() != 0 {
    20  		t.Errorf("failed to initialize List: expected item count 0, got %d", l.GetItemCount())
    21  	} else if l.GetCurrentItemIndex() != 0 {
    22  		t.Errorf("failed to initialize List: expected current item 0, got %d", l.GetCurrentItemIndex())
    23  	}
    24  
    25  	// Add item 0
    26  
    27  	itemA := NewListItem(listTextA)
    28  	itemA.SetSecondaryText(listTextB)
    29  	itemA.SetShortcut('a')
    30  	l.AddItem(itemA)
    31  	if l.GetItemCount() != 1 {
    32  		t.Errorf("failed to update List: expected item count 1, got %d", l.GetItemCount())
    33  	} else if l.GetCurrentItemIndex() != 0 {
    34  		t.Errorf("failed to update List: expected current item 0, got %d", l.GetCurrentItemIndex())
    35  	}
    36  
    37  	// Get item 0 text
    38  
    39  	mainText, secondaryText := l.GetItemText(0)
    40  	if mainText != listTextA {
    41  		t.Errorf("failed to update List: expected main text %s, got %s", listTextA, mainText)
    42  	} else if secondaryText != listTextB {
    43  		t.Errorf("failed to update List: expected secondary text %s, got %s", listTextB, secondaryText)
    44  	}
    45  
    46  	// Add item 1
    47  
    48  	itemB := NewListItem(listTextB)
    49  	itemB.SetSecondaryText(listTextC)
    50  	itemB.SetShortcut('a')
    51  	l.AddItem(itemB)
    52  	if l.GetItemCount() != 2 {
    53  		t.Errorf("failed to update List: expected item count 1, got %v", l.GetItemCount())
    54  	} else if l.GetCurrentItemIndex() != 0 {
    55  		t.Errorf("failed to update List: expected current item 0, got %v", l.GetCurrentItemIndex())
    56  	}
    57  
    58  	// Get item 1 text
    59  
    60  	mainText, secondaryText = l.GetItemText(1)
    61  	if mainText != listTextB {
    62  		t.Errorf("failed to update List: expected main text %s, got %s", listTextB, mainText)
    63  	} else if secondaryText != listTextC {
    64  		t.Errorf("failed to update List: expected secondary text %s, got %s", listTextC, secondaryText)
    65  	}
    66  
    67  	// Draw
    68  
    69  	app, err := newTestApp(l)
    70  	if err != nil {
    71  		t.Errorf("failed to initialize Application: %s", err)
    72  	}
    73  
    74  	l.Draw(app.screen)
    75  }
    76  

View as plain text