Skip to content

Commit

Permalink
test(framework): move from stretchr/testify to shoenig/test
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoksr committed Oct 24, 2024
1 parent 54b99ea commit f9430ba
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 95 deletions.
111 changes: 55 additions & 56 deletions defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/shoenig/test/must"
)

func TestApplyDefaults(t *testing.T) {
Expand Down Expand Up @@ -96,8 +95,8 @@ func TestApplyDefaultsErrors(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := applyDefaults(tt.config, tt.defaults)
require.Error(t, err)
assert.Contains(t, err.Error(), tt.expected)
must.Error(t, err)
must.ErrorContains(t, err, tt.expected)
})
}
}
Expand All @@ -116,8 +115,8 @@ func testBasicTypes(t *testing.T) {
}

err := applyDefaults(config, defaults)
require.NoError(t, err)
assert.Equal(t, &SimpleStruct{Name: "Default", Age: 30}, config)
must.NoError(t, err)
must.Eq(t, &SimpleStruct{Name: "Default", Age: 30}, config)
}

func testNestedAndEmbeddedStructs(t *testing.T) {
Expand Down Expand Up @@ -186,8 +185,8 @@ func testNestedAndEmbeddedStructs(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := applyDefaults(tt.config, tt.defaults)
require.NoError(t, err)
assert.Equal(t, tt.expected, tt.config)
must.NoError(t, err)
must.Eq(t, tt.expected, tt.config)
})
}
}
Expand Down Expand Up @@ -262,8 +261,8 @@ func testSlicesAndPointers(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := applyDefaults(tt.config, tt.defaults)
require.NoError(t, err)
assert.Equal(t, tt.expected, tt.config)
must.NoError(t, err)
must.Eq(t, tt.expected, tt.config)
})
}
}
Expand All @@ -284,8 +283,8 @@ func testMultipleDefaults(t *testing.T) {
}

err := applyDefaults(config, defaults)
require.NoError(t, err)
assert.Equal(t, &SimpleStruct{Name: "Third", Age: 30}, config)
must.NoError(t, err)
must.Eq(t, &SimpleStruct{Name: "Third", Age: 30}, config)
}

func testComplexNestedStruct(t *testing.T) {
Expand Down Expand Up @@ -342,7 +341,7 @@ func testComplexNestedStruct(t *testing.T) {
}

err := applyDefaults(config, defaults)
require.NoError(t, err)
must.NoError(t, err)

expected := &ComplexStruct{
Name: "DefaultName",
Expand All @@ -360,7 +359,7 @@ func testComplexNestedStruct(t *testing.T) {
},
}

assert.Equal(t, expected, config)
must.Eq(t, expected, config)
}

func testInterfaceSlice(t *testing.T) {
Expand Down Expand Up @@ -400,25 +399,25 @@ func testInterfaceSlice(t *testing.T) {
}

err := applyDefaults(config, defaults)
require.NoError(t, err)
must.NoError(t, err)

devices := config.Devices
require.Len(t, devices, 3)
must.Len(t, 3, devices)

light1, ok := devices[0].(*LightDevice)
require.True(t, ok)
assert.True(t, light1.Enabled)
assert.Equal(t, 50, light1.Brightness)
must.True(t, ok)
must.True(t, light1.Enabled)
must.Eq(t, 50, light1.Brightness)

light2, ok := devices[1].(*LightDevice)
require.True(t, ok)
assert.False(t, light2.Enabled)
assert.Equal(t, 75, light2.Brightness)
must.True(t, ok)
must.False(t, light2.Enabled)
must.Eq(t, 75, light2.Brightness)

thermostat, ok := devices[2].(*ThermostatDevice)
require.True(t, ok)
assert.True(t, thermostat.Enabled)
assert.InEpsilon(t, 20.0, thermostat.Temperature, 0.001)
must.True(t, ok)
must.True(t, thermostat.Enabled)
must.InDelta(t, 20.0, thermostat.Temperature, 0.001)
}

func testCircularReferences(t *testing.T) {
Expand All @@ -439,13 +438,13 @@ func testCircularReferences(t *testing.T) {
}

err := applyDefaults(config, defaults)
require.Error(t, err)
assert.Equal(t, ErrCircularReference, err)
must.Error(t, err)
must.Eq(t, ErrCircularReference, err)

assert.Equal(t, "Start", config.Name)
assert.Equal(t, "Middle", config.Next.Name)
assert.Equal(t, "Default", config.Next.Next.Name)
assert.Equal(t, config, config.Next.Next.Next)
must.Eq(t, "Start", config.Name)
must.Eq(t, "Middle", config.Next.Name)
must.Eq(t, "Default", config.Next.Next.Name)
must.Eq(t, config, config.Next.Next.Next)
}

func testInterfaceFields(t *testing.T) {
Expand All @@ -472,17 +471,17 @@ func testInterfaceFields(t *testing.T) {
}

err := applyDefaults(config, defaults)
require.NoError(t, err)
must.NoError(t, err)

concData, ok := config.Data.(*ConcreteType)
require.True(t, ok)
assert.Equal(t, "default", concData.Value)
must.True(t, ok)
must.Eq(t, "default", concData.Value)

nestedContainer, ok := config.NestedContainer.(*InterfaceContainer)
require.True(t, ok)
must.True(t, ok)
nestedData, ok := nestedContainer.Data.(*ConcreteType)
require.True(t, ok)
assert.Equal(t, "nested", nestedData.Value)
must.True(t, ok)
must.Eq(t, "nested", nestedData.Value)
}

func testMaps(t *testing.T) {
Expand Down Expand Up @@ -522,14 +521,14 @@ func testMaps(t *testing.T) {
}

err := applyDefaults(config, defaults)
require.NoError(t, err)

assert.Equal(t, "value", config.StringMap["existing"])
assert.Equal(t, "value", config.StringMap["default"])
assert.Equal(t, 42, config.IntMap["default"])
assert.Equal(t, SimpleStruct{Name: "Alice", Age: 30}, config.StructMap["existing"])
assert.Equal(t, SimpleStruct{Name: "DefaultName", Age: 20}, config.StructMap["empty"])
assert.Equal(t, SimpleStruct{Name: "Default", Age: 25}, config.StructMap["default"])
must.NoError(t, err)

must.Eq(t, "value", config.StringMap["existing"])
must.Eq(t, "value", config.StringMap["default"])
must.Eq(t, 42, config.IntMap["default"])
must.Eq(t, SimpleStruct{Name: "Alice", Age: 30}, config.StructMap["existing"])
must.Eq(t, SimpleStruct{Name: "DefaultName", Age: 20}, config.StructMap["empty"])
must.Eq(t, SimpleStruct{Name: "Default", Age: 25}, config.StructMap["default"])
}

func testEmbeddedStructs(t *testing.T) {
Expand Down Expand Up @@ -570,15 +569,15 @@ func testEmbeddedStructs(t *testing.T) {
}

err := applyDefaults(config, defaults)
require.NoError(t, err)
must.NoError(t, err)

unexportedValue := reflect.ValueOf(config).Elem().FieldByName("unexportedEmbedded")
privateFieldValue := unexportedValue.FieldByName("privateField")
assert.Equal(t, "", privateFieldValue.String())
must.Eq(t, "", privateFieldValue.String())

assert.Equal(t, "default level 1", config.Level1Field)
assert.Equal(t, 42, config.Level2Field)
assert.True(t, config.TopLevelField)
must.Eq(t, "default level 1", config.Level1Field)
must.Eq(t, 42, config.Level2Field)
must.True(t, config.TopLevelField)
}

type Animal interface {
Expand Down Expand Up @@ -623,15 +622,15 @@ func testSlicesOfInterfaces(t *testing.T) {
}

err := applyDefaults(config, defaults)
require.NoError(t, err)
must.NoError(t, err)

require.Len(t, config.Animals, 2)
must.Len(t, 2, config.Animals)

dog, ok := config.Animals[0].(*Dog)
require.True(t, ok)
assert.Equal(t, "Buddy", dog.Name)
must.True(t, ok)
must.Eq(t, "Buddy", dog.Name)

cat, ok := config.Animals[1].(*Cat)
require.True(t, ok)
assert.Equal(t, "DefaultCat", cat.Name)
must.True(t, ok)
must.Eq(t, "DefaultCat", cat.Name)
}
8 changes: 2 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ module github.com/nikoksr/konfetty

go 1.22.5

require github.com/stretchr/testify v1.9.0
require github.com/shoenig/test v1.11.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require github.com/google/go-cmp v0.6.0 // indirect
14 changes: 4 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/shoenig/test v1.11.0 h1:NoPa5GIoBwuqzIviCrnUJa+t5Xb4xi5Z+zODJnIDsEQ=
github.com/shoenig/test v1.11.0/go.mod h1:UxJ6u/x2v/TNs/LoLxBNJRV9DiwBBKYxXSyczsBHFoI=
45 changes: 22 additions & 23 deletions konfetty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/shoenig/test/must"

"github.com/nikoksr/konfetty"
)
Expand All @@ -21,10 +20,10 @@ func TestFromStruct(t *testing.T) {

config := &TestConfig{Name: "Alice", Age: 30, IsAdmin: true}
processor := konfetty.FromStruct(config)

result, err := processor.Build()
require.NoError(t, err)
assert.Equal(t, config, result)

must.NoError(t, err)
must.Eq(t, config, result)
}

func TestFromLoaderFunc(t *testing.T) {
Expand All @@ -36,8 +35,8 @@ func TestFromLoaderFunc(t *testing.T) {
processor := konfetty.FromLoaderFunc(loader)

result, err := processor.Build()
require.NoError(t, err)
assert.Equal(t, &TestConfig{Name: "Bob", Age: 25, IsAdmin: false}, result)
must.NoError(t, err)
must.Eq(t, &TestConfig{Name: "Bob", Age: 25, IsAdmin: false}, result)
}

type MockProvider struct {
Expand All @@ -56,8 +55,8 @@ func TestFromProvider(t *testing.T) {
processor := konfetty.FromProvider(provider)

result, err := processor.Build()
require.NoError(t, err)
assert.Equal(t, &TestConfig{Name: "Charlie", Age: 35, IsAdmin: true}, result)
must.NoError(t, err)
must.Eq(t, &TestConfig{Name: "Charlie", Age: 35, IsAdmin: true}, result)
}

func TestWithDefaults(t *testing.T) {
Expand All @@ -67,8 +66,8 @@ func TestWithDefaults(t *testing.T) {
processor := konfetty.FromStruct(config).WithDefaults(TestConfig{Name: "Default", Age: 18, IsAdmin: false})

result, err := processor.Build()
require.NoError(t, err)
assert.Equal(t, &TestConfig{Name: "Default", Age: 18, IsAdmin: false}, result)
must.NoError(t, err)
must.Eq(t, &TestConfig{Name: "Default", Age: 18, IsAdmin: false}, result)
}

func TestWithTransformer(t *testing.T) {
Expand All @@ -82,8 +81,8 @@ func TestWithTransformer(t *testing.T) {
processor := konfetty.FromStruct(config).WithTransformer(transformer)

result, err := processor.Build()
require.NoError(t, err)
assert.Equal(t, &TestConfig{Name: "Mr. Dave", Age: 21}, result)
must.NoError(t, err)
must.Eq(t, &TestConfig{Name: "Mr. Dave", Age: 21}, result)
}

func TestWithValidator(t *testing.T) {
Expand All @@ -99,8 +98,8 @@ func TestWithValidator(t *testing.T) {
processor := konfetty.FromStruct(config).WithValidator(validator)

_, err := processor.Build()
require.Error(t, err)
assert.Contains(t, err.Error(), "age must be 18 or older")
must.Error(t, err)
must.ErrorContains(t, err, "age must be 18 or older")
}

func TestBuildWithAllOptions(t *testing.T) {
Expand All @@ -120,8 +119,8 @@ func TestBuildWithAllOptions(t *testing.T) {
})

result, err := processor.Build()
require.NoError(t, err)
assert.Equal(t, &TestConfig{Name: "Mr. Default", Age: 18, IsAdmin: false}, result)
must.NoError(t, err)
must.Eq(t, &TestConfig{Name: "Mr. Default", Age: 18, IsAdmin: false}, result)
}

func TestBuildErrorCases(t *testing.T) {
Expand All @@ -136,8 +135,8 @@ func TestBuildErrorCases(t *testing.T) {
processor := konfetty.FromLoaderFunc(loader)

_, err := processor.Build()
require.Error(t, err)
assert.Contains(t, err.Error(), "loader error")
must.Error(t, err)
must.ErrorContains(t, err, "loader error")
})

t.Run("ProviderError", func(t *testing.T) {
Expand All @@ -147,8 +146,8 @@ func TestBuildErrorCases(t *testing.T) {
processor := konfetty.FromProvider(provider)

_, err := processor.Build()
require.Error(t, err)
assert.Contains(t, err.Error(), "provider error")
must.Error(t, err)
must.ErrorContains(t, err, "provider error")
})

t.Run("ValidatorError", func(t *testing.T) {
Expand All @@ -161,7 +160,7 @@ func TestBuildErrorCases(t *testing.T) {
processor := konfetty.FromStruct(config).WithValidator(validator)

_, err := processor.Build()
require.Error(t, err)
assert.Contains(t, err.Error(), "validator error")
must.Error(t, err)
must.ErrorContains(t, err, "validator error")
})
}

0 comments on commit f9430ba

Please sign in to comment.