Skip to content

Commit

Permalink
fix + more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
radkomih committed Jan 12, 2024
1 parent 4fea4df commit 3b3c03a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
Binary file modified build/runtime.wasm
Binary file not shown.
6 changes: 6 additions & 0 deletions primitives/types/raw_origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ func DecodeRawOrigin(buffer *bytes.Buffer) (RawOrigin, error) {
switch b {
case RawOriginRoot:
return NewRawOriginRoot(), nil
case RawOriginSigned:
address, err := DecodeAccountId(buffer)
if err != nil {
return RawOrigin{}, err
}
return NewRawOriginSigned(address), nil
case RawOriginNone:
return NewRawOriginNone(), nil
default:
Expand Down
76 changes: 64 additions & 12 deletions primitives/types/raw_origin_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package types

import (
"bytes"
"encoding/hex"
"io"
"testing"

sc "github.com/LimeChain/goscale"
Expand All @@ -12,13 +14,16 @@ var (
bytesAddress, _ = hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000000")
address, _ = NewAccountId(sc.BytesToSequenceU8(bytesAddress)...)

signedOrigin = NewRawOriginSigned(address)
rootOrigin = NewRawOriginRoot()
signedOrigin = NewRawOriginSigned(address)
signedOriginInvalidAddress = NewRawOriginSigned(AccountId{})
noneOrigin = NewRawOriginNone()
)

func Test_NewRawOriginRoot(t *testing.T) {
expect := RawOrigin{sc.NewVaryingData(RawOriginRoot)}

assert.Equal(t, expect, NewRawOriginRoot())
assert.Equal(t, expect, rootOrigin)
}

func Test_NewRawOriginSigned(t *testing.T) {
Expand All @@ -30,7 +35,7 @@ func Test_NewRawOriginSigned(t *testing.T) {
func Test_NewRawOriginNone(t *testing.T) {
expect := RawOrigin{sc.NewVaryingData(RawOriginNone)}

assert.Equal(t, expect, NewRawOriginNone())
assert.Equal(t, expect, noneOrigin)
}

func Test_RawOriginFrom(t *testing.T) {
Expand All @@ -46,25 +51,25 @@ func Test_RawOriginFrom_Empty(t *testing.T) {

result := RawOriginFrom(option)

assert.Equal(t, NewRawOriginNone(), result)
assert.Equal(t, noneOrigin, result)
}

func Test_RawOrigin_IsRootOrigin(t *testing.T) {
assert.Equal(t, true, NewRawOriginRoot().IsRootOrigin())
assert.Equal(t, true, rootOrigin.IsRootOrigin())
assert.Equal(t, false, signedOrigin.IsRootOrigin())
assert.Equal(t, false, NewRawOriginNone().IsRootOrigin())
assert.Equal(t, false, noneOrigin.IsRootOrigin())
}

func Test_RawOrigin_IsSignedOrigin(t *testing.T) {
assert.Equal(t, false, NewRawOriginRoot().IsSignedOrigin())
assert.Equal(t, false, rootOrigin.IsSignedOrigin())
assert.Equal(t, true, signedOrigin.IsSignedOrigin())
assert.Equal(t, false, NewRawOriginNone().IsSignedOrigin())
assert.Equal(t, false, noneOrigin.IsSignedOrigin())
}

func Test_RawOrigin_IsNoneOrigin(t *testing.T) {
assert.Equal(t, false, NewRawOriginRoot().IsNoneOrigin())
assert.Equal(t, false, rootOrigin.IsNoneOrigin())
assert.Equal(t, false, signedOrigin.IsNoneOrigin())
assert.Equal(t, true, NewRawOriginNone().IsNoneOrigin())
assert.Equal(t, true, noneOrigin.IsNoneOrigin())
}

func Test_RawOrigin_AsSigned(t *testing.T) {
Expand All @@ -75,17 +80,64 @@ func Test_RawOrigin_AsSigned(t *testing.T) {
}

func Test_RawOriginRoot_AsSigned_TypeError(t *testing.T) {
address, err := NewRawOriginRoot().AsSigned()
address, err := rootOrigin.AsSigned()

assert.Error(t, err)
assert.Equal(t, "not a valid 'RawOrigin' type", err.Error())
assert.Equal(t, AccountId{}, address)
}

func Test_RawOriginNone_AsSigned_TypeError(t *testing.T) {
address, err := NewRawOriginNone().AsSigned()
address, err := noneOrigin.AsSigned()

assert.Error(t, err)
assert.Equal(t, "not a valid 'RawOrigin' type", err.Error())
assert.Equal(t, AccountId{}, address)
}

func Test_DecodeRawOrigin_Root(t *testing.T) {
buffer := bytes.NewBuffer(rootOrigin.Bytes())

result, err := DecodeRawOrigin(buffer)

assert.NoError(t, err)
assert.Equal(t, rootOrigin, result)
}

func Test_DecodeRawOrigin_Signed(t *testing.T) {
buffer := bytes.NewBuffer(signedOrigin.Bytes())

result, err := DecodeRawOrigin(buffer)

assert.NoError(t, err)
assert.Equal(t, signedOrigin, result)
}

func Test_DecodeRawOrigin_Signed_InvalidAddress(t *testing.T) {
buffer := bytes.NewBuffer(signedOriginInvalidAddress.Bytes())

result, err := DecodeRawOrigin(buffer)

assert.Error(t, err)
assert.Equal(t, io.EOF, err)
assert.Equal(t, RawOrigin{}, result)
}

func Test_DecodeRawOrigin_None(t *testing.T) {
buffer := bytes.NewBuffer(noneOrigin.Bytes())

result, err := DecodeRawOrigin(buffer)

assert.NoError(t, err)
assert.Equal(t, noneOrigin, result)
}

func Test_DecodeRawOrigin_InvalidType(t *testing.T) {
buffer := bytes.NewBuffer([]byte{0x03})

result, err := DecodeRawOrigin(buffer)

assert.Error(t, err)
assert.Equal(t, "not a valid 'RawOrigin' type", err.Error())
assert.Equal(t, RawOrigin{}, result)
}

0 comments on commit 3b3c03a

Please sign in to comment.