git.frostfs.info/TrueCloudLab/frostfs-sdk-go@v0.0.0-20241022124111-5361f0ecebd3/object/type_test.go (about)

     1  package object_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	v2object "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
     7  	"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestType_ToV2(t *testing.T) {
    12  	typs := []struct {
    13  		t  object.Type
    14  		t2 v2object.Type
    15  	}{
    16  		{
    17  			t:  object.TypeRegular,
    18  			t2: v2object.TypeRegular,
    19  		},
    20  		{
    21  			t:  object.TypeTombstone,
    22  			t2: v2object.TypeTombstone,
    23  		},
    24  		{
    25  			t:  object.TypeLock,
    26  			t2: v2object.TypeLock,
    27  		},
    28  	}
    29  
    30  	for _, item := range typs {
    31  		t2 := item.t.ToV2()
    32  
    33  		require.Equal(t, item.t2, t2)
    34  
    35  		require.Equal(t, item.t, object.TypeFromV2(item.t2))
    36  	}
    37  }
    38  
    39  func TestType_String(t *testing.T) {
    40  	toPtr := func(v object.Type) *object.Type {
    41  		return &v
    42  	}
    43  
    44  	testEnumStrings(t, new(object.Type), []enumStringItem{
    45  		{val: toPtr(object.TypeTombstone), str: "TOMBSTONE"},
    46  		{val: toPtr(object.TypeRegular), str: "REGULAR"},
    47  		{val: toPtr(object.TypeLock), str: "LOCK"},
    48  	})
    49  }
    50  
    51  type enumIface interface {
    52  	FromString(string) bool
    53  	String() string
    54  }
    55  
    56  type enumStringItem struct {
    57  	val enumIface
    58  	str string
    59  }
    60  
    61  func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) {
    62  	for _, item := range items {
    63  		require.Equal(t, item.str, item.val.String())
    64  
    65  		s := item.val.String()
    66  
    67  		require.True(t, e.FromString(s), s)
    68  
    69  		require.EqualValues(t, item.val, e, item.val)
    70  	}
    71  
    72  	// incorrect strings
    73  	for _, str := range []string{
    74  		"some string",
    75  		"undefined",
    76  	} {
    77  		require.False(t, e.FromString(str))
    78  	}
    79  }