turbo-editors/turbo-corepublic Fork 0
v1.0.2
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

statusbar_test.go · 118 lines · 3.2 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 21h ago1package ui
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestStatusBarDrawsItsHints(t *testing.T) {
9 screen, root := newTestScreen(t, 40, 2)
10 bar := NewStatusBar(
11 StatusItem{Key: "F1", Label: "Help"},
12 StatusItem{Key: "F2", Label: "Save"},
13 )
14 bar.SetBounds(Rect{X: 0, Y: 1, W: 40, H: 1})
15
16 bar.Draw(root, testTheme(t))
17 screen.Show()
18
19 got := screenLines(t, screen)[1]
20 if !strings.Contains(got, "F1 Help") || !strings.Contains(got, "F2 Save") {
21 t.Errorf("the status bar is %q, want both hints", got)
22 }
23}
24
25func TestAMessageReplacesTheHints(t *testing.T) {
26 screen, root := newTestScreen(t, 40, 2)
27 bar := NewStatusBar(StatusItem{Key: "F2", Label: "Save"})
28 bar.SetBounds(Rect{X: 0, Y: 1, W: 40, H: 1})
29
30 bar.SetMessage("File saved")
31 bar.Draw(root, testTheme(t))
32 screen.Show()
33
34 got := screenLines(t, screen)[1]
35 if !strings.Contains(got, "File saved") {
36 t.Errorf("the status bar is %q, want the message", got)
37 }
38 if strings.Contains(got, "F2") {
39 t.Errorf("the status bar is %q, want the hints hidden while a message shows", got)
40 }
41 if bar.Message() != "File saved" {
42 t.Errorf("Message() = %q", bar.Message())
43 }
44
45 bar.SetMessage("")
46 bar.Draw(root, testTheme(t))
47 screen.Show()
48 if !strings.Contains(screenLines(t, screen)[1], "F2") {
49 t.Error("clearing the message did not bring the hints back")
50 }
51}
52
53func TestExtraTextIsRightAligned(t *testing.T) {
54 screen, root := newTestScreen(t, 40, 2)
55 bar := NewStatusBar(StatusItem{Key: "F1", Label: "Help"})
56 bar.SetBounds(Rect{X: 0, Y: 1, W: 40, H: 1})
57
58 bar.SetExtra("12:5")
59 bar.Draw(root, testTheme(t))
60 screen.Show()
61
62 got := screenLines(t, screen)[1]
63 if !strings.HasSuffix(strings.TrimRight(got, " "), "12:5") {
64 t.Errorf("the status bar is %q, want the extra text at the right", got)
65 }
66}
67
68func TestClickingAHintRunsItsAction(t *testing.T) {
69 ran := ""
70 bar := NewStatusBar(
71 StatusItem{Key: "F1", Label: "Help", Action: func() { ran = "help" }},
72 StatusItem{Key: "F2", Label: "Save", Action: func() { ran = "save" }},
73 )
74 bar.SetBounds(Rect{X: 0, Y: 5, W: 40, H: 1})
75
76 // "F1 Help" occupies columns 1..8, so "F2 Save" starts at 10.
77 bar.HandleMouse(clickEvent(11, 5))
78
79 if ran != "save" {
80 t.Errorf("the action that ran was %q, want save", ran)
81 }
82}
83
84func TestAClickOffTheStatusBarIsNotItsBusiness(t *testing.T) {
85 bar := NewStatusBar(StatusItem{Key: "F1", Label: "Help", Action: func() {}})
86 bar.SetBounds(Rect{X: 0, Y: 5, W: 40, H: 1})
87
88 if bar.HandleMouse(clickEvent(3, 0)) {
89 t.Error("the status bar claimed a click from another row")
90 }
91}
92
93func TestClickingPastTheLastHintDoesNothing(t *testing.T) {
94 ran := 0
95 bar := NewStatusBar(StatusItem{Key: "F1", Label: "Help", Action: func() { ran++ }})
96 bar.SetBounds(Rect{X: 0, Y: 5, W: 40, H: 1})
97
98 bar.HandleMouse(clickEvent(35, 5))
99
100 if ran != 0 {
101 t.Error("a click on the empty part of the bar ran an action")
102 }
103}
104
105func TestSetItemsReplacesTheHints(t *testing.T) {
106 screen, root := newTestScreen(t, 40, 2)
107 bar := NewStatusBar(StatusItem{Key: "F1", Label: "Help"})
108 bar.SetBounds(Rect{X: 0, Y: 1, W: 40, H: 1})
109
110 bar.SetItems(StatusItem{Key: "Esc", Label: "Cancel"})
111 bar.Draw(root, testTheme(t))
112 screen.Show()
113
114 got := screenLines(t, screen)[1]
115 if strings.Contains(got, "Help") || !strings.Contains(got, "Esc Cancel") {
116 t.Errorf("the status bar is %q, want only the new hint", got)
117 }
118}