Skip to content

Commit

Permalink
chore: added tests for uint types
Browse files Browse the repository at this point in the history
  • Loading branch information
JCrawsh committed Nov 14, 2023
1 parent 7478e99 commit 27e6a4e
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 42 deletions.
31 changes: 9 additions & 22 deletions binary-codec/types/uint16.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,31 @@ type UInt16 struct{}
// method will attempt to convert it into a corresponding type code. If the conversion fails, an error is returned.
func (u *UInt16) FromJson(value any) ([]byte, error) {
switch v := value.(type) {
case uint16:
value = v
case uint:
value = uint16(v)
case int:
value = uint16(v)
case transactions.TxType:
tc, err := definitions.Get().GetTransactionTypeCodeByTransactionTypeName(string(v))
if err != nil {
return nil, err
}
value = int(tc)
value = uint16(tc)
case ledger.LedgerEntryType:
tc, err := definitions.Get().GetLedgerEntryTypeCodeByLedgerEntryTypeName(string(v))
if err != nil {
return nil, err
}
value = int(tc)
value = uint16(tc)
}
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, uint16(value.(int)))
err := binary.Write(buf, binary.BigEndian, value)
if err != nil {
return nil, err
}
return buf.Bytes(), nil

// if _, ok := value.(string); ok {
// tc, err := definitions.Get().GetTransactionTypeCodeByTransactionTypeName(value.(string))
// if err != nil {
// tc, err = definitions.Get().GetLedgerEntryTypeCodeByLedgerEntryTypeName(value.(string))
// if err != nil {
// return nil, err
// }
// }
// value = int(tc)
// }

// buf := new(bytes.Buffer)
// err := binary.Write(buf, binary.BigEndian, uint16(value.(int)))

// if err != nil {
// return nil, err
// }
// return buf.Bytes(), nil
}

// ToJson takes a BinaryParser and optional parameters, and converts the serialized byte data
Expand Down
63 changes: 63 additions & 0 deletions binary-codec/types/uint16_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package types

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/xyield/xrpl-go/model/ledger"
"github.com/xyield/xrpl-go/model/transactions"
)

func TestUInt16FromJson(t *testing.T) {
tt := []struct {
description string
input any
expected []byte
expectedErr error
}{
{
description: "convert uint16",
input: uint16(1),
expected: []byte{0, 1},
expectedErr: nil,
},
{
description: "convert uint",
input: uint(1),
expected: []byte{0, 1},
expectedErr: nil,
},
{
description: "convert int",
input: int(1),
expected: []byte{0, 1},
expectedErr: nil,
},
{
description: "convert TxType",
input: transactions.PaymentTx,
expected: []byte{0, 0},
expectedErr: nil,
},
{
description: "convert LedgerEntryType",
input: ledger.AccountRootEntry,
expected: []byte{0, 97},
expectedErr: nil,
},
}

for _, tc := range tt {
t.Run(tc.description, func(t *testing.T) {
u16 := &UInt16{}
got, err := u16.FromJson(tc.input)
if tc.expectedErr != nil {
require.EqualError(t, err, tc.expectedErr.Error())
require.Empty(t, got)
} else {
require.NoError(t, err)
require.Equal(t, tc.expected, got)
}
})
}
}
20 changes: 14 additions & 6 deletions binary-codec/types/uint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@ package types
import (
"bytes"
"encoding/binary"
"errors"

"github.com/xyield/xrpl-go/binary-codec/serdes"
"github.com/xyield/xrpl-go/model/transactions/types"
)

var ErrInvalidUInt32 = errors.New("invalid type for UInt32")

// UInt32 represents a 32-bit unsigned integer.
type UInt32 struct{}

// FromJson converts a JSON value into a serialized byte slice representing a 32-bit unsigned integer.
// The input value is assumed to be an integer. If the serialization fails, an error is returned.
func (u *UInt32) FromJson(value any) ([]byte, error) {
v := expandInt(value)
v, err := expandInt(value)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, v)
err = binary.Write(buf, binary.BigEndian, v)

if err != nil {
return nil, err
Expand All @@ -35,13 +41,15 @@ func (u *UInt32) ToJson(p *serdes.BinaryParser, opts ...int) (any, error) {
return int(binary.BigEndian.Uint32(b)), nil
}

func expandInt(v any) uint32 {
func expandInt(v any) (uint32, error) {
switch v := v.(type) {
case types.FlagsI:
return v.ToUint()
return v.ToUint(), nil
case uint:
return uint32(v)
return uint32(v), nil
case uint32:
return v, nil
default:
return v.(uint32)
return 0, ErrInvalidUInt32
}
}
56 changes: 56 additions & 0 deletions binary-codec/types/uint32_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package types

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/xyield/xrpl-go/model/transactions/types"
)

func TestUint32FromJson(t *testing.T) {
tt := []struct {
description string
input any
expected []byte
expectedErr error
}{
{
description: "convert uint32",
input: uint32(1),
expected: []byte{0, 0, 0, 1},
expectedErr: nil,
},
{
description: "convert uint",
input: uint(1),
expected: []byte{0, 0, 0, 1},
expectedErr: nil,
},
{
description: "convert flag",
input: types.SetFlag(1),
expected: []byte{0, 0, 0, 1},
expectedErr: nil,
},
{
description: "invalid type should error",
input: "invalid",
expected: nil,
expectedErr: ErrInvalidUInt32,
},
}

for _, tc := range tt {
t.Run(tc.description, func(t *testing.T) {
u32 := &UInt32{}
got, err := u32.FromJson(tc.input)
if tc.expectedErr != nil {
require.EqualError(t, err, tc.expectedErr.Error())
require.Empty(t, got)
} else {
require.NoError(t, err)
require.Equal(t, tc.expected, got)
}
})
}
}
22 changes: 11 additions & 11 deletions binary-codec/types/uint8.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ type UInt8 struct{}
// If the input value is a string, it's assumed to be a transaction result name, and the method will
// attempt to convert it into a transaction result type code. If the conversion fails, an error is returned.
func (u *UInt8) FromJson(value any) ([]byte, error) {
if s, ok := value.(string); ok {
tc, err := definitions.Get().GetTransactionResultTypeCodeByTransactionResultName(s)
var u8 uint8

switch v := value.(type) {
case string:
tc, err := definitions.Get().GetTransactionResultTypeCodeByTransactionResultName(v)
if err != nil {
return nil, err
}
value = tc
}

var intValue int

switch v := value.(type) {
u8 = uint8(tc)
case uint8:
u8 = v
case int:
intValue = v
u8 = uint8(v)
case int32:
intValue = int(v)
u8 = uint8(v)
}

buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, uint8(intValue))
err := binary.Write(buf, binary.BigEndian, u8)
if err != nil {
return nil, err
}
Expand Down
56 changes: 56 additions & 0 deletions binary-codec/types/uint8_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package types

import (
"testing"

"github.com/stretchr/testify/require"
"github.com/xyield/xrpl-go/binary-codec/definitions"
)

func TestUint8FromJson(t *testing.T) {
tt := []struct {
description string
input any
expected []byte
expectedErr error
}{
{
description: "find transaction code",
input: "tecAMM_ACCOUNT",
expected: []byte{168},
expectedErr: nil,
},
{
description: "regular uint8",
input: uint8(30),
expected: []byte{30},
expectedErr: nil,
},
{
description: "regular int",
input: int(30),
expected: []byte{30},
expectedErr: nil,
},
{
description: "invalid transaction result",
input: "invalid",
expected: nil,
expectedErr: &definitions.NotFoundError{Instance: "TransactionResultName", Input: "invalid"},
},
}

for _, tc := range tt {
t.Run(tc.description, func(t *testing.T) {
u8 := &UInt8{}
got, err := u8.FromJson(tc.input)
if tc.expectedErr != nil {
require.EqualError(t, err, tc.expectedErr.Error())
require.Empty(t, got)
} else {
require.NoError(t, err)
require.Equal(t, tc.expected, got)
}
})
}
}
6 changes: 3 additions & 3 deletions model/ledger/signer_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type SignerList struct {
PreviousTxnLgrSeq uint32
OwnerNode string
SignerEntries []SignerEntryWrapper
SignerListID uint64
SignerQuorum uint64
SignerListID uint32
SignerQuorum uint32
}

type SignerEntryWrapper struct {
Expand All @@ -29,7 +29,7 @@ type SignerEntryWrapper struct {

type SignerEntry struct {
Account types.Address
SignerWeight uint64
SignerWeight uint16
WalletLocator types.Hash256 `json:",omitempty"`
}

Expand Down

0 comments on commit 27e6a4e

Please sign in to comment.