Skip to content

Commit

Permalink
split encode()
Browse files Browse the repository at this point in the history
Signed-off-by: kevindiu <kevindiujp@gmail.com>
  • Loading branch information
kevindiu committed Jun 2, 2022
1 parent c85b54c commit 5ad9c5a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
13 changes: 7 additions & 6 deletions internal/conv/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package conv

import (
"io"
"io/ioutil"
"reflect"
"strings"
Expand Down Expand Up @@ -56,16 +57,16 @@ func F32stos(fs []float32) (s string) {

// Utf8ToSjis converts a UTF8 string to sjis string.
func Utf8ToSjis(s string) (string, error) {
b, err := ioutil.ReadAll(transform.NewReader(strings.NewReader(s), japanese.ShiftJIS.NewEncoder()))
if err != nil {
return "", err
}
return string(b), nil
return encode(strings.NewReader(s), japanese.ShiftJIS.NewEncoder())
}

// Utf8ToEucjp converts a UTF8 string to eucjp string.
func Utf8ToEucjp(s string) (string, error) {
b, err := ioutil.ReadAll(transform.NewReader(strings.NewReader(s), japanese.EUCJP.NewEncoder()))
return encode(strings.NewReader(s), japanese.EUCJP.NewEncoder())
}

func encode(r io.Reader, t transform.Transformer) (string, error) {
b, err := ioutil.ReadAll(transform.NewReader(r, t))
if err != nil {
return "", err
}
Expand Down
82 changes: 82 additions & 0 deletions internal/conv/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
package conv

import (
"io"
"reflect"
"strings"
"testing"
"testing/iotest"

"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/test/goleak"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/transform"
)

var testData = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Expand Down Expand Up @@ -376,3 +381,80 @@ func TestUtf8ToEucjp(t *testing.T) {
})
}
}

func Test_encode(t *testing.T) {
type args struct {
r io.Reader
t transform.Transformer
}
type want struct {
want string
err error
}
type test struct {
name string
args args
want want
checkFunc func(want, string, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, got string, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
if !reflect.DeepEqual(got, w.want) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "success to encode string",
args: args{
r: strings.NewReader("こんにちは"),
t: japanese.EUCJP.NewEncoder(),
},
want: want{
want: "\xa4\xb3\xa4\xf3\xa4ˤ\xc1\xa4\xcf",
},
},
func() test {
err := errors.New("invalid reader")
return test{
name: "fail to encode string",
args: args{
r: iotest.ErrReader(err),
t: japanese.EUCJP.NewEncoder(),
},
want: want{
err: err,
},
}
}(),
}

for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
checkFunc = defaultCheckFunc
}

got, err := encode(test.args.r, test.args.t)
if err := checkFunc(test.want, got, err); err != nil {
tt.Errorf("error = %v", err)
}

})
}
}

0 comments on commit 5ad9c5a

Please sign in to comment.