github.com/iotexproject/iotex-core@v1.14.1-rc1/blockindex/contractstaking/delta_state_test.go (about)

     1  package contractstaking
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestDeltaState_Transfer(t *testing.T) {
    10  	require := require.New(t)
    11  
    12  	cases := []struct {
    13  		name     string
    14  		state    deltaState
    15  		action   deltaAction
    16  		expected deltaState
    17  		err      string
    18  	}{
    19  		{"unchanged->add", deltaStateUnchanged, deltaActionAdd, deltaStateAdded, ""},
    20  		{"unchanged->remove", deltaStateUnchanged, deltaActionRemove, deltaStateRemoved, ""},
    21  		{"unchanged->modify", deltaStateUnchanged, deltaActionModify, deltaStateModified, ""},
    22  		{"added->add", deltaStateAdded, deltaActionAdd, deltaStateUnchanged, "invalid delta action 0 on state 1"},
    23  		{"added->remove", deltaStateAdded, deltaActionRemove, deltaStateUnchanged, ""},
    24  		{"added->modify", deltaStateAdded, deltaActionModify, deltaStateAdded, ""},
    25  		{"removed->add", deltaStateRemoved, deltaActionAdd, deltaStateUnchanged, "invalid delta state 2"},
    26  		{"removed->remove", deltaStateRemoved, deltaActionRemove, deltaStateUnchanged, "invalid delta state 2"},
    27  		{"removed->modify", deltaStateRemoved, deltaActionModify, deltaStateUnchanged, "invalid delta state 2"},
    28  		{"modified->add", deltaStateModified, deltaActionAdd, deltaStateUnchanged, "invalid delta action 0 on state 3"},
    29  		{"modified->remove", deltaStateModified, deltaActionRemove, deltaStateRemoved, ""},
    30  		{"modified->modify", deltaStateModified, deltaActionModify, deltaStateModified, ""},
    31  		{"invalid state", deltaState(100), deltaActionAdd, deltaState(100), "invalid delta state 100"},
    32  		{"invalid action", deltaStateUnchanged, deltaAction(100), deltaStateUnchanged, "invalid delta action 100 on state 0"},
    33  	}
    34  
    35  	for _, c := range cases {
    36  		t.Run(c.name, func(t *testing.T) {
    37  			s, err := c.state.Transfer(c.action)
    38  			if len(c.err) > 0 {
    39  				require.Error(err)
    40  				require.Contains(err.Error(), c.err)
    41  			} else {
    42  				require.NoError(err)
    43  				require.Equal(c.expected, s)
    44  			}
    45  		})
    46  	}
    47  }
    48  
    49  func TestDeltaState_ZeroValue(t *testing.T) {
    50  	require := require.New(t)
    51  
    52  	var state deltaState
    53  	require.Equal(deltaStateUnchanged, state)
    54  }