Skip to content

Instantly share code, notes, and snippets.

@brianfoshee
Last active March 18, 2016 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianfoshee/ce32db4ec0e6726d69a3 to your computer and use it in GitHub Desktop.
Save brianfoshee/ce32db4ec0e6726d69a3 to your computer and use it in GitHub Desktop.
// compareJSON is a lightweight alternative to
// https://github.com/onsi/gomega/blob/master/matchers/match_json_matcher.go
func compareJSON(actual []byte, expected []byte) error {
var aval interface{}
var eval interface{}
if err := json.Unmarshal(actual, &aval); err != nil {
return err
}
if err := json.Unmarshal(expected, &eval); err != nil {
return err
}
if !reflect.DeepEqual(aval, eval) {
var oa, oe bytes.Buffer
if err := json.Indent(&oa, actual, "", "\t"); err != nil {
return err
}
if err := json.Indent(&oe, expected, "", "\t"); err != nil {
return err
}
return fmt.Errorf("\nExpected: %s\nActual: %s", oe.Bytes(), oa.Bytes())
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment