github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/smartcontract/callflag/call_flags_test.go (about)

     1  package callflag
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/nspcc-dev/neo-go/internal/testserdes"
     7  	"github.com/stretchr/testify/require"
     8  	"gopkg.in/yaml.v3"
     9  )
    10  
    11  func TestCallFlag_Has(t *testing.T) {
    12  	require.True(t, AllowCall.Has(AllowCall))
    13  	require.True(t, (AllowCall | AllowNotify).Has(AllowCall))
    14  	require.False(t, (AllowCall).Has(AllowCall|AllowNotify))
    15  	require.True(t, All.Has(ReadOnly))
    16  }
    17  
    18  func TestCallFlagString(t *testing.T) {
    19  	var cases = map[CallFlag]string{
    20  		NoneFlag:               "None",
    21  		All:                    "All",
    22  		ReadStates:             "ReadStates",
    23  		States:                 "States",
    24  		ReadOnly:               "ReadOnly",
    25  		States | AllowCall:     "ReadOnly, WriteStates",
    26  		ReadOnly | AllowNotify: "ReadOnly, AllowNotify",
    27  		States | AllowNotify:   "States, AllowNotify",
    28  	}
    29  	for f, s := range cases {
    30  		require.Equal(t, s, f.String())
    31  	}
    32  }
    33  
    34  func TestFromString(t *testing.T) {
    35  	var cases = map[string]struct {
    36  		flag CallFlag
    37  		err  bool
    38  	}{
    39  		"None":                   {NoneFlag, false},
    40  		"All":                    {All, false},
    41  		"ReadStates":             {ReadStates, false},
    42  		"States":                 {States, false},
    43  		"ReadOnly":               {ReadOnly, false},
    44  		"ReadOnly, WriteStates":  {States | AllowCall, false},
    45  		"States, AllowCall":      {States | AllowCall, false},
    46  		"AllowCall, States":      {States | AllowCall, false},
    47  		"States, ReadOnly":       {States | AllowCall, false},
    48  		" AllowCall,AllowNotify": {AllowNotify | AllowCall, false},
    49  		"BlahBlah":               {NoneFlag, true},
    50  		"States, All":            {NoneFlag, true},
    51  		"ReadStates,,AllowCall":  {NoneFlag, true},
    52  		"ReadStates;AllowCall":   {NoneFlag, true},
    53  		"readstates":             {NoneFlag, true},
    54  		"  All":                  {NoneFlag, true},
    55  		"None, All":              {NoneFlag, true},
    56  	}
    57  	for s, res := range cases {
    58  		f, err := FromString(s)
    59  		require.True(t, res.err == (err != nil), "Input: '"+s+"'")
    60  		require.Equal(t, res.flag, f)
    61  	}
    62  }
    63  
    64  func TestMarshalUnmarshalJSON(t *testing.T) {
    65  	var f = States
    66  	testserdes.MarshalUnmarshalJSON(t, &f, new(CallFlag))
    67  	f = States | AllowNotify
    68  	testserdes.MarshalUnmarshalJSON(t, &f, new(CallFlag))
    69  
    70  	forig := f
    71  	err := f.UnmarshalJSON([]byte("42"))
    72  	require.Error(t, err)
    73  	require.Equal(t, forig, f)
    74  
    75  	err = f.UnmarshalJSON([]byte(`"State"`))
    76  	require.Error(t, err)
    77  	require.Equal(t, forig, f)
    78  }
    79  
    80  func TestMarshalUnmarshalYAML(t *testing.T) {
    81  	for _, expected := range []CallFlag{States, States | AllowNotify} {
    82  		data, err := yaml.Marshal(expected)
    83  		require.NoError(t, err)
    84  
    85  		var f CallFlag
    86  		require.NoError(t, yaml.Unmarshal(data, &f))
    87  		require.Equal(t, expected, f)
    88  	}
    89  
    90  	var f CallFlag
    91  	require.Error(t, yaml.Unmarshal([]byte(`[]`), &f))
    92  }