forked from sclevine/agouti
/
be_found.go
42 lines (33 loc) · 952 Bytes
/
be_found.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
38
39
40
41
42
package internal
import (
"fmt"
"strings"
"github.com/onsi/gomega/format"
)
type BeFoundMatcher struct{}
func (m *BeFoundMatcher) Match(actual interface{}) (success bool, err error) {
actualSelection, ok := actual.(interface {
Count() (int, error)
})
if !ok {
return false, fmt.Errorf("BeFound matcher requires a *Selection. Got:\n%s", format.Object(actual, 1))
}
count, err := actualSelection.Count()
if err != nil {
switch {
case strings.HasSuffix(err.Error(), "element not found"):
return false, nil
case strings.HasSuffix(err.Error(), "element index out of range"):
return false, nil
default:
return false, err
}
}
return count > 0, nil
}
func (m *BeFoundMatcher) FailureMessage(actual interface{}) (message string) {
return booleanMessage(actual, "to be found")
}
func (m *BeFoundMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return booleanMessage(actual, "not to be found")
}