neoneo-go/cli/smartcontract/smart_contract_test.go

67 lines
1.6 KiB
Go
Raw Normal View History

package smartcontract
import (
"flag"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/require"
"github.com/urfave/cli"
)
func TestInitSmartContract(t *testing.T) {
d, err := ioutil.TempDir("./", "")
require.NoError(t, err)
os.Chdir(d)
2021-03-01 11:14:15 +00:00
t.Cleanup(func() {
os.Chdir("..")
os.RemoveAll(d)
2021-03-01 11:14:15 +00:00
})
contractName := "testContract"
set := flag.NewFlagSet("flagSet", flag.ExitOnError)
set.String("name", contractName, "")
ctx := cli.NewContext(cli.NewApp(), set, nil)
require.NoError(t, initSmartContract(ctx))
dirInfo, err := os.Stat(contractName)
require.NoError(t, err)
require.True(t, dirInfo.IsDir())
files, err := ioutil.ReadDir(contractName)
require.NoError(t, err)
require.Equal(t, 2, len(files))
require.Equal(t, "main.go", files[0].Name())
require.Equal(t, "neo-go.yml", files[1].Name())
main, err := ioutil.ReadFile(contractName + "/" + files[0].Name())
require.NoError(t, err)
require.Equal(t,
`package `+contractName+`
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
var notificationName string
// init initializes notificationName before calling any other smart-contract method
func init() {
notificationName = "Hello world!"
}
// RuntimeNotify sends runtime notification with "Hello world!" name
func RuntimeNotify(args []interface{}) {
runtime.Notify(notificationName, args)
}`, string(main))
manifest, err := ioutil.ReadFile(contractName + "/" + files[1].Name())
require.NoError(t, err)
require.Equal(t,
2020-11-20 08:02:58 +00:00
`name: testContract
safemethods: []
2020-11-20 08:02:58 +00:00
supportedstandards: []
events:
- name: Hello world!
parameters:
- name: args
type: Array
`, string(manifest))
}