github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zutil/options_test.go (about)

     1  //go:build go1.18
     2  // +build go1.18
     3  
     4  package zutil_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/sohaha/zlsgo"
    10  	"github.com/sohaha/zlsgo/zutil"
    11  )
    12  
    13  func TestOptional(t *testing.T) {
    14  	tt := zlsgo.NewTest(t)
    15  
    16  	o := zutil.Optional(TestSt{Name: "test"})
    17  	tt.Equal("test", o.Name)
    18  	tt.Equal(0, o.I)
    19  
    20  	o = zutil.Optional(TestSt{Name: "test2"}, func(o *TestSt) {
    21  		o.I = 1
    22  	}, func(ts *TestSt) {
    23  		ts.I = ts.I + 1
    24  	})
    25  	tt.Equal("test2", o.Name)
    26  	tt.Equal(2, o.I)
    27  
    28  	o2 := zutil.Optional(&TestSt{Name: "test"}, func(ts **TestSt) {
    29  		(*ts).I = 1
    30  	})
    31  	tt.Equal("test", o2.Name)
    32  	tt.Equal(1, o2.I)
    33  }