1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-07 22:22:29 +08:00

feat: add some function for strutil package #88 (#89)

This commit is contained in:
燕归来
2023-04-17 16:07:14 +08:00
committed by GitHub
parent 975c303a31
commit 04abb7a3ea
2 changed files with 117 additions and 2 deletions

View File

@@ -1,9 +1,9 @@
package strutil
import (
"testing"
"github.com/duke-git/lancet/v2/internal"
"reflect"
"testing"
)
func TestCamelCase(t *testing.T) {
@@ -349,3 +349,52 @@ func TestRemoveNonPrintable(t *testing.T) {
assert.Equal("hello world", RemoveNonPrintable("hello\u00a0 \u200bworld\n"))
assert.Equal("你好😄", RemoveNonPrintable("你好😄"))
}
func TestString2Bytes(t *testing.T) {
assert := internal.NewAssert(t, "TestString2Bytes")
str := "abc"
bytes := StringToBytes(str)
assert.Equal(reflect.DeepEqual(bytes, []byte{'a', 'b', 'c'}), true)
}
func TestBytes2String(t *testing.T) {
assert := internal.NewAssert(t, "TestBytes2String")
bytes := []byte{'a', 'b', 'c'}
str := BytesToString(bytes)
assert.Equal(str == "abc", true)
}
func TestIsBlank(t *testing.T) {
assert := internal.NewAssert(t, "TestIsBlank")
assert.Equal(IsBlank(""), true)
assert.Equal(IsBlank("\t\v\f\n"), true)
assert.Equal(IsBlank(" 中文"), false)
}
func TestHasPrefixAny(t *testing.T) {
assert := internal.NewAssert(t, "TestHasPrefixAny")
str := "foo bar"
prefixes := []string{"fo", "xyz", "hello"}
notMatches := []string{"oom", "world"}
assert.Equal(HasPrefixAny(str, prefixes), true)
assert.Equal(HasPrefixAny(str, notMatches), false)
}
func TestHasSuffixAny(t *testing.T) {
assert := internal.NewAssert(t, "TestHasSuffixAny")
str := "foo bar"
suffixes := []string{"bar", "xyz", "hello"}
notMatches := []string{"oom", "world"}
assert.Equal(HasSuffixAny(str, suffixes), true)
assert.Equal(HasSuffixAny(str, notMatches), false)
}
func TestIndexOffset(t *testing.T) {
assert := internal.NewAssert(t, "TestIndexOffset")
str := "foo bar hello world"
assert.Equal(IndexOffset(str, "o", 5), 12)
assert.Equal(IndexOffset(str, "o", 0), 1)
assert.Equal(IndexOffset(str, "d", len(str)-1), len(str)-1)
assert.Equal(IndexOffset(str, "d", len(str)), -1)
assert.Equal(IndexOffset(str, "f", -1), -1)
}