Skip to content

Commit

Permalink
chore: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannm committed Jul 21, 2024
1 parent 5e684ec commit 028eeb5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 13 deletions.
20 changes: 10 additions & 10 deletions kit/utils/buf/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,24 @@ func (bb *Bytes) CopyFromWithOffset(data []byte, offset int) {
copy(bb.b[offset:], data)
}

func (bb *Bytes) CopyFrom(data []byte) {
copy(bb.b, data)
func (bb *Bytes) CopyFrom(src []byte) {
copy(bb.b, src)
}

func (bb *Bytes) CopyTo(data []byte) []byte {
copy(data, bb.b)
func (bb *Bytes) CopyTo(dst []byte) []byte {
copy(dst, bb.b)

return data
return dst
}

func (bb *Bytes) AppendFrom(data []byte) {
bb.b = append(bb.b, data...)
func (bb *Bytes) AppendFrom(src []byte) {
bb.b = append(bb.b, src...)
}

func (bb *Bytes) AppendTo(data []byte) []byte {
data = append(data, bb.b...)
func (bb *Bytes) AppendTo(dst []byte) []byte {
dst = append(dst, bb.b...)

return data
return dst
}

func (bb *Bytes) AppendByte(b byte) {
Expand Down
71 changes: 71 additions & 0 deletions kit/utils/buf/bytes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package buf_test

import (
"bytes"
"io"
"testing"

"github.com/clubpay/ronykit/kit/utils"
"github.com/clubpay/ronykit/kit/utils/buf"
)

func TestBytes(t *testing.T) {
b1 := buf.GetLen(100)
b2 := buf.GetLen(100)
b3 := buf.GetCap(100)
src := utils.S2B(utils.RandomID(100))
b1.CopyFrom(src)
if !bytes.Equal(*b1.Bytes(), src) {
t.Errorf("bytes not equal")
}
n, err := io.Copy(b2, b1)
if err != nil {
t.Error(err)
}
if n != 100 {
t.Errorf("copied bytes not equal")
}
if b2.Len() != 200 {
t.Errorf("copied bytes not equal: %d", b2.Len())
}

n, err = io.Copy(b3, b1)
if err != nil {
t.Error(err)
}
if n != 0 {
t.Errorf("copied bytes not equal: %d", n)
}
if b3.Len() != 0 {
t.Errorf("copied bytes not equal: %d", b3.Len())
}

b1.Reset()
b1.AppendFrom(src)
n, err = io.Copy(b3, b1)
if err != nil {
t.Error(err)
}
if n != 100 {
t.Errorf("copied bytes not equal: %d", n)
}
if b3.Len() != 100 {
t.Errorf("copied bytes not equal: %d", b3.Len())
}
if !bytes.Equal(*b3.Bytes(), src) {
t.Errorf("bytes not equal")
}
}

func BenchmarkBytesPool(b *testing.B) {
bp := buf.NewBytesPool(32, 4098)
data := utils.S2B(utils.RandomID(512))
for i := 0; i < b.N; i++ {
bb := bp.GetCap(1024)
_, err := bb.Write(data)
if err != nil {
b.Fatal(err)
}
bb.Release()
}
}
9 changes: 7 additions & 2 deletions kit/utils/reflector/reflector.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ func destruct(mType reflect.Type, tags ...string) *Reflected {
fi.unsafe = true
}

r.obj[fi.name] = fi
if _, ok := r.obj[fi.name]; !ok {
r.obj[fi.name] = fi
}

if fi.f.Anonymous {
ll.PushFront(destructInput{t: ft.Type, offset: ft.Offset, indexes: idx})

Expand All @@ -116,7 +119,9 @@ func destruct(mType reflect.Type, tags ...string) *Reflected {
if r.byTag[t] == nil {
r.byTag[t] = Fields{}
}
r.byTag[t][v] = fi
if _, ok := r.byTag[t][v]; !ok {
r.byTag[t][v] = fi
}
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion kit/utils/reflector/reflector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ type testMessage struct {
type testEmbed1 struct {
YY int64 `json:"yy" otherTag:"yyOther"`
MM string `json:"mM" otherTag:"mMOther"`
Y int64 `json:"y"`
}

type testEmbed2 struct {
YYY int64 `json:"yyy" otherTag:"yyyOther"`
MMM string `json:"mMM" otherTag:"mMMOther"`
Y int64 `json:"y"`
}

var _ = Describe("Reflector", func() {
Expand All @@ -40,19 +42,24 @@ var _ = Describe("Reflector", func() {
testEmbed1: testEmbed1{
YY: 123,
MM: "mM",
Y: 11,
},
testEmbed2: testEmbed2{
YYY: 1234,
MMM: "mMM",
Y: 12,
},
X: "xValue",
Y: 10,
z: "zValue",
M: nil,
M: map[string]string{
"x": "100",
},
}
rObj := r.Load(m, "json")

It("Load by Struct Fields", func() {
Expect(m.Y).To(Equal(int64(10)))
obj := rObj.Obj()
Expect(obj.GetInt64(m, "YY")).To(Equal(int64(123)))
Expect(obj.GetString(m, "MM")).To(Equal("mM"))
Expand All @@ -61,11 +68,14 @@ var _ = Describe("Reflector", func() {
Expect(obj.GetStringDefault(m, "X", "")).To(Equal(m.X))
Expect(obj.GetInt64Default(m, "Y", 0)).To(Equal(m.Y))
Expect(obj.GetStringDefault(m, "z", "")).To(BeEmpty())
Expect(obj.Get(m, "M")).To(Equal(map[string]string{"x": "100"}))
Expect(obj.GetInt64(m, "Y")).To(Equal(int64(10)))
})

It("Load by ToJSON tag", func() {
byTag, ok := rObj.ByTag("json")
Expect(ok).To(BeTrue())
Expect(byTag.GetInt64Default(m, "y", 0)).To(Equal(int64(11)))
Expect(byTag.GetStringDefault(m, "xTag", "")).To(Equal(m.X))
Expect(byTag.GetInt64Default(m, "yTag", 0)).To(Equal(m.Y))
Expect(byTag.GetInt64Default(m, "yy", 0)).To(Equal(m.YY))
Expand Down

0 comments on commit 028eeb5

Please sign in to comment.