...
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
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
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
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
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
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
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