Skip to content

Commit

Permalink
Merge pull request #32 from goschtalt/switch-to-cmp
Browse files Browse the repository at this point in the history
feat:Leverage testify better for comparisons.
  • Loading branch information
schmidtw authored Apr 20, 2023
2 parents 7265458 + fd05b16 commit a050b3c
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 86 deletions.
3 changes: 1 addition & 2 deletions expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package goschtalt

import (
"errors"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -96,7 +95,7 @@ func TestExpand(t *testing.T) {
c.opts.expansions[0].mapper = nil
c.opts.expansions[0].text = ""

assert.True(reflect.DeepEqual(tc.want, c.opts.expansions[0]))
assert.Equal(tc.want, c.opts.expansions[0])
}
}

Expand Down
2 changes: 1 addition & 1 deletion filegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestWalk(t *testing.T) {
},
expectedErr: iofs.ErrInvalid,
}, {
description: "No file or directory with this patth.",
description: "No file or directory with this path.",
grp: filegroup{
paths: []string{"invalid"},
},
Expand Down
6 changes: 0 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ module github.com/goschtalt/goschtalt
go 1.18

require (
github.com/google/go-cmp v0.5.9
github.com/k0kubun/pp/v3 v3.2.0
github.com/mitchellh/hashstructure v1.1.0
github.com/mitchellh/mapstructure v1.5.0
github.com/stretchr/testify v1.8.2
Expand All @@ -14,12 +12,8 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/k0kubun/pp/v3 v3.2.0 h1:h33hNTZ9nVFNP3u2Fsgz8JXiF5JINoZfFq4SvKJwNcs=
github.com/k0kubun/pp/v3 v3.2.0/go.mod h1:ODtJQbQcIRfAD3N+theGCV1m/CBxweERz2dapdz1EwA=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mitchellh/hashstructure v1.1.0 h1:P6P1hdjqAAknpY/M1CGipelZgp+4y9ja9kmUZPXP+H0=
github.com/mitchellh/hashstructure v1.1.0/go.mod h1:xUDAozZz0Wmdiufv0uyhnHkUTN6/6d8ulp4AwfLKrmA=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand All @@ -35,11 +26,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
gonum.org/v1/gonum v0.12.0 h1:xKuo6hzt+gMav00meVPUlXwSdoEJP46BR+wdxQEFK2o=
gonum.org/v1/gonum v0.12.0/go.mod h1:73TDxJfAAHeA8Mk9mf8NlIppyhQNo5GLTcYeqgo2lvY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
39 changes: 27 additions & 12 deletions goschtalt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"testing/fstest"
"time"

"github.com/google/go-cmp/cmp"
"github.com/goschtalt/goschtalt/pkg/meta"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -167,6 +166,7 @@ func TestCompile(t *testing.T) {
expect any
files []string
expectedErr error
compare func(assert *assert.Assertions, a, b any) bool
}{
{
description: "A normal case with options.",
Expand Down Expand Up @@ -444,6 +444,24 @@ func TestCompile(t *testing.T) {
Func: strings.ToUpper,
},
files: []string{"record1"},
compare: func(assert *assert.Assertions, z, y any) bool {
a := z.(withAll)
b := y.(withAll)

if a.Func != nil {
if !assert.NotNil(b.Func) {
return false
}
str := "Random334 String"
if !assert.Equal(a.Func(str), b.Func(str)) {
return false
}
}

return assert.Equal(a.Foo, b.Foo) &&
assert.Equal(a.Duration, b.Duration) &&
assert.Equal(a.T, b.T)
},
}, {

description: "An empty case.",
Expand Down Expand Up @@ -597,19 +615,16 @@ func TestCompile(t *testing.T) {
err = cfg.Unmarshal(tc.key, &tc.want)
require.NoError(err)

assert.Empty(cmp.Diff(tc.expect, tc.want,
cmp.Comparer(
func(a, b func(string) string) bool {
test := "TestString"
return a(test) == b(test)
},
),
))
if tc.compare != nil {
assert.True(tc.compare(assert, tc.expect, tc.want))
} else {
assert.Equal(tc.expect, tc.want)
}

// check the file order
got, err := cfg.ShowOrder()
require.NoError(err)
assert.Empty(cmp.Diff(tc.files, got))
assert.Equal(tc.files, got)

assert.NotEmpty(tell)
return
Expand Down Expand Up @@ -666,7 +681,7 @@ func TestOrderList(t *testing.T) {

got := cfg.OrderList(tc.in)

assert.Empty(cmp.Diff(tc.expect, got))
assert.Equal(tc.expect, got)
})
}
}
Expand Down Expand Up @@ -697,7 +712,7 @@ func TestExtensions(t *testing.T) {

got := cfg.Extensions()

assert.Empty(cmp.Diff(tc.expect, got))
assert.Equal(tc.expect, got)
})
}
}
Expand Down
12 changes: 1 addition & 11 deletions internal/natsort/natural_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
package natsort

import (
"fmt"
"math/rand"
"sort"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gonum.org/v1/gonum/stat/combin"
Expand Down Expand Up @@ -215,15 +213,7 @@ func run(assert *assert.Assertions, require *require.Assertions, list, want []st

Sort(list)

if !cmp.Equal(list, want) {
fmt.Println("List order resulted in a failure:")
for _, item := range start {
fmt.Printf(" %s\n", item)
}
require.Empty(cmp.Diff(list, want))
} else {
assert.Empty(cmp.Diff(list, want))
}
assert.Equal(list, want)
}

func BenchmarkSort1(b *testing.B) {
Expand Down
3 changes: 1 addition & 2 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/goschtalt/goschtalt/pkg/encoder"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -106,7 +105,7 @@ func TestMarshal(t *testing.T) {

if tc.expectedErr == nil {
assert.NoError(err)
assert.Empty(cmp.Diff(tc.expected, string(got)))
assert.Equal(tc.expected, string(got))
return
}

Expand Down
27 changes: 10 additions & 17 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ import (
"errors"
"fmt"
"path/filepath"
"reflect"
"sort"
"testing"
"testing/fstest"

"github.com/goschtalt/goschtalt/pkg/decoder"
"github.com/goschtalt/goschtalt/pkg/encoder"
"github.com/k0kubun/pp/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestOptions(t *testing.T) {
unknown := errors.New("unknown")

unknownErr := errors.New("unknown err")
testErr := errors.New("test err")
fs := fstest.MapFS{}
abs := fstest.MapFS{}
Expand Down Expand Up @@ -50,7 +47,7 @@ func TestOptions(t *testing.T) {

sorter(got)

return reflect.DeepEqual(got, want)
return assert.Equal(t, got, want)
}

tests := []struct {
Expand Down Expand Up @@ -303,7 +300,7 @@ func TestOptions(t *testing.T) {
str: "WithEncoder( 'json', 'yml' )",
initCodecs: true,
check: func(cfg *options) bool {
return reflect.DeepEqual([]string{"json", "yml"},
return assert.Equal(t, []string{"json", "yml"},
cfg.encoders.extensions())
},
}, {
Expand All @@ -312,7 +309,7 @@ func TestOptions(t *testing.T) {
str: "WithEncoder( 'foo' )",
initCodecs: true,
check: func(cfg *options) bool {
return reflect.DeepEqual([]string{"foo"},
return assert.Equal(t, []string{"foo"},
cfg.encoders.extensions())
},
}, {
Expand All @@ -325,7 +322,7 @@ func TestOptions(t *testing.T) {
str: "WithDecoder( 'json', 'yml' )",
initCodecs: true,
check: func(cfg *options) bool {
return reflect.DeepEqual([]string{"json", "yml"},
return assert.Equal(t, []string{"json", "yml"},
cfg.decoders.extensions())
},
}, {
Expand All @@ -334,7 +331,7 @@ func TestOptions(t *testing.T) {
str: "WithDecoder( 'foo' )",
initCodecs: true,
check: func(cfg *options) bool {
return reflect.DeepEqual([]string{"foo"},
return assert.Equal(t, []string{"foo"},
cfg.decoders.extensions())
},
}, {
Expand Down Expand Up @@ -519,12 +516,12 @@ func TestOptions(t *testing.T) {
description: "AddBuffer( '', bytes )",
opt: AddBuffer("", []byte("bytes")),
str: "AddBuffer( '', []byte )",
expectErr: unknown,
expectErr: unknownErr,
}, {
description: "AddBufferFunc( filename.ext, nil )",
opt: AddBufferFunc("filename.ext", nil),
str: "AddBufferFunc( 'filename.ext', nil )",
expectErr: unknown,
expectErr: unknownErr,
}, {
description: "AddBufferFunc( filename.ext, bytes )",
opt: AddBufferFunc("filename.ext", retBuf),
Expand Down Expand Up @@ -694,16 +691,12 @@ func TestOptions(t *testing.T) {
if tc.check != nil {
assert.True(tc.check(&cfg))
} else {
assert.True(reflect.DeepEqual(tc.goal, cfg))
if !reflect.DeepEqual(tc.goal, cfg) {
pp.Printf("Want:\n%s\n", tc.goal)
pp.Printf("Got:\n%s\n", cfg)
}
assert.Equal(tc.goal, cfg)
}
return
}

if !errors.Is(unknown, tc.expectErr) {
if !errors.Is(unknownErr, tc.expectErr) {
assert.ErrorIs(err, tc.expectErr)
return
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/adapter/adapters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/goschtalt/goschtalt"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -377,7 +376,7 @@ func TestEndToEnd(t *testing.T) {
return
}

assert.Empty(cmp.Diff(tc.from, got))
assert.Equal(tc.from, got)
assert.NoError(err)
})
}
Expand Down
Loading

0 comments on commit a050b3c

Please sign in to comment.