forked from sclevine/agouti
/
match_text.go
37 lines (29 loc) · 905 Bytes
/
match_text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package selection
import (
"fmt"
"github.com/onsi/gomega/format"
"regexp"
)
type MatchTextMatcher struct {
Regexp string
actualText string
}
func (m *MatchTextMatcher) Match(actual interface{}) (success bool, err error) {
actualSelection, ok := actual.(interface {
Text() (string, error)
})
if !ok {
return false, fmt.Errorf("MatchText matcher requires a Selection. Got:\n%s", format.Object(actual, 1))
}
m.actualText, err = actualSelection.Text()
if err != nil {
return false, err
}
return regexp.MatchString(m.Regexp, m.actualText)
}
func (m *MatchTextMatcher) FailureMessage(actual interface{}) (message string) {
return selectorMessage(actual, "to have text matching", m.Regexp, m.actualText)
}
func (m *MatchTextMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return selectorMessage(actual, "not to have text matching", m.Regexp, m.actualText)
}