mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-01-18 05:17:55 +02:00
36 lines
841 B
Go
36 lines
841 B
Go
|
package components
|
||
|
|
||
|
// for making assertions on string values
|
||
|
type matcher struct {
|
||
|
// e.g. "contains 'foo'"
|
||
|
name string
|
||
|
// returns a bool that says whether the test passed and if it returns false, it
|
||
|
// also returns a string of the error message
|
||
|
testFn func(string) (bool, string)
|
||
|
// this is printed when there's an error so that it's clear what the context of the assertion is
|
||
|
prefix string
|
||
|
}
|
||
|
|
||
|
func NewMatcher(name string, testFn func(string) (bool, string)) *matcher {
|
||
|
return &matcher{name: name, testFn: testFn}
|
||
|
}
|
||
|
|
||
|
func (self *matcher) test(value string) (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) context(prefix string) *matcher {
|
||
|
self.prefix = prefix
|
||
|
|
||
|
return self
|
||
|
}
|