This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
/
match_yaml.go
103 lines (80 loc) · 2.61 KB
/
match_yaml.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package gomegamatchers
import (
"fmt"
"gopkg.in/yaml.v2"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
"github.com/pivotal-cf-experimental/gomegamatchers/internal/deepequal"
"github.com/pivotal-cf-experimental/gomegamatchers/internal/prettyprint"
)
func HelpfullyMatchYAML(expected interface{}) types.GomegaMatcher {
return &HelpfullyMatchYAMLMatcher{
YAMLToMatch: expected,
}
}
type HelpfullyMatchYAMLMatcher struct {
YAMLToMatch interface{}
}
func (matcher *HelpfullyMatchYAMLMatcher) Match(actual interface{}) (success bool, err error) {
equal, _, err := matcher.equal(matcher.YAMLToMatch, actual)
if err != nil {
return false, err
}
return equal, nil
}
func (matcher *HelpfullyMatchYAMLMatcher) FailureMessage(actual interface{}) (message string) {
_, message, err := matcher.equal(matcher.YAMLToMatch, actual)
if err != nil {
return err.Error()
}
return message
}
func (matcher *HelpfullyMatchYAMLMatcher) NegatedFailureMessage(actual interface{}) (message string) {
actualString, _ := matcher.prettyPrint(actual)
expectedString, _ := matcher.prettyPrint(matcher.YAMLToMatch)
return format.Message(actualString, "not to match YAML of", expectedString)
}
func (matcher *HelpfullyMatchYAMLMatcher) equal(expected interface{}, actual interface{}) (bool, string, error) {
actualString, err := matcher.prettyPrint(actual)
if err != nil {
return false, "", err
}
expectedString, err := matcher.prettyPrint(expected)
if err != nil {
return false, "", err
}
var actualValue interface{}
var expectedValue interface{}
// this is guarded by prettyPrint
yaml.Unmarshal([]byte(actualString), &actualValue)
yaml.Unmarshal([]byte(expectedString), &expectedValue)
equal, difference := deepequal.Compare(expectedValue, actualValue)
return equal, prettyprint.ExpectationFailure(difference), nil
}
func (matcher *HelpfullyMatchYAMLMatcher) prettyPrint(input interface{}) (formatted string, err error) {
inputString, ok := toString(input)
if !ok {
return "", fmt.Errorf("HelpfullyMatchYAMLMatcher matcher requires a string or stringer. Got:\n%s", format.Object(input, 1))
}
var data interface{}
if err := yaml.Unmarshal([]byte(inputString), &data); err != nil {
return "", err
}
buf, _ := yaml.Marshal(data)
return string(buf), nil
}
func toString(value interface{}) (string, bool) {
valueString, isString := value.(string)
if isString {
return valueString, true
}
valueBytes, isBytes := value.([]byte)
if isBytes {
return string(valueBytes), true
}
valueStringer, isStringer := value.(fmt.Stringer)
if isStringer {
return valueStringer.String(), true
}
return "", false
}