Skip to content

Commit

Permalink
main_test.go: Test showHelp key (see 6e62971)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnd-au committed Oct 18, 2024
1 parent 21d960a commit cfd6866
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Keymaps struct {
ToggleDate []string
OpenWeb []string
Now []string
Help []string
Quit []string
}

Expand All @@ -53,6 +54,7 @@ var DefaultKeymaps = Keymaps{
ToggleDate: []string{"d"},
OpenWeb: []string{"o"},
Now: []string{"t"},
Help: []string{"?"},
Quit: []string{"q", "ctrl+c", "esc"},
}

Expand Down Expand Up @@ -141,6 +143,7 @@ func LoadConfig(tzConfigs []string) (*Config, error) {
mergedConfig.Keymaps.ToggleDate,
mergedConfig.Keymaps.OpenWeb,
mergedConfig.Keymaps.Now,
mergedConfig.Keymaps.Help,
mergedConfig.Keymaps.Quit,
}
var keysUsed = make(map[string]bool)
Expand Down
1 change: 1 addition & 0 deletions config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type ConfigFileKeymaps struct {
ToggleDate []string `toml:"toggle_date"`
OpenWeb []string `toml:"open_web"`
Now []string `toml:"now"`
Help []string `toml:"help"`
Quit []string `toml:"quit"`
}

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case match(key, m.keymaps.ToggleDate):
m.showDates = !m.showDates

case key == "?":
case match(key, m.keymaps.Help):
m.showHelp = !m.showHelp
}

Expand Down
48 changes: 48 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
utcMinuteAfterMidnightModel = model{
zones: DefaultZones[len(DefaultZones) - 1:],
clock: *NewClockTime(utcMinuteAfterMidnightTime),
keymaps: DefaultKeymaps,
isMilitary: true,
showDates: true,
}
Expand Down Expand Up @@ -155,6 +156,53 @@ func TestUpdateDecHour(t *testing.T) {
}
}

func TestUpdateShowHelpMsg(t *testing.T) {
// "?" key -> help
msg := tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune{'?'},
Alt: false,
}

m := utcMinuteAfterMidnightModel
if m.showHelp {
t.Error("showHelp should be disabled by default")
}

status1 := status(m)

if _, cmd := m.Update(msg); cmd != nil {
t.Fatalf("Expected nil Cmd, but got %v", cmd)
}

if !m.showHelp {
t.Error("showHelp not enabled by '?' key")
}

oldHasDarkBackground := hasDarkBackground
hasDarkBackground = true
status2 := status(m)
hasDarkBackground = oldHasDarkBackground

if status2 == status1 || !strings.Contains(status2, "d: toggle date") {
t.Errorf("Expected help, but got:\n%v", status2)
}

if _, cmd := m.Update(msg); cmd != nil {
t.Fatalf("Expected nil Cmd, but got %v", cmd)
}

if m.showHelp {
t.Error("showHelp not toggled")
}

status3 := status(m)

if status3 != status1 {
t.Errorf("Expected final status identical to initial status, but got:\n%v", status3)
}
}

func TestUpdateQuitMsg(t *testing.T) {
// "q" key -> quit
msg := tea.KeyMsg{
Expand Down

0 comments on commit cfd6866

Please sign in to comment.