mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-09 01:17:06 +02:00
cleaner test assertions
This commit is contained in:
@ -3,10 +3,9 @@ package components
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jesseduffield/gocui"
|
||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||
integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types"
|
||||
)
|
||||
@ -21,46 +20,6 @@ func NewAssert(gui integrationTypes.GuiDriver) *Assert {
|
||||
return &Assert{gui: gui}
|
||||
}
|
||||
|
||||
func Contains(target string) *matcher {
|
||||
return NewMatcher(
|
||||
fmt.Sprintf("contains '%s'", target),
|
||||
func(value string) (bool, string) {
|
||||
return strings.Contains(value, target), fmt.Sprintf("Expected '%s' to be found in '%s'", target, value)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func NotContains(target string) *matcher {
|
||||
return NewMatcher(
|
||||
fmt.Sprintf("does not contain '%s'", target),
|
||||
func(value string) (bool, string) {
|
||||
return !strings.Contains(value, target), fmt.Sprintf("Expected '%s' to NOT be found in '%s'", target, value)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func MatchesRegexp(target string) *matcher {
|
||||
return NewMatcher(
|
||||
fmt.Sprintf("matches regular expression '%s'", target),
|
||||
func(value string) (bool, string) {
|
||||
matched, err := regexp.MatchString(target, value)
|
||||
if err != nil {
|
||||
return false, fmt.Sprintf("Unexpected error parsing regular expression '%s': %s", target, err.Error())
|
||||
}
|
||||
return matched, fmt.Sprintf("Expected '%s' to match regular expression '%s'", value, target)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func Equals(target string) *matcher {
|
||||
return NewMatcher(
|
||||
fmt.Sprintf("equals '%s'", target),
|
||||
func(value string) (bool, string) {
|
||||
return target == value, fmt.Sprintf("Expected '%s' to equal '%s'", value, target)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (self *Assert) WorkingTreeFileCount(expectedCount int) {
|
||||
self.assertWithRetries(func() (bool, string) {
|
||||
actualCount := len(self.gui.Model().Files)
|
||||
@ -114,13 +73,6 @@ func (self *Assert) HeadCommitMessage(matcher *matcher) {
|
||||
)
|
||||
}
|
||||
|
||||
func (self *Assert) CurrentViewName(expectedViewName string) {
|
||||
self.assertWithRetries(func() (bool, string) {
|
||||
actual := self.gui.CurrentContext().GetView().Name()
|
||||
return actual == expectedViewName, fmt.Sprintf("Expected current view name to be '%s', but got '%s'", expectedViewName, actual)
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Assert) CurrentWindowName(expectedWindowName string) {
|
||||
self.assertWithRetries(func() (bool, string) {
|
||||
actual := self.gui.CurrentContext().GetView().Name()
|
||||
@ -143,21 +95,6 @@ func (self *Assert) InListContext() {
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Assert) CurrentLine(matcher *matcher) {
|
||||
self.matchString(matcher, "Unexpected selected line.",
|
||||
func() string {
|
||||
return self.gui.CurrentContext().GetView().SelectedLine()
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (self *Assert) CurrentLineIdx(expected int) {
|
||||
self.assertWithRetries(func() (bool, string) {
|
||||
actual := self.gui.CurrentContext().GetView().SelectedLineIdx()
|
||||
return expected == actual, fmt.Sprintf("Expected selected line index to be %d, got %d", expected, actual)
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Assert) InPrompt() {
|
||||
self.assertWithRetries(func() (bool, string) {
|
||||
currentView := self.gui.CurrentContext().GetView()
|
||||
@ -200,87 +137,6 @@ func (self *Assert) NotInPopup() {
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Assert) CurrentViewTitle(matcher *matcher) {
|
||||
self.matchString(matcher, "Unexpected current view title.",
|
||||
func() string {
|
||||
return self.gui.CurrentContext().GetView().Title
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (self *Assert) ViewContent(viewName string, matcher *matcher) {
|
||||
self.matchString(matcher, fmt.Sprintf("Unexpected content in view '%s'.", viewName),
|
||||
func() string {
|
||||
return self.gui.View(viewName).Buffer()
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// asserts that the given view has lines matching the given matchers.
|
||||
func (self *Assert) ViewLines(viewName string, matchers ...*matcher) {
|
||||
self.assertWithRetries(func() (bool, string) {
|
||||
lines := self.gui.View(viewName).BufferLines()
|
||||
return len(lines) == len(matchers), fmt.Sprintf("unexpected number of lines in view. Expected %d, got %d", len(matchers), len(lines))
|
||||
})
|
||||
|
||||
for i, matcher := range matchers {
|
||||
self.matchString(matcher, fmt.Sprintf("Unexpected content in view '%s'.", viewName),
|
||||
func() string {
|
||||
return self.gui.View(viewName).BufferLines()[i]
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Assert) CurrentViewLines(matchers ...*matcher) {
|
||||
self.ViewLines(self.gui.CurrentContext().GetView().Name(), matchers...)
|
||||
}
|
||||
|
||||
// asserts that the given view has lines matching the given matchers. So if three matchers
|
||||
// are passed, we only check the first three lines of the view.
|
||||
func (self *Assert) ViewTopLines(viewName string, matchers ...*matcher) {
|
||||
self.assertWithRetries(func() (bool, string) {
|
||||
lines := self.gui.View(viewName).BufferLines()
|
||||
return len(lines) >= len(matchers), fmt.Sprintf("unexpected number of lines in view. Expected at least %d, got %d", len(matchers), len(lines))
|
||||
})
|
||||
|
||||
for i, matcher := range matchers {
|
||||
self.matchString(matcher, fmt.Sprintf("Unexpected content in view '%s'.", viewName),
|
||||
func() string {
|
||||
return self.gui.View(viewName).BufferLines()[i]
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Assert) CurrentViewTopLines(matchers ...*matcher) {
|
||||
self.ViewTopLines(self.gui.CurrentContext().GetView().Name(), matchers...)
|
||||
}
|
||||
|
||||
func (self *Assert) CurrentViewContent(matcher *matcher) {
|
||||
self.matchString(matcher, "Unexpected content in current view.",
|
||||
func() string {
|
||||
return self.gui.CurrentContext().GetView().Buffer()
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (self *Assert) MainViewContent(matcher *matcher) {
|
||||
self.matchString(matcher, "Unexpected main view content.",
|
||||
func() string {
|
||||
return self.gui.MainView().Buffer()
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (self *Assert) SecondaryViewContent(matcher *matcher) {
|
||||
self.matchString(matcher, "Unexpected secondary view title.",
|
||||
func() string {
|
||||
return self.gui.SecondaryView().Buffer()
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (self *Assert) matchString(matcher *matcher, context string, getValue func() string) {
|
||||
self.assertWithRetries(func() (bool, string) {
|
||||
value := getValue()
|
||||
@ -325,3 +181,35 @@ func (self *Assert) FileSystemPathNotPresent(path string) {
|
||||
return os.IsNotExist(err), fmt.Sprintf("Expected path '%s' to not exist, but it does", path)
|
||||
})
|
||||
}
|
||||
|
||||
func (self *Assert) CurrentView() *ViewAsserter {
|
||||
return &ViewAsserter{
|
||||
context: "current view",
|
||||
getView: func() *gocui.View { return self.gui.CurrentContext().GetView() },
|
||||
assert: self,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Assert) View(viewName string) *ViewAsserter {
|
||||
return &ViewAsserter{
|
||||
context: fmt.Sprintf("%s view", viewName),
|
||||
getView: func() *gocui.View { return self.gui.View(viewName) },
|
||||
assert: self,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Assert) MainView() *ViewAsserter {
|
||||
return &ViewAsserter{
|
||||
context: "main view",
|
||||
getView: func() *gocui.View { return self.gui.MainView() },
|
||||
assert: self,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Assert) SecondaryView() *ViewAsserter {
|
||||
return &ViewAsserter{
|
||||
context: "secondary view",
|
||||
getView: func() *gocui.View { return self.gui.SecondaryView() },
|
||||
assert: self,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user