Skip to content

Commit

Permalink
Add Size() int method to Encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Oct 2, 2021
1 parent bd1d91b commit 2d948b1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (e *Encoder) toWriter(bytes []byte) (err error) {
return
}

// Size returns the count of bytes written.
func (e *Encoder) Size() int {
return e.count
}

func (e *Encoder) WriteBytes(b []byte, writeLength bool) error {
if traceEnabled {
zlog.Debug("encode: write byte array", zap.Int("len", len(b)))
Expand Down
25 changes: 25 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ import (
"github.com/stretchr/testify/require"
)

func TestEncoder_Size(t *testing.T) {
{
buf := new(bytes.Buffer)

enc := NewBinEncoder(buf)
assert.Equal(t, enc.Size(), 0)
enc.Encode(SafeString("hello"))

assert.Equal(t, enc.Size(), 6)
enc.WriteBool(true)
assert.Equal(t, enc.Size(), 7)
}
{
buf := new(bytes.Buffer)

enc := NewBorshEncoder(buf)
assert.Equal(t, enc.Size(), 0)
enc.WriteByte(123)

assert.Equal(t, enc.Size(), 1)
enc.WriteBool(true)
assert.Equal(t, enc.Size(), 2)
}
}

func TestEncoder_AliastTestType(t *testing.T) {
buf := new(bytes.Buffer)
enc := NewBinEncoder(buf)
Expand Down

0 comments on commit 2d948b1

Please sign in to comment.