github.com/timandy/routine@v1.1.4-0.20240507073150-e4a3e1fe2ba5/thread_local_map_entry_test.go (about)

     1  package routine
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestEntry_Clone(t *testing.T) {
    10  	expect := &personCloneable{Id: 1, Name: "Hello"}
    11  	e := entry(expect)
    12  	value := entryValue[*personCloneable](e)
    13  	assert.NotNil(t, value)
    14  	assert.Equal(t, *expect, *value)
    15  	//
    16  	c, ok := entryAssert[Cloneable](e)
    17  	assert.True(t, ok)
    18  	assert.Equal(t, *expect, *c.(*personCloneable))
    19  	//
    20  	copied := entry(c.Clone())
    21  	value2 := entryValue[*personCloneable](copied)
    22  	assert.NotNil(t, value2)
    23  	assert.Equal(t, *expect, *value2)
    24  	//
    25  	c3, ok2 := entryAssert[Cloneable](copied)
    26  	assert.True(t, ok2)
    27  	assert.Equal(t, *expect, *c3.(*personCloneable))
    28  }
    29  
    30  //===
    31  
    32  func TestEntry_Value_Nil(t *testing.T) {
    33  	e := entry(nil)
    34  	value := entryValue[any](e)
    35  	assert.Nil(t, value)
    36  }
    37  
    38  func TestEntry_Value_NotNil(t *testing.T) {
    39  	expect := 1
    40  	e := entry(expect)
    41  	value := entryValue[int](e)
    42  	assert.Equal(t, expect, value)
    43  }
    44  
    45  func TestEntry_Value_Default(t *testing.T) {
    46  	expect := 0
    47  	e := entry(expect)
    48  	value := entryValue[int](e)
    49  	assert.Equal(t, expect, value)
    50  }
    51  
    52  func TestEntry_Value_Interface_Nil(t *testing.T) {
    53  	var expect Cloneable
    54  	e := entry(expect)
    55  	value := entryValue[Cloneable](e)
    56  	assert.Nil(t, value)
    57  }
    58  
    59  func TestEntry_Value_Interface_NotNil(t *testing.T) {
    60  	var expect Cloneable = &personCloneable{Id: 1, Name: "Hello"}
    61  	e := entry(expect)
    62  	value := entryValue[Cloneable](e)
    63  	assert.Same(t, expect, value)
    64  }
    65  
    66  func TestEntry_Value_Pointer_Nil(t *testing.T) {
    67  	var expect *personCloneable
    68  	e := entry(expect)
    69  	value := entryValue[*personCloneable](e)
    70  	assert.Nil(t, value)
    71  }
    72  
    73  func TestEntry_Value_Pointer_NotNil(t *testing.T) {
    74  	expect := &personCloneable{Id: 1, Name: "Hello"}
    75  	e := entry(expect)
    76  	value := entryValue[*personCloneable](e)
    77  	assert.Same(t, expect, value)
    78  }
    79  
    80  func TestEntry_Value_Struct_Default(t *testing.T) {
    81  	expect := personCloneable{}
    82  	e := entry(expect)
    83  	value := entryValue[personCloneable](e)
    84  	assert.Equal(t, expect, value)
    85  }
    86  
    87  func TestEntry_Value_Struct_NotDefault(t *testing.T) {
    88  	expect := personCloneable{Id: 1, Name: "Hello"}
    89  	e := entry(expect)
    90  	value := entryValue[personCloneable](e)
    91  	assert.Equal(t, expect, value)
    92  }
    93  
    94  //===
    95  
    96  func TestEntry_Assert_Nil(t *testing.T) {
    97  	e := entry(nil)
    98  	value, ok := entryAssert[any](e)
    99  	assert.False(t, ok)
   100  	assert.Nil(t, value)
   101  }
   102  
   103  func TestEntry_Assert_NotNil(t *testing.T) {
   104  	expect := 1
   105  	e := entry(expect)
   106  	value, ok := entryAssert[int](e)
   107  	assert.True(t, ok)
   108  	assert.Equal(t, expect, value)
   109  }
   110  
   111  func TestEntry_Assert_Default(t *testing.T) {
   112  	expect := 0
   113  	e := entry(expect)
   114  	value, ok := entryAssert[int](e)
   115  	assert.True(t, ok)
   116  	assert.Equal(t, expect, value)
   117  }
   118  
   119  func TestEntry_Assert_Interface_Nil(t *testing.T) {
   120  	var expect Cloneable
   121  	e := entry(expect)
   122  	value, ok := entryAssert[Cloneable](e)
   123  	assert.False(t, ok)
   124  	assert.Nil(t, value)
   125  }
   126  
   127  func TestEntry_Assert_Interface_NotNil(t *testing.T) {
   128  	var expect Cloneable = &personCloneable{Id: 1, Name: "Hello"}
   129  	e := entry(expect)
   130  	value, ok := entryAssert[Cloneable](e)
   131  	assert.True(t, ok)
   132  	assert.Same(t, expect, value)
   133  }
   134  
   135  func TestEntry_Assert_Pointer_Nil(t *testing.T) {
   136  	var expect *personCloneable
   137  	e := entry(expect)
   138  	value, ok := entryAssert[*personCloneable](e)
   139  	assert.True(t, ok)
   140  	assert.Nil(t, value)
   141  }
   142  
   143  func TestEntry_Assert_Pointer_NotNil(t *testing.T) {
   144  	expect := &personCloneable{Id: 1, Name: "Hello"}
   145  	e := entry(expect)
   146  	value, ok := entryAssert[*personCloneable](e)
   147  	assert.True(t, ok)
   148  	assert.Same(t, expect, value)
   149  }
   150  
   151  func TestEntry_Assert_Struct_Default(t *testing.T) {
   152  	expect := personCloneable{}
   153  	e := entry(expect)
   154  	value, ok := entryAssert[personCloneable](e)
   155  	assert.True(t, ok)
   156  	assert.Equal(t, expect, value)
   157  }
   158  
   159  func TestEntry_Assert_Struct_NotDefault(t *testing.T) {
   160  	expect := personCloneable{Id: 1, Name: "Hello"}
   161  	e := entry(expect)
   162  	value, ok := entryAssert[personCloneable](e)
   163  	assert.True(t, ok)
   164  	assert.Equal(t, expect, value)
   165  }