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>
This commit is contained in:
Alex Mikitik
2025-04-07 03:36:37 -07:00
committed by GitHub
parent 923b2ab478
commit 014204cfb7
22 changed files with 681 additions and 352 deletions
+8 -8
View File
@@ -9,7 +9,7 @@ import (
"testing" "testing"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/require" "github.com/pelletier/go-toml/v2/internal/assert"
) )
var bench_inputs = []struct { var bench_inputs = []struct {
@@ -35,11 +35,11 @@ func TestUnmarshalDatasetCode(t *testing.T) {
buf := fixture(t, tc.name) buf := fixture(t, tc.name)
var v interface{} var v interface{}
require.NoError(t, toml.Unmarshal(buf, &v)) assert.NoError(t, toml.Unmarshal(buf, &v))
b, err := json.Marshal(v) b, err := json.Marshal(v)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, len(b), tc.jsonLen) assert.Equal(t, len(b), tc.jsonLen)
}) })
} }
} }
@@ -53,7 +53,7 @@ func BenchmarkUnmarshalDataset(b *testing.B) {
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
var v interface{} var v interface{}
require.NoError(b, toml.Unmarshal(buf, &v)) assert.NoError(b, toml.Unmarshal(buf, &v))
} }
}) })
} }
@@ -68,13 +68,13 @@ func fixture(tb testing.TB, path string) []byte {
if os.IsNotExist(err) { if os.IsNotExist(err) {
tb.Skip("benchmark fixture not found:", file) tb.Skip("benchmark fixture not found:", file)
} }
require.NoError(tb, err) assert.NoError(tb, err)
defer f.Close() defer f.Close()
gz, err := gzip.NewReader(f) gz, err := gzip.NewReader(f)
require.NoError(tb, err) assert.NoError(tb, err)
buf, err := ioutil.ReadAll(gz) buf, err := ioutil.ReadAll(gz)
require.NoError(tb, err) assert.NoError(tb, err)
return buf return buf
} }
+4 -4
View File
@@ -7,7 +7,7 @@ import (
"time" "time"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/require" "github.com/pelletier/go-toml/v2/internal/assert"
) )
func TestUnmarshalSimple(t *testing.T) { func TestUnmarshalSimple(t *testing.T) {
@@ -345,10 +345,10 @@ type benchmarkDoc struct {
func TestUnmarshalReferenceFile(t *testing.T) { func TestUnmarshalReferenceFile(t *testing.T) {
bytes, err := ioutil.ReadFile("benchmark.toml") bytes, err := ioutil.ReadFile("benchmark.toml")
require.NoError(t, err) assert.NoError(t, err)
d := benchmarkDoc{} d := benchmarkDoc{}
err = toml.Unmarshal(bytes, &d) err = toml.Unmarshal(bytes, &d)
require.NoError(t, err) assert.NoError(t, err)
expected := benchmarkDoc{ expected := benchmarkDoc{
Table: struct { Table: struct {
@@ -627,7 +627,7 @@ trimmed in raw strings.
}, },
} }
require.Equal(t, expected, d) assert.Equal(t, expected, d)
} }
var hugoFrontMatterbytes = []byte(` var hugoFrontMatterbytes = []byte(`
+2 -3
View File
@@ -5,8 +5,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/pelletier/go-toml/v2/internal/assert"
"github.com/stretchr/testify/require"
) )
func TestConvert(t *testing.T) { func TestConvert(t *testing.T) {
@@ -54,7 +53,7 @@ a = 42
useJsonNumber = e.useJsonNumber useJsonNumber = e.useJsonNumber
err := convert(strings.NewReader(e.input), b) err := convert(strings.NewReader(e.input), b)
if e.errors { if e.errors {
require.Error(t, err) assert.Error(t, err)
} else { } else {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, e.expected, b.String()) assert.Equal(t, e.expected, b.String())
+2 -3
View File
@@ -7,8 +7,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/pelletier/go-toml/v2/internal/assert"
"github.com/stretchr/testify/require"
) )
func TestConvert(t *testing.T) { func TestConvert(t *testing.T) {
@@ -46,7 +45,7 @@ a = 42`),
b := new(bytes.Buffer) b := new(bytes.Buffer)
err := convert(e.input, b) err := convert(e.input, b)
if e.errors { if e.errors {
require.Error(t, err) assert.Error(t, err)
} else { } else {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, e.expected, b.String()) assert.Equal(t, e.expected, b.String())
+2 -3
View File
@@ -5,8 +5,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/pelletier/go-toml/v2/internal/assert"
"github.com/stretchr/testify/require"
) )
func TestConvert(t *testing.T) { func TestConvert(t *testing.T) {
@@ -36,7 +35,7 @@ a = 42.0
b := new(bytes.Buffer) b := new(bytes.Buffer)
err := convert(strings.NewReader(e.input), b) err := convert(strings.NewReader(e.input), b)
if e.errors { if e.errors {
require.Error(t, err) assert.Error(t, err)
} else { } else {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, e.expected, b.String()) assert.Equal(t, e.expected, b.String())
+1 -1
View File
@@ -7,8 +7,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/pelletier/go-toml/v2/internal/assert"
"github.com/pelletier/go-toml/v2/unstable" "github.com/pelletier/go-toml/v2/unstable"
"github.com/stretchr/testify/assert"
) )
//nolint:funlen //nolint:funlen
+15 -15
View File
@@ -4,28 +4,28 @@ import (
"testing" "testing"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/require" "github.com/pelletier/go-toml/v2/internal/assert"
) )
func TestFastSimpleInt(t *testing.T) { func TestFastSimpleInt(t *testing.T) {
m := map[string]int64{} m := map[string]int64{}
err := toml.Unmarshal([]byte(`a = 42`), &m) err := toml.Unmarshal([]byte(`a = 42`), &m)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]int64{"a": 42}, m) assert.Equal(t, map[string]int64{"a": 42}, m)
} }
func TestFastSimpleFloat(t *testing.T) { func TestFastSimpleFloat(t *testing.T) {
m := map[string]float64{} m := map[string]float64{}
err := toml.Unmarshal([]byte("a = 42\nb = 1.1\nc = 12341234123412341234123412341234"), &m) err := toml.Unmarshal([]byte("a = 42\nb = 1.1\nc = 12341234123412341234123412341234"), &m)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]float64{"a": 42, "b": 1.1, "c": 1.2341234123412342e+31}, m) assert.Equal(t, map[string]float64{"a": 42, "b": 1.1, "c": 1.2341234123412342e+31}, m)
} }
func TestFastSimpleString(t *testing.T) { func TestFastSimpleString(t *testing.T) {
m := map[string]string{} m := map[string]string{}
err := toml.Unmarshal([]byte(`a = "hello"`), &m) err := toml.Unmarshal([]byte(`a = "hello"`), &m)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]string{"a": "hello"}, m) assert.Equal(t, map[string]string{"a": "hello"}, m)
} }
func TestFastSimpleInterface(t *testing.T) { func TestFastSimpleInterface(t *testing.T) {
@@ -33,8 +33,8 @@ func TestFastSimpleInterface(t *testing.T) {
err := toml.Unmarshal([]byte(` err := toml.Unmarshal([]byte(`
a = "hello" a = "hello"
b = 42`), &m) b = 42`), &m)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]interface{}{ assert.Equal(t, map[string]interface{}{
"a": "hello", "a": "hello",
"b": int64(42), "b": int64(42),
}, m) }, m)
@@ -46,8 +46,8 @@ func TestFastMultipartKeyInterface(t *testing.T) {
a.interim = "test" a.interim = "test"
a.b.c = "hello" a.b.c = "hello"
b = 42`), &m) b = 42`), &m)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]interface{}{ assert.Equal(t, map[string]interface{}{
"a": map[string]interface{}{ "a": map[string]interface{}{
"interim": "test", "interim": "test",
"b": map[string]interface{}{ "b": map[string]interface{}{
@@ -66,8 +66,8 @@ func TestFastExistingMap(t *testing.T) {
ints.one = 1 ints.one = 1
ints.two = 2 ints.two = 2
strings.yo = "hello"`), &m) strings.yo = "hello"`), &m)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]interface{}{ assert.Equal(t, map[string]interface{}{
"ints": map[string]interface{}{ "ints": map[string]interface{}{
"one": int64(1), "one": int64(1),
"two": int64(2), "two": int64(2),
@@ -90,9 +90,9 @@ func TestFastArrayTable(t *testing.T) {
m := map[string]interface{}{} m := map[string]interface{}{}
err := toml.Unmarshal(b, &m) err := toml.Unmarshal(b, &m)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]interface{}{ assert.Equal(t, map[string]interface{}{
"root": map[string]interface{}{ "root": map[string]interface{}{
"nested": []interface{}{ "nested": []interface{}{
map[string]interface{}{ map[string]interface{}{
+2 -2
View File
@@ -6,7 +6,7 @@ import (
"testing" "testing"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/require" "github.com/pelletier/go-toml/v2/internal/assert"
) )
func FuzzUnmarshal(f *testing.F) { func FuzzUnmarshal(f *testing.F) {
@@ -48,6 +48,6 @@ func FuzzUnmarshal(f *testing.F) {
if err != nil { if err != nil {
t.Fatalf("failed round trip: %s", err) t.Fatalf("failed round trip: %s", err)
} }
require.Equal(t, v, v2) assert.Equal(t, v, v2)
}) })
} }
-8
View File
@@ -1,11 +1,3 @@
module github.com/pelletier/go-toml/v2 module github.com/pelletier/go-toml/v2
go 1.21.0 go 1.21.0
require github.com/stretchr/testify v1.9.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
)
-10
View File
@@ -1,10 +0,0 @@
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=
+135
View File
@@ -0,0 +1,135 @@
package assert
import (
"bytes"
"fmt"
"reflect"
"strings"
"testing"
)
// True asserts that an expression is true.
func True(t testing.TB, ok bool, msgAndArgs ...any) {
if ok {
return
}
t.Helper()
t.Fatal(formatMsgAndArgs("Expected expression to be true", msgAndArgs...))
}
// False asserts that an expression is false.
func False(t testing.TB, ok bool, msgAndArgs ...any) {
if !ok {
return
}
t.Helper()
t.Fatal(formatMsgAndArgs("Expected expression to be false", msgAndArgs...))
}
// Equal asserts that "expected" and "actual" are equal.
func Equal[T any](t testing.TB, expected, actual T, msgAndArgs ...any) {
if objectsAreEqual(expected, actual) {
return
}
t.Helper()
msg := formatMsgAndArgs("Expected values to be equal:", msgAndArgs...)
t.Fatalf("%s\n%s", msg, diff(expected, actual))
}
// Error asserts that an error is not nil.
func Error(t testing.TB, err error, msgAndArgs ...any) {
if err != nil {
return
}
t.Helper()
t.Fatal(formatMsgAndArgs("Expected an error", msgAndArgs...))
}
// NoError asserts that an error is nil.
func NoError(t testing.TB, err error, msgAndArgs ...any) {
if err == nil {
return
}
t.Helper()
msg := formatMsgAndArgs("Unexpected error:", msgAndArgs...)
t.Fatalf("%s\n%+v", msg, err)
}
// Panics asserts that the given function panics.
func Panics(t testing.TB, fn func(), msgAndArgs ...any) {
t.Helper()
defer func() {
if recover() == nil {
msg := formatMsgAndArgs("Expected function to panic", msgAndArgs...)
t.Fatal(msg)
}
}()
fn()
}
// Zero asserts that a value is its zero value.
func Zero[T any](t testing.TB, value T, msgAndArgs ...any) {
var zero T
if objectsAreEqual(value, zero) {
return
}
val := reflect.ValueOf(value)
if (val.Kind() == reflect.Slice || val.Kind() == reflect.Map || val.Kind() == reflect.Array) && val.Len() == 0 {
return
}
t.Helper()
msg := formatMsgAndArgs("Expected zero value but got:", msgAndArgs...)
t.Fatalf("%s\n%s", msg, fmt.Sprintf("%v", value))
}
func NotZero[T any](t testing.TB, value T, msgAndArgs ...any) {
var zero T
if !objectsAreEqual(value, zero) {
val := reflect.ValueOf(value)
if !((val.Kind() == reflect.Slice || val.Kind() == reflect.Map || val.Kind() == reflect.Array) && val.Len() == 0) {
return
}
}
t.Helper()
msg := formatMsgAndArgs("Unexpected zero value:", msgAndArgs...)
t.Fatalf("%s\n%s", msg, fmt.Sprintf("%v", value))
}
func formatMsgAndArgs(msg string, args ...any) string {
if len(args) == 0 {
return msg
}
format, ok := args[0].(string)
if !ok {
panic("message argument must be a fmt string")
}
return fmt.Sprintf(format, args[1:]...)
}
func diff(expected, actual any) string {
lines := []string{
"expected:",
fmt.Sprintf("%v", expected),
"actual:",
fmt.Sprintf("%v", actual),
}
return strings.Join(lines, "\n")
}
func objectsAreEqual(expected, actual any) bool {
if expected == nil || actual == nil {
return expected == actual
}
if exp, eok := expected.([]byte); eok {
if act, aok := actual.([]byte); aok {
return bytes.Equal(exp, act)
}
}
if exp, eok := expected.(string); eok {
if act, aok := actual.(string); aok {
return exp == act
}
}
return reflect.DeepEqual(expected, actual)
}
+184
View File
@@ -0,0 +1,184 @@
package assert
import (
"fmt"
"testing"
)
type Data struct {
Label string
Value int64
}
func TestBadMessage(t *testing.T) {
invalidMessage := func() { True(t, false, 1234) }
assertOk(t, "Non-fmt message value", func(t testing.TB) {
Panics(t, invalidMessage)
})
assertFail(t, "Non-fmt message value", func(t testing.TB) {
True(t, false, "example %s", "message")
})
}
func TestTrue(t *testing.T) {
assertOk(t, "Succeed", func(t testing.TB) {
True(t, 1 > 0)
})
assertFail(t, "Fail", func(t testing.TB) {
True(t, 1 < 0)
})
}
func TestFalse(t *testing.T) {
assertOk(t, "Succeed", func(t testing.TB) {
False(t, 1 < 0)
})
assertFail(t, "Fail", func(t testing.TB) {
False(t, 1 > 0)
})
}
func TestEqual(t *testing.T) {
assertOk(t, "Nil", func(t testing.TB) {
Equal(t, interface{}(nil), interface{}(nil))
})
assertOk(t, "Identical structs", func(t testing.TB) {
Equal(t, Data{"expected", 1234}, Data{"expected", 1234})
})
assertFail(t, "Different structs", func(t testing.TB) {
Equal(t, Data{"expected", 1234}, Data{"actual", 1234})
})
assertOk(t, "Identical numbers", func(t testing.TB) {
Equal(t, 1234, 1234)
})
assertFail(t, "Identical numbers", func(t testing.TB) {
Equal(t, 1234, 1324)
})
assertOk(t, "Zero-length byte arrays", func(t testing.TB) {
Equal(t, []byte(nil), []byte(""))
})
assertOk(t, "Identical byte arrays", func(t testing.TB) {
Equal(t, []byte{1, 2, 3, 4}, []byte{1, 2, 3, 4})
})
assertFail(t, "Different byte arrays", func(t testing.TB) {
Equal(t, []byte{1, 2, 3, 4}, []byte{1, 3, 2, 4})
})
assertOk(t, "Identical strings", func(t testing.TB) {
Equal(t, "example", "example")
})
assertFail(t, "Identical strings", func(t testing.TB) {
Equal(t, "example", "elpmaxe")
})
}
func TestError(t *testing.T) {
assertOk(t, "Error", func(t testing.TB) {
Error(t, fmt.Errorf("example"))
})
assertFail(t, "Nil", func(t testing.TB) {
Error(t, nil)
})
}
func TestNoError(t *testing.T) {
assertFail(t, "Error", func(t testing.TB) {
NoError(t, fmt.Errorf("example"))
})
assertOk(t, "Nil", func(t testing.TB) {
NoError(t, nil)
})
}
func TestPanics(t *testing.T) {
willPanic := func() { panic("example") }
wontPanic := func() {}
assertOk(t, "Will panic", func(t testing.TB) {
Panics(t, willPanic)
})
assertFail(t, "Won't panic", func(t testing.TB) {
Panics(t, wontPanic)
})
}
func TestZero(t *testing.T) {
assertOk(t, "Empty struct", func(t testing.TB) {
Zero(t, Data{})
})
assertFail(t, "Non-empty struct", func(t testing.TB) {
Zero(t, Data{Label: "example"})
})
assertOk(t, "Nil slice", func(t testing.TB) {
var slice []int
Zero(t, slice)
})
assertFail(t, "Non-empty slice", func(t testing.TB) {
slice := []int{1, 2, 3, 4}
Zero(t, slice)
})
assertOk(t, "Zero-length slice", func(t testing.TB) {
slice := []int{}
Zero(t, slice)
})
}
func TestNotZero(t *testing.T) {
assertFail(t, "Empty struct", func(t testing.TB) {
zero := Data{}
NotZero(t, zero)
})
assertOk(t, "Non-empty struct", func(t testing.TB) {
notZero := Data{Label: "example"}
NotZero(t, notZero)
})
assertFail(t, "Nil slice", func(t testing.TB) {
var slice []int
NotZero(t, slice)
})
assertFail(t, "Zero-length slice", func(t testing.TB) {
slice := []int{}
NotZero(t, slice)
})
assertOk(t, "Non-empty slice", func(t testing.TB) {
slice := []int{1, 2, 3, 4}
NotZero(t, slice)
})
}
type testCase struct {
*testing.T
failed string
}
func (t *testCase) Fatal(args ...interface{}) {
t.failed = fmt.Sprint(args...)
}
func (t *testCase) Fatalf(message string, args ...interface{}) {
t.failed = fmt.Sprintf(message, args...)
}
func assertFail(t *testing.T, name string, fn func(t testing.TB)) {
t.Helper()
t.Run(name, func(t *testing.T) {
t.Helper()
test := &testCase{T: t}
fn(test)
if test.failed == "" {
t.Fatal("Test expected to fail but did not")
} else {
t.Log(test.failed)
}
})
}
func assertOk(t *testing.T, name string, fn func(t testing.TB)) {
t.Helper()
t.Run(name, func(t *testing.T) {
t.Helper()
test := &testCase{T: t}
fn(test)
if test.failed != "" {
t.Fatal("Test expected to succeed but did not:\n", test.failed)
}
})
}
+27 -28
View File
@@ -11,8 +11,7 @@ import (
"testing" "testing"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/assert" "github.com/pelletier/go-toml/v2/internal/assert"
"github.com/stretchr/testify/require"
) )
func processMain(args []string, input io.Reader, stdout, stderr io.Writer, f ConvertFn) int { func processMain(args []string, input io.Reader, stdout, stderr io.Writer, f ConvertFn) int {
@@ -30,8 +29,8 @@ func TestProcessMainStdin(t *testing.T) {
}) })
assert.Equal(t, 0, exit) assert.Equal(t, 0, exit)
assert.Empty(t, stdout.String()) assert.Zero(t, stdout.String())
assert.Empty(t, stderr.String()) assert.Zero(t, stderr.String())
} }
func TestProcessMainStdinErr(t *testing.T) { func TestProcessMainStdinErr(t *testing.T) {
@@ -44,8 +43,8 @@ func TestProcessMainStdinErr(t *testing.T) {
}) })
assert.Equal(t, -1, exit) assert.Equal(t, -1, exit)
assert.Empty(t, stdout.String()) assert.Zero(t, stdout.String())
assert.NotEmpty(t, stderr.String()) assert.NotZero(t, stderr.String())
} }
func TestProcessMainStdinDecodeErr(t *testing.T) { func TestProcessMainStdinDecodeErr(t *testing.T) {
@@ -59,16 +58,16 @@ func TestProcessMainStdinDecodeErr(t *testing.T) {
}) })
assert.Equal(t, -1, exit) assert.Equal(t, -1, exit)
assert.Empty(t, stdout.String()) assert.Zero(t, stdout.String())
assert.Contains(t, stderr.String(), "error occurred at") assert.True(t, strings.Contains(stderr.String(), "error occurred at"))
} }
func TestProcessMainFileExists(t *testing.T) { func TestProcessMainFileExists(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "example") tmpfile, err := ioutil.TempFile("", "example")
require.NoError(t, err) assert.NoError(t, err)
defer os.Remove(tmpfile.Name()) defer os.Remove(tmpfile.Name())
_, err = tmpfile.Write([]byte(`some data`)) _, err = tmpfile.Write([]byte(`some data`))
require.NoError(t, err) assert.NoError(t, err)
stdout := new(bytes.Buffer) stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer) stderr := new(bytes.Buffer)
@@ -78,8 +77,8 @@ func TestProcessMainFileExists(t *testing.T) {
}) })
assert.Equal(t, 0, exit) assert.Equal(t, 0, exit)
assert.Empty(t, stdout.String()) assert.Zero(t, stdout.String())
assert.Empty(t, stderr.String()) assert.Zero(t, stderr.String())
} }
func TestProcessMainFileDoesNotExist(t *testing.T) { func TestProcessMainFileDoesNotExist(t *testing.T) {
@@ -91,22 +90,22 @@ func TestProcessMainFileDoesNotExist(t *testing.T) {
}) })
assert.Equal(t, -1, exit) assert.Equal(t, -1, exit)
assert.Empty(t, stdout.String()) assert.Zero(t, stdout.String())
assert.NotEmpty(t, stderr.String()) assert.NotZero(t, stderr.String())
} }
func TestProcessMainFilesInPlace(t *testing.T) { func TestProcessMainFilesInPlace(t *testing.T) {
dir, err := ioutil.TempDir("", "") dir, err := ioutil.TempDir("", "")
require.NoError(t, err) assert.NoError(t, err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
path1 := path.Join(dir, "file1") path1 := path.Join(dir, "file1")
path2 := path.Join(dir, "file2") path2 := path.Join(dir, "file2")
err = ioutil.WriteFile(path1, []byte("content 1"), 0600) err = ioutil.WriteFile(path1, []byte("content 1"), 0600)
require.NoError(t, err) assert.NoError(t, err)
err = ioutil.WriteFile(path2, []byte("content 2"), 0600) err = ioutil.WriteFile(path2, []byte("content 2"), 0600)
require.NoError(t, err) assert.NoError(t, err)
p := Program{ p := Program{
Fn: dummyFileFn, Fn: dummyFileFn,
@@ -115,15 +114,15 @@ func TestProcessMainFilesInPlace(t *testing.T) {
exit := p.main([]string{path1, path2}, os.Stdin, os.Stdout, os.Stderr) exit := p.main([]string{path1, path2}, os.Stdin, os.Stdout, os.Stderr)
require.Equal(t, 0, exit) assert.Equal(t, 0, exit)
v1, err := ioutil.ReadFile(path1) v1, err := ioutil.ReadFile(path1)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "1", string(v1)) assert.Equal(t, "1", string(v1))
v2, err := ioutil.ReadFile(path2) v2, err := ioutil.ReadFile(path2)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "2", string(v2)) assert.Equal(t, "2", string(v2))
} }
func TestProcessMainFilesInPlaceErrRead(t *testing.T) { func TestProcessMainFilesInPlaceErrRead(t *testing.T) {
@@ -134,18 +133,18 @@ func TestProcessMainFilesInPlaceErrRead(t *testing.T) {
exit := p.main([]string{"/this/path/is/invalid"}, os.Stdin, os.Stdout, os.Stderr) exit := p.main([]string{"/this/path/is/invalid"}, os.Stdin, os.Stdout, os.Stderr)
require.Equal(t, -1, exit) assert.Equal(t, -1, exit)
} }
func TestProcessMainFilesInPlaceFailFn(t *testing.T) { func TestProcessMainFilesInPlaceFailFn(t *testing.T) {
dir, err := ioutil.TempDir("", "") dir, err := ioutil.TempDir("", "")
require.NoError(t, err) assert.NoError(t, err)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
path1 := path.Join(dir, "file1") path1 := path.Join(dir, "file1")
err = ioutil.WriteFile(path1, []byte("content 1"), 0600) err = ioutil.WriteFile(path1, []byte("content 1"), 0600)
require.NoError(t, err) assert.NoError(t, err)
p := Program{ p := Program{
Fn: func(io.Reader, io.Writer) error { return fmt.Errorf("oh no") }, Fn: func(io.Reader, io.Writer) error { return fmt.Errorf("oh no") },
@@ -154,11 +153,11 @@ func TestProcessMainFilesInPlaceFailFn(t *testing.T) {
exit := p.main([]string{path1}, os.Stdin, os.Stdout, os.Stderr) exit := p.main([]string{path1}, os.Stdin, os.Stdout, os.Stderr)
require.Equal(t, -1, exit) assert.Equal(t, -1, exit)
v1, err := ioutil.ReadFile(path1) v1, err := ioutil.ReadFile(path1)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "content 1", string(v1)) assert.Equal(t, "content 1", string(v1))
} }
func dummyFileFn(r io.Reader, w io.Writer) error { func dummyFileFn(r io.Reader, w io.Writer) error {
+6 -8
View File
@@ -4,9 +4,7 @@ import (
"testing" "testing"
"unsafe" "unsafe"
"github.com/stretchr/testify/assert" "github.com/pelletier/go-toml/v2/internal/assert"
"github.com/stretchr/testify/require"
"github.com/pelletier/go-toml/v2/internal/danger" "github.com/pelletier/go-toml/v2/internal/danger"
) )
@@ -72,7 +70,7 @@ func TestSubsliceOffsetInvalid(t *testing.T) {
for _, e := range examples { for _, e := range examples {
t.Run(e.desc, func(t *testing.T) { t.Run(e.desc, func(t *testing.T) {
d, s := e.test() d, s := e.test()
require.Panics(t, func() { assert.Panics(t, func() {
danger.SubsliceOffset(d, s) danger.SubsliceOffset(d, s)
}) })
}) })
@@ -83,9 +81,9 @@ func TestStride(t *testing.T) {
a := []byte{1, 2, 3, 4} a := []byte{1, 2, 3, 4}
x := &a[1] x := &a[1]
n := (*byte)(danger.Stride(unsafe.Pointer(x), unsafe.Sizeof(byte(0)), 1)) n := (*byte)(danger.Stride(unsafe.Pointer(x), unsafe.Sizeof(byte(0)), 1))
require.Equal(t, &a[2], n) assert.Equal(t, &a[2], n)
n = (*byte)(danger.Stride(unsafe.Pointer(x), unsafe.Sizeof(byte(0)), -1)) n = (*byte)(danger.Stride(unsafe.Pointer(x), unsafe.Sizeof(byte(0)), -1))
require.Equal(t, &a[0], n) assert.Equal(t, &a[0], n)
} }
func TestBytesRange(t *testing.T) { func TestBytesRange(t *testing.T) {
@@ -166,12 +164,12 @@ func TestBytesRange(t *testing.T) {
t.Run(e.desc, func(t *testing.T) { t.Run(e.desc, func(t *testing.T) {
start, end := e.test() start, end := e.test()
if e.expected == nil { if e.expected == nil {
require.Panics(t, func() { assert.Panics(t, func() {
danger.BytesRange(start, end) danger.BytesRange(start, end)
}) })
} else { } else {
res := danger.BytesRange(start, end) res := danger.BytesRange(start, end)
require.Equal(t, e.expected, res) assert.Equal(t, e.expected, res)
} }
}) })
} }
@@ -9,7 +9,7 @@ import (
"time" "time"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/require" "github.com/pelletier/go-toml/v2/internal/assert"
) )
func TestDocMarshal(t *testing.T) { func TestDocMarshal(t *testing.T) {
@@ -107,13 +107,13 @@ name = 'List.Second'
` `
result, err := toml.Marshal(docData) result, err := toml.Marshal(docData)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, marshalTestToml, string(result)) assert.Equal(t, marshalTestToml, string(result))
} }
func TestBasicMarshalQuotedKey(t *testing.T) { func TestBasicMarshalQuotedKey(t *testing.T) {
result, err := toml.Marshal(quotedKeyMarshalTestData) result, err := toml.Marshal(quotedKeyMarshalTestData)
require.NoError(t, err) assert.NoError(t, err)
expected := `'Z.string-àéù' = 'Hello' expected := `'Z.string-àéù' = 'Hello'
'Yfloat-𝟘' = 3.5 'Yfloat-𝟘' = 3.5
@@ -128,7 +128,7 @@ String2 = 'Two'
String2 = 'Three' String2 = 'Three'
` `
require.Equal(t, string(expected), string(result)) assert.Equal(t, string(expected), string(result))
} }
@@ -153,7 +153,7 @@ func TestEmptyMarshal(t *testing.T) {
Map: map[string]string{}, Map: map[string]string{},
} }
result, err := toml.Marshal(doc) result, err := toml.Marshal(doc)
require.NoError(t, err) assert.NoError(t, err)
expected := `title = 'Placeholder' expected := `title = 'Placeholder'
bool = false bool = false
@@ -164,7 +164,7 @@ stringlist = []
[map] [map]
` `
require.Equal(t, string(expected), string(result)) assert.Equal(t, string(expected), string(result))
} }
type textMarshaler struct { type textMarshaler struct {
@@ -187,13 +187,13 @@ func TestTextMarshaler(t *testing.T) {
t.Run("at root", func(t *testing.T) { t.Run("at root", func(t *testing.T) {
_, err := toml.Marshal(m) _, err := toml.Marshal(m)
// in v2 we do not allow TextMarshaler at root // in v2 we do not allow TextMarshaler at root
require.Error(t, err) assert.Error(t, err)
}) })
t.Run("leaf", func(t *testing.T) { t.Run("leaf", func(t *testing.T) {
res, err := toml.Marshal(wrap{m}) res, err := toml.Marshal(wrap{m})
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "TM = 'Sally Fields'\n", string(res)) assert.Equal(t, "TM = 'Sally Fields'\n", string(res))
}) })
} }
@@ -16,8 +16,7 @@ import (
"time" "time"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/assert" "github.com/pelletier/go-toml/v2/internal/assert"
"github.com/stretchr/testify/require"
) )
type basicMarshalTestStruct struct { type basicMarshalTestStruct struct {
@@ -123,7 +122,7 @@ func TestInterface(t *testing.T) {
var config Conf var config Conf
config.Inter = &NestedStruct{} config.Inter = &NestedStruct{}
err := toml.Unmarshal(doc, &config) err := toml.Unmarshal(doc, &config)
require.NoError(t, err) assert.NoError(t, err)
expected := Conf{ expected := Conf{
Name: "rui", Name: "rui",
Age: 18, Age: 18,
@@ -139,8 +138,8 @@ func TestInterface(t *testing.T) {
func TestBasicUnmarshal(t *testing.T) { func TestBasicUnmarshal(t *testing.T) {
result := basicMarshalTestStruct{} result := basicMarshalTestStruct{}
err := toml.Unmarshal(basicTestToml, &result) err := toml.Unmarshal(basicTestToml, &result)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, basicTestData, result) assert.Equal(t, basicTestData, result)
} }
type quotedKeyMarshalTestStruct struct { type quotedKeyMarshalTestStruct struct {
@@ -300,7 +299,7 @@ func TestDocUnmarshal(t *testing.T) {
result := testDoc{} result := testDoc{}
err := toml.Unmarshal(marshalTestToml, &result) err := toml.Unmarshal(marshalTestToml, &result)
expected := docData expected := docData
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expected, result) assert.Equal(t, expected, result)
} }
@@ -340,7 +339,7 @@ shouldntBeHere = 2
func TestUnexportedUnmarshal(t *testing.T) { func TestUnexportedUnmarshal(t *testing.T) {
result := unexportedMarshalTestStruct{} result := unexportedMarshalTestStruct{}
err := toml.Unmarshal(unexportedTestToml, &result) err := toml.Unmarshal(unexportedTestToml, &result)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, unexportedTestData, result) assert.Equal(t, unexportedTestData, result)
} }
@@ -456,7 +455,7 @@ func TestEmptytomlUnmarshal(t *testing.T) {
result := emptyMarshalTestStruct{} result := emptyMarshalTestStruct{}
err := toml.Unmarshal(emptyTestToml, &result) err := toml.Unmarshal(emptyTestToml, &result)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, emptyTestData, result) assert.Equal(t, emptyTestData, result)
} }
@@ -504,7 +503,7 @@ Str = "Hello"
func TestPointerUnmarshal(t *testing.T) { func TestPointerUnmarshal(t *testing.T) {
result := pointerMarshalTestStruct{} result := pointerMarshalTestStruct{}
err := toml.Unmarshal(pointerTestToml, &result) err := toml.Unmarshal(pointerTestToml, &result)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, pointerTestData, result) assert.Equal(t, pointerTestData, result)
} }
@@ -540,7 +539,7 @@ StringPtr = [["Three", "Four"]]
func TestNestedUnmarshal(t *testing.T) { func TestNestedUnmarshal(t *testing.T) {
result := nestedMarshalTestStruct{} result := nestedMarshalTestStruct{}
err := toml.Unmarshal(nestedTestToml, &result) err := toml.Unmarshal(nestedTestToml, &result)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, nestedTestData, result) assert.Equal(t, nestedTestData, result)
} }
@@ -834,7 +833,7 @@ func TestUnmarshalTabInStringAndQuotedKey(t *testing.T) {
t.Run(test.desc, func(t *testing.T) { t.Run(test.desc, func(t *testing.T) {
result := Test{} result := Test{}
err := toml.Unmarshal(test.input, &result) err := toml.Unmarshal(test.input, &result)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, test.expected, result) assert.Equal(t, test.expected, result)
}) })
} }
@@ -963,7 +962,7 @@ func TestUnmarshalTypeTableHeader(t *testing.T) {
} }
expected := map[header]map[string]int{ expected := map[header]map[string]int{
"test": map[string]int{"a": 1}, "test": {"a": 1},
} }
if !reflect.DeepEqual(result, expected) { if !reflect.DeepEqual(result, expected) {
@@ -1090,7 +1089,7 @@ func TestUnmarshalCheckConversionFloatInt(t *testing.T) {
for _, test := range testCases { for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) { t.Run(test.desc, func(t *testing.T) {
err := toml.Unmarshal([]byte(test.input), &conversionCheck{}) err := toml.Unmarshal([]byte(test.input), &conversionCheck{})
require.Error(t, err) assert.Error(t, err)
}) })
} }
} }
@@ -1125,7 +1124,7 @@ func TestUnmarshalOverflow(t *testing.T) {
for _, test := range testCases { for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) { t.Run(test.desc, func(t *testing.T) {
err := toml.Unmarshal([]byte(test.input), &overflow{}) err := toml.Unmarshal([]byte(test.input), &overflow{})
require.Error(t, err) assert.Error(t, err)
}) })
} }
} }
@@ -1745,7 +1744,7 @@ Age = 23
} }
actual := OuterStruct{} actual := OuterStruct{}
err := toml.Unmarshal(doc, &actual) err := toml.Unmarshal(doc, &actual)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }
@@ -1830,7 +1829,7 @@ InnerField = "After4"
} }
err := toml.Unmarshal(doc, &actual) err := toml.Unmarshal(doc, &actual)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }
@@ -1879,7 +1878,7 @@ type arrayTooSmallStruct struct {
func TestUnmarshalSlice(t *testing.T) { func TestUnmarshalSlice(t *testing.T) {
var actual sliceStruct var actual sliceStruct
err := toml.Unmarshal(sliceTomlDemo, &actual) err := toml.Unmarshal(sliceTomlDemo, &actual)
require.NoError(t, err) assert.NoError(t, err)
expected := sliceStruct{ expected := sliceStruct{
Slice: []string{"Howdy", "Hey There"}, Slice: []string{"Howdy", "Hey There"},
SlicePtr: &[]string{"Howdy", "Hey There"}, SlicePtr: &[]string{"Howdy", "Hey There"},
@@ -1930,7 +1929,7 @@ func TestUnmarshalMixedTypeSlice(t *testing.T) {
}, },
} }
err := toml.Unmarshal(doc, &actual) err := toml.Unmarshal(doc, &actual)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, expected, actual) assert.Equal(t, expected, actual)
} }
@@ -1939,7 +1938,7 @@ func TestUnmarshalArray(t *testing.T) {
var actual arrayStruct var actual arrayStruct
err = toml.Unmarshal(sliceTomlDemo, &actual) err = toml.Unmarshal(sliceTomlDemo, &actual)
require.NoError(t, err) assert.NoError(t, err)
expected := arrayStruct{ expected := arrayStruct{
Slice: [4]string{"Howdy", "Hey There"}, Slice: [4]string{"Howdy", "Hey There"},
@@ -1998,8 +1997,13 @@ func TestDecoderStrict(t *testing.T) {
} }
err := strictDecoder(input).Decode(&doc) err := strictDecoder(input).Decode(&doc)
require.Error(t, err) assert.Error(t, err)
require.IsType(t, &toml.StrictMissingError{}, err)
assert.Equal(t,
reflect.TypeOf(err), reflect.TypeOf(&toml.StrictMissingError{}),
"Expected a *toml.StrictMissingError, got: %v", reflect.TypeOf(err),
)
se := err.(*toml.StrictMissingError) se := err.(*toml.StrictMissingError)
keys := []toml.Key{} keys := []toml.Key{}
@@ -2015,10 +2019,10 @@ func TestDecoderStrict(t *testing.T) {
{"undecoded", "array"}, {"undecoded", "array"},
} }
require.Equal(t, expectedKeys, keys) assert.Equal(t, expectedKeys, keys)
err = decoder(input).Decode(&doc) err = decoder(input).Decode(&doc)
require.NoError(t, err) assert.NoError(t, err)
var m map[string]interface{} var m map[string]interface{}
err = decoder(input).Decode(&m) err = decoder(input).Decode(&m)
@@ -2036,7 +2040,7 @@ func TestDecoderStrictValid(t *testing.T) {
} }
err := strictDecoder(input).Decode(&doc) err := strictDecoder(input).Decode(&doc)
require.NoError(t, err) assert.NoError(t, err)
} }
type docUnmarshalTOML struct { type docUnmarshalTOML struct {
@@ -2087,7 +2091,7 @@ func TestCustomUnmarshal(t *testing.T) {
var d parent var d parent
err := toml.Unmarshal([]byte(input), &d) err := toml.Unmarshal([]byte(input), &d)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "ok1", d.Doc.Decoded.Key) assert.Equal(t, "ok1", d.Doc.Decoded.Key)
assert.Equal(t, "ok2", d.DocPointer.Decoded.Key) assert.Equal(t, "ok2", d.DocPointer.Decoded.Key)
} }
@@ -2153,7 +2157,7 @@ Int = 21
Float = 2.0 Float = 2.0
` `
err := toml.Unmarshal([]byte(input), &doc) err := toml.Unmarshal([]byte(input), &doc)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, 12, doc.UnixTime.Value) assert.Equal(t, 12, doc.UnixTime.Value)
assert.Equal(t, 42, doc.Version.Value) assert.Equal(t, 42, doc.Version.Value)
assert.Equal(t, 1, doc.Bool.Value) assert.Equal(t, 1, doc.Bool.Value)
@@ -2223,7 +2227,10 @@ func TestUnmarshalEmptyInterface(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
require.IsType(t, map[string]interface{}{}, v) assert.Equal(t,
reflect.TypeOf(map[string]interface{}{}), reflect.TypeOf(v),
"Expected map[string]interface{}{} type, got: %v", reflect.TypeOf(v),
)
x := v.(map[string]interface{}) x := v.(map[string]interface{})
assert.Equal(t, "pelletier", x["User"]) assert.Equal(t, "pelletier", x["User"])
+6 -2
View File
@@ -4,7 +4,7 @@ import (
"testing" "testing"
"unsafe" "unsafe"
"github.com/stretchr/testify/require" "github.com/pelletier/go-toml/v2/internal/assert"
) )
func TestEntrySize(t *testing.T) { func TestEntrySize(t *testing.T) {
@@ -12,5 +12,9 @@ func TestEntrySize(t *testing.T) {
// performance of unmarshaling documents. Should only be increased with care // performance of unmarshaling documents. Should only be increased with care
// and a very good reason. // and a very good reason.
maxExpectedEntrySize := 48 maxExpectedEntrySize := 48
require.LessOrEqual(t, int(unsafe.Sizeof(entry{})), maxExpectedEntrySize) assert.True(t,
int(unsafe.Sizeof(entry{})) <= maxExpectedEntrySize,
"Expected entry to be less than or equal to %d, got: %d",
maxExpectedEntrySize, int(unsafe.Sizeof(entry{})),
)
} }
+28 -28
View File
@@ -5,73 +5,73 @@ import (
"time" "time"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/require" "github.com/pelletier/go-toml/v2/internal/assert"
) )
func TestLocalDate_AsTime(t *testing.T) { func TestLocalDate_AsTime(t *testing.T) {
d := toml.LocalDate{2021, 6, 8} d := toml.LocalDate{2021, 6, 8}
cast := d.AsTime(time.UTC) cast := d.AsTime(time.UTC)
require.Equal(t, time.Date(2021, time.June, 8, 0, 0, 0, 0, time.UTC), cast) assert.Equal(t, time.Date(2021, time.June, 8, 0, 0, 0, 0, time.UTC), cast)
} }
func TestLocalDate_String(t *testing.T) { func TestLocalDate_String(t *testing.T) {
d := toml.LocalDate{2021, 6, 8} d := toml.LocalDate{2021, 6, 8}
require.Equal(t, "2021-06-08", d.String()) assert.Equal(t, "2021-06-08", d.String())
} }
func TestLocalDate_MarshalText(t *testing.T) { func TestLocalDate_MarshalText(t *testing.T) {
d := toml.LocalDate{2021, 6, 8} d := toml.LocalDate{2021, 6, 8}
b, err := d.MarshalText() b, err := d.MarshalText()
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, []byte("2021-06-08"), b) assert.Equal(t, []byte("2021-06-08"), b)
} }
func TestLocalDate_UnmarshalMarshalText(t *testing.T) { func TestLocalDate_UnmarshalMarshalText(t *testing.T) {
d := toml.LocalDate{} d := toml.LocalDate{}
err := d.UnmarshalText([]byte("2021-06-08")) err := d.UnmarshalText([]byte("2021-06-08"))
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, toml.LocalDate{2021, 6, 8}, d) assert.Equal(t, toml.LocalDate{2021, 6, 8}, d)
err = d.UnmarshalText([]byte("what")) err = d.UnmarshalText([]byte("what"))
require.Error(t, err) assert.Error(t, err)
} }
func TestLocalTime_String(t *testing.T) { func TestLocalTime_String(t *testing.T) {
d := toml.LocalTime{20, 12, 1, 2, 9} d := toml.LocalTime{20, 12, 1, 2, 9}
require.Equal(t, "20:12:01.000000002", d.String()) assert.Equal(t, "20:12:01.000000002", d.String())
d = toml.LocalTime{20, 12, 1, 0, 0} d = toml.LocalTime{20, 12, 1, 0, 0}
require.Equal(t, "20:12:01", d.String()) assert.Equal(t, "20:12:01", d.String())
d = toml.LocalTime{20, 12, 1, 0, 9} d = toml.LocalTime{20, 12, 1, 0, 9}
require.Equal(t, "20:12:01.000000000", d.String()) assert.Equal(t, "20:12:01.000000000", d.String())
d = toml.LocalTime{20, 12, 1, 100, 0} d = toml.LocalTime{20, 12, 1, 100, 0}
require.Equal(t, "20:12:01.0000001", d.String()) assert.Equal(t, "20:12:01.0000001", d.String())
} }
func TestLocalTime_MarshalText(t *testing.T) { func TestLocalTime_MarshalText(t *testing.T) {
d := toml.LocalTime{20, 12, 1, 2, 9} d := toml.LocalTime{20, 12, 1, 2, 9}
b, err := d.MarshalText() b, err := d.MarshalText()
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, []byte("20:12:01.000000002"), b) assert.Equal(t, []byte("20:12:01.000000002"), b)
} }
func TestLocalTime_UnmarshalMarshalText(t *testing.T) { func TestLocalTime_UnmarshalMarshalText(t *testing.T) {
d := toml.LocalTime{} d := toml.LocalTime{}
err := d.UnmarshalText([]byte("20:12:01.000000002")) err := d.UnmarshalText([]byte("20:12:01.000000002"))
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, toml.LocalTime{20, 12, 1, 2, 9}, d) assert.Equal(t, toml.LocalTime{20, 12, 1, 2, 9}, d)
err = d.UnmarshalText([]byte("what")) err = d.UnmarshalText([]byte("what"))
require.Error(t, err) assert.Error(t, err)
err = d.UnmarshalText([]byte("20:12:01.000000002 bad")) err = d.UnmarshalText([]byte("20:12:01.000000002 bad"))
require.Error(t, err) assert.Error(t, err)
} }
func TestLocalTime_RoundTrip(t *testing.T) { func TestLocalTime_RoundTrip(t *testing.T) {
var d struct{ A toml.LocalTime } var d struct{ A toml.LocalTime }
err := toml.Unmarshal([]byte("a=20:12:01.500"), &d) err := toml.Unmarshal([]byte("a=20:12:01.500"), &d)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "20:12:01.500", d.A.String()) assert.Equal(t, "20:12:01.500", d.A.String())
} }
func TestLocalDateTime_AsTime(t *testing.T) { func TestLocalDateTime_AsTime(t *testing.T) {
@@ -80,7 +80,7 @@ func TestLocalDateTime_AsTime(t *testing.T) {
toml.LocalTime{20, 12, 1, 2, 9}, toml.LocalTime{20, 12, 1, 2, 9},
} }
cast := d.AsTime(time.UTC) cast := d.AsTime(time.UTC)
require.Equal(t, time.Date(2021, time.June, 8, 20, 12, 1, 2, time.UTC), cast) assert.Equal(t, time.Date(2021, time.June, 8, 20, 12, 1, 2, time.UTC), cast)
} }
func TestLocalDateTime_String(t *testing.T) { func TestLocalDateTime_String(t *testing.T) {
@@ -88,7 +88,7 @@ func TestLocalDateTime_String(t *testing.T) {
toml.LocalDate{2021, 6, 8}, toml.LocalDate{2021, 6, 8},
toml.LocalTime{20, 12, 1, 2, 9}, toml.LocalTime{20, 12, 1, 2, 9},
} }
require.Equal(t, "2021-06-08T20:12:01.000000002", d.String()) assert.Equal(t, "2021-06-08T20:12:01.000000002", d.String())
} }
func TestLocalDateTime_MarshalText(t *testing.T) { func TestLocalDateTime_MarshalText(t *testing.T) {
@@ -97,22 +97,22 @@ func TestLocalDateTime_MarshalText(t *testing.T) {
toml.LocalTime{20, 12, 1, 2, 9}, toml.LocalTime{20, 12, 1, 2, 9},
} }
b, err := d.MarshalText() b, err := d.MarshalText()
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, []byte("2021-06-08T20:12:01.000000002"), b) assert.Equal(t, []byte("2021-06-08T20:12:01.000000002"), b)
} }
func TestLocalDateTime_UnmarshalMarshalText(t *testing.T) { func TestLocalDateTime_UnmarshalMarshalText(t *testing.T) {
d := toml.LocalDateTime{} d := toml.LocalDateTime{}
err := d.UnmarshalText([]byte("2021-06-08 20:12:01.000000002")) err := d.UnmarshalText([]byte("2021-06-08 20:12:01.000000002"))
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, toml.LocalDateTime{ assert.Equal(t, toml.LocalDateTime{
toml.LocalDate{2021, 6, 8}, toml.LocalDate{2021, 6, 8},
toml.LocalTime{20, 12, 1, 2, 9}, toml.LocalTime{20, 12, 1, 2, 9},
}, d) }, d)
err = d.UnmarshalText([]byte("what")) err = d.UnmarshalText([]byte("what"))
require.Error(t, err) assert.Error(t, err)
err = d.UnmarshalText([]byte("2021-06-08 20:12:01.000000002 bad")) err = d.UnmarshalText([]byte("2021-06-08 20:12:01.000000002 bad"))
require.Error(t, err) assert.Error(t, err)
} }
+90 -66
View File
@@ -6,13 +6,13 @@ import (
"fmt" "fmt"
"math" "math"
"math/big" "math/big"
"reflect"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/assert" "github.com/pelletier/go-toml/v2/internal/assert"
"github.com/stretchr/testify/require"
) )
type marshalTextKey struct { type marshalTextKey struct {
@@ -30,6 +30,27 @@ func (k marshalBadTextKey) MarshalText() ([]byte, error) {
return nil, fmt.Errorf("error") return nil, fmt.Errorf("error")
} }
func toFloat(x interface{}) float64 {
// Shortened version of testify/toFloat
var xf float64
switch xn := x.(type) {
case float32:
xf = float64(xn)
case float64:
xf = xn
}
return xf
}
func inDelta(t *testing.T, expected, actual interface{}, delta float64) {
dt := toFloat(expected) - toFloat(actual)
assert.True(t,
dt < -delta && dt < delta,
"Difference between %v and %v is %v, but difference was %v",
expected, actual, delta, dt,
)
}
func TestMarshal(t *testing.T) { func TestMarshal(t *testing.T) {
someInt := 42 someInt := 42
@@ -764,18 +785,18 @@ Three = [1, 2, 3]
t.Run(e.desc, func(t *testing.T) { t.Run(e.desc, func(t *testing.T) {
b, err := toml.Marshal(e.v) b, err := toml.Marshal(e.v)
if e.err { if e.err {
require.Error(t, err) assert.Error(t, err)
return return
} }
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, e.expected, string(b)) assert.Equal(t, e.expected, string(b))
// make sure the output is always valid TOML // make sure the output is always valid TOML
defaultMap := map[string]interface{}{} defaultMap := map[string]interface{}{}
err = toml.Unmarshal(b, &defaultMap) err = toml.Unmarshal(b, &defaultMap)
require.NoError(t, err) assert.NoError(t, err)
testWithAllFlags(t, func(t *testing.T, flags int) { testWithAllFlags(t, func(t *testing.T, flags int) {
t.Helper() t.Helper()
@@ -785,13 +806,13 @@ Three = [1, 2, 3]
setFlags(enc, flags) setFlags(enc, flags)
err := enc.Encode(e.v) err := enc.Encode(e.v)
require.NoError(t, err) assert.NoError(t, err)
inlineMap := map[string]interface{}{} inlineMap := map[string]interface{}{}
err = toml.Unmarshal(buf.Bytes(), &inlineMap) err = toml.Unmarshal(buf.Bytes(), &inlineMap)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, defaultMap, inlineMap) assert.Equal(t, defaultMap, inlineMap)
}) })
}) })
} }
@@ -858,8 +879,8 @@ nan = nan
` `
actual, err := toml.Marshal(v) actual, err := toml.Marshal(v)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, expected, string(actual)) assert.Equal(t, expected, string(actual))
v64 := map[string]float64{ v64 := map[string]float64{
"nan": math.NaN(), "nan": math.NaN(),
@@ -868,8 +889,8 @@ nan = nan
} }
actual, err = toml.Marshal(v64) actual, err = toml.Marshal(v64)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, expected, string(actual)) assert.Equal(t, expected, string(actual))
} }
//nolint:funlen //nolint:funlen
@@ -929,7 +950,7 @@ func TestMarshalIndentTables(t *testing.T) {
enc := toml.NewEncoder(&buf) enc := toml.NewEncoder(&buf)
enc.SetIndentTables(true) enc.SetIndentTables(true)
err := enc.Encode(e.v) err := enc.Encode(e.v)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, e.expected, buf.String()) assert.Equal(t, e.expected, buf.String())
}) })
} }
@@ -949,13 +970,13 @@ func (c *customTextMarshaler) MarshalText() ([]byte, error) {
func TestMarshalTextMarshaler_NoRoot(t *testing.T) { func TestMarshalTextMarshaler_NoRoot(t *testing.T) {
c := customTextMarshaler{} c := customTextMarshaler{}
_, err := toml.Marshal(&c) _, err := toml.Marshal(&c)
require.Error(t, err) assert.Error(t, err)
} }
func TestMarshalTextMarshaler_Error(t *testing.T) { func TestMarshalTextMarshaler_Error(t *testing.T) {
m := map[string]interface{}{"a": &customTextMarshaler{value: 1}} m := map[string]interface{}{"a": &customTextMarshaler{value: 1}}
_, err := toml.Marshal(m) _, err := toml.Marshal(m)
require.Error(t, err) assert.Error(t, err)
} }
func TestMarshalTextMarshaler_ErrorInline(t *testing.T) { func TestMarshalTextMarshaler_ErrorInline(t *testing.T) {
@@ -968,13 +989,13 @@ func TestMarshalTextMarshaler_ErrorInline(t *testing.T) {
} }
_, err := toml.Marshal(d) _, err := toml.Marshal(d)
require.Error(t, err) assert.Error(t, err)
} }
func TestMarshalTextMarshaler(t *testing.T) { func TestMarshalTextMarshaler(t *testing.T) {
m := map[string]interface{}{"a": &customTextMarshaler{value: 2}} m := map[string]interface{}{"a": &customTextMarshaler{value: 2}}
r, err := toml.Marshal(m) r, err := toml.Marshal(m)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "a = '::2'\n", string(r)) assert.Equal(t, "a = '::2'\n", string(r))
} }
@@ -988,7 +1009,7 @@ func TestEncodeToBrokenWriter(t *testing.T) {
w := brokenWriter{} w := brokenWriter{}
enc := toml.NewEncoder(&w) enc := toml.NewEncoder(&w)
err := enc.Encode(map[string]string{"hello": "world"}) err := enc.Encode(map[string]string{"hello": "world"})
require.Error(t, err) assert.Error(t, err)
} }
func TestEncoderSetIndentSymbol(t *testing.T) { func TestEncoderSetIndentSymbol(t *testing.T) {
@@ -997,7 +1018,7 @@ func TestEncoderSetIndentSymbol(t *testing.T) {
enc.SetIndentTables(true) enc.SetIndentTables(true)
enc.SetIndentSymbol(">>>") enc.SetIndentSymbol(">>>")
err := enc.Encode(map[string]map[string]string{"parent": {"hello": "world"}}) err := enc.Encode(map[string]map[string]string{"parent": {"hello": "world"}})
require.NoError(t, err) assert.NoError(t, err)
expected := `[parent] expected := `[parent]
>>>hello = 'world' >>>hello = 'world'
` `
@@ -1016,7 +1037,7 @@ func TestEncoderSetMarshalJsonNumbers(t *testing.T) {
"E": json.Number("0.0"), "E": json.Number("0.0"),
"F": json.Number(""), "F": json.Number(""),
}) })
require.NoError(t, err) assert.NoError(t, err)
expected := `A = 1.1 expected := `A = 1.1
B = 0.042 B = 0.042
C = 42 C = 42
@@ -1053,7 +1074,7 @@ func TestEncoderOmitempty(t *testing.T) {
d := doc{} d := doc{}
b, err := toml.Marshal(d) b, err := toml.Marshal(d)
require.NoError(t, err) assert.NoError(t, err)
expected := `` expected := ``
@@ -1070,7 +1091,7 @@ func TestEncoderTagFieldName(t *testing.T) {
d := doc{String: "world"} d := doc{String: "world"}
b, err := toml.Marshal(d) b, err := toml.Marshal(d)
require.NoError(t, err) assert.NoError(t, err)
expected := `hello = 'world' expected := `hello = 'world'
'#' = '' '#' = ''
@@ -1085,11 +1106,11 @@ func TestIssue436(t *testing.T) {
var v interface{} var v interface{}
err := json.Unmarshal(data, &v) err := json.Unmarshal(data, &v)
require.NoError(t, err) assert.NoError(t, err)
var buf bytes.Buffer var buf bytes.Buffer
err = toml.NewEncoder(&buf).Encode(v) err = toml.NewEncoder(&buf).Encode(v)
require.NoError(t, err) assert.NoError(t, err)
expected := `[[a]] expected := `[[a]]
[a.b] [a.b]
@@ -1111,27 +1132,30 @@ func TestIssue424(t *testing.T) {
msg2 := Message2{"Hello\\World"} msg2 := Message2{"Hello\\World"}
toml1, err := toml.Marshal(msg1) toml1, err := toml.Marshal(msg1)
require.NoError(t, err) assert.NoError(t, err)
toml2, err := toml.Marshal(msg2) toml2, err := toml.Marshal(msg2)
require.NoError(t, err) assert.NoError(t, err)
msg1parsed := Message1{} msg1parsed := Message1{}
err = toml.Unmarshal(toml1, &msg1parsed) err = toml.Unmarshal(toml1, &msg1parsed)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, msg1, msg1parsed) assert.Equal(t, msg1, msg1parsed)
msg2parsed := Message2{} msg2parsed := Message2{}
err = toml.Unmarshal(toml2, &msg2parsed) err = toml.Unmarshal(toml2, &msg2parsed)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, msg2, msg2parsed) assert.Equal(t, msg2, msg2parsed)
} }
func TestIssue567(t *testing.T) { func TestIssue567(t *testing.T) {
var m map[string]interface{} var m map[string]interface{}
err := toml.Unmarshal([]byte("A = 12:08:05"), &m) err := toml.Unmarshal([]byte("A = 12:08:05"), &m)
require.NoError(t, err) assert.NoError(t, err)
require.IsType(t, m["A"], toml.LocalTime{}) assert.Equal(t,
reflect.TypeOf(m["A"]), reflect.TypeOf(toml.LocalTime{}),
"Expected type '%v', got: %v", reflect.TypeOf(m["A"]), reflect.TypeOf(toml.LocalTime{}),
)
} }
func TestIssue590(t *testing.T) { func TestIssue590(t *testing.T) {
@@ -1140,7 +1164,7 @@ func TestIssue590(t *testing.T) {
Option CustomType `toml:"option"` Option CustomType `toml:"option"`
} }
err := toml.Unmarshal([]byte("option = 42"), &cfg) err := toml.Unmarshal([]byte("option = 42"), &cfg)
require.NoError(t, err) assert.NoError(t, err)
} }
func TestIssue571(t *testing.T) { func TestIssue571(t *testing.T) {
@@ -1156,14 +1180,14 @@ func TestIssue571(t *testing.T) {
Float64: 43, Float64: 43,
} }
b, err := toml.Marshal(foo) b, err := toml.Marshal(foo)
require.NoError(t, err) assert.NoError(t, err)
var foo2 Foo var foo2 Foo
err = toml.Unmarshal(b, &foo2) err = toml.Unmarshal(b, &foo2)
require.NoError(t, err) assert.NoError(t, err)
assert.InDelta(t, 42, foo2.Float32, closeEnough) inDelta(t, 42, foo2.Float32, closeEnough)
assert.InDelta(t, 43, foo2.Float64, closeEnough) inDelta(t, 43, foo2.Float64, closeEnough)
} }
func TestIssue678(t *testing.T) { func TestIssue678(t *testing.T) {
@@ -1176,13 +1200,13 @@ func TestIssue678(t *testing.T) {
} }
out, err := toml.Marshal(cfg) out, err := toml.Marshal(cfg)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "BigInt = '123'\n", string(out)) assert.Equal(t, "BigInt = '123'\n", string(out))
cfg2 := &Config{} cfg2 := &Config{}
err = toml.Unmarshal(out, cfg2) err = toml.Unmarshal(out, cfg2)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, cfg, cfg2) assert.Equal(t, cfg, cfg2)
} }
func TestIssue752(t *testing.T) { func TestIssue752(t *testing.T) {
@@ -1197,8 +1221,8 @@ func TestIssue752(t *testing.T) {
c := Container{} c := Container{}
out, err := toml.Marshal(c) out, err := toml.Marshal(c)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "", string(out)) assert.Equal(t, "", string(out))
} }
func TestIssue768(t *testing.T) { func TestIssue768(t *testing.T) {
@@ -1207,14 +1231,14 @@ func TestIssue768(t *testing.T) {
} }
out, err := toml.Marshal(&cfg{}) out, err := toml.Marshal(&cfg{})
require.NoError(t, err) assert.NoError(t, err)
expected := `# This is a multiline comment. expected := `# This is a multiline comment.
# This is line 2. # This is line 2.
Name = '' Name = ''
` `
require.Equal(t, expected, string(out)) assert.Equal(t, expected, string(out))
} }
func TestIssue786(t *testing.T) { func TestIssue786(t *testing.T) {
@@ -1230,9 +1254,9 @@ func TestIssue786(t *testing.T) {
x := Test{} x := Test{}
b, err := toml.Marshal(x) b, err := toml.Marshal(x)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "", string(b)) assert.Equal(t, "", string(b))
type General struct { type General struct {
From string `toml:"from,omitempty" json:"from,omitempty" comment:"from in graphite-web format, the local TZ is used"` From string `toml:"from,omitempty" json:"from,omitempty" comment:"from in graphite-web format, the local TZ is used"`
@@ -1277,7 +1301,7 @@ from = '-2d'
randomize = true randomize = true
` `
require.Equal(t, expected, buf.String()) assert.Equal(t, expected, buf.String())
} }
func TestMarshalIssue888(t *testing.T) { func TestMarshalIssue888(t *testing.T) {
@@ -1316,7 +1340,7 @@ func TestMarshalIssue888(t *testing.T) {
FieldB = 'field b 2' FieldB = 'field b 2'
` `
require.Equal(t, expected, buf.String()) assert.Equal(t, expected, buf.String())
} }
func TestMarshalNestedAnonymousStructs(t *testing.T) { func TestMarshalNestedAnonymousStructs(t *testing.T) {
@@ -1352,8 +1376,8 @@ value = ''
` `
result, err := toml.Marshal(doc) result, err := toml.Marshal(doc)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, expected, string(result)) assert.Equal(t, expected, string(result))
} }
func TestMarshalNestedAnonymousStructs_DuplicateField(t *testing.T) { func TestMarshalNestedAnonymousStructs_DuplicateField(t *testing.T) {
@@ -1378,9 +1402,9 @@ value = ''
` `
result, err := toml.Marshal(doc) result, err := toml.Marshal(doc)
require.NoError(t, err) assert.NoError(t, err)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, expected, string(result)) assert.Equal(t, expected, string(result))
} }
func TestMarshalNestedAnonymousStructs_PointerEmbedded(t *testing.T) { func TestMarshalNestedAnonymousStructs_PointerEmbedded(t *testing.T) {
@@ -1411,8 +1435,8 @@ func TestMarshalNestedAnonymousStructs_PointerEmbedded(t *testing.T) {
` `
result, err := toml.Marshal(doc) result, err := toml.Marshal(doc)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, expected, string(result)) assert.Equal(t, expected, string(result))
} }
func TestLocalTime(t *testing.T) { func TestLocalTime(t *testing.T) {
@@ -1429,12 +1453,12 @@ func TestLocalTime(t *testing.T) {
` `
out, err := toml.Marshal(v) out, err := toml.Marshal(v)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, expected, string(out)) assert.Equal(t, expected, string(out))
} }
func TestMarshalUint64Overflow(t *testing.T) { func TestMarshalUint64Overflow(t *testing.T) {
// The TOML spec only requires implementation to provide support for the // The TOML spec only asserts implementation to provide support for the
// int64 range. To avoid generating TOML documents that would not be // int64 range. To avoid generating TOML documents that would not be
// supported by standard-compliant parsers, uint64 > max int64 cannot be // supported by standard-compliant parsers, uint64 > max int64 cannot be
// marshaled. // marshaled.
@@ -1443,7 +1467,7 @@ func TestMarshalUint64Overflow(t *testing.T) {
} }
_, err := toml.Marshal(x) _, err := toml.Marshal(x)
require.Error(t, err) assert.Error(t, err)
} }
func TestIndentWithInlineTable(t *testing.T) { func TestIndentWithInlineTable(t *testing.T) {
@@ -1463,7 +1487,7 @@ func TestIndentWithInlineTable(t *testing.T) {
enc.SetIndentTables(true) enc.SetIndentTables(true)
enc.SetTablesInline(true) enc.SetTablesInline(true)
enc.SetArraysMultiline(true) enc.SetArraysMultiline(true)
require.NoError(t, enc.Encode(x)) assert.NoError(t, enc.Encode(x))
assert.Equal(t, expected, buf.String()) assert.Equal(t, expected, buf.String())
} }
@@ -1520,7 +1544,7 @@ func TestMarshalCommented(t *testing.T) {
} }
out, err := toml.Marshal(c) out, err := toml.Marshal(c)
require.NoError(t, err) assert.NoError(t, err)
expected := `# Int = 42 expected := `# Int = 42
# String = 'root' # String = 'root'
@@ -1552,7 +1576,7 @@ func TestMarshalCommented(t *testing.T) {
# Values = [4, 5, 6] # Values = [4, 5, 6]
` `
require.Equal(t, expected, string(out)) assert.Equal(t, expected, string(out))
} }
func TestMarshalIndentedCustomTypeArray(t *testing.T) { func TestMarshalIndentedCustomTypeArray(t *testing.T) {
@@ -1588,8 +1612,8 @@ func TestMarshalIndentedCustomTypeArray(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
enc := toml.NewEncoder(&buf) enc := toml.NewEncoder(&buf)
enc.SetIndentTables(true) enc.SetIndentTables(true)
require.NoError(t, enc.Encode(c)) assert.NoError(t, enc.Encode(c))
require.Equal(t, expected, buf.String()) assert.Equal(t, expected, buf.String())
} }
func ExampleMarshal() { func ExampleMarshal() {
@@ -1759,7 +1783,7 @@ func TestReadmeComments(t *testing.T) {
}, },
} }
out, err := toml.Marshal(example) out, err := toml.Marshal(example)
require.NoError(t, err) assert.NoError(t, err)
expected := `# Host IP to connect to. expected := `# Host IP to connect to.
host = '127.0.0.1' host = '127.0.0.1'
@@ -1771,5 +1795,5 @@ port = 4242
# cipher = 'AEAD-AES128-GCM-SHA256' # cipher = 'AEAD-AES128-GCM-SHA256'
# version = 'TLS 1.3' # version = 'TLS 1.3'
` `
require.Equal(t, expected, string(out)) assert.Equal(t, expected, string(out))
} }
+4 -4
View File
@@ -9,8 +9,8 @@ import (
"testing" "testing"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"github.com/pelletier/go-toml/v2/internal/assert"
"github.com/pelletier/go-toml/v2/internal/testsuite" "github.com/pelletier/go-toml/v2/internal/testsuite"
"github.com/stretchr/testify/require"
) )
func testgenInvalid(t *testing.T, input string) { func testgenInvalid(t *testing.T, input string) {
@@ -45,15 +45,15 @@ func testgenValid(t *testing.T, input string, jsonRef string) {
t.Fatalf("failed parsing toml: %s", err) t.Fatalf("failed parsing toml: %s", err)
} }
j, err := testsuite.ValueToTaggedJSON(doc) j, err := testsuite.ValueToTaggedJSON(doc)
require.NoError(t, err) assert.NoError(t, err)
var ref interface{} var ref interface{}
err = json.Unmarshal([]byte(jsonRef), &ref) err = json.Unmarshal([]byte(jsonRef), &ref)
require.NoError(t, err) assert.NoError(t, err)
var actual interface{} var actual interface{}
err = json.Unmarshal([]byte(j), &actual) err = json.Unmarshal([]byte(j), &actual)
require.NoError(t, err) assert.NoError(t, err)
testsuite.CmpJSON(t, "", ref, actual) testsuite.CmpJSON(t, "", ref, actual)
} }
+112 -113
View File
@@ -12,9 +12,8 @@ import (
"time" "time"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"github.com/pelletier/go-toml/v2/internal/assert"
"github.com/pelletier/go-toml/v2/unstable" "github.com/pelletier/go-toml/v2/unstable"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
type unmarshalTextKey struct { type unmarshalTextKey struct {
@@ -109,7 +108,7 @@ func TestDecodeReaderError(t *testing.T) {
dec := toml.NewDecoder(r) dec := toml.NewDecoder(r)
m := map[string]interface{}{} m := map[string]interface{}{}
err := dec.Decode(&m) err := dec.Decode(&m)
require.Error(t, err) assert.Error(t, err)
} }
// nolint:funlen // nolint:funlen
@@ -187,9 +186,9 @@ func TestUnmarshal_Integers(t *testing.T) {
doc := doc{} doc := doc{}
err := toml.Unmarshal([]byte(`A = `+e.input), &doc) err := toml.Unmarshal([]byte(`A = `+e.input), &doc)
if e.err { if e.err {
require.Error(t, err) assert.Error(t, err)
} else { } else {
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, e.expected, doc.A) assert.Equal(t, e.expected, doc.A)
} }
}) })
@@ -321,9 +320,9 @@ func TestUnmarshal_Floats(t *testing.T) {
doc := doc{} doc := doc{}
err := toml.Unmarshal([]byte(`A = `+e.input), &doc) err := toml.Unmarshal([]byte(`A = `+e.input), &doc)
if e.err { if e.err {
require.Error(t, err) assert.Error(t, err)
} else { } else {
require.NoError(t, err) assert.NoError(t, err)
if e.testFn != nil { if e.testFn != nil {
e.testFn(t, doc.A) e.testFn(t, doc.A)
} else { } else {
@@ -2094,9 +2093,9 @@ B = "data"`,
if err == nil { if err == nil {
t.Log("=>", test.target) t.Log("=>", test.target)
} }
require.Error(t, err) assert.Error(t, err)
} else { } else {
require.NoError(t, err) assert.NoError(t, err)
if test.assert != nil { if test.assert != nil {
test.assert(t, test) test.assert(t, test)
} else { } else {
@@ -2158,14 +2157,14 @@ func TestUnmarshalOverflows(t *testing.T) {
doc := "A = " + v doc := "A = " + v
err := toml.Unmarshal([]byte(doc), e.t) err := toml.Unmarshal([]byte(doc), e.t)
t.Log("input:", doc) t.Log("input:", doc)
require.Error(t, err) assert.Error(t, err)
}) })
} }
t.Run(fmt.Sprintf("%T ok", e.t), func(t *testing.T) { t.Run(fmt.Sprintf("%T ok", e.t), func(t *testing.T) {
doc := "A = 1" doc := "A = 1"
err := toml.Unmarshal([]byte(doc), e.t) err := toml.Unmarshal([]byte(doc), e.t)
t.Log("input:", doc) t.Log("input:", doc)
require.NoError(t, err) assert.NoError(t, err)
}) })
} }
} }
@@ -2179,9 +2178,9 @@ func TestUnmarshalErrors(t *testing.T) {
s := mystruct{} s := mystruct{}
err := toml.Unmarshal([]byte(data), &s) err := toml.Unmarshal([]byte(data), &s)
require.Error(t, err) assert.Error(t, err)
require.Equal(t, "toml: cannot decode TOML integer into struct field toml_test.mystruct.Bar of type string", err.Error()) assert.Equal(t, "toml: cannot decode TOML integer into struct field toml_test.mystruct.Bar of type string", err.Error())
} }
func TestUnmarshalStringInvalidStructField(t *testing.T) { func TestUnmarshalStringInvalidStructField(t *testing.T) {
@@ -2203,16 +2202,16 @@ port = "bad"
file := strings.NewReader(data) file := strings.NewReader(data)
err := toml.NewDecoder(file).Decode(&cfg) err := toml.NewDecoder(file).Decode(&cfg)
require.Error(t, err) assert.Error(t, err)
x := err.(*toml.DecodeError) x := err.(*toml.DecodeError)
require.Equal(t, "toml: cannot decode TOML string into struct field toml_test.Server.Port of type int", x.Error()) assert.Equal(t, "toml: cannot decode TOML string into struct field toml_test.Server.Port of type int", x.Error())
expected := `1| [server] expected := `1| [server]
2| path = "/my/path" 2| path = "/my/path"
3| port = "bad" 3| port = "bad"
| ~~~~~ cannot decode TOML string into struct field toml_test.Server.Port of type int` | ~~~~~ cannot decode TOML string into struct field toml_test.Server.Port of type int`
require.Equal(t, expected, x.String()) assert.Equal(t, expected, x.String())
} }
func TestUnmarshalIntegerInvalidStructField(t *testing.T) { func TestUnmarshalIntegerInvalidStructField(t *testing.T) {
@@ -2234,38 +2233,38 @@ port = 50
file := strings.NewReader(data) file := strings.NewReader(data)
err := toml.NewDecoder(file).Decode(&cfg) err := toml.NewDecoder(file).Decode(&cfg)
require.Error(t, err) assert.Error(t, err)
x := err.(*toml.DecodeError) x := err.(*toml.DecodeError)
require.Equal(t, "toml: cannot decode TOML integer into struct field toml_test.Server.Path of type string", x.Error()) assert.Equal(t, "toml: cannot decode TOML integer into struct field toml_test.Server.Path of type string", x.Error())
expected := `1| [server] expected := `1| [server]
2| path = 100 2| path = 100
| ~~~ cannot decode TOML integer into struct field toml_test.Server.Path of type string | ~~~ cannot decode TOML integer into struct field toml_test.Server.Path of type string
3| port = 50` 3| port = 50`
require.Equal(t, expected, x.String()) assert.Equal(t, expected, x.String())
} }
func TestUnmarshalInvalidTarget(t *testing.T) { func TestUnmarshalInvalidTarget(t *testing.T) {
x := "foo" x := "foo"
err := toml.Unmarshal([]byte{}, x) err := toml.Unmarshal([]byte{}, x)
require.Error(t, err) assert.Error(t, err)
var m *map[string]interface{} var m *map[string]interface{}
err = toml.Unmarshal([]byte{}, m) err = toml.Unmarshal([]byte{}, m)
require.Error(t, err) assert.Error(t, err)
} }
func TestUnmarshalFloat32(t *testing.T) { func TestUnmarshalFloat32(t *testing.T) {
t.Run("fits", func(t *testing.T) { t.Run("fits", func(t *testing.T) {
doc := "A = 1.2" doc := "A = 1.2"
err := toml.Unmarshal([]byte(doc), &map[string]float32{}) err := toml.Unmarshal([]byte(doc), &map[string]float32{})
require.NoError(t, err) assert.NoError(t, err)
}) })
t.Run("overflows", func(t *testing.T) { t.Run("overflows", func(t *testing.T) {
doc := "A = 4.40282346638528859811704183484516925440e+38" doc := "A = 4.40282346638528859811704183484516925440e+38"
err := toml.Unmarshal([]byte(doc), &map[string]float32{}) err := toml.Unmarshal([]byte(doc), &map[string]float32{})
require.Error(t, err) assert.Error(t, err)
}) })
} }
@@ -2357,7 +2356,7 @@ bar = 42`,
x = &struct{}{} x = &struct{}{}
} }
err := d.Decode(x) err := d.Decode(x)
require.NoError(t, err) assert.NoError(t, err)
}) })
}) })
} }
@@ -2379,15 +2378,15 @@ val1 = "test1"
} }
err := toml.Unmarshal(configFile, cfg) err := toml.Unmarshal(configFile, cfg)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "test2", cfg.Val2) assert.Equal(t, "test2", cfg.Val2)
} }
func TestIssue287(t *testing.T) { func TestIssue287(t *testing.T) {
b := `y=[[{}]]` b := `y=[[{}]]`
v := map[string]interface{}{} v := map[string]interface{}{}
err := toml.Unmarshal([]byte(b), &v) err := toml.Unmarshal([]byte(b), &v)
require.NoError(t, err) assert.NoError(t, err)
expected := map[string]interface{}{ expected := map[string]interface{}{
"y": []interface{}{ "y": []interface{}{
@@ -2396,7 +2395,7 @@ func TestIssue287(t *testing.T) {
}, },
}, },
} }
require.Equal(t, expected, v) assert.Equal(t, expected, v)
} }
type ( type (
@@ -2415,7 +2414,7 @@ name = "decode"
version = "0.1.0"`) version = "0.1.0"`)
m := Map458{} m := Map458{}
err := toml.Unmarshal(s, &m) err := toml.Unmarshal(s, &m)
require.NoError(t, err) assert.NoError(t, err)
a := m.A("package") a := m.A("package")
expected := Slice458{ expected := Slice458{
map[string]interface{}{ map[string]interface{}{
@@ -2454,7 +2453,7 @@ func TestIssue484(t *testing.T) {
var cfg Config484 var cfg Config484
err := toml.Unmarshal(raw, &cfg) err := toml.Unmarshal(raw, &cfg)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, Config484{ assert.Equal(t, Config484{
Integers: []Integer484{{1}, {2}, {3}, {100}}, Integers: []Integer484{{1}, {2}, {3}, {100}},
}, cfg) }, cfg)
@@ -2472,7 +2471,7 @@ bar = 2021-04-08
} }
ss := new(s) ss := new(s)
err := toml.Unmarshal([]byte(data), ss) err := toml.Unmarshal([]byte(data), ss)
require.NoError(t, err) assert.NoError(t, err)
} }
func TestIssue508(t *testing.T) { func TestIssue508(t *testing.T) {
@@ -2488,15 +2487,15 @@ func TestIssue508(t *testing.T) {
t1 := text{} t1 := text{}
err := toml.Unmarshal(b, &t1) err := toml.Unmarshal(b, &t1)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "This is a title", t1.head.Title) assert.Equal(t, "This is a title", t1.head.Title)
} }
func TestIssue507(t *testing.T) { func TestIssue507(t *testing.T) {
data := []byte{'0', '=', '\n', '0', 'a', 'm', 'e'} data := []byte{'0', '=', '\n', '0', 'a', 'm', 'e'}
m := map[string]interface{}{} m := map[string]interface{}{}
err := toml.Unmarshal(data, &m) err := toml.Unmarshal(data, &m)
require.Error(t, err) assert.Error(t, err)
} }
type uuid [16]byte type uuid [16]byte
@@ -2519,8 +2518,8 @@ func TestIssue564(t *testing.T) {
var config Config var config Config
err := toml.Unmarshal([]byte(`id = "0818a52b97b94768941ba1172c76cf6c"`), &config) err := toml.Unmarshal([]byte(`id = "0818a52b97b94768941ba1172c76cf6c"`), &config)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, uuid{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, config.ID) assert.Equal(t, uuid{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, config.ID)
} }
func TestIssue575(t *testing.T) { func TestIssue575(t *testing.T) {
@@ -2557,7 +2556,7 @@ xz_hash = "1a48f723fea1f17d786ce6eadd9d00914d38062d28fd9c455ed3c3801905b388"
var dist doc var dist doc
err := toml.Unmarshal(b, &dist) err := toml.Unmarshal(b, &dist)
require.NoError(t, err) assert.NoError(t, err)
expected := doc{ expected := doc{
Pkg: map[string]pkg{ Pkg: map[string]pkg{
@@ -2574,60 +2573,60 @@ xz_hash = "1a48f723fea1f17d786ce6eadd9d00914d38062d28fd9c455ed3c3801905b388"
}, },
} }
require.Equal(t, expected, dist) assert.Equal(t, expected, dist)
} }
func TestIssue579(t *testing.T) { func TestIssue579(t *testing.T) {
var v interface{} var v interface{}
err := toml.Unmarshal([]byte(`[foo`), &v) err := toml.Unmarshal([]byte(`[foo`), &v)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue581(t *testing.T) { func TestIssue581(t *testing.T) {
var v interface{} var v interface{}
err := toml.Unmarshal([]byte(`P=[#`), &v) err := toml.Unmarshal([]byte(`P=[#`), &v)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue585(t *testing.T) { func TestIssue585(t *testing.T) {
var v interface{} var v interface{}
err := toml.Unmarshal([]byte(`a=1979-05127T 0`), &v) err := toml.Unmarshal([]byte(`a=1979-05127T 0`), &v)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue586(t *testing.T) { func TestIssue586(t *testing.T) {
var v interface{} var v interface{}
err := toml.Unmarshal([]byte(`a={ `), &v) err := toml.Unmarshal([]byte(`a={ `), &v)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue588(t *testing.T) { func TestIssue588(t *testing.T) {
var v interface{} var v interface{}
err := toml.Unmarshal([]byte(`a=[1#`), &v) err := toml.Unmarshal([]byte(`a=[1#`), &v)
require.Error(t, err) assert.Error(t, err)
} }
// Support lowercase 'T' and 'Z' // Support lowercase 'T' and 'Z'
func TestIssue600(t *testing.T) { func TestIssue600(t *testing.T) {
var v interface{} var v interface{}
err := toml.Unmarshal([]byte(`a=1979-05-27t00:32:00z`), &v) err := toml.Unmarshal([]byte(`a=1979-05-27t00:32:00z`), &v)
require.NoError(t, err) assert.NoError(t, err)
} }
func TestIssue596(t *testing.T) { func TestIssue596(t *testing.T) {
var v interface{} var v interface{}
err := toml.Unmarshal([]byte(`a=1979-05-27T90:+2:99`), &v) err := toml.Unmarshal([]byte(`a=1979-05-27T90:+2:99`), &v)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue602(t *testing.T) { func TestIssue602(t *testing.T) {
var v interface{} var v interface{}
err := toml.Unmarshal([]byte(""), &v) err := toml.Unmarshal([]byte(""), &v)
require.NoError(t, err) assert.NoError(t, err)
var expected interface{} = map[string]interface{}{} var expected interface{} = map[string]interface{}{}
require.Equal(t, expected, v) assert.Equal(t, expected, v)
} }
func TestIssue623(t *testing.T) { func TestIssue623(t *testing.T) {
@@ -2639,31 +2638,31 @@ func TestIssue623(t *testing.T) {
foo = "bar"` foo = "bar"`
err := toml.Unmarshal([]byte(values), &definition) err := toml.Unmarshal([]byte(values), &definition)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue631(t *testing.T) { func TestIssue631(t *testing.T) {
v := map[string]interface{}{} v := map[string]interface{}{}
err := toml.Unmarshal([]byte("\"\\b\u007f\"= 2"), &v) err := toml.Unmarshal([]byte("\"\\b\u007f\"= 2"), &v)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue658(t *testing.T) { func TestIssue658(t *testing.T) {
var v map[string]interface{} var v map[string]interface{}
err := toml.Unmarshal([]byte("e={b=1,b=4}"), &v) err := toml.Unmarshal([]byte("e={b=1,b=4}"), &v)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue662(t *testing.T) { func TestIssue662(t *testing.T) {
var v map[string]interface{} var v map[string]interface{}
err := toml.Unmarshal([]byte("a=[{b=1,b=2}]"), &v) err := toml.Unmarshal([]byte("a=[{b=1,b=2}]"), &v)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue666(t *testing.T) { func TestIssue666(t *testing.T) {
var v map[string]interface{} var v map[string]interface{}
err := toml.Unmarshal([]byte("a={}\na={}"), &v) err := toml.Unmarshal([]byte("a={}\na={}"), &v)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue677(t *testing.T) { func TestIssue677(t *testing.T) {
@@ -2687,7 +2686,7 @@ Program = "hugo"
p := tomlParser{} p := tomlParser{}
err := toml.Unmarshal([]byte(doc), &p) err := toml.Unmarshal([]byte(doc), &p)
require.NoError(t, err) assert.NoError(t, err)
expected := tomlParser{ expected := tomlParser{
Build: &_tomlJob{ Build: &_tomlJob{
@@ -2699,7 +2698,7 @@ Program = "hugo"
}, },
}, },
} }
require.Equal(t, expected, p) assert.Equal(t, expected, p)
} }
func TestIssue701(t *testing.T) { func TestIssue701(t *testing.T) {
@@ -2733,50 +2732,50 @@ z=0
func TestIssue703(t *testing.T) { func TestIssue703(t *testing.T) {
var v interface{} var v interface{}
err := toml.Unmarshal([]byte("[a]\nx.y=0\n[a.x]"), &v) err := toml.Unmarshal([]byte("[a]\nx.y=0\n[a.x]"), &v)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue708(t *testing.T) { func TestIssue708(t *testing.T) {
v := map[string]string{} v := map[string]string{}
err := toml.Unmarshal([]byte("0=\"\"\"\\\r\n\"\"\""), &v) err := toml.Unmarshal([]byte("0=\"\"\"\\\r\n\"\"\""), &v)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]string{"0": ""}, v) assert.Equal(t, map[string]string{"0": ""}, v)
} }
func TestIssue710(t *testing.T) { func TestIssue710(t *testing.T) {
v := map[string]toml.LocalTime{} v := map[string]toml.LocalTime{}
err := toml.Unmarshal([]byte(`0=00:00:00.0000000000`), &v) err := toml.Unmarshal([]byte(`0=00:00:00.0000000000`), &v)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]toml.LocalTime{"0": {Precision: 9}}, v) assert.Equal(t, map[string]toml.LocalTime{"0": {Precision: 9}}, v)
v1 := map[string]toml.LocalTime{} v1 := map[string]toml.LocalTime{}
err = toml.Unmarshal([]byte(`0=00:00:00.0000000001`), &v1) err = toml.Unmarshal([]byte(`0=00:00:00.0000000001`), &v1)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]toml.LocalTime{"0": {Precision: 9}}, v1) assert.Equal(t, map[string]toml.LocalTime{"0": {Precision: 9}}, v1)
v2 := map[string]toml.LocalTime{} v2 := map[string]toml.LocalTime{}
err = toml.Unmarshal([]byte(`0=00:00:00.1111111119`), &v2) err = toml.Unmarshal([]byte(`0=00:00:00.1111111119`), &v2)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]toml.LocalTime{"0": {Nanosecond: 111111111, Precision: 9}}, v2) assert.Equal(t, map[string]toml.LocalTime{"0": {Nanosecond: 111111111, Precision: 9}}, v2)
} }
func TestIssue715(t *testing.T) { func TestIssue715(t *testing.T) {
var v interface{} var v interface{}
err := toml.Unmarshal([]byte("0=+"), &v) err := toml.Unmarshal([]byte("0=+"), &v)
require.Error(t, err) assert.Error(t, err)
err = toml.Unmarshal([]byte("0=-"), &v) err = toml.Unmarshal([]byte("0=-"), &v)
require.Error(t, err) assert.Error(t, err)
err = toml.Unmarshal([]byte("0=+A"), &v) err = toml.Unmarshal([]byte("0=+A"), &v)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue714(t *testing.T) { func TestIssue714(t *testing.T) {
var v interface{} var v interface{}
err := toml.Unmarshal([]byte("0."), &v) err := toml.Unmarshal([]byte("0."), &v)
require.Error(t, err) assert.Error(t, err)
err = toml.Unmarshal([]byte("0={0=0,"), &v) err = toml.Unmarshal([]byte("0={0=0,"), &v)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue772(t *testing.T) { func TestIssue772(t *testing.T) {
@@ -2794,8 +2793,8 @@ func TestIssue772(t *testing.T) {
config := Config{} config := Config{}
err := toml.Unmarshal(defaultConfigFile, &config) err := toml.Unmarshal(defaultConfigFile, &config)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "reach-masterdev-", config.FileHandling.FilePattern) assert.Equal(t, "reach-masterdev-", config.FileHandling.FilePattern)
} }
func TestIssue774(t *testing.T) { func TestIssue774(t *testing.T) {
@@ -2811,14 +2810,14 @@ func TestIssue774(t *testing.T) {
c.SCP = []ScpData{{Host: "main.domain.com"}} c.SCP = []ScpData{{Host: "main.domain.com"}}
b, err := toml.Marshal(c) b, err := toml.Marshal(c)
require.NoError(t, err) assert.NoError(t, err)
expected := `# Array of Secure Copy Configurations expected := `# Array of Secure Copy Configurations
[[scp]] [[scp]]
Host = 'main.domain.com' Host = 'main.domain.com'
` `
require.Equal(t, expected, string(b)) assert.Equal(t, expected, string(b))
} }
func TestIssue799(t *testing.T) { func TestIssue799(t *testing.T) {
@@ -2834,7 +2833,7 @@ answer = 42
} }
err := toml.Unmarshal([]byte(testTOML), &s) err := toml.Unmarshal([]byte(testTOML), &s)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue807(t *testing.T) { func TestIssue807(t *testing.T) {
@@ -2848,14 +2847,14 @@ func TestIssue807(t *testing.T) {
var m M var m M
err := toml.Unmarshal([]byte(`name = 'foo'`), &m) err := toml.Unmarshal([]byte(`name = 'foo'`), &m)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "foo", m.Name) assert.Equal(t, "foo", m.Name)
} }
func TestIssue850(t *testing.T) { func TestIssue850(t *testing.T) {
data := make(map[string]string) data := make(map[string]string)
err := toml.Unmarshal([]byte("foo = {}"), &data) err := toml.Unmarshal([]byte("foo = {}"), &data)
require.Error(t, err) assert.Error(t, err)
} }
func TestIssue851(t *testing.T) { func TestIssue851(t *testing.T) {
@@ -2866,11 +2865,11 @@ func TestIssue851(t *testing.T) {
content := "params = {a=\"1\",b=\"2\"}" content := "params = {a=\"1\",b=\"2\"}"
var target Target var target Target
err := toml.Unmarshal([]byte(content), &target) err := toml.Unmarshal([]byte(content), &target)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]string{"a": "1", "b": "2"}, target.Params) assert.Equal(t, map[string]string{"a": "1", "b": "2"}, target.Params)
err = toml.Unmarshal([]byte(content), &target) err = toml.Unmarshal([]byte(content), &target)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, map[string]string{"a": "1", "b": "2"}, target.Params) assert.Equal(t, map[string]string{"a": "1", "b": "2"}, target.Params)
} }
func TestIssue866(t *testing.T) { func TestIssue866(t *testing.T) {
@@ -2952,11 +2951,11 @@ fizz = "abc"
blah.a = "def"`) blah.a = "def"`)
var cfg config var cfg config
err := toml.Unmarshal(b, &cfg) err := toml.Unmarshal(b, &cfg)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "abc", cfg.Fizz) assert.Equal(t, "abc", cfg.Fizz)
require.Equal(t, "def", cfg.blah.A) assert.Equal(t, "def", cfg.blah.A)
require.Equal(t, "def", cfg.A) assert.Equal(t, "def", cfg.A)
} }
func TestIssue931(t *testing.T) { func TestIssue931(t *testing.T) {
@@ -2979,7 +2978,7 @@ func TestIssue931(t *testing.T) {
`) `)
toml.Unmarshal(b, &its) toml.Unmarshal(b, &its)
require.Equal(t, items{[]item{{"c"}, {"d"}}}, its) assert.Equal(t, items{[]item{{"c"}, {"d"}}}, its)
} }
func TestIssue931Interface(t *testing.T) { func TestIssue931Interface(t *testing.T) {
@@ -3000,7 +2999,7 @@ func TestIssue931Interface(t *testing.T) {
`) `)
toml.Unmarshal(b, &its) toml.Unmarshal(b, &its)
require.Equal(t, items{[]interface{}{item{"Name": "c"}, item{"Name": "d"}}}, its) assert.Equal(t, items{[]interface{}{item{"Name": "c"}, item{"Name": "d"}}}, its)
} }
func TestIssue931SliceInterface(t *testing.T) { func TestIssue931SliceInterface(t *testing.T) {
@@ -3026,7 +3025,7 @@ func TestIssue931SliceInterface(t *testing.T) {
`) `)
toml.Unmarshal(b, &its) toml.Unmarshal(b, &its)
require.Equal(t, items{[]interface{}{item{"Name": "c"}, item{"Name": "d"}}}, its) assert.Equal(t, items{[]interface{}{item{"Name": "c"}, item{"Name": "d"}}}, its)
} }
func TestUnmarshalDecodeErrors(t *testing.T) { func TestUnmarshalDecodeErrors(t *testing.T) {
@@ -3465,7 +3464,7 @@ world'`,
t.Run(e.desc, func(t *testing.T) { t.Run(e.desc, func(t *testing.T) {
m := map[string]interface{}{} m := map[string]interface{}{}
err := toml.Unmarshal([]byte(e.data), &m) err := toml.Unmarshal([]byte(e.data), &m)
require.Error(t, err) assert.Error(t, err)
var de *toml.DecodeError var de *toml.DecodeError
if !errors.As(err, &de) { if !errors.As(err, &de) {
@@ -3474,7 +3473,7 @@ world'`,
if e.msg != "" { if e.msg != "" {
t.Log("\n" + de.String()) t.Log("\n" + de.String())
require.Equal(t, "toml: "+e.msg, de.Error()) assert.Equal(t, "toml: "+e.msg, de.Error())
} }
}) })
} }
@@ -3505,9 +3504,9 @@ func TestOmitEmpty(t *testing.T) {
}}} }}}
b, err := toml.Marshal(d) b, err := toml.Marshal(d)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, "X = [{Foo = 'test', Inner = {V = 'alue'}}]\n", string(b)) assert.Equal(t, "X = [{Foo = 'test', Inner = {V = 'alue'}}]\n", string(b))
} }
func TestUnmarshalTags(t *testing.T) { func TestUnmarshalTags(t *testing.T) {
@@ -3534,8 +3533,8 @@ comma = 'ok'
} }
err := toml.Unmarshal([]byte(data), &d) err := toml.Unmarshal([]byte(data), &d)
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, expected, d) assert.Equal(t, expected, d)
} }
func TestASCIIControlCharacters(t *testing.T) { func TestASCIIControlCharacters(t *testing.T) {
@@ -3566,7 +3565,7 @@ func TestASCIIControlCharacters(t *testing.T) {
t.Helper() t.Helper()
m := map[string]interface{}{} m := map[string]interface{}{}
err := toml.Unmarshal(input, &m) err := toml.Unmarshal(input, &m)
require.Error(t, err) assert.Error(t, err)
var de *toml.DecodeError var de *toml.DecodeError
if !errors.As(err, &de) { if !errors.As(err, &de) {
@@ -3668,15 +3667,15 @@ func TestLocalDateTime(t *testing.T) {
doc := `a = ` + e.input doc := `a = ` + e.input
m := map[string]toml.LocalDateTime{} m := map[string]toml.LocalDateTime{}
err := toml.Unmarshal([]byte(doc), &m) err := toml.Unmarshal([]byte(doc), &m)
require.NoError(t, err) assert.NoError(t, err)
actual := m["a"] actual := m["a"]
golang, err := time.Parse("2006-01-02T15:04:05.999999999", e.input) golang, err := time.Parse("2006-01-02T15:04:05.999999999", e.input)
require.NoError(t, err) assert.NoError(t, err)
expected := toml.LocalDateTime{ expected := toml.LocalDateTime{
toml.LocalDate{golang.Year(), int(golang.Month()), golang.Day()}, toml.LocalDate{golang.Year(), int(golang.Month()), golang.Day()},
toml.LocalTime{golang.Hour(), golang.Minute(), golang.Second(), golang.Nanosecond(), e.prec}, toml.LocalTime{golang.Hour(), golang.Minute(), golang.Second(), golang.Nanosecond(), e.prec},
} }
require.Equal(t, expected, actual) assert.Equal(t, expected, actual)
}) })
} }
} }
@@ -3750,11 +3749,11 @@ func TestUnmarshal_RecursiveTable(t *testing.T) {
foo := Foo{} foo := Foo{}
err := toml.Unmarshal([]byte(e.input), &foo) err := toml.Unmarshal([]byte(e.input), &foo)
if e.err { if e.err {
require.Error(t, err) assert.Error(t, err)
} else { } else {
require.NoError(t, err) assert.NoError(t, err)
j, err := json.Marshal(foo) j, err := json.Marshal(foo)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, e.expected, string(j)) assert.Equal(t, e.expected, string(j))
} }
}) })
@@ -3841,11 +3840,11 @@ func TestUnmarshal_RecursiveTableArray(t *testing.T) {
foo := Foo{} foo := Foo{}
err := toml.Unmarshal([]byte(e.input), &foo) err := toml.Unmarshal([]byte(e.input), &foo)
if e.err { if e.err {
require.Error(t, err) assert.Error(t, err)
} else { } else {
require.NoError(t, err) assert.NoError(t, err)
j, err := json.Marshal(foo) j, err := json.Marshal(foo)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, e.expected, string(j)) assert.Equal(t, e.expected, string(j))
} }
}) })
@@ -3861,8 +3860,8 @@ func TestUnmarshalEmbedNonString(t *testing.T) {
d := doc{} d := doc{}
err := toml.Unmarshal([]byte(`foo = 'bar'`), &d) err := toml.Unmarshal([]byte(`foo = 'bar'`), &d)
require.NoError(t, err) assert.NoError(t, err)
require.Nil(t, d.Foo) assert.Equal(t, d.Foo, nil)
} }
func TestUnmarshal_Nil(t *testing.T) { func TestUnmarshal_Nil(t *testing.T) {
@@ -3898,11 +3897,11 @@ func TestUnmarshal_Nil(t *testing.T) {
foo := Foo{} foo := Foo{}
err := toml.Unmarshal([]byte(e.input), &foo) err := toml.Unmarshal([]byte(e.input), &foo)
if e.err { if e.err {
require.Error(t, err) assert.Error(t, err)
} else { } else {
require.NoError(t, err) assert.NoError(t, err)
j, err := toml.Marshal(foo) j, err := toml.Marshal(foo)
require.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, e.expected, string(j)) assert.Equal(t, e.expected, string(j))
} }
}) })
@@ -3987,14 +3986,14 @@ foo = "bar"`,
err := decoder.Decode(&foo) err := decoder.Decode(&foo)
if e.err { if e.err {
require.Error(t, err) assert.Error(t, err)
} else { } else {
require.NoError(t, err) assert.NoError(t, err)
require.Equal(t, len(foo.Unmarshalers), len(e.expected.Unmarshalers)) assert.Equal(t, len(foo.Unmarshalers), len(e.expected.Unmarshalers))
for i := 0; i < len(foo.Unmarshalers); i++ { for i := 0; i < len(foo.Unmarshalers); i++ {
require.Equal(t, foo.Unmarshalers[i], e.expected.Unmarshalers[i]) assert.Equal(t, foo.Unmarshalers[i], e.expected.Unmarshalers[i])
} }
require.Equal(t, foo.Foo, e.expected.Foo) assert.Equal(t, foo.Foo, e.expected.Foo)
} }
}) })
} }
+9 -9
View File
@@ -6,7 +6,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/pelletier/go-toml/v2/internal/assert"
) )
func TestParser_AST_Numbers(t *testing.T) { func TestParser_AST_Numbers(t *testing.T) {
@@ -141,9 +141,9 @@ func TestParser_AST_Numbers(t *testing.T) {
p.NextExpression() p.NextExpression()
err := p.Error() err := p.Error()
if e.err { if e.err {
require.Error(t, err) assert.Error(t, err)
} else { } else {
require.NoError(t, err) assert.NoError(t, err)
expected := astNode{ expected := astNode{
Kind: KeyValue, Kind: KeyValue,
@@ -168,8 +168,8 @@ type (
func compareNode(t *testing.T, e astNode, n *Node) { func compareNode(t *testing.T, e astNode, n *Node) {
t.Helper() t.Helper()
require.Equal(t, e.Kind, n.Kind) assert.Equal(t, e.Kind, n.Kind)
require.Equal(t, e.Data, n.Data) assert.Equal(t, e.Data, n.Data)
compareIterator(t, e.Children, n.Children()) compareIterator(t, e.Children, n.Children())
} }
@@ -341,9 +341,9 @@ func TestParser_AST(t *testing.T) {
p.NextExpression() p.NextExpression()
err := p.Error() err := p.Error()
if e.err { if e.err {
require.Error(t, err) assert.Error(t, err)
} else { } else {
require.NoError(t, err) assert.NoError(t, err)
compareNode(t, e.ast, p.Expression()) compareNode(t, e.ast, p.Expression())
} }
}) })
@@ -431,9 +431,9 @@ func TestParser_AST_DateTimes(t *testing.T) {
p.NextExpression() p.NextExpression()
err := p.Error() err := p.Error()
if e.err { if e.err {
require.Error(t, err) assert.Error(t, err)
} else { } else {
require.NoError(t, err) assert.NoError(t, err)
expected := astNode{ expected := astNode{
Kind: KeyValue, Kind: KeyValue,