github.com/MetalBlockchain/metalgo@v1.11.9/vms/avm/fx_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package avm
     5  
     6  import (
     7  	"errors"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  var (
    14  	errCalledInitialize      = errors.New("unexpectedly called Initialize")
    15  	errCalledBootstrapping   = errors.New("unexpectedly called Bootstrapping")
    16  	errCalledBootstrapped    = errors.New("unexpectedly called Bootstrapped")
    17  	errCalledVerifyTransfer  = errors.New("unexpectedly called VerifyTransfer")
    18  	errCalledVerifyOperation = errors.New("unexpectedly called VerifyOperation")
    19  )
    20  
    21  type FxTest struct {
    22  	T *testing.T
    23  
    24  	CantInitialize,
    25  	CantBootstrapping,
    26  	CantBootstrapped,
    27  	CantVerifyTransfer,
    28  	CantVerifyOperation bool
    29  
    30  	InitializeF      func(vm interface{}) error
    31  	BootstrappingF   func() error
    32  	BootstrappedF    func() error
    33  	VerifyTransferF  func(tx, in, cred, utxo interface{}) error
    34  	VerifyOperationF func(tx, op, cred interface{}, utxos []interface{}) error
    35  }
    36  
    37  func (fx *FxTest) Default(cant bool) {
    38  	fx.CantInitialize = cant
    39  	fx.CantBootstrapping = cant
    40  	fx.CantBootstrapped = cant
    41  	fx.CantVerifyTransfer = cant
    42  	fx.CantVerifyOperation = cant
    43  }
    44  
    45  func (fx *FxTest) Initialize(vm interface{}) error {
    46  	if fx.InitializeF != nil {
    47  		return fx.InitializeF(vm)
    48  	}
    49  	if !fx.CantInitialize {
    50  		return nil
    51  	}
    52  	if fx.T != nil {
    53  		require.FailNow(fx.T, errCalledInitialize.Error())
    54  	}
    55  	return errCalledInitialize
    56  }
    57  
    58  func (fx *FxTest) Bootstrapping() error {
    59  	if fx.BootstrappingF != nil {
    60  		return fx.BootstrappingF()
    61  	}
    62  	if !fx.CantBootstrapping {
    63  		return nil
    64  	}
    65  	if fx.T != nil {
    66  		require.FailNow(fx.T, errCalledBootstrapping.Error())
    67  	}
    68  	return errCalledBootstrapping
    69  }
    70  
    71  func (fx *FxTest) Bootstrapped() error {
    72  	if fx.BootstrappedF != nil {
    73  		return fx.BootstrappedF()
    74  	}
    75  	if !fx.CantBootstrapped {
    76  		return nil
    77  	}
    78  	if fx.T != nil {
    79  		require.FailNow(fx.T, errCalledBootstrapped.Error())
    80  	}
    81  	return errCalledBootstrapped
    82  }
    83  
    84  func (fx *FxTest) VerifyTransfer(tx, in, cred, utxo interface{}) error {
    85  	if fx.VerifyTransferF != nil {
    86  		return fx.VerifyTransferF(tx, in, cred, utxo)
    87  	}
    88  	if !fx.CantVerifyTransfer {
    89  		return nil
    90  	}
    91  	if fx.T != nil {
    92  		require.FailNow(fx.T, errCalledVerifyTransfer.Error())
    93  	}
    94  	return errCalledVerifyTransfer
    95  }
    96  
    97  func (fx *FxTest) VerifyOperation(tx, op, cred interface{}, utxos []interface{}) error {
    98  	if fx.VerifyOperationF != nil {
    99  		return fx.VerifyOperationF(tx, op, cred, utxos)
   100  	}
   101  	if !fx.CantVerifyOperation {
   102  		return nil
   103  	}
   104  	if fx.T != nil {
   105  		require.FailNow(fx.T, errCalledVerifyOperation.Error())
   106  	}
   107  	return errCalledVerifyOperation
   108  }