forked from sclevine/agouti
/
have_attribute.go
42 lines (32 loc) · 1.17 KB
/
have_attribute.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"
"github.com/onsi/gomega/format"
)
type HaveAttributeMatcher struct {
ExpectedAttribute string
ExpectedValue string
actualValue string
}
func (m *HaveAttributeMatcher) Match(actual interface{}) (success bool, err error) {
actualSelection, ok := actual.(interface {
Attribute(attribute string) (string, error)
})
if !ok {
return false, fmt.Errorf("HaveAttribute matcher requires a *Selection. Got:\n%s", format.Object(actual, 1))
}
m.actualValue, err = actualSelection.Attribute(m.ExpectedAttribute)
if err != nil {
return false, err
}
return m.actualValue == m.ExpectedValue, nil
}
func (m *HaveAttributeMatcher) FailureMessage(actual interface{}) (message string) {
return valueMessage(actual, "to have attribute matching", m.attribute(m.ExpectedValue), m.attribute(m.actualValue))
}
func (m *HaveAttributeMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return valueMessage(actual, "not to have attribute matching", m.attribute(m.ExpectedValue), m.attribute(m.actualValue))
}
func (m *HaveAttributeMatcher) attribute(value string) string {
return fmt.Sprintf(`[%s="%s"]`, m.ExpectedAttribute, value)
}