Skip to content

Commit 014204c

Browse files
mikattackAlex Mikitik
and
Alex Mikitik
authored
Replace stretchr/testify with an internal test suite (#981)
As recommended, an `internal/assert` package was added with a reduced set of assertions. All tests were then refactored to use the internal assertions. When more complex assertions were used, they have been rewritten using logic and the simplified assertions. Fancy formatting for failures was omitted. The `internal/assert/assertions.diff` function could be overwritten for better formatting. That is where diff libraries are used in other test suites. Refs: #872 Co-authored-by: Alex Mikitik <alex.mikitik@oracle.com>
1 parent 923b2ab commit 014204c

22 files changed

+681
-352
lines changed

benchmark/bench_datasets_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/pelletier/go-toml/v2"
12-
"github.com/stretchr/testify/require"
12+
"github.com/pelletier/go-toml/v2/internal/assert"
1313
)
1414

1515
var bench_inputs = []struct {
@@ -35,11 +35,11 @@ func TestUnmarshalDatasetCode(t *testing.T) {
3535
buf := fixture(t, tc.name)
3636

3737
var v interface{}
38-
require.NoError(t, toml.Unmarshal(buf, &v))
38+
assert.NoError(t, toml.Unmarshal(buf, &v))
3939

4040
b, err := json.Marshal(v)
41-
require.NoError(t, err)
42-
require.Equal(t, len(b), tc.jsonLen)
41+
assert.NoError(t, err)
42+
assert.Equal(t, len(b), tc.jsonLen)
4343
})
4444
}
4545
}
@@ -53,7 +53,7 @@ func BenchmarkUnmarshalDataset(b *testing.B) {
5353
b.ResetTimer()
5454
for i := 0; i < b.N; i++ {
5555
var v interface{}
56-
require.NoError(b, toml.Unmarshal(buf, &v))
56+
assert.NoError(b, toml.Unmarshal(buf, &v))
5757
}
5858
})
5959
}
@@ -68,13 +68,13 @@ func fixture(tb testing.TB, path string) []byte {
6868
if os.IsNotExist(err) {
6969
tb.Skip("benchmark fixture not found:", file)
7070
}
71-
require.NoError(tb, err)
71+
assert.NoError(tb, err)
7272
defer f.Close()
7373

7474
gz, err := gzip.NewReader(f)
75-
require.NoError(tb, err)
75+
assert.NoError(tb, err)
7676

7777
buf, err := ioutil.ReadAll(gz)
78-
require.NoError(tb, err)
78+
assert.NoError(tb, err)
7979
return buf
8080
}

benchmark/benchmark_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88

99
"github.com/pelletier/go-toml/v2"
10-
"github.com/stretchr/testify/require"
10+
"github.com/pelletier/go-toml/v2/internal/assert"
1111
)
1212

1313
func TestUnmarshalSimple(t *testing.T) {
@@ -345,10 +345,10 @@ type benchmarkDoc struct {
345345

346346
func TestUnmarshalReferenceFile(t *testing.T) {
347347
bytes, err := ioutil.ReadFile("benchmark.toml")
348-
require.NoError(t, err)
348+
assert.NoError(t, err)
349349
d := benchmarkDoc{}
350350
err = toml.Unmarshal(bytes, &d)
351-
require.NoError(t, err)
351+
assert.NoError(t, err)
352352

353353
expected := benchmarkDoc{
354354
Table: struct {
@@ -627,7 +627,7 @@ trimmed in raw strings.
627627
},
628628
}
629629

630-
require.Equal(t, expected, d)
630+
assert.Equal(t, expected, d)
631631
}
632632

633633
var hugoFrontMatterbytes = []byte(`

cmd/jsontoml/main_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import (
55
"strings"
66
"testing"
77

8-
"github.com/stretchr/testify/assert"
9-
"github.com/stretchr/testify/require"
8+
"github.com/pelletier/go-toml/v2/internal/assert"
109
)
1110

1211
func TestConvert(t *testing.T) {
@@ -54,7 +53,7 @@ a = 42
5453
useJsonNumber = e.useJsonNumber
5554
err := convert(strings.NewReader(e.input), b)
5655
if e.errors {
57-
require.Error(t, err)
56+
assert.Error(t, err)
5857
} else {
5958
assert.NoError(t, err)
6059
assert.Equal(t, e.expected, b.String())

cmd/tomljson/main_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import (
77
"strings"
88
"testing"
99

10-
"github.com/stretchr/testify/assert"
11-
"github.com/stretchr/testify/require"
10+
"github.com/pelletier/go-toml/v2/internal/assert"
1211
)
1312

1413
func TestConvert(t *testing.T) {
@@ -46,7 +45,7 @@ a = 42`),
4645
b := new(bytes.Buffer)
4746
err := convert(e.input, b)
4847
if e.errors {
49-
require.Error(t, err)
48+
assert.Error(t, err)
5049
} else {
5150
assert.NoError(t, err)
5251
assert.Equal(t, e.expected, b.String())

cmd/tomll/main_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import (
55
"strings"
66
"testing"
77

8-
"github.com/stretchr/testify/assert"
9-
"github.com/stretchr/testify/require"
8+
"github.com/pelletier/go-toml/v2/internal/assert"
109
)
1110

1211
func TestConvert(t *testing.T) {
@@ -36,7 +35,7 @@ a = 42.0
3635
b := new(bytes.Buffer)
3736
err := convert(strings.NewReader(e.input), b)
3837
if e.errors {
39-
require.Error(t, err)
38+
assert.Error(t, err)
4039
} else {
4140
assert.NoError(t, err)
4241
assert.Equal(t, e.expected, b.String())

errors_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"strings"
88
"testing"
99

10+
"github.com/pelletier/go-toml/v2/internal/assert"
1011
"github.com/pelletier/go-toml/v2/unstable"
11-
"github.com/stretchr/testify/assert"
1212
)
1313

1414
//nolint:funlen

fast_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,37 @@ import (
44
"testing"
55

66
"github.com/pelletier/go-toml/v2"
7-
"github.com/stretchr/testify/require"
7+
"github.com/pelletier/go-toml/v2/internal/assert"
88
)
99

1010
func TestFastSimpleInt(t *testing.T) {
1111
m := map[string]int64{}
1212
err := toml.Unmarshal([]byte(`a = 42`), &m)
13-
require.NoError(t, err)
14-
require.Equal(t, map[string]int64{"a": 42}, m)
13+
assert.NoError(t, err)
14+
assert.Equal(t, map[string]int64{"a": 42}, m)
1515
}
1616

1717
func TestFastSimpleFloat(t *testing.T) {
1818
m := map[string]float64{}
1919
err := toml.Unmarshal([]byte("a = 42\nb = 1.1\nc = 12341234123412341234123412341234"), &m)
20-
require.NoError(t, err)
21-
require.Equal(t, map[string]float64{"a": 42, "b": 1.1, "c": 1.2341234123412342e+31}, m)
20+
assert.NoError(t, err)
21+
assert.Equal(t, map[string]float64{"a": 42, "b": 1.1, "c": 1.2341234123412342e+31}, m)
2222
}
2323

2424
func TestFastSimpleString(t *testing.T) {
2525
m := map[string]string{}
2626
err := toml.Unmarshal([]byte(`a = "hello"`), &m)
27-
require.NoError(t, err)
28-
require.Equal(t, map[string]string{"a": "hello"}, m)
27+
assert.NoError(t, err)
28+
assert.Equal(t, map[string]string{"a": "hello"}, m)
2929
}
3030

3131
func TestFastSimpleInterface(t *testing.T) {
3232
m := map[string]interface{}{}
3333
err := toml.Unmarshal([]byte(`
3434
a = "hello"
3535
b = 42`), &m)
36-
require.NoError(t, err)
37-
require.Equal(t, map[string]interface{}{
36+
assert.NoError(t, err)
37+
assert.Equal(t, map[string]interface{}{
3838
"a": "hello",
3939
"b": int64(42),
4040
}, m)
@@ -46,8 +46,8 @@ func TestFastMultipartKeyInterface(t *testing.T) {
4646
a.interim = "test"
4747
a.b.c = "hello"
4848
b = 42`), &m)
49-
require.NoError(t, err)
50-
require.Equal(t, map[string]interface{}{
49+
assert.NoError(t, err)
50+
assert.Equal(t, map[string]interface{}{
5151
"a": map[string]interface{}{
5252
"interim": "test",
5353
"b": map[string]interface{}{
@@ -66,8 +66,8 @@ func TestFastExistingMap(t *testing.T) {
6666
ints.one = 1
6767
ints.two = 2
6868
strings.yo = "hello"`), &m)
69-
require.NoError(t, err)
70-
require.Equal(t, map[string]interface{}{
69+
assert.NoError(t, err)
70+
assert.Equal(t, map[string]interface{}{
7171
"ints": map[string]interface{}{
7272
"one": int64(1),
7373
"two": int64(2),
@@ -90,9 +90,9 @@ func TestFastArrayTable(t *testing.T) {
9090
m := map[string]interface{}{}
9191

9292
err := toml.Unmarshal(b, &m)
93-
require.NoError(t, err)
93+
assert.NoError(t, err)
9494

95-
require.Equal(t, map[string]interface{}{
95+
assert.Equal(t, map[string]interface{}{
9696
"root": map[string]interface{}{
9797
"nested": []interface{}{
9898
map[string]interface{}{

fuzz_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"testing"
77

88
"github.com/pelletier/go-toml/v2"
9-
"github.com/stretchr/testify/require"
9+
"github.com/pelletier/go-toml/v2/internal/assert"
1010
)
1111

1212
func FuzzUnmarshal(f *testing.F) {
@@ -48,6 +48,6 @@ func FuzzUnmarshal(f *testing.F) {
4848
if err != nil {
4949
t.Fatalf("failed round trip: %s", err)
5050
}
51-
require.Equal(t, v, v2)
51+
assert.Equal(t, v, v2)
5252
})
5353
}

go.mod

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
module github.com/pelletier/go-toml/v2
22

33
go 1.21.0
4-
5-
require github.com/stretchr/testify v1.9.0
6-
7-
require (
8-
github.com/davecgh/go-spew v1.1.1 // indirect
9-
github.com/pmezard/go-difflib v1.0.0 // indirect
10-
gopkg.in/yaml.v3 v3.0.1 // indirect
11-
)

go.sum

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +0,0 @@
1-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2-
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5-
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
6-
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9-
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10-
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)