Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | package matchers |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "reflect" |
| 8 | "strings" |
| 9 | |
| 10 | "github.com/onsi/gomega/format" |
| 11 | ) |
| 12 | |
| 13 | type MatchJSONMatcher struct { |
| 14 | JSONToMatch interface{} |
| 15 | firstFailurePath []interface{} |
| 16 | } |
| 17 | |
| 18 | func (matcher *MatchJSONMatcher) Match(actual interface{}) (success bool, err error) { |
| 19 | actualString, expectedString, err := matcher.prettyPrint(actual) |
| 20 | if err != nil { |
| 21 | return false, err |
| 22 | } |
| 23 | |
| 24 | var aval interface{} |
| 25 | var eval interface{} |
| 26 | |
| 27 | // this is guarded by prettyPrint |
| 28 | json.Unmarshal([]byte(actualString), &aval) |
| 29 | json.Unmarshal([]byte(expectedString), &eval) |
| 30 | var equal bool |
| 31 | equal, matcher.firstFailurePath = deepEqual(aval, eval) |
| 32 | return equal, nil |
| 33 | } |
| 34 | |
| 35 | func (matcher *MatchJSONMatcher) FailureMessage(actual interface{}) (message string) { |
| 36 | actualString, expectedString, _ := matcher.prettyPrint(actual) |
| 37 | return formattedMessage(format.Message(actualString, "to match JSON of", expectedString), matcher.firstFailurePath) |
| 38 | } |
| 39 | |
| 40 | func (matcher *MatchJSONMatcher) NegatedFailureMessage(actual interface{}) (message string) { |
| 41 | actualString, expectedString, _ := matcher.prettyPrint(actual) |
| 42 | return formattedMessage(format.Message(actualString, "not to match JSON of", expectedString), matcher.firstFailurePath) |
| 43 | } |
| 44 | |
| 45 | func formattedMessage(comparisonMessage string, failurePath []interface{}) string { |
| 46 | var diffMessage string |
| 47 | if len(failurePath) == 0 { |
| 48 | diffMessage = "" |
| 49 | } else { |
| 50 | diffMessage = fmt.Sprintf("\n\nfirst mismatched key: %s", formattedFailurePath(failurePath)) |
| 51 | } |
| 52 | return fmt.Sprintf("%s%s", comparisonMessage, diffMessage) |
| 53 | } |
| 54 | |
| 55 | func formattedFailurePath(failurePath []interface{}) string { |
| 56 | formattedPaths := []string{} |
| 57 | for i := len(failurePath) - 1; i >= 0; i-- { |
| 58 | switch p := failurePath[i].(type) { |
| 59 | case int: |
| 60 | formattedPaths = append(formattedPaths, fmt.Sprintf(`[%d]`, p)) |
| 61 | default: |
| 62 | if i != len(failurePath)-1 { |
| 63 | formattedPaths = append(formattedPaths, ".") |
| 64 | } |
| 65 | formattedPaths = append(formattedPaths, fmt.Sprintf(`"%s"`, p)) |
| 66 | } |
| 67 | } |
| 68 | return strings.Join(formattedPaths, "") |
| 69 | } |
| 70 | |
| 71 | func (matcher *MatchJSONMatcher) prettyPrint(actual interface{}) (actualFormatted, expectedFormatted string, err error) { |
| 72 | actualString, ok := toString(actual) |
| 73 | if !ok { |
| 74 | return "", "", fmt.Errorf("MatchJSONMatcher matcher requires a string, stringer, or []byte. Got actual:\n%s", format.Object(actual, 1)) |
| 75 | } |
| 76 | expectedString, ok := toString(matcher.JSONToMatch) |
| 77 | if !ok { |
| 78 | return "", "", fmt.Errorf("MatchJSONMatcher matcher requires a string, stringer, or []byte. Got expected:\n%s", format.Object(matcher.JSONToMatch, 1)) |
| 79 | } |
| 80 | |
| 81 | abuf := new(bytes.Buffer) |
| 82 | ebuf := new(bytes.Buffer) |
| 83 | |
| 84 | if err := json.Indent(abuf, []byte(actualString), "", " "); err != nil { |
| 85 | return "", "", fmt.Errorf("Actual '%s' should be valid JSON, but it is not.\nUnderlying error:%s", actualString, err) |
| 86 | } |
| 87 | |
| 88 | if err := json.Indent(ebuf, []byte(expectedString), "", " "); err != nil { |
| 89 | return "", "", fmt.Errorf("Expected '%s' should be valid JSON, but it is not.\nUnderlying error:%s", expectedString, err) |
| 90 | } |
| 91 | |
| 92 | return abuf.String(), ebuf.String(), nil |
| 93 | } |
| 94 | |
| 95 | func deepEqual(a interface{}, b interface{}) (bool, []interface{}) { |
| 96 | var errorPath []interface{} |
| 97 | if reflect.TypeOf(a) != reflect.TypeOf(b) { |
| 98 | return false, errorPath |
| 99 | } |
| 100 | |
| 101 | switch a.(type) { |
| 102 | case []interface{}: |
| 103 | if len(a.([]interface{})) != len(b.([]interface{})) { |
| 104 | return false, errorPath |
| 105 | } |
| 106 | |
| 107 | for i, v := range a.([]interface{}) { |
| 108 | elementEqual, keyPath := deepEqual(v, b.([]interface{})[i]) |
| 109 | if !elementEqual { |
| 110 | return false, append(keyPath, i) |
| 111 | } |
| 112 | } |
| 113 | return true, errorPath |
| 114 | |
| 115 | case map[string]interface{}: |
| 116 | if len(a.(map[string]interface{})) != len(b.(map[string]interface{})) { |
| 117 | return false, errorPath |
| 118 | } |
| 119 | |
| 120 | for k, v1 := range a.(map[string]interface{}) { |
| 121 | v2, ok := b.(map[string]interface{})[k] |
| 122 | if !ok { |
| 123 | return false, errorPath |
| 124 | } |
| 125 | elementEqual, keyPath := deepEqual(v1, v2) |
| 126 | if !elementEqual { |
| 127 | return false, append(keyPath, k) |
| 128 | } |
| 129 | } |
| 130 | return true, errorPath |
| 131 | |
| 132 | default: |
| 133 | return a == b, errorPath |
| 134 | } |
| 135 | } |