Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: replace ifs with asserts to simplify tests #1656

Merged
merged 1 commit into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBindFlagValueSet(t *testing.T) {
Expand All @@ -29,9 +30,7 @@ func TestBindFlagValueSet(t *testing.T) {
flagValueSet := pflagValueSet{flagSet}

err := BindFlagValues(flagValueSet)
if err != nil {
t.Fatalf("error binding flag set, %v", err)
}
require.NoError(t, err, "error binding flag set")

flagSet.VisitAll(func(flag *pflag.Flag) {
flag.Value.Set(mutatedTestValues[flag.Name])
Expand Down
32 changes: 10 additions & 22 deletions internal/encoding/decoder_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package encoding

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type decoder struct {
Expand All @@ -22,23 +24,17 @@ func TestDecoderRegistry_RegisterDecoder(t *testing.T) {
registry := NewDecoderRegistry()

err := registry.RegisterDecoder("myformat", decoder{})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
})

t.Run("AlreadyRegistered", func(t *testing.T) {
registry := NewDecoderRegistry()

err := registry.RegisterDecoder("myformat", decoder{})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

err = registry.RegisterDecoder("myformat", decoder{})
if err != ErrDecoderFormatAlreadyRegistered {
t.Fatalf("expected ErrDecoderFormatAlreadyRegistered, got: %v", err)
}
assert.ErrorIs(t, err, ErrDecoderFormatAlreadyRegistered)
})
}

Expand All @@ -52,20 +48,14 @@ func TestDecoderRegistry_Decode(t *testing.T) {
}

err := registry.RegisterDecoder("myformat", decoder)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

v := map[string]any{}

err = registry.Decode("myformat", []byte("key: value"), v)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if !reflect.DeepEqual(decoder.v, v) {
t.Fatalf("decoded value does not match the expected one\nactual: %+v\nexpected: %+v", v, decoder.v)
}
assert.Equal(t, decoder.v, v)
})

t.Run("DecoderNotFound", func(t *testing.T) {
Expand All @@ -74,8 +64,6 @@ func TestDecoderRegistry_Decode(t *testing.T) {
v := map[string]any{}

err := registry.Decode("myformat", nil, v)
if err != ErrDecoderNotFound {
t.Fatalf("expected ErrDecoderNotFound, got: %v", err)
}
assert.ErrorIs(t, err, ErrDecoderNotFound)
})
}
24 changes: 8 additions & 16 deletions internal/encoding/dotenv/codec_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dotenv

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// original form of the data
Expand All @@ -23,13 +25,9 @@ func TestCodec_Encode(t *testing.T) {
codec := Codec{}

b, err := codec.Encode(data)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if encoded != string(b) {
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
}
assert.Equal(t, encoded, string(b))
}

func TestCodec_Decode(t *testing.T) {
Expand All @@ -39,13 +37,9 @@ func TestCodec_Decode(t *testing.T) {
v := map[string]any{}

err := codec.Decode([]byte(original), v)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if !reflect.DeepEqual(data, v) {
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", v, data)
}
assert.Equal(t, data, v)
})

t.Run("InvalidData", func(t *testing.T) {
Expand All @@ -54,9 +48,7 @@ func TestCodec_Decode(t *testing.T) {
v := map[string]any{}

err := codec.Decode([]byte(`invalid data`), v)
if err == nil {
t.Fatal("expected decoding to fail")
}
require.Error(t, err)

t.Logf("decoding failed as expected: %s", err)
})
Expand Down
31 changes: 10 additions & 21 deletions internal/encoding/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package encoding

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type encoder struct {
Expand All @@ -17,23 +20,17 @@ func TestEncoderRegistry_RegisterEncoder(t *testing.T) {
registry := NewEncoderRegistry()

err := registry.RegisterEncoder("myformat", encoder{})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
})

t.Run("AlreadyRegistered", func(t *testing.T) {
registry := NewEncoderRegistry()

err := registry.RegisterEncoder("myformat", encoder{})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

err = registry.RegisterEncoder("myformat", encoder{})
if err != ErrEncoderFormatAlreadyRegistered {
t.Fatalf("expected ErrEncoderFormatAlreadyRegistered, got: %v", err)
}
assert.ErrorIs(t, err, ErrEncoderFormatAlreadyRegistered)
})
}

Expand All @@ -45,26 +42,18 @@ func TestEncoderRegistry_Decode(t *testing.T) {
}

err := registry.RegisterEncoder("myformat", encoder)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

b, err := registry.Encode("myformat", map[string]any{"key": "value"})
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if string(b) != "key: value" {
t.Fatalf("expected 'key: value', got: %#v", string(b))
}
assert.Equal(t, "key: value", string(b))
})

t.Run("EncoderNotFound", func(t *testing.T) {
registry := NewEncoderRegistry()

_, err := registry.Encode("myformat", map[string]any{"key": "value"})
if err != ErrEncoderNotFound {
t.Fatalf("expected ErrEncoderNotFound, got: %v", err)
}
assert.ErrorIs(t, err, ErrEncoderNotFound)
})
}
24 changes: 8 additions & 16 deletions internal/encoding/hcl/codec_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package hcl

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// original form of the data
Expand Down Expand Up @@ -100,13 +102,9 @@ func TestCodec_Encode(t *testing.T) {
codec := Codec{}

b, err := codec.Encode(data)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if encoded != string(b) {
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
}
assert.Equal(t, encoded, string(b))
}

func TestCodec_Decode(t *testing.T) {
Expand All @@ -116,13 +114,9 @@ func TestCodec_Decode(t *testing.T) {
v := map[string]any{}

err := codec.Decode([]byte(original), v)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if !reflect.DeepEqual(decoded, v) {
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", v, decoded)
}
assert.Equal(t, decoded, v)
})

t.Run("InvalidData", func(t *testing.T) {
Expand All @@ -131,9 +125,7 @@ func TestCodec_Decode(t *testing.T) {
v := map[string]any{}

err := codec.Decode([]byte(`invalid data`), v)
if err == nil {
t.Fatal("expected decoding to fail")
}
require.Error(t, err)

t.Logf("decoding failed as expected: %s", err)
})
Expand Down
32 changes: 10 additions & 22 deletions internal/encoding/ini/codec_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ini

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// original form of the data
Expand Down Expand Up @@ -48,13 +50,9 @@ func TestCodec_Encode(t *testing.T) {
codec := Codec{}

b, err := codec.Encode(data)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if encoded != string(b) {
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
}
assert.Equal(t, encoded, string(b))
})

t.Run("Default", func(t *testing.T) {
Expand All @@ -70,13 +68,9 @@ func TestCodec_Encode(t *testing.T) {
}

b, err := codec.Encode(data)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if encoded != string(b) {
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", string(b), encoded)
}
assert.Equal(t, encoded, string(b))
})
}

Expand All @@ -87,13 +81,9 @@ func TestCodec_Decode(t *testing.T) {
v := map[string]any{}

err := codec.Decode([]byte(original), v)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

if !reflect.DeepEqual(decoded, v) {
t.Fatalf("decoded value does not match the expected one\nactual: %#v\nexpected: %#v", v, decoded)
}
assert.Equal(t, decoded, v)
})

t.Run("InvalidData", func(t *testing.T) {
Expand All @@ -102,9 +92,7 @@ func TestCodec_Decode(t *testing.T) {
v := map[string]any{}

err := codec.Decode([]byte(`invalid data`), v)
if err == nil {
t.Fatal("expected decoding to fail")
}
require.Error(t, err)

t.Logf("decoding failed as expected: %s", err)
})
Expand Down
Loading
Loading