Add struct tests2 (#978)

* Add unit-tests for structs

* Remote commented imports

* Add tweaks per review feedback
This commit is contained in:
Jonathan Claudius 2022-03-09 19:40:36 -05:00 committed by GitHub
parent 7d64e40b93
commit 991f8fdb4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,125 @@
package vaa
import (
"encoding/hex"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestChainIDFromString(t *testing.T) {
type test struct {
input string
output ChainID
}
// Positive Test Cases
p_tests := []test{
{input: "solana", output: ChainIDSolana},
{input: "ethereum", output: ChainIDEthereum},
{input: "terra", output: ChainIDTerra},
{input: "bsc", output: ChainIDBSC},
{input: "polygon", output: ChainIDPolygon},
{input: "avalanche", output: ChainIDAvalanche},
{input: "oasis", output: ChainIDOasis},
{input: "fantom", output: ChainIDFantom},
{input: "algorand", output: ChainIDAlgorand},
{input: "ethereum-ropsten", output: ChainIDEthereumRopsten},
{input: "Solana", output: ChainIDSolana},
{input: "Ethereum", output: ChainIDEthereum},
{input: "Terra", output: ChainIDTerra},
{input: "Bsc", output: ChainIDBSC},
{input: "Polygon", output: ChainIDPolygon},
{input: "Avalanche", output: ChainIDAvalanche},
{input: "Oasis", output: ChainIDOasis},
{input: "Fantom", output: ChainIDFantom},
{input: "Algorand", output: ChainIDAlgorand},
{input: "Karura", output: ChainIDKarura},
{input: "Acala", output: ChainIDAcala},
}
// Negative Test Cases
n_tests := []test{
{input: "Unknown", output: ChainIDUnset},
}
for _, tc := range p_tests {
t.Run(tc.input, func(t *testing.T) {
chainId, err := ChainIDFromString(tc.input)
assert.Equal(t, tc.output, chainId)
assert.Nil(t, err)
})
}
for _, tc := range n_tests {
t.Run(tc.input, func(t *testing.T) {
chainId, err := ChainIDFromString(tc.input)
assert.Equal(t, tc.output, chainId)
assert.NotNil(t, err)
})
}
}
func TestAddress_MarshalJSON(t *testing.T) {
addr := Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
expected := "223030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303422"
marshalJsonAddress, err := addr.MarshalJSON()
assert.Equal(t, hex.EncodeToString(marshalJsonAddress), expected)
assert.Nil(t, err)
}
func TestAddress_String(t *testing.T) {
addr := Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
expected := "0000000000000000000000000000000000000000000000000000000000000004"
assert.Equal(t, addr.String(), expected)
}
func TestAddress_Bytes(t *testing.T) {
addr := Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
expected := []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4}
assert.Equal(t, addr.Bytes(), expected)
}
func TestSignatureData_MarshalJSON(t *testing.T) {
sigData := SignatureData{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0}
marshalJsonSigData, err := sigData.MarshalJSON()
require.Nil(t, err)
expected := "223030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303430303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303034303022"
assert.Equal(t, hex.EncodeToString(marshalJsonSigData), expected)
}
func TestSignature_DataString(t *testing.T) {
sigData := SignatureData{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0}
expected := "0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000400"
assert.Equal(t, sigData.String(), expected)
}
func TestChainId_String(t *testing.T) {
type test struct {
input ChainID
output string
}
tests := []test{
{input: 0, output: "unset"},
{input: 1, output: "solana"},
{input: 2, output: "ethereum"},
{input: 3, output: "terra"},
{input: 4, output: "bsc"},
{input: 5, output: "polygon"},
{input: 6, output: "avalanche"},
{input: 7, output: "oasis"},
{input: 10, output: "fantom"},
{input: 8, output: "algorand"},
{input: 10001, output: "ethereum-ropsten"},
{input: 11, output: "karura"},
{input: 12, output: "acala"},
}
for _, tc := range tests {
t.Run(tc.output, func(t *testing.T) {
assert.Equal(t, ChainID(tc.input).String(), tc.output)
})
}
}