Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | package matchers |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/onsi/gomega/format" |
| 9 | "gopkg.in/yaml.v2" |
| 10 | ) |
| 11 | |
| 12 | type MatchYAMLMatcher struct { |
| 13 | YAMLToMatch interface{} |
| 14 | } |
| 15 | |
| 16 | func (matcher *MatchYAMLMatcher) Match(actual interface{}) (success bool, err error) { |
| 17 | actualString, expectedString, err := matcher.toStrings(actual) |
| 18 | if err != nil { |
| 19 | return false, err |
| 20 | } |
| 21 | |
| 22 | var aval interface{} |
| 23 | var eval interface{} |
| 24 | |
| 25 | if err := yaml.Unmarshal([]byte(actualString), &aval); err != nil { |
| 26 | return false, fmt.Errorf("Actual '%s' should be valid YAML, but it is not.\nUnderlying error:%s", actualString, err) |
| 27 | } |
| 28 | if err := yaml.Unmarshal([]byte(expectedString), &eval); err != nil { |
| 29 | return false, fmt.Errorf("Expected '%s' should be valid YAML, but it is not.\nUnderlying error:%s", expectedString, err) |
| 30 | } |
| 31 | |
| 32 | return reflect.DeepEqual(aval, eval), nil |
| 33 | } |
| 34 | |
| 35 | func (matcher *MatchYAMLMatcher) FailureMessage(actual interface{}) (message string) { |
| 36 | actualString, expectedString, _ := matcher.toNormalisedStrings(actual) |
| 37 | return format.Message(actualString, "to match YAML of", expectedString) |
| 38 | } |
| 39 | |
| 40 | func (matcher *MatchYAMLMatcher) NegatedFailureMessage(actual interface{}) (message string) { |
| 41 | actualString, expectedString, _ := matcher.toNormalisedStrings(actual) |
| 42 | return format.Message(actualString, "not to match YAML of", expectedString) |
| 43 | } |
| 44 | |
| 45 | func (matcher *MatchYAMLMatcher) toNormalisedStrings(actual interface{}) (actualFormatted, expectedFormatted string, err error) { |
| 46 | actualString, expectedString, err := matcher.toStrings(actual) |
| 47 | return normalise(actualString), normalise(expectedString), err |
| 48 | } |
| 49 | |
| 50 | func normalise(input string) string { |
| 51 | var val interface{} |
| 52 | err := yaml.Unmarshal([]byte(input), &val) |
| 53 | if err != nil { |
| 54 | panic(err) // guarded by Match |
| 55 | } |
| 56 | output, err := yaml.Marshal(val) |
| 57 | if err != nil { |
| 58 | panic(err) // guarded by Unmarshal |
| 59 | } |
| 60 | return strings.TrimSpace(string(output)) |
| 61 | } |
| 62 | |
| 63 | func (matcher *MatchYAMLMatcher) toStrings(actual interface{}) (actualFormatted, expectedFormatted string, err error) { |
| 64 | actualString, ok := toString(actual) |
| 65 | if !ok { |
| 66 | return "", "", fmt.Errorf("MatchYAMLMatcher matcher requires a string, stringer, or []byte. Got actual:\n%s", format.Object(actual, 1)) |
| 67 | } |
| 68 | expectedString, ok := toString(matcher.YAMLToMatch) |
| 69 | if !ok { |
| 70 | return "", "", fmt.Errorf("MatchYAMLMatcher matcher requires a string, stringer, or []byte. Got expected:\n%s", format.Object(matcher.YAMLToMatch, 1)) |
| 71 | } |
| 72 | |
| 73 | return actualString, expectedString, nil |
| 74 | } |