mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
chore: for backward compatibility (#4852)
Signed-off-by: kevin <wanjunfeng@gmail.com>
This commit is contained in:
@@ -15,6 +15,12 @@ var (
|
||||
ErrInvalidStopPosition = errors.New("stop position is invalid")
|
||||
)
|
||||
|
||||
// Contains checks if str is in list.
|
||||
// Deprecated: use slices.Contains instead.
|
||||
func Contains(list []string, str string) bool {
|
||||
return slices.Contains(list, str)
|
||||
}
|
||||
|
||||
// Filter filters chars from s with given filter function.
|
||||
func Filter(s string, filter func(r rune) bool) string {
|
||||
var n int
|
||||
|
||||
@@ -7,6 +7,28 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestContainsString(t *testing.T) {
|
||||
cases := []struct {
|
||||
slice []string
|
||||
value string
|
||||
expect bool
|
||||
}{
|
||||
{[]string{"1"}, "1", true},
|
||||
{[]string{"1"}, "2", false},
|
||||
{[]string{"1", "2"}, "1", true},
|
||||
{[]string{"1", "2"}, "3", false},
|
||||
{nil, "3", false},
|
||||
{nil, "", false},
|
||||
}
|
||||
|
||||
for _, each := range cases {
|
||||
t.Run(path.Join(each.slice...), func(t *testing.T) {
|
||||
actual := Contains(each.slice, each.value)
|
||||
assert.Equal(t, each.expect, actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotEmpty(t *testing.T) {
|
||||
cases := []struct {
|
||||
args []string
|
||||
|
||||
9
core/syncx/once.go
Normal file
9
core/syncx/once.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package syncx
|
||||
|
||||
import "sync"
|
||||
|
||||
// Once returns a func that guarantees fn can only called once.
|
||||
// Deprecated: use sync.OnceFunc instead.
|
||||
func Once(fn func()) func() {
|
||||
return sync.OnceFunc(fn)
|
||||
}
|
||||
33
core/syncx/once_test.go
Normal file
33
core/syncx/once_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package syncx
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestOnce(t *testing.T) {
|
||||
var v int
|
||||
add := Once(func() {
|
||||
v++
|
||||
})
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
add()
|
||||
}
|
||||
|
||||
assert.Equal(t, 1, v)
|
||||
}
|
||||
|
||||
func BenchmarkOnce(b *testing.B) {
|
||||
var v int
|
||||
add := Once(func() {
|
||||
v++
|
||||
})
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
add()
|
||||
}
|
||||
assert.Equal(b, 1, v)
|
||||
}
|
||||
Reference in New Issue
Block a user