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:
@@ -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)
|
||||
}
|
||||
@@ -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
@@ -11,8 +11,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/pelletier/go-toml/v2/internal/assert"
|
||||
)
|
||||
|
||||
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.Empty(t, stdout.String())
|
||||
assert.Empty(t, stderr.String())
|
||||
assert.Zero(t, stdout.String())
|
||||
assert.Zero(t, stderr.String())
|
||||
}
|
||||
|
||||
func TestProcessMainStdinErr(t *testing.T) {
|
||||
@@ -44,8 +43,8 @@ func TestProcessMainStdinErr(t *testing.T) {
|
||||
})
|
||||
|
||||
assert.Equal(t, -1, exit)
|
||||
assert.Empty(t, stdout.String())
|
||||
assert.NotEmpty(t, stderr.String())
|
||||
assert.Zero(t, stdout.String())
|
||||
assert.NotZero(t, stderr.String())
|
||||
}
|
||||
|
||||
func TestProcessMainStdinDecodeErr(t *testing.T) {
|
||||
@@ -59,16 +58,16 @@ func TestProcessMainStdinDecodeErr(t *testing.T) {
|
||||
})
|
||||
|
||||
assert.Equal(t, -1, exit)
|
||||
assert.Empty(t, stdout.String())
|
||||
assert.Contains(t, stderr.String(), "error occurred at")
|
||||
assert.Zero(t, stdout.String())
|
||||
assert.True(t, strings.Contains(stderr.String(), "error occurred at"))
|
||||
}
|
||||
|
||||
func TestProcessMainFileExists(t *testing.T) {
|
||||
tmpfile, err := ioutil.TempFile("", "example")
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
defer os.Remove(tmpfile.Name())
|
||||
_, err = tmpfile.Write([]byte(`some data`))
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
stdout := new(bytes.Buffer)
|
||||
stderr := new(bytes.Buffer)
|
||||
@@ -78,8 +77,8 @@ func TestProcessMainFileExists(t *testing.T) {
|
||||
})
|
||||
|
||||
assert.Equal(t, 0, exit)
|
||||
assert.Empty(t, stdout.String())
|
||||
assert.Empty(t, stderr.String())
|
||||
assert.Zero(t, stdout.String())
|
||||
assert.Zero(t, stderr.String())
|
||||
}
|
||||
|
||||
func TestProcessMainFileDoesNotExist(t *testing.T) {
|
||||
@@ -91,22 +90,22 @@ func TestProcessMainFileDoesNotExist(t *testing.T) {
|
||||
})
|
||||
|
||||
assert.Equal(t, -1, exit)
|
||||
assert.Empty(t, stdout.String())
|
||||
assert.NotEmpty(t, stderr.String())
|
||||
assert.Zero(t, stdout.String())
|
||||
assert.NotZero(t, stderr.String())
|
||||
}
|
||||
|
||||
func TestProcessMainFilesInPlace(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", "")
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
path1 := path.Join(dir, "file1")
|
||||
path2 := path.Join(dir, "file2")
|
||||
|
||||
err = ioutil.WriteFile(path1, []byte("content 1"), 0600)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
err = ioutil.WriteFile(path2, []byte("content 2"), 0600)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
p := Program{
|
||||
Fn: dummyFileFn,
|
||||
@@ -115,15 +114,15 @@ func TestProcessMainFilesInPlace(t *testing.T) {
|
||||
|
||||
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)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "1", string(v1))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "1", string(v1))
|
||||
|
||||
v2, err := ioutil.ReadFile(path2)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "2", string(v2))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "2", string(v2))
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
require.Equal(t, -1, exit)
|
||||
assert.Equal(t, -1, exit)
|
||||
}
|
||||
|
||||
func TestProcessMainFilesInPlaceFailFn(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", "")
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
defer os.RemoveAll(dir)
|
||||
|
||||
path1 := path.Join(dir, "file1")
|
||||
|
||||
err = ioutil.WriteFile(path1, []byte("content 1"), 0600)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
p := Program{
|
||||
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)
|
||||
|
||||
require.Equal(t, -1, exit)
|
||||
assert.Equal(t, -1, exit)
|
||||
|
||||
v1, err := ioutil.ReadFile(path1)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "content 1", string(v1))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "content 1", string(v1))
|
||||
}
|
||||
|
||||
func dummyFileFn(r io.Reader, w io.Writer) error {
|
||||
|
||||
@@ -4,9 +4,7 @@ import (
|
||||
"testing"
|
||||
"unsafe"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/pelletier/go-toml/v2/internal/assert"
|
||||
"github.com/pelletier/go-toml/v2/internal/danger"
|
||||
)
|
||||
|
||||
@@ -72,7 +70,7 @@ func TestSubsliceOffsetInvalid(t *testing.T) {
|
||||
for _, e := range examples {
|
||||
t.Run(e.desc, func(t *testing.T) {
|
||||
d, s := e.test()
|
||||
require.Panics(t, func() {
|
||||
assert.Panics(t, func() {
|
||||
danger.SubsliceOffset(d, s)
|
||||
})
|
||||
})
|
||||
@@ -83,9 +81,9 @@ func TestStride(t *testing.T) {
|
||||
a := []byte{1, 2, 3, 4}
|
||||
x := &a[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))
|
||||
require.Equal(t, &a[0], n)
|
||||
assert.Equal(t, &a[0], n)
|
||||
}
|
||||
|
||||
func TestBytesRange(t *testing.T) {
|
||||
@@ -166,12 +164,12 @@ func TestBytesRange(t *testing.T) {
|
||||
t.Run(e.desc, func(t *testing.T) {
|
||||
start, end := e.test()
|
||||
if e.expected == nil {
|
||||
require.Panics(t, func() {
|
||||
assert.Panics(t, func() {
|
||||
danger.BytesRange(start, end)
|
||||
})
|
||||
} else {
|
||||
res := danger.BytesRange(start, end)
|
||||
require.Equal(t, e.expected, res)
|
||||
assert.Equal(t, e.expected, res)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/pelletier/go-toml/v2/internal/assert"
|
||||
)
|
||||
|
||||
func TestDocMarshal(t *testing.T) {
|
||||
@@ -107,13 +107,13 @@ name = 'List.Second'
|
||||
`
|
||||
|
||||
result, err := toml.Marshal(docData)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, marshalTestToml, string(result))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, marshalTestToml, string(result))
|
||||
}
|
||||
|
||||
func TestBasicMarshalQuotedKey(t *testing.T) {
|
||||
result, err := toml.Marshal(quotedKeyMarshalTestData)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
expected := `'Z.string-àéù' = 'Hello'
|
||||
'Yfloat-𝟘' = 3.5
|
||||
@@ -128,7 +128,7 @@ String2 = 'Two'
|
||||
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{},
|
||||
}
|
||||
result, err := toml.Marshal(doc)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
expected := `title = 'Placeholder'
|
||||
bool = false
|
||||
@@ -164,7 +164,7 @@ stringlist = []
|
||||
[map]
|
||||
`
|
||||
|
||||
require.Equal(t, string(expected), string(result))
|
||||
assert.Equal(t, string(expected), string(result))
|
||||
}
|
||||
|
||||
type textMarshaler struct {
|
||||
@@ -187,13 +187,13 @@ func TestTextMarshaler(t *testing.T) {
|
||||
t.Run("at root", func(t *testing.T) {
|
||||
_, err := toml.Marshal(m)
|
||||
// in v2 we do not allow TextMarshaler at root
|
||||
require.Error(t, err)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
|
||||
t.Run("leaf", func(t *testing.T) {
|
||||
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"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/pelletier/go-toml/v2/internal/assert"
|
||||
)
|
||||
|
||||
type basicMarshalTestStruct struct {
|
||||
@@ -123,7 +122,7 @@ func TestInterface(t *testing.T) {
|
||||
var config Conf
|
||||
config.Inter = &NestedStruct{}
|
||||
err := toml.Unmarshal(doc, &config)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
expected := Conf{
|
||||
Name: "rui",
|
||||
Age: 18,
|
||||
@@ -139,8 +138,8 @@ func TestInterface(t *testing.T) {
|
||||
func TestBasicUnmarshal(t *testing.T) {
|
||||
result := basicMarshalTestStruct{}
|
||||
err := toml.Unmarshal(basicTestToml, &result)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, basicTestData, result)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, basicTestData, result)
|
||||
}
|
||||
|
||||
type quotedKeyMarshalTestStruct struct {
|
||||
@@ -300,7 +299,7 @@ func TestDocUnmarshal(t *testing.T) {
|
||||
result := testDoc{}
|
||||
err := toml.Unmarshal(marshalTestToml, &result)
|
||||
expected := docData
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, result)
|
||||
}
|
||||
|
||||
@@ -340,7 +339,7 @@ shouldntBeHere = 2
|
||||
func TestUnexportedUnmarshal(t *testing.T) {
|
||||
result := unexportedMarshalTestStruct{}
|
||||
err := toml.Unmarshal(unexportedTestToml, &result)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, unexportedTestData, result)
|
||||
}
|
||||
|
||||
@@ -456,7 +455,7 @@ func TestEmptytomlUnmarshal(t *testing.T) {
|
||||
|
||||
result := emptyMarshalTestStruct{}
|
||||
err := toml.Unmarshal(emptyTestToml, &result)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, emptyTestData, result)
|
||||
}
|
||||
|
||||
@@ -504,7 +503,7 @@ Str = "Hello"
|
||||
func TestPointerUnmarshal(t *testing.T) {
|
||||
result := pointerMarshalTestStruct{}
|
||||
err := toml.Unmarshal(pointerTestToml, &result)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, pointerTestData, result)
|
||||
}
|
||||
|
||||
@@ -540,7 +539,7 @@ StringPtr = [["Three", "Four"]]
|
||||
func TestNestedUnmarshal(t *testing.T) {
|
||||
result := nestedMarshalTestStruct{}
|
||||
err := toml.Unmarshal(nestedTestToml, &result)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, nestedTestData, result)
|
||||
}
|
||||
|
||||
@@ -834,7 +833,7 @@ func TestUnmarshalTabInStringAndQuotedKey(t *testing.T) {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
result := Test{}
|
||||
err := toml.Unmarshal(test.input, &result)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, test.expected, result)
|
||||
})
|
||||
}
|
||||
@@ -963,7 +962,7 @@ func TestUnmarshalTypeTableHeader(t *testing.T) {
|
||||
}
|
||||
|
||||
expected := map[header]map[string]int{
|
||||
"test": map[string]int{"a": 1},
|
||||
"test": {"a": 1},
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
@@ -1090,7 +1089,7 @@ func TestUnmarshalCheckConversionFloatInt(t *testing.T) {
|
||||
for _, test := range testCases {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
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 {
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
err := toml.Unmarshal([]byte(test.input), &overflow{})
|
||||
require.Error(t, err)
|
||||
assert.Error(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1745,7 +1744,7 @@ Age = 23
|
||||
}
|
||||
actual := OuterStruct{}
|
||||
err := toml.Unmarshal(doc, &actual)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, actual)
|
||||
}
|
||||
|
||||
@@ -1830,7 +1829,7 @@ InnerField = "After4"
|
||||
}
|
||||
|
||||
err := toml.Unmarshal(doc, &actual)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, actual)
|
||||
}
|
||||
|
||||
@@ -1879,7 +1878,7 @@ type arrayTooSmallStruct struct {
|
||||
func TestUnmarshalSlice(t *testing.T) {
|
||||
var actual sliceStruct
|
||||
err := toml.Unmarshal(sliceTomlDemo, &actual)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
expected := sliceStruct{
|
||||
Slice: []string{"Howdy", "Hey There"},
|
||||
SlicePtr: &[]string{"Howdy", "Hey There"},
|
||||
@@ -1930,7 +1929,7 @@ func TestUnmarshalMixedTypeSlice(t *testing.T) {
|
||||
},
|
||||
}
|
||||
err := toml.Unmarshal(doc, &actual)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, actual)
|
||||
}
|
||||
|
||||
@@ -1939,7 +1938,7 @@ func TestUnmarshalArray(t *testing.T) {
|
||||
|
||||
var actual arrayStruct
|
||||
err = toml.Unmarshal(sliceTomlDemo, &actual)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
expected := arrayStruct{
|
||||
Slice: [4]string{"Howdy", "Hey There"},
|
||||
@@ -1998,8 +1997,13 @@ func TestDecoderStrict(t *testing.T) {
|
||||
}
|
||||
|
||||
err := strictDecoder(input).Decode(&doc)
|
||||
require.Error(t, err)
|
||||
require.IsType(t, &toml.StrictMissingError{}, err)
|
||||
assert.Error(t, err)
|
||||
|
||||
assert.Equal(t,
|
||||
reflect.TypeOf(err), reflect.TypeOf(&toml.StrictMissingError{}),
|
||||
"Expected a *toml.StrictMissingError, got: %v", reflect.TypeOf(err),
|
||||
)
|
||||
|
||||
se := err.(*toml.StrictMissingError)
|
||||
|
||||
keys := []toml.Key{}
|
||||
@@ -2015,10 +2019,10 @@ func TestDecoderStrict(t *testing.T) {
|
||||
{"undecoded", "array"},
|
||||
}
|
||||
|
||||
require.Equal(t, expectedKeys, keys)
|
||||
assert.Equal(t, expectedKeys, keys)
|
||||
|
||||
err = decoder(input).Decode(&doc)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
|
||||
var m map[string]interface{}
|
||||
err = decoder(input).Decode(&m)
|
||||
@@ -2036,7 +2040,7 @@ func TestDecoderStrictValid(t *testing.T) {
|
||||
}
|
||||
|
||||
err := strictDecoder(input).Decode(&doc)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
type docUnmarshalTOML struct {
|
||||
@@ -2087,7 +2091,7 @@ func TestCustomUnmarshal(t *testing.T) {
|
||||
|
||||
var d parent
|
||||
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, "ok2", d.DocPointer.Decoded.Key)
|
||||
}
|
||||
@@ -2153,7 +2157,7 @@ Int = 21
|
||||
Float = 2.0
|
||||
`
|
||||
err := toml.Unmarshal([]byte(input), &doc)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 12, doc.UnixTime.Value)
|
||||
assert.Equal(t, 42, doc.Version.Value)
|
||||
assert.Equal(t, 1, doc.Bool.Value)
|
||||
@@ -2223,7 +2227,10 @@ func TestUnmarshalEmptyInterface(t *testing.T) {
|
||||
if err != nil {
|
||||
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{})
|
||||
assert.Equal(t, "pelletier", x["User"])
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"testing"
|
||||
"unsafe"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/pelletier/go-toml/v2/internal/assert"
|
||||
)
|
||||
|
||||
func TestEntrySize(t *testing.T) {
|
||||
@@ -12,5 +12,9 @@ func TestEntrySize(t *testing.T) {
|
||||
// performance of unmarshaling documents. Should only be increased with care
|
||||
// and a very good reason.
|
||||
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{})),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user