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

feat: add Utf8ToGbk and GbkToUtf8

This commit is contained in:
dudaodong
2023-06-13 14:49:40 +08:00
parent 5a8ff17b52
commit d20f8783b2
4 changed files with 180 additions and 0 deletions

View File

@@ -4,8 +4,10 @@ import (
"fmt"
"reflect"
"testing"
"unicode/utf8"
"github.com/duke-git/lancet/internal"
"github.com/duke-git/lancet/validator"
)
func TestToChar(t *testing.T) {
@@ -339,3 +341,25 @@ func TestToInterface(t *testing.T) {
assert.EqualValues(nil, nilVal)
assert.Equal(false, ok)
}
func TestUtf8ToGbk(t *testing.T) {
assert := internal.NewAssert(t, "TestUtf8ToGbk")
utf8Data := []byte("hello")
gbkData, err := Utf8ToGbk(utf8Data)
assert.Equal(true, utf8.Valid(utf8Data))
assert.Equal(true, validator.IsGBK(gbkData))
assert.IsNil(err)
}
func TestGbkToUtf8(t *testing.T) {
assert := internal.NewAssert(t, "TestGbkToUtf8")
gbkData, err := Utf8ToGbk([]byte("hello"))
utf8Data, err := GbkToUtf8(gbkData)
assert.IsNil(err)
assert.Equal(true, utf8.Valid(utf8Data))
assert.Equal("hello", string(utf8Data))
}