From 794215104585e71a128c04ba1c9f9090eb03193a Mon Sep 17 00:00:00 2001 From: Joeky Zhan Date: Sun, 14 Apr 2019 12:52:03 +0800 Subject: [PATCH] Use reflect.DeepEqual to compare json strings Signed-off-by: Joeky --- examples/api/api_test.go | 34 +++++++--------------------------- examples/db/api_test.go | 21 +++++++++++++-------- 2 files changed, 20 insertions(+), 35 deletions(-) diff --git a/examples/api/api_test.go b/examples/api/api_test.go index 964ef5a..11a3b38 100644 --- a/examples/api/api_test.go +++ b/examples/api/api_test.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "reflect" "github.com/DATA-DOG/godog" "github.com/DATA-DOG/godog/gherkin" @@ -51,44 +52,23 @@ func (a *apiFeature) theResponseCodeShouldBe(code int) error { } func (a *apiFeature) theResponseShouldMatchJSON(body *gherkin.DocString) (err error) { - var expected, actual []byte - var exp, act interface{} + var expected, actual interface{} // re-encode expected response - if err = json.Unmarshal([]byte(body.Content), &exp); err != nil { - return - } - if expected, err = json.MarshalIndent(exp, "", " "); err != nil { + if err = json.Unmarshal([]byte(body.Content), &expected); err != nil { return } // re-encode actual response too - if err = json.Unmarshal(a.resp.Body.Bytes(), &act); err != nil { - return - } - if actual, err = json.MarshalIndent(act, "", " "); err != nil { + if err = json.Unmarshal(a.resp.Body.Bytes(), &actual); err != nil { return } // the matching may be adapted per different requirements. - if len(actual) != len(expected) { - return fmt.Errorf( - "expected json length: %d does not match actual: %d:\n%s", - len(expected), - len(actual), - string(actual), - ) + if !reflect.DeepEqual(expected, actual) { + return fmt.Errorf("expected JSON does not match actual, %v vs. %v", expected, actual) } - - for i, b := range actual { - if b != expected[i] { - return fmt.Errorf( - "expected JSON does not match actual, showing up to last matched character:\n%s", - string(actual[:i+1]), - ) - } - } - return + return nil } func FeatureContext(s *godog.Suite) { diff --git a/examples/db/api_test.go b/examples/db/api_test.go index b40b9b7..77e45aa 100644 --- a/examples/db/api_test.go +++ b/examples/db/api_test.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "net/http/httptest" + "reflect" "strings" txdb "github.com/DATA-DOG/go-txdb" @@ -71,19 +72,23 @@ func (a *apiFeature) theResponseCodeShouldBe(code int) error { } func (a *apiFeature) theResponseShouldMatchJSON(body *gherkin.DocString) (err error) { - var expected, actual []byte - var data interface{} - if err = json.Unmarshal([]byte(body.Content), &data); err != nil { + var expected, actual interface{} + + // re-encode expected response + if err = json.Unmarshal([]byte(body.Content), &expected); err != nil { return } - if expected, err = json.Marshal(data); err != nil { + + // re-encode actual response too + if err = json.Unmarshal(a.resp.Body.Bytes(), &actual); err != nil { return } - actual = a.resp.Body.Bytes() - if string(actual) != string(expected) { - err = fmt.Errorf("expected json %s, does not match actual: %s", string(expected), string(actual)) + + // the matching may be adapted per different requirements. + if !reflect.DeepEqual(expected, actual) { + return fmt.Errorf("expected JSON does not match actual, %v vs. %v", expected, actual) } - return + return nil } func (a *apiFeature) thereAreUsers(users *gherkin.DataTable) error {