a more complex custom command test

This commit is contained in:
Jesse Duffield 2022-08-14 20:13:39 +10:00
parent 9c0d860980
commit 53979f7cec
17 changed files with 263 additions and 43 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/gui/types"
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
"golang.org/x/exp/constraints"
)
// through this struct we assert on the state of the lazygit gui
@ -19,6 +20,43 @@ func NewAssert(gui integrationTypes.GuiDriver) *Assert {
return &Assert{gui: gui}
}
// for making assertions on string values
type matcher[T any] struct {
testFn func(T) (bool, string)
prefix string
}
func (self *matcher[T]) test(value T) (bool, string) {
ok, message := self.testFn(value)
if ok {
return true, ""
}
if self.prefix != "" {
return false, self.prefix + " " + message
}
return false, message
}
func (self *matcher[T]) context(prefix string) *matcher[T] {
self.prefix = prefix
return self
}
func Contains(target string) *matcher[string] {
return &matcher[string]{testFn: func(value string) (bool, string) {
return strings.Contains(value, target), fmt.Sprintf("Expected '%s' to contain '%s'", value, target)
}}
}
func Equals[T constraints.Ordered](target T) *matcher[T] {
return &matcher[T]{testFn: func(value T) (bool, string) {
return target == value, fmt.Sprintf("Expected '%T' to equal '%T'", value, target)
}}
}
func (self *Assert) WorkingTreeFileCount(expectedCount int) {
self.assertWithRetries(func() (bool, string) {
actualCount := len(self.gui.Model().Files)
@ -41,22 +79,16 @@ func (self *Assert) CommitCount(expectedCount int) {
})
}
func (self *Assert) HeadCommitMessage(expectedMessage string) {
func (self *Assert) MatchHeadCommitMessage(matcher *matcher[string]) {
self.assertWithRetries(func() (bool, string) {
if len(self.gui.Model().Commits) == 0 {
return false, "Expected at least one commit to be present"
}
headCommit := self.gui.Model().Commits[0]
if headCommit.Name != expectedMessage {
return false, fmt.Sprintf(
"Expected commit message to be '%s', but got '%s'",
expectedMessage, headCommit.Name,
)
}
return true, ""
return len(self.gui.Model().Commits) == 0, "Expected at least one commit to be present"
})
self.matchString(matcher, "Unexpected commit message.",
func() string {
return self.gui.Model().Commits[0].Name
},
)
}
func (self *Assert) CurrentViewName(expectedViewName string) {
@ -81,10 +113,70 @@ func (self *Assert) InListContext() {
})
}
func (self *Assert) SelectedLineContains(text string) {
func (self *Assert) MatchSelectedLine(matcher *matcher[string]) {
self.matchString(matcher, "Unexpected selected line.",
func() string {
return self.gui.CurrentContext().GetView().SelectedLine()
},
)
}
func (self *Assert) InPrompt() {
self.assertWithRetries(func() (bool, string) {
line := self.gui.CurrentContext().GetView().SelectedLine()
return strings.Contains(line, text), fmt.Sprintf("Expected selected line to contain '%s', but got '%s'", text, line)
currentView := self.gui.CurrentContext().GetView()
return currentView.Name() == "confirmation" && currentView.Editable, fmt.Sprintf("Expected prompt popup to be focused")
})
}
func (self *Assert) InConfirm() {
self.assertWithRetries(func() (bool, string) {
currentView := self.gui.CurrentContext().GetView()
return currentView.Name() == "confirmation" && !currentView.Editable, fmt.Sprintf("Expected confirmation popup to be focused")
})
}
func (self *Assert) InAlert() {
// basically the same thing as a confirmation popup with the current implementation
self.assertWithRetries(func() (bool, string) {
currentView := self.gui.CurrentContext().GetView()
return currentView.Name() == "confirmation" && !currentView.Editable, fmt.Sprintf("Expected alert popup to be focused")
})
}
func (self *Assert) InMenu() {
self.assertWithRetries(func() (bool, string) {
return self.gui.CurrentContext().GetView().Name() == "menu", fmt.Sprintf("Expected popup menu to be focused")
})
}
func (self *Assert) MatchCurrentViewTitle(matcher *matcher[string]) {
self.matchString(matcher, "Unexpected current view title.",
func() string {
return self.gui.CurrentContext().GetView().Title
},
)
}
func (self *Assert) MatchMainViewContent(matcher *matcher[string]) {
self.matchString(matcher, "Unexpected main view content.",
func() string {
return self.gui.MainView().Buffer()
},
)
}
func (self *Assert) MatchSecondaryViewContent(matcher *matcher[string]) {
self.matchString(matcher, "Unexpected secondary view title.",
func() string {
return self.gui.SecondaryView().Buffer()
},
)
}
func (self *Assert) matchString(matcher *matcher[string], context string, getValue func() string) {
self.assertWithRetries(func() (bool, string) {
value := getValue()
return matcher.context(context).test(value)
})
}