mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 08:48:32 +02:00
merge: main branch
This commit is contained in:
71
internal/utils/testing/expect.go
Normal file
71
internal/utils/testing/expect.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package expect
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if common.IsTest {
|
||||
// force verbose output
|
||||
os.Args = append([]string{os.Args[0], "-test.v"}, os.Args[1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
func Must[Result any](r Result, err error) Result {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
var (
|
||||
NoError = require.NoError
|
||||
HasError = require.Error
|
||||
True = require.True
|
||||
False = require.False
|
||||
Nil = require.Nil
|
||||
NotNil = require.NotNil
|
||||
ErrorContains = require.ErrorContains
|
||||
Panics = require.Panics
|
||||
Greater = require.Greater
|
||||
Less = require.Less
|
||||
GreaterOrEqual = require.GreaterOrEqual
|
||||
LessOrEqual = require.LessOrEqual
|
||||
)
|
||||
|
||||
func ErrorIs(t *testing.T, expected error, err error, msgAndArgs ...any) {
|
||||
t.Helper()
|
||||
require.ErrorIs(t, err, expected, msgAndArgs...)
|
||||
}
|
||||
|
||||
func ErrorT[T error](t *testing.T, err error, msgAndArgs ...any) {
|
||||
t.Helper()
|
||||
var errAs T
|
||||
require.ErrorAs(t, err, &errAs, msgAndArgs...)
|
||||
}
|
||||
|
||||
func Equal[T any](t *testing.T, got T, want T, msgAndArgs ...any) {
|
||||
t.Helper()
|
||||
require.EqualValues(t, want, got, msgAndArgs...)
|
||||
}
|
||||
|
||||
func NotEqual[T any](t *testing.T, got T, want T, msgAndArgs ...any) {
|
||||
t.Helper()
|
||||
require.NotEqual(t, want, got, msgAndArgs...)
|
||||
}
|
||||
|
||||
func Contains[T any](t *testing.T, got T, wants []T, msgAndArgs ...any) {
|
||||
t.Helper()
|
||||
require.Contains(t, wants, got, msgAndArgs...)
|
||||
}
|
||||
|
||||
func Type[T any](t *testing.T, got any, msgAndArgs ...any) (_ T) {
|
||||
t.Helper()
|
||||
_, ok := got.(T)
|
||||
require.True(t, ok, msgAndArgs...)
|
||||
return got.(T)
|
||||
}
|
||||
14
internal/utils/testing/sonic.go
Normal file
14
internal/utils/testing/sonic.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package expect
|
||||
|
||||
import (
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if common.IsTest {
|
||||
sonic.ConfigDefault = sonic.Config{
|
||||
SortMapKeys: true,
|
||||
}.Froze()
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package expect
|
||||
|
||||
import (
|
||||
"os"
|
||||
@@ -14,13 +14,6 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
func Must[Result any](r Result, err error) Result {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func ExpectNoError(t *testing.T, err error) {
|
||||
t.Helper()
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user