github.com/koko1123/flow-go-1@v0.29.6/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/koko1123/flow-go-1/fvm/environment"
    13  	reusableRuntime "github.com/koko1123/flow-go-1/fvm/runtime"
    14  	"github.com/koko1123/flow-go-1/fvm/runtime/testutil"
    15  	"github.com/koko1123/flow-go-1/fvm/tracing"
    16  	"github.com/koko1123/flow-go-1/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  			runtime := environment.NewRuntime(
    59  				environment.RuntimeParams{
    60  					ReusableCadenceRuntimePool: reusableRuntime.NewCustomReusableCadenceRuntimePool(
    61  						0,
    62  						func(_ runtime.Config) runtime.Runtime {
    63  							return &testutil.TestInterpreterRuntime{
    64  								InvokeContractFunc: tc.contractFunction,
    65  							}
    66  						},
    67  					),
    68  				},
    69  			)
    70  			invoker := environment.NewSystemContracts(
    71  				flow.Mainnet.Chain(),
    72  				tracer,
    73  				environment.NewProgramLogger(
    74  					tracer,
    75  					environment.DefaultProgramLoggerParams()),
    76  				runtime)
    77  			value, err := invoker.Invoke(
    78  				environment.ContractFunctionSpec{
    79  					AddressFromChain: func(_ flow.Chain) flow.Address {
    80  						return flow.Address{}
    81  					},
    82  					FunctionName: "functionName",
    83  				},
    84  				[]cadence.Value{})
    85  
    86  			tc.require(t, value, err)
    87  		})
    88  	}
    89  }