Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sunspirit99 committed Sep 26, 2024
1 parent 9897b66 commit 1479db8
Showing 1 changed file with 302 additions and 0 deletions.
302 changes: 302 additions & 0 deletions gno.land/pkg/sdk/vm/msg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
package vm

import (
"testing"

"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/stretchr/testify/assert"
)

func TestMsgAddPackage(t *testing.T) {
t.Parallel()

creator := crypto.AddressFromPreimage([]byte("addr1"))
pkgName := "test"
pkgPath := "gno.land/r/namespace/test"
files := []*std.MemFile{
{
Name: "test.gno",
Body: `package test
func Echo() string {return "hello world"}`,
},
}

tests := []struct {
name string
msg MsgAddPackage
expectSignBytes string
expectErr error
}{
{
name: "valid message",
msg: MsgAddPackage{
Creator: creator,
Package: &std.MemPackage{
Name: pkgName,
Path: pkgPath,
Files: files,
},
Deposit: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectSignBytes: `{"creator":"g14ch5q26mhx3jk5cxl88t278nper264ces4m8nt",` +
`"deposit":"1000ugnot","package":{"files":[{"body":"package test\n\t\tfunc Echo() string {return \"hello world\"}",` +
`"name":"test.gno"}],"name":"test","path":"gno.land/r/namespace/test"}}`,
expectErr: nil,
},
{
name: "missing creator address",
msg: MsgAddPackage{
Creator: crypto.Address{},
Package: &std.MemPackage{
Name: pkgName,
Path: pkgPath,
Files: files,
},
Deposit: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: std.InvalidAddressError{},
},
{
name: "missing package path",
msg: MsgAddPackage{
Creator: creator,
Package: &std.MemPackage{
Name: pkgName,
Path: "",
Files: files,
},
Deposit: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: InvalidPkgPathError{},
},
{
name: "invalid deposit coins",
msg: MsgAddPackage{
Creator: creator,
Package: &std.MemPackage{
Name: pkgName,
Path: pkgPath,
Files: files,
},
Deposit: std.Coins{std.Coin{
Denom: "ugnot",
Amount: -1000, // invalid amount
}},
},
expectErr: std.InvalidCoinsError{},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

if err := tc.msg.ValidateBasic(); err != nil {
assert.ErrorIs(t, err, tc.expectErr)
} else {
assert.Equal(t, tc.expectSignBytes, string(tc.msg.GetSignBytes()))
}
})
}
}

func TestMsgCall(t *testing.T) {
t.Parallel()

caller := crypto.AddressFromPreimage([]byte("addr1"))
pkgPath := "gno.land/r/namespace/test"
funcName := "MyFunction"
args := []string{"arg1", "arg2"}

tests := []struct {
name string
msg MsgCall
expectSignBytes string
expectErr error
}{
{
name: "valid message",
msg: MsgCall{
Caller: caller,
PkgPath: pkgPath,
Func: funcName,
Args: args,
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectSignBytes: `{"args":["arg1","arg2"],"caller":"g14ch5q26mhx3jk5cxl88t278nper264ces4m8nt",` +
`"func":"MyFunction","pkg_path":"gno.land/r/namespace/test","send":"1000ugnot"}`,
expectErr: nil,
},
{
name: "invalid caller address",
msg: MsgCall{
Caller: crypto.Address{},
PkgPath: pkgPath,
Func: funcName,
Args: args,
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: std.InvalidAddressError{},
},
{
name: "missing package path",
msg: MsgCall{
Caller: caller,
PkgPath: "",
Func: funcName,
Args: args,
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: InvalidPkgPathError{},
},
{
name: "pkgPath should not be a realm path",
msg: MsgCall{
Caller: caller,
PkgPath: "gno.land/p/namespace/test", // this is not a valid realm path
Func: funcName,
Args: args,
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: InvalidPkgPathError{},
},
{
name: "missing function name to call",
msg: MsgCall{
Caller: caller,
PkgPath: pkgPath,
Func: "",
Args: args,
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: InvalidExprError{},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

if err := tc.msg.ValidateBasic(); err != nil {
assert.ErrorIs(t, err, tc.expectErr)
} else {
assert.Equal(t, tc.expectSignBytes, string(tc.msg.GetSignBytes()))
}
})
}
}

func TestMsgRun(t *testing.T) {
t.Parallel()

caller := crypto.AddressFromPreimage([]byte("addr1"))
pkgName := "main"
pkgPath := "gno.land/r/" + caller.String() + "/run"
pkgFiles := []*std.MemFile{
{
Name: "main.gno",
Body: `package main
func Echo() string {return "hello world"}`,
},
}

tests := []struct {
name string
msg MsgRun
expectSignBytes string
expectErr error
}{
{
name: "valid message",
msg: MsgRun{
Caller: caller,
Package: &std.MemPackage{
Name: pkgName,
Path: pkgPath,
Files: pkgFiles,
},
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectSignBytes: `{"caller":"g14ch5q26mhx3jk5cxl88t278nper264ces4m8nt",` +
`"package":{"files":[{"body":"package main\n\t\tfunc Echo() string {return \"hello world\"}",` +
`"name":"main.gno"}],"name":"main","path":"gno.land/r/g14ch5q26mhx3jk5cxl88t278nper264ces4m8nt/run"},` +
`"send":"1000ugnot"}`,
expectErr: nil,
},
{
name: "invalid caller address",
msg: MsgRun{
Caller: crypto.Address{},
Package: &std.MemPackage{
Name: pkgName,
Path: pkgPath,
Files: pkgFiles,
},
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: std.InvalidAddressError{},
},
{
name: "invalid package path",
msg: MsgRun{
Caller: caller,
Package: &std.MemPackage{
Name: pkgName,
Path: "gno.land/r/namespace/test", // this is not a valid run path
Files: pkgFiles,
},
Send: std.Coins{std.Coin{
Denom: "ugnot",
Amount: 1000,
}},
},
expectErr: InvalidPkgPathError{},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

if err := tc.msg.ValidateBasic(); err != nil {
assert.ErrorIs(t, err, tc.expectErr)
} else {
assert.Equal(t, tc.expectSignBytes, string(tc.msg.GetSignBytes()))
}
})
}
}

0 comments on commit 1479db8

Please sign in to comment.