github.com/wfusion/gofusion@v1.1.14/test/common/utils/cases/clone_test.go (about)

     1  package cases
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/suite"
     9  
    10  	"github.com/wfusion/gofusion/common/utils"
    11  	"github.com/wfusion/gofusion/common/utils/clone"
    12  	"github.com/wfusion/gofusion/log"
    13  
    14  	testUtl "github.com/wfusion/gofusion/test/common/utils"
    15  )
    16  
    17  func TestClone(t *testing.T) {
    18  	t.Parallel()
    19  	testingSuite := &Clone{Test: new(testUtl.Test)}
    20  	suite.Run(t, testingSuite)
    21  }
    22  
    23  type Clone struct {
    24  	*testUtl.Test
    25  }
    26  
    27  func (t *Clone) BeforeTest(suiteName, testName string) {
    28  	t.Catch(func() {
    29  		log.Info(context.Background(), "right before %s %s", suiteName, testName)
    30  	})
    31  }
    32  
    33  func (t *Clone) AfterTest(suiteName, testName string) {
    34  	t.Catch(func() {
    35  		log.Info(context.Background(), "right after %s %s", suiteName, testName)
    36  	})
    37  }
    38  
    39  func (t *Clone) TestUnsafeAny() {
    40  	t.Catch(func() {
    41  		type cases struct {
    42  			name string
    43  			src  any
    44  		}
    45  		given := []cases{
    46  			{
    47  				name: "basic",
    48  				src: struct {
    49  					Str       string
    50  					Int       int
    51  					Int64     int64
    52  					Uint      uint
    53  					Uint64    uint64
    54  					Float64   float64
    55  					Complex64 complex64
    56  					Bool      bool
    57  					Now       time.Time
    58  					Loc       *time.Location
    59  				}{
    60  					Str:       "1",
    61  					Int:       1,
    62  					Int64:     1,
    63  					Uint:      1,
    64  					Uint64:    1,
    65  					Float64:   1,
    66  					Complex64: complex64(1),
    67  					Bool:      true,
    68  					Now:       time.Now(),
    69  					Loc:       utils.Must(time.LoadLocation("Asia/Shanghai")),
    70  				},
    71  			},
    72  		}
    73  
    74  		for _, cs := range given {
    75  			t.Run(cs.name, func() {
    76  				dst := clone.Clone(cs.src)
    77  				t.Require().EqualValues(cs.src, dst)
    78  			})
    79  		}
    80  	})
    81  }
    82  
    83  func (t *Clone) TestUnsafe() {
    84  	t.Catch(func() {
    85  		type cases struct {
    86  			Name string
    87  			name string
    88  			src  any
    89  			Src  any
    90  		}
    91  		cs := cases{Name: "public name", name: "private name", src: 123, Src: 321}
    92  
    93  		dst := clone.Clone(cs)
    94  		t.Require().EqualValues(cs, dst)
    95  	})
    96  }
    97  
    98  func (t *Clone) TestBase() {
    99  	t.Catch(func() {
   100  
   101  	})
   102  }