github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/environment/system_contracts_test.go (about) 1 package environment_test 2 3 import ( 4 "testing" 5 6 "github.com/onflow/cadence" 7 "github.com/onflow/cadence/runtime" 8 "github.com/onflow/cadence/runtime/common" 9 "github.com/onflow/cadence/runtime/sema" 10 "github.com/stretchr/testify/require" 11 12 "github.com/onflow/flow-go/fvm/environment" 13 reusableRuntime "github.com/onflow/flow-go/fvm/runtime" 14 "github.com/onflow/flow-go/fvm/runtime/testutil" 15 "github.com/onflow/flow-go/fvm/tracing" 16 "github.com/onflow/flow-go/model/flow" 17 ) 18 19 func TestSystemContractsInvoke(t *testing.T) { 20 21 type testCase struct { 22 name string 23 contractFunction func( 24 a common.AddressLocation, 25 s string, 26 values []cadence.Value, 27 types []sema.Type, 28 ctx runtime.Context, 29 ) (cadence.Value, error) 30 require func(t *testing.T, v cadence.Value, err error) 31 } 32 33 testCases := []testCase{ 34 { 35 name: "noop", 36 contractFunction: func(a common.AddressLocation, s string, values []cadence.Value, types []sema.Type, ctx runtime.Context) (cadence.Value, error) { 37 return nil, nil 38 }, 39 require: func(t *testing.T, v cadence.Value, err error) { 40 require.NoError(t, err) 41 }, 42 }, 43 { 44 name: "value gets returned as is", 45 contractFunction: func(a common.AddressLocation, s string, values []cadence.Value, types []sema.Type, ctx runtime.Context) (cadence.Value, error) { 46 return cadence.String("value"), nil 47 }, 48 require: func(t *testing.T, v cadence.Value, err error) { 49 require.NoError(t, err) 50 require.Equal(t, cadence.String("value"), v) 51 }, 52 }, 53 } 54 55 for _, tc := range testCases { 56 t.Run(tc.name, func(t *testing.T) { 57 tracer := tracing.NewTracerSpan() 58 runtimePool := reusableRuntime.NewCustomReusableCadenceRuntimePool( 59 0, 60 runtime.Config{}, 61 func(_ runtime.Config) runtime.Runtime { 62 return &testutil.TestInterpreterRuntime{ 63 InvokeContractFunc: tc.contractFunction, 64 } 65 }, 66 ) 67 runtime := environment.NewRuntime( 68 environment.RuntimeParams{ 69 ReusableCadenceRuntimePool: runtimePool, 70 }, 71 ) 72 invoker := environment.NewSystemContracts( 73 flow.Mainnet.Chain(), 74 tracer, 75 environment.NewProgramLogger( 76 tracer, 77 environment.DefaultProgramLoggerParams()), 78 runtime) 79 value, err := invoker.Invoke( 80 environment.ContractFunctionSpec{ 81 AddressFromChain: func(_ flow.Chain) flow.Address { 82 return flow.EmptyAddress 83 }, 84 FunctionName: "functionName", 85 }, 86 []cadence.Value{}) 87 88 tc.require(t, value, err) 89 }) 90 } 91 }