parent
f0c3066ef6
commit
15ed905757
2 changed files with 41 additions and 0 deletions
|
@ -25,6 +25,18 @@ func NewParameter(name string, typ smartcontract.ParamType) Parameter {
|
|||
}
|
||||
}
|
||||
|
||||
// IsValid checks Parameter consistency and correctness.
|
||||
func (p *Parameter) IsValid() error {
|
||||
if p.Name == "" {
|
||||
return errors.New("empty or absent name")
|
||||
}
|
||||
if p.Type == smartcontract.VoidType {
|
||||
return errors.New("void parameter")
|
||||
}
|
||||
_, err := smartcontract.ConvertToParamType(int(p.Type))
|
||||
return err
|
||||
}
|
||||
|
||||
// ToStackItem converts Parameter to stackitem.Item.
|
||||
func (p *Parameter) ToStackItem() stackitem.Item {
|
||||
return stackitem.NewStruct([]stackitem.Item{
|
||||
|
@ -60,6 +72,12 @@ func (p *Parameter) FromStackItem(item stackitem.Item) error {
|
|||
|
||||
// AreValid checks all parameters for validity and consistency.
|
||||
func (p Parameters) AreValid() error {
|
||||
for i := range p {
|
||||
err := p[i].IsValid()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(p) < 2 {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -9,6 +9,29 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestParametersAreValid(t *testing.T) {
|
||||
ps := Parameters{}
|
||||
require.NoError(t, ps.AreValid()) // No parameters.
|
||||
|
||||
ps = append(ps, Parameter{})
|
||||
require.Error(t, ps.AreValid())
|
||||
|
||||
ps[0].Name = "qwerty"
|
||||
require.NoError(t, ps.AreValid())
|
||||
|
||||
ps[0].Type = 0x42 // Invalid type.
|
||||
require.Error(t, ps.AreValid())
|
||||
|
||||
ps[0].Type = smartcontract.VoidType
|
||||
require.Error(t, ps.AreValid())
|
||||
|
||||
ps[0].Type = smartcontract.BoolType
|
||||
require.NoError(t, ps.AreValid())
|
||||
|
||||
ps = append(ps, Parameter{Name: "qwerty"})
|
||||
require.Error(t, ps.AreValid())
|
||||
}
|
||||
|
||||
func TestParameter_ToStackItemFromStackItem(t *testing.T) {
|
||||
p := &Parameter{
|
||||
Name: "param",
|
||||
|
|
Loading…
Reference in a new issue