summaryrefslogtreecommitdiffstats
path: root/pkg/gui/context_test.go
diff options
context:
space:
mode:
authorDerTeta <derteta@gmx.de>2021-09-25 23:00:17 +0200
committerJesse Duffield <jessedduffield@gmail.com>2021-12-06 22:37:28 +1100
commit3e3151f86ace9dec0af9f554cb0efaa6f43e1831 (patch)
tree8b13644ac2db38c1c514b18232fe84fa8bac9bf0 /pkg/gui/context_test.go
parent28cdcddb0ac425e0ef2300135d4a6d6f07a19ab6 (diff)
Fix: Don't access a view if it's `nil`
The way the `if` expression in `deactivateContext` was composed, it was possible to have it to evaluate to `true` even though the `view` variable was `nil`. As far as I can tell, this seems to be only possible during tests. Nonetheless, I think the expression looks more "correct" this way.
Diffstat (limited to 'pkg/gui/context_test.go')
-rw-r--r--pkg/gui/context_test.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/pkg/gui/context_test.go b/pkg/gui/context_test.go
new file mode 100644
index 000000000..abfcab4cd
--- /dev/null
+++ b/pkg/gui/context_test.go
@@ -0,0 +1,40 @@
+package gui
+
+import (
+ "testing"
+
+ "github.com/jesseduffield/gocui"
+ "github.com/stretchr/testify/assert"
+)
+
+func TestCanDeactivatePopupContextsWithoutViews(t *testing.T) {
+ contexts := []func(gui *Gui) Context {
+ func(gui *Gui) Context { return gui.State.Contexts.Credentials },
+ func(gui *Gui) Context { return gui.State.Contexts.Confirmation },
+ func(gui *Gui) Context { return gui.State.Contexts.CommitMessage },
+ func(gui *Gui) Context { return gui.State.Contexts.Search },
+ }
+
+ for _, c := range contexts {
+ gui := NewDummyGui()
+ context := c(gui)
+ gui.g = &gocui.Gui{}
+
+ gui.deactivateContext(context)
+
+ // This really only checks a prerequisit, not the effect of deactivateContext
+ view, _ := gui.g.View(context.GetViewName())
+ assert.Nil(t, view, string(context.GetKey()))
+ }
+}
+
+func TestCanDeactivateCommitFilesContextsWithoutViews(t *testing.T) {
+ gui := NewDummyGui()
+ gui.g = &gocui.Gui{}
+
+ gui.deactivateContext(gui.State.Contexts.CommitFiles)
+
+ // This really only checks a prerequisite, not the effect of deactivateContext
+ view, _ := gui.g.View(gui.State.Contexts.CommitFiles.GetViewName())
+ assert.Nil(t, view)
+}