github.com/jxskiss/gopkg@v0.17.3/easy/utils_test.go (about) 1 package easy 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 ) 7 8 func TestSetDefault(t *testing.T) { 9 intValues := []interface{}{int(1), int32(1), uint16(1), uint64(1)} 10 for _, value := range intValues { 11 var tmp int16 12 SetDefault(&tmp, value) 13 assert.Equal(t, int16(1), tmp) 14 } 15 16 var ptr *testObject 17 var tmp = &testObject{A: 1, B: "b"} 18 SetDefault(&ptr, tmp) 19 assert.Equal(t, testObject{A: 1, B: "b"}, *ptr) 20 assert.Equal(t, tmp, ptr) 21 } 22 23 func TestSetDefault_ShouldPanic(t *testing.T) { 24 var ptr *testObject 25 var tmp = &testObject{A: 1, B: "b"} 26 27 err := Safe(func() { 28 SetDefault(ptr, tmp) 29 })() 30 assert.NotNil(t, err) 31 assert.Contains(t, err.Error(), "SetDefault") 32 assert.Contains(t, err.Error(), "must be a non-nil pointer") 33 }