Skip to content

Commit

Permalink
Add required tests for go/mysql/collations/charset
Browse files Browse the repository at this point in the history
Signed-off-by: Noble Mittal <[email protected]>
  • Loading branch information
beingnoble03 committed Mar 11, 2024
1 parent 8ee8e34 commit e833235
Show file tree
Hide file tree
Showing 2 changed files with 348 additions and 48 deletions.
294 changes: 278 additions & 16 deletions go/mysql/collations/charset/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ limitations under the License.
package charset

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

// TODO: These types can be removed, use binary_charset instead.
type testCharset1 struct{}

func (c *testCharset1) Name() string {
Expand Down Expand Up @@ -75,26 +73,290 @@ func (c *testCharset2) EncodeRune([]byte, rune) int {
return 0
}

func (c *testCharset2) DecodeRune([]byte) (rune, int) {
return 1, 1
func (c *testCharset2) DecodeRune(bytes []byte) (rune, int) {
if len(bytes) < 1 {
return RuneError, 0
}
return rune(bytes[0]), 1
}

func (c *testCharset2) Convert(_, src []byte, from Charset) ([]byte, error) {
return src, nil
}

func TestConvert(t *testing.T) {
dstCharset := &testCharset1{}
srcCharset := &testCharset2{}
src := []byte("src")
testCases := []struct {
src []byte
srcCharset Charset
dst []byte
dstCharset Charset
want []byte
err string
}{
{
src: []byte("testSrc"),
srcCharset: Charset_utf8mb3{},
dst: []byte("testDst"),
dstCharset: Charset_utf8mb4{},
want: []byte("testDsttestSrc"),
},
{
src: []byte("testSrc"),
srcCharset: Charset_utf8mb3{},
dst: nil,
dstCharset: Charset_utf8mb4{},
want: []byte("testSrc"),
},
{
src: []byte("testSrc"),
srcCharset: Charset_utf8mb4{},
dst: nil,
dstCharset: Charset_utf8mb3{},
want: []byte("testSrc"),
},
{
src: []byte("testSrc"),
srcCharset: Charset_utf8mb4{},
dst: []byte("testDst"),
dstCharset: Charset_utf8mb3{},
want: []byte("testDsttestSrc"),
},
{
src: []byte("😊😂🤢"),
srcCharset: Charset_utf8mb4{},
dst: []byte("testDst"),
dstCharset: Charset_utf8mb3{},
want: []byte("testDst???"),
err: "Cannot convert string",
},
{
src: []byte("testSrc"),
srcCharset: Charset_binary{},
dst: []byte("testDst"),
dstCharset: Charset_utf8mb3{},
want: []byte("testDsttestSrc"),
},
{
src: []byte{00, 65, 00, 66},
srcCharset: Charset_ucs2{},
dst: []byte("testDst"),
dstCharset: Charset_utf8mb3{},
want: []byte("testDstAB"),
},
{
src: []byte{00, 65, 00, 66},
srcCharset: Charset_ucs2{},
dst: nil,
dstCharset: Charset_utf8mb3{},
want: []byte("AB"),
},
{
src: []byte("😊😂🤢"),
srcCharset: Charset_utf8mb3{},
dst: nil,
dstCharset: &testCharset2{},
want: []byte("😊😂🤢"),
},
}

res, err := Convert(nil, dstCharset, src, srcCharset)
assert.NoError(t, err)
assert.Equal(t, src, res)
for _, tc := range testCases {
res, err := Convert(tc.dst, tc.dstCharset, tc.src, tc.srcCharset)

if tc.err != "" {
assert.ErrorContains(t, err, tc.err)
assert.Equal(t, tc.want, res)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.want, res)
}
}
}

func TestExpand(t *testing.T) {
testCases := []struct {
dst []rune
src []byte
srcCharset Charset
want []rune
}{
{
dst: []rune("testDst"),
src: []byte("testSrc"),
srcCharset: Charset_utf8mb3{},
want: []rune("testSrc"),
},
{
dst: nil,
src: []byte("testSrc"),
srcCharset: Charset_utf8mb3{},
want: []rune("testSrc"),
},
{
dst: nil,
src: []byte("testSrc"),
srcCharset: Charset_binary{},
want: []rune("testSrc"),
},
{
dst: []rune("testDst"),
src: []byte("testSrc"),
srcCharset: Charset_binary{},
want: []rune("testDsttestSrc"),
},
{
dst: []rune("testDst"),
src: []byte{0, 0, 0, 0x41},
srcCharset: Charset_utf32{},
want: []rune("testDstA"),
},
{
dst: nil,
src: []byte{0xFF},
srcCharset: Charset_latin1{},
want: []rune("ÿ"),
},
// multibyte case
{
dst: []rune("testDst"),
src: []byte("😊😂🤢"),
srcCharset: Charset_utf8mb4{},
want: []rune("😊😂🤢"),
},
}

for _, tc := range testCases {
res := Expand(tc.dst, tc.src, tc.srcCharset)

assert.Equal(t, tc.want, res)
}
}

func TestCollapse(t *testing.T) {
testCases := []struct {
dst []byte
src []rune
dstCharset Charset
want []byte
}{
{
dst: []byte("testDst"),
src: []rune("testSrc"),
dstCharset: Charset_utf8mb3{},
want: []byte("testDsttestSrc"),
},
{
dst: nil,
src: []rune("testSrc"),
dstCharset: Charset_utf8mb3{},
want: []byte("testSrc"),
},
{
dst: []byte("testDst"),
src: []rune("testSrc"),
dstCharset: Charset_utf8mb4{},
want: []byte("testDsttestSrc"),
},
{
dst: []byte("testDst"),
src: []rune("testSrc"),
dstCharset: Charset_binary{},
want: []byte("testDsttestSrc"),
},
{
dst: nil,
src: []rune("testSrc"),
dstCharset: Charset_binary{},
want: []byte("testSrc"),
},
{
dst: []byte("dst"),
src: []rune("src"),
dstCharset: Charset_ucs2{},
want: []byte{100, 115, 116, 0, 115, 0, 114, 0, 99},
},
{
dst: nil,
src: []rune("src"),
dstCharset: Charset_ucs2{},
want: []byte{0, 115, 0, 114, 0, 99},
},
// unsupported encoding case
{
dst: nil,
src: []rune{0xffff1},
dstCharset: Charset_ucs2{},
want: []byte{0, 63},
},
}

for _, tc := range testCases {
res := Collapse(tc.dst, tc.src, tc.dstCharset)

assert.Equal(t, tc.want, res)
}
}

func TestConvertFromUTF8(t *testing.T) {
dst := []byte("dst")
res, err = Convert(dst, dstCharset, src, srcCharset)
assert.NoError(t, err)
assert.Equal(t, []byte("dstsrc"), res)
src := []byte("😊😂🤢")

// TODO: Write more tests
res, err = Convert(nil, &testCharset2{}, src, &testCharset1{})
res, err := ConvertFromUTF8(dst, Charset_utf8mb4{}, src)
assert.NoError(t, err)
fmt.Println(res)
assert.Equal(t, []byte("dst😊😂🤢"), res)

res, err = ConvertFromUTF8(dst, Charset_utf8mb3{}, src)
assert.ErrorContains(t, err, "Cannot convert string")
assert.Equal(t, []byte("dst???"), res)
}

func TestConvertFromBinary(t *testing.T) {
testCases := []struct {
dst []byte
cs Charset
in []byte
want []byte
err string
}{
{
dst: []byte("testDst"),
cs: Charset_utf8mb4{},
in: []byte("testString"),
want: []byte("testDsttestString"),
},
{
cs: Charset_utf16le{},
in: []byte("testForOddLen"),
want: append([]byte{0}, []byte("testForOddLen")...),
},
{
cs: Charset_utf16{},
in: []byte("testForEvenLen"),
want: []byte("testForEvenLen"),
},
// multibyte case
{
dst: []byte("testDst"),
cs: Charset_utf8mb4{},
in: []byte("😊😂🤢"),
want: []byte("testDst😊😂🤢"),
},
// unsuppported encoding case
{
cs: Charset_utf32{},
in: []byte{0xff},
err: "Cannot convert string",
},
}

for _, tc := range testCases {
got, err := ConvertFromBinary(tc.dst, tc.cs, tc.in)

if tc.want == nil {
assert.ErrorContains(t, err, tc.err)
assert.Nil(t, got)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.want, got)
}
}
}
Loading

0 comments on commit e833235

Please sign in to comment.