2019-07-11 15:38:37 +00:00
|
|
|
package chain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type basicContract struct{}
|
|
|
|
type basicParams struct {
|
|
|
|
b byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *basicParams) UnmarshalCBOR(in []byte) (int, error) {
|
|
|
|
b.b = in[0]
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
|
2019-07-11 16:15:44 +00:00
|
|
|
func (b basicContract) Exports() []interface{} {
|
|
|
|
return []interface{}{
|
|
|
|
b.InvokeSomething0,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
b.InvokeSomething10,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 15:47:05 +00:00
|
|
|
func (basicContract) InvokeSomething0(act *Actor, vmctx *VMContext,
|
2019-07-11 15:38:37 +00:00
|
|
|
params *basicParams) (InvokeRet, error) {
|
|
|
|
return InvokeRet{
|
|
|
|
returnCode: params.b,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-07-11 15:47:05 +00:00
|
|
|
func (basicContract) InvokeSomething10(act *Actor, vmctx *VMContext,
|
2019-07-11 15:38:37 +00:00
|
|
|
params *basicParams) (InvokeRet, error) {
|
|
|
|
return InvokeRet{
|
2019-07-11 15:47:05 +00:00
|
|
|
returnCode: params.b + 10,
|
2019-07-11 15:38:37 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvokerBasic(t *testing.T) {
|
|
|
|
inv := invoker{}
|
|
|
|
code, err := inv.transform(basicContract{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
ret, err := code[0](nil, nil, []byte{1})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, byte(1), ret.returnCode, "return code should be 1")
|
|
|
|
|
2019-07-11 16:15:44 +00:00
|
|
|
ret, err = code[10](nil, nil, []byte{2})
|
2019-07-11 15:38:37 +00:00
|
|
|
assert.NoError(t, err)
|
2019-07-11 16:15:44 +00:00
|
|
|
assert.Equal(t, byte(12), ret.returnCode, "return code should be 1")
|
2019-07-11 15:38:37 +00:00
|
|
|
}
|