Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
Add tests to types
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Castell committed Dec 21, 2023
1 parent 7a8d91a commit a4521b1
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 2 deletions.
5 changes: 3 additions & 2 deletions rpc/types/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -53,7 +54,7 @@ func (b ArgBytes) MarshalText() ([]byte, error) {
func (b *ArgBytes) UnmarshalText(input []byte) error {
hh, err := decodeToHex(input)
if err != nil {
return nil
return fmt.Errorf("invalid hex: %w", err)
}
aux := make([]byte, len(hh))
copy(aux[:], hh[:])
Expand Down Expand Up @@ -81,7 +82,7 @@ type ArgHash common.Hash
// UnmarshalText unmarshals from text
func (arg *ArgHash) UnmarshalText(input []byte) error {
if !hexIsValid(string(input)) {
return fmt.Errorf("invalid hash, it needs to be a hexadecimal value")
return errors.New("invalid hash, it needs to be a hexadecimal value")
}

str := strings.TrimPrefix(string(input), "0x")
Expand Down
186 changes: 186 additions & 0 deletions rpc/types/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package types

import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)

func TestArgUint64_MarshalText(t *testing.T) {
tests := []struct {
name string
arg ArgUint64
expected []byte
}{
{
name: "Positive number",
arg: ArgUint64(123),
expected: []byte("0x7b"),
},
{
name: "Zero",
arg: ArgUint64(0),
expected: []byte("0x0"),
},
{
name: "Large number",
arg: ArgUint64(18446744073709551615),
expected: []byte("0xffffffffffffffff"),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := test.arg.MarshalText()
assert.NoError(t, err)
assert.Equal(t, test.expected, result)
})
}
}

func TestArgUint64_UnmarshalText(t *testing.T) {
tests := []struct {
name string
input []byte
expected ArgUint64
wantErr bool
}{
{
name: "Valid input",
input: []byte("0x7b"),
expected: ArgUint64(123),
wantErr: false,
},
{
name: "Invalid input",
input: []byte("0xabcj"),
expected: ArgUint64(0),
wantErr: true,
},
{
name: "Empty input",
input: []byte(""),
expected: ArgUint64(0),
wantErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var arg ArgUint64
err := arg.UnmarshalText(test.input)
if (err != nil) != test.wantErr {
t.Errorf("Unexpected error status, got error: %v, want error: %v", err, test.wantErr)
}
if arg != test.expected {
t.Errorf("Unexpected result, got: %v, want: %v", arg, test.expected)
}
})
}
}

func TestArgBytes_MarshalText(t *testing.T) {
tests := []struct {
name string
arg ArgBytes
expected []byte
}{
{
name: "Non-empty bytes",
arg: ArgBytes{0x01, 0x02, 0x03},
expected: []byte("0x010203"),
},
{
name: "Empty bytes",
arg: ArgBytes{},
expected: []byte("0x"),
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := test.arg.MarshalText()
assert.NoError(t, err)
assert.Equal(t, test.expected, result)
})
}
}

func TestArgBytes_UnmarshalText(t *testing.T) {
tests := []struct {
name string
input []byte
expected ArgBytes
wantErr bool
}{
{
name: "Valid input",
input: []byte("0x010203"),
expected: ArgBytes{0x01, 0x02, 0x03},
wantErr: false,
},
{
name: "Invalid input",
input: []byte("0xabcj"),
expected: nil,
wantErr: true,
},
{
name: "Empty input",
input: []byte(""),
expected: ArgBytes{},
wantErr: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var arg ArgBytes
err := arg.UnmarshalText(test.input)
if (err != nil) != test.wantErr {
t.Errorf("Unexpected error status, got error: %v, want error: %v", err, test.wantErr)
}
assert.Equal(t, test.expected, arg)
})
}
}

func TestArgHash_UnmarshalText(t *testing.T) {
tests := []struct {
name string
input []byte
expected ArgHash
wantErr bool
}{
{
name: "Valid input",
input: []byte("0x1234567890abcdef"),
expected: ArgHash(common.HexToHash("1234567890abcdef")),
wantErr: false,
},
{
name: "Invalid input",
input: []byte("0xabcj"),
expected: ArgHash{},
wantErr: true,
},
{
name: "Empty input",
input: []byte(""),
expected: ArgHash{},
wantErr: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
var arg ArgHash
err := arg.UnmarshalText(test.input)
if (err != nil) != test.wantErr {
t.Errorf("Unexpected error status, got error: %v, want error: %v", err, test.wantErr)
}
assert.Equal(t, test.expected, arg)
})
}
}

0 comments on commit a4521b1

Please sign in to comment.