From 7562c9a6d27760c64e6b0990a538a5416769d975 Mon Sep 17 00:00:00 2001 From: Jade McGough Date: Thu, 3 Jan 2019 14:29:22 -0800 Subject: [PATCH] chore(http): make test diffs more readable (#2203) * chore(http): make test diffs more readable * fixes * add back telegraf output spacing * whitespace hell * jsonEqual2 -> jsonEqual --- http/api_handler_test.go | 7 +++--- http/auth_test.go | 8 +++---- http/bucket_test.go | 28 ++++++++++++------------ http/dashboard_test.go | 32 +++++++++++++-------------- http/health_test.go | 4 ++-- http/org_test.go | 12 +++++----- http/router_test.go | 12 +++++----- http/task_service_test.go | 16 +++++++------- http/telegraf_test.go | 16 +++++++------- http/user_resource_mapping_test.go | 8 +++---- http/view_test.go | 35 ++++++++++-------------------- 11 files changed, 83 insertions(+), 95 deletions(-) diff --git a/http/api_handler_test.go b/http/api_handler_test.go index b3eee60a169..dfa710dddb2 100644 --- a/http/api_handler_test.go +++ b/http/api_handler_test.go @@ -1,11 +1,12 @@ package http import ( - "go.uber.org/zap" "io/ioutil" "net/http" "net/http/httptest" "testing" + + "go.uber.org/zap" ) func TestAPIHandler_NotFound(t *testing.T) { @@ -64,8 +65,8 @@ func TestAPIHandler_NotFound(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. get %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. get\n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. -got/+want%s", tt.name, diff) } }) diff --git a/http/auth_test.go b/http/auth_test.go index dd2a799eeb9..25707940ed2 100644 --- a/http/auth_test.go +++ b/http/auth_test.go @@ -184,7 +184,7 @@ func TestService_handleGetAuthorizations(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleGetAuthorizations() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, diff, _ := jsonEqual2(string(body), tt.wants.body); tt.wants.body != "" && !eq { + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { t.Errorf("%q. handleGetAuthorizations() = -got/+want %s", tt.name, diff) } @@ -347,7 +347,7 @@ func TestService_handleGetAuthorization(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleGetAuthorization() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, diff, err := jsonEqual2(string(body), tt.wants.body); err != nil || (tt.wants.body != "" && !eq) { + if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil || (tt.wants.body != "" && !eq) { t.Errorf("%q. handleGetAuthorization() = -got/+want %s**", tt.name, diff) } }) @@ -485,7 +485,7 @@ func TestService_handlePostAuthorization(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handlePostAuthorization() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, diff, _ := jsonEqual2(string(body), tt.wants.body); tt.wants.body != "" && !eq { + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { t.Errorf("%q. handlePostAuthorization() = -got/+want%s", tt.name, diff) } }) @@ -593,7 +593,7 @@ func TestService_handleDeleteAuthorization(t *testing.T) { t.Errorf("%q. handleDeleteAuthorization() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, diff, _ := jsonEqual2(string(body), tt.wants.body); !eq { + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); !eq { t.Errorf("%q. handleDeleteAuthorization() = ***%s***", tt.name, diff) } }) diff --git a/http/bucket_test.go b/http/bucket_test.go index e5aa859384f..3f5cc99c80e 100644 --- a/http/bucket_test.go +++ b/http/bucket_test.go @@ -161,8 +161,8 @@ func TestService_handleGetBuckets(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleGetBuckets() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleGetBuckets() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleGetBuckets() = ***%s***", tt.name, diff) } }) } @@ -282,8 +282,8 @@ func TestService_handleGetBucket(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleGetBucket() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleGetBucket() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleGetBucket() = ***%s***", tt.name, diff) } }) } @@ -373,8 +373,8 @@ func TestService_handlePostBucket(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handlePostBucket() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handlePostBucket() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handlePostBucket() = ***%s***", tt.name, diff) } }) } @@ -474,8 +474,8 @@ func TestService_handleDeleteBucket(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleDeleteBucket() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleDeleteBucket() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleDeleteBucket() = ***%s***", tt.name, diff) } }) } @@ -764,8 +764,8 @@ func TestService_handlePatchBucket(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handlePatchBucket() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handlePatchBucket() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handlePatchBucket() = ***%s***", tt.name, diff) } }) } @@ -852,8 +852,8 @@ func TestService_handlePostBucketMember(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handlePostBucketMember() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handlePostBucketMember() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handlePostBucketMember() = ***%s***", tt.name, diff) } }) } @@ -940,8 +940,8 @@ func TestService_handlePostBucketOwner(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handlePostBucketOwner() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handlePostBucketOwner() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handlePostBucketOwner() = ***%s***", tt.name, diff) } }) } diff --git a/http/dashboard_test.go b/http/dashboard_test.go index 53c341e48b6..743e53420ea 100644 --- a/http/dashboard_test.go +++ b/http/dashboard_test.go @@ -228,8 +228,8 @@ func TestService_handleGetDashboards(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleGetDashboards() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleGetDashboards() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleGetDashboards() = ***%s***", tt.name, diff) } }) @@ -380,8 +380,8 @@ func TestService_handleGetDashboard(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleGetDashboard() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleGetDashboard() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleGetDashboard() = ***%s***", tt.name, diff) } }) } @@ -504,8 +504,8 @@ func TestService_handlePostDashboard(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handlePostDashboard() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handlePostDashboard() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handlePostDashboard() = ***%s***", tt.name, diff) } }) } @@ -605,8 +605,8 @@ func TestService_handleDeleteDashboard(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleDeleteDashboard() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleDeleteDashboard() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleDeleteDashboard() = ***%s***", tt.name, diff) } }) } @@ -791,8 +791,8 @@ func TestService_handlePatchDashboard(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handlePatchDashboard() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handlePatchDashboard() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handlePatchDashboard() = ***%s***", tt.name, diff) } }) } @@ -897,8 +897,8 @@ func TestService_handlePostDashboardCell(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handlePostDashboardCell() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handlePostDashboardCell() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handlePostDashboardCell() = ***%s***", tt.name, diff) } }) } @@ -981,8 +981,8 @@ func TestService_handleDeleteDashboardCell(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleDeleteDashboardCell() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleDeleteDashboardCell() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleDeleteDashboardCell() = ***%s***", tt.name, diff) } }) } @@ -1119,8 +1119,8 @@ func TestService_handlePatchDashboardCell(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handlePatchDashboardCell() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handlePatchDashboardCell() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handlePatchDashboardCell() = ***%s***", tt.name, diff) } }) } diff --git a/http/health_test.go b/http/health_test.go index 18fd3a8e47e..21b8378a445 100644 --- a/http/health_test.go +++ b/http/health_test.go @@ -43,8 +43,8 @@ func TestHealthHandler(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. HealthHandler() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. HealthHandler() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. HealthHandler() = ***%s***", tt.name, diff) } }) } diff --git a/http/org_test.go b/http/org_test.go index 96b6f558809..92eb0a9d038 100644 --- a/http/org_test.go +++ b/http/org_test.go @@ -142,8 +142,8 @@ func TestSecretService_handleGetSecrets(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("handleGetSecrets() = %v, want %v", content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("handleGetSecrets() = \n***%v***\n,\nwant\n***%v***", string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("handleGetSecrets() = ***%s***", diff) } }) @@ -218,8 +218,8 @@ func TestSecretService_handlePatchSecrets(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("handlePatchSecrets() = %v, want %v", content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("handlePatchSecrets() = \n***%v***\n,\nwant\n***%v***", string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("handlePatchSecrets() = ***%s***", diff) } }) @@ -294,8 +294,8 @@ func TestSecretService_handleDeleteSecrets(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("handleDeleteSecrets() = %v, want %v", content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("handleDeleteSecrets() = \n***%v***\n,\nwant\n***%v***", string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("handleDeleteSecrets() = ***%s***", diff) } }) diff --git a/http/router_test.go b/http/router_test.go index f5e61f411e6..64f483992d0 100644 --- a/http/router_test.go +++ b/http/router_test.go @@ -96,8 +96,8 @@ func TestRouter_NotFound(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. get %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. get\n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. get ***%s***", tt.name, diff) } }) } @@ -193,8 +193,8 @@ func TestRouter_Panic(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. get %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. get\n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. get ***%s***", tt.name, diff) } }) @@ -290,8 +290,8 @@ func TestRouter_MethodNotAllowed(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. get %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. get\n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. get ***%s***", tt.name, diff) } }) diff --git a/http/task_service_test.go b/http/task_service_test.go index fba79195e39..12cb993e5e2 100644 --- a/http/task_service_test.go +++ b/http/task_service_test.go @@ -162,8 +162,8 @@ func TestTaskHandler_handleGetTasks(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleGetTasks() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleGetTasks() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleGetTasks() = ***%s***", tt.name, diff) } }) } @@ -264,8 +264,8 @@ func TestTaskHandler_handlePostTasks(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handlePostTask() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handlePostTask() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handlePostTask() = ***%s***", tt.name, diff) } }) } @@ -368,8 +368,8 @@ func TestTaskHandler_handleGetRun(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleGetRun() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleGetRun() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleGetRun() = ***%s***", tt.name, diff) } }) } @@ -476,8 +476,8 @@ func TestTaskHandler_handleGetRuns(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleGetRuns() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleGetRuns() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleGetRuns() = ***%s***", tt.name, diff) } }) } diff --git a/http/telegraf_test.go b/http/telegraf_test.go index 2cf8c6ba54b..8db0c4dc9c6 100644 --- a/http/telegraf_test.go +++ b/http/telegraf_test.go @@ -119,8 +119,8 @@ func TestTelegrafHandler_handleGetTelegrafs(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleGetTelegrafs() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleGetTelegrafs() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleGetTelegrafs() = ***%s***", tt.name, diff) } }) } @@ -642,8 +642,8 @@ func TestTelegrafHandler_handleGetTelegraf(t *testing.T) { } if strings.Contains(tt.wants.contentType, "application/json") { - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleGetTelegraf() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleGetTelegraf() = ***%s***", tt.name, diff) } } else if string(body) != tt.wants.body { t.Errorf("%q. handleGetTelegraf() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) @@ -729,8 +729,8 @@ func Test_newTelegrafResponses(t *testing.T) { if err != nil { t.Fatalf("newTelegrafResponses() JSON marshal %v", err) } - if eq, _ := jsonEqual(string(got), tt.want); tt.want != "" && !eq { - t.Errorf("%q. newTelegrafResponses() = \n***%v***\n,\nwant\n***%v***", tt.name, string(got), tt.want) + if eq, diff, _ := jsonEqual(string(got), tt.want); tt.want != "" && !eq { + t.Errorf("%q. newTelegrafResponses() = ***%s***", tt.name, diff) } }) } @@ -807,8 +807,8 @@ func Test_newTelegrafResponse(t *testing.T) { if err != nil { t.Fatalf("newTelegrafResponse() JSON marshal %v", err) } - if eq, _ := jsonEqual(string(got), tt.want); tt.want != "" && !eq { - t.Errorf("%q. newTelegrafResponse() = \n%v\n,\nwant\n%v", tt.name, string(got), tt.want) + if eq, diff, _ := jsonEqual(string(got), tt.want); tt.want != "" && !eq { + t.Errorf("%q. newTelegrafResponse() = ***%s***", tt.name, diff) } }) } diff --git a/http/user_resource_mapping_test.go b/http/user_resource_mapping_test.go index 8c1debde382..0df279f6c58 100644 --- a/http/user_resource_mapping_test.go +++ b/http/user_resource_mapping_test.go @@ -203,8 +203,8 @@ func TestUserResourceMappingService_GetMembersHandler(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. GetMembersHandler() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), fmt.Sprintf(tt.wants.body, resourceType)); tt.wants.body != "" && !eq { - t.Errorf("%q. GetMembersHandler() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), fmt.Sprintf(tt.wants.body, resourceType)) + if eq, diff, _ := jsonEqual(string(body), fmt.Sprintf(tt.wants.body, resourceType)); tt.wants.body != "" && !eq { + t.Errorf("%q. GetMembersHandler() = ***%s***", tt.name, diff) } }) } @@ -353,8 +353,8 @@ func TestUserResourceMappingService_PostMembersHandler(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. PostMembersHandler() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. PostMembersHandler() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. PostMembersHandler() = ***%s***", tt.name, diff) } }) } diff --git a/http/view_test.go b/http/view_test.go index b3f24c1b97e..058bf2c13fc 100644 --- a/http/view_test.go +++ b/http/view_test.go @@ -158,8 +158,8 @@ func TestService_handleGetViews(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleGetViews() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleGetViews() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleGetViews() = ***%s***", tt.name, diff) } }) @@ -271,8 +271,8 @@ func TestService_handleGetView(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleGetView() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleGetView() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleGetView() = ***%s***", tt.name, diff) } }) } @@ -370,8 +370,8 @@ func TestService_handlePostViews(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handlePostViews() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handlePostViews() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handlePostViews() = ***%s***", tt.name, diff) } }) } @@ -468,8 +468,8 @@ func TestService_handleDeleteView(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handleDeleteView() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handleDeleteView() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handleDeleteView() = ***%s***", tt.name, diff) } }) } @@ -640,27 +640,14 @@ func TestService_handlePatchView(t *testing.T) { if tt.wants.contentType != "" && content != tt.wants.contentType { t.Errorf("%q. handlePatchView() = %v, want %v", tt.name, content, tt.wants.contentType) } - if eq, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { - t.Errorf("%q. handlePatchView() = \n***%v***\n,\nwant\n***%v***", tt.name, string(body), tt.wants.body) + if eq, diff, _ := jsonEqual(string(body), tt.wants.body); tt.wants.body != "" && !eq { + t.Errorf("%q. handlePatchView() = ***%s***", tt.name, diff) } }) } } -func jsonEqual(s1, s2 string) (eq bool, err error) { - var o1, o2 interface{} - - if err = json.Unmarshal([]byte(s1), &o1); err != nil { - return - } - if err = json.Unmarshal([]byte(s2), &o2); err != nil { - return - } - - return cmp.Equal(o1, o2), nil -} - -func jsonEqual2(s1, s2 string) (eq bool, diff string, err error) { +func jsonEqual(s1, s2 string) (eq bool, diff string, err error) { var o1, o2 interface{} if s1 == s2 { return true, "", nil