From c493ecc9521887297d89453c43724f403f20f914 Mon Sep 17 00:00:00 2001 From: Thomas Poignant Date: Thu, 1 Apr 2021 14:22:25 +0200 Subject: [PATCH] Use testify everywhere (#80) --- internal/cache/cache_test.go | 44 ++++++++----------- internal/model/flag_test.go | 5 +-- internal/retriever/retriever_file_test.go | 10 ++--- internal/retriever/retriever_http_test.go | 5 +-- variation_test.go | 51 ++++++++--------------- 5 files changed, 43 insertions(+), 72 deletions(-) diff --git a/internal/cache/cache_test.go b/internal/cache/cache_test.go index 36c79932ad9..0d3711220d6 100644 --- a/internal/cache/cache_test.go +++ b/internal/cache/cache_test.go @@ -73,17 +73,15 @@ func Test_FlagCache_yaml(t *testing.T) { t.Run(tt.name, func(t *testing.T) { fCache := New(NewNotificationService([]notifier.Notifier{})) err := fCache.UpdateCache(tt.args.loadedFlags, "yaml") - if (err != nil) != tt.wantErr { - t.Errorf("UpdateCache() error = %v, wantErr %v", err, tt.wantErr) + if tt.wantErr { + assert.Error(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr) return } - + assert.NoError(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr) // If no error we compare with expected - if err == nil { - for key, value := range tt.expected { - got, _ := fCache.GetFlag(key) - assert.Equal(t, value, got) - } + for key, value := range tt.expected { + got, _ := fCache.GetFlag(key) + assert.Equal(t, value, got) } fCache.Close() }) @@ -146,17 +144,14 @@ func Test_FlagCache_json(t *testing.T) { t.Run(tt.name, func(t *testing.T) { fCache := New(NewNotificationService([]notifier.Notifier{})) err := fCache.UpdateCache(tt.args.loadedFlags, "json") - if (err != nil) != tt.wantErr { - t.Errorf("UpdateCache() error = %v, wantErr %v", err, tt.wantErr) + if tt.wantErr { + assert.Error(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr) return } - - // If no error we compare with expected - if err == nil { - for key, value := range tt.expected { - got, _ := fCache.GetFlag(key) - assert.Equal(t, value, got) - } + assert.NoError(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr) + for key, value := range tt.expected { + got, _ := fCache.GetFlag(key) + assert.Equal(t, value, got) } fCache.Close() }) @@ -215,17 +210,14 @@ default = false`) t.Run(tt.name, func(t *testing.T) { fCache := New(NewNotificationService([]notifier.Notifier{})) err := fCache.UpdateCache(tt.args.loadedFlags, "TOML") - if (err != nil) != tt.wantErr { - t.Errorf("UpdateCache() error = %v, wantErr %v", err, tt.wantErr) + if tt.wantErr { + assert.Error(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr) return } - - // If no error we compare with expected - if err == nil { - for key, value := range tt.expected { - got, _ := fCache.GetFlag(key) - assert.Equal(t, value, got) - } + assert.NoError(t, err, "UpdateCache() error = %v, wantErr %v", err, tt.wantErr) + for key, value := range tt.expected { + got, _ := fCache.GetFlag(key) + assert.Equal(t, value, got) } fCache.Close() }) diff --git a/internal/model/flag_test.go b/internal/model/flag_test.go index 51539261139..2c2802ecfdb 100644 --- a/internal/model/flag_test.go +++ b/internal/model/flag_test.go @@ -326,9 +326,8 @@ func TestFlag_String(t *testing.T) { False: tt.fields.False, Default: tt.fields.Default, } - if got := f.String(); got != tt.want { - t.Errorf("String() = %v, want %v", got, tt.want) - } + got := f.String() + assert.Equal(t, tt.want, got, "String() = %v, want %v", got, tt.want) }) } } diff --git a/internal/retriever/retriever_file_test.go b/internal/retriever/retriever_file_test.go index 1e4ae3edebe..4918f4795e5 100644 --- a/internal/retriever/retriever_file_test.go +++ b/internal/retriever/retriever_file_test.go @@ -2,7 +2,7 @@ package retriever_test import ( "context" - "github.com/google/go-cmp/cmp" + "github.com/stretchr/testify/assert" "testing" "github.com/thomaspoignant/go-feature-flag/internal/retriever" @@ -54,13 +54,11 @@ func Test_localRetriever_Retrieve(t *testing.T) { t.Run(tt.name, func(t *testing.T) { l := retriever.NewLocalRetriever(tt.fields.path) got, err := l.Retrieve(context.Background()) - if (err != nil) != tt.wantErr { - t.Errorf("Retrieve() error = %v, wantErr %v", err, tt.wantErr) + if tt.wantErr { + assert.Error(t, err, "Retrieve() error = %v, wantErr %v", err, tt.wantErr) return } - if !cmp.Equal(got, tt.want) { - t.Errorf(cmp.Diff(got, tt.want)) - } + assert.Equal(t, tt.want, got) }) } } diff --git a/internal/retriever/retriever_http_test.go b/internal/retriever/retriever_http_test.go index 7098bdcaadc..ee9c0d5aebf 100644 --- a/internal/retriever/retriever_http_test.go +++ b/internal/retriever/retriever_http_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "errors" - "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" "io/ioutil" "net/http" @@ -167,9 +166,7 @@ func Test_httpRetriever_Retrieve(t *testing.T) { } if !t.Failed() { - if !cmp.Equal(got, tt.want) { - t.Errorf(cmp.Diff(got, tt.want)) - } + assert.Equal(t, tt.want, got) } }) } diff --git a/variation_test.go b/variation_test.go index 533ea6b14fe..9d8b6b3616e 100644 --- a/variation_test.go +++ b/variation_test.go @@ -2,7 +2,6 @@ package ffclient import ( "errors" - "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" "io/ioutil" "log" @@ -176,13 +175,11 @@ func TestBoolVariation(t *testing.T) { got, err := BoolVariation(tt.args.flagKey, tt.args.user, tt.args.defaultValue) - if (err != nil) != tt.wantErr { - t.Errorf("BoolVariation() error = %v, wantErr %v", err, tt.wantErr) + if tt.wantErr { + assert.Error(t, err, "BoolVariation() error = %v, wantErr %v", err, tt.wantErr) return } - if got != tt.want { - t.Errorf("BoolVariation() got = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, got, "BoolVariation() got = %v, want %v", got, tt.want) if tt.expectedLog != "" { content, _ := ioutil.ReadFile(file.Name()) @@ -338,14 +335,11 @@ func TestFloat64Variation(t *testing.T) { got, err := Float64Variation(tt.args.flagKey, tt.args.user, tt.args.defaultValue) - if (err != nil) != tt.wantErr { - t.Errorf("Float64Variation() error = %v, wantErr %v", err, tt.wantErr) + if tt.wantErr { + assert.Error(t, err, "Float64Variation() error = %v, wantErr %v", err, tt.wantErr) return } - if got != tt.want { - t.Errorf("Float64Variation() got = %v, want %v", got, tt.want) - } - + assert.Equal(t, tt.want, got, "Float64Variation() got = %v, want %v", got, tt.want) if tt.expectedLog != "" { content, _ := ioutil.ReadFile(file.Name()) assert.Regexp(t, tt.expectedLog, string(content)) @@ -500,14 +494,11 @@ func TestJSONArrayVariation(t *testing.T) { got, err := JSONArrayVariation(tt.args.flagKey, tt.args.user, tt.args.defaultValue) - if (err != nil) != tt.wantErr { - t.Errorf("JSONArrayVariation() error = %v, wantErr %v", err, tt.wantErr) + if tt.wantErr { + assert.Error(t, err, "JSONArrayVariation() error = %v, wantErr %v", err, tt.wantErr) return } - if !cmp.Equal(got, tt.want) { - t.Errorf("JSONArrayVariation() got = %v, want %v", got, tt.want) - } - + assert.Equal(t, tt.want, got, "JSONArrayVariation() got = %v, want %v", got, tt.want) if tt.expectedLog != "" { content, _ := ioutil.ReadFile(file.Name()) assert.Regexp(t, tt.expectedLog, string(content)) @@ -662,13 +653,11 @@ func TestJSONVariation(t *testing.T) { got, err := JSONVariation(tt.args.flagKey, tt.args.user, tt.args.defaultValue) - if (err != nil) != tt.wantErr { - t.Errorf("JSONVariation() error = %v, wantErr %v", err, tt.wantErr) + if tt.wantErr { + assert.Error(t, err, "JSONVariation() error = %v, wantErr %v", err, tt.wantErr) return } - if !cmp.Equal(got, tt.want) { - t.Errorf("JSONVariation() got = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, got, "JSONVariation() got = %v, want %v", got, tt.want) if tt.expectedLog != "" { content, _ := ioutil.ReadFile(file.Name()) @@ -825,13 +814,11 @@ func TestStringVariation(t *testing.T) { } got, err := StringVariation(tt.args.flagKey, tt.args.user, tt.args.defaultValue) - if (err != nil) != tt.wantErr { - t.Errorf("StringVariation() error = %v, wantErr %v", err, tt.wantErr) + if tt.wantErr { + assert.Error(t, err, "StringVariation() error = %v, wantErr %v", err, tt.wantErr) return } - if got != tt.want { - t.Errorf("StringVariation() got = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, got, "StringVariation() got = %v, want %v", got, tt.want) if tt.expectedLog != "" { content, _ := ioutil.ReadFile(file.Name()) @@ -987,13 +974,11 @@ func TestIntVariation(t *testing.T) { } got, err := IntVariation(tt.args.flagKey, tt.args.user, tt.args.defaultValue) - if (err != nil) != tt.wantErr { - t.Errorf("IntVariation() error = %v, wantErr %v", err, tt.wantErr) + if tt.wantErr { + assert.Error(t, err, "IntVariation() error = %v, wantErr %v", err, tt.wantErr) return } - if got != tt.want { - t.Errorf("IntVariation() got = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, got, "IntVariation() got = %v, want %v", got, tt.want) if tt.expectedLog != "" { content, _ := ioutil.ReadFile(file.Name())