github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/easy/utils_test.go (about)

     1  package easy
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  type testObject struct {
    10  	A int
    11  	B string
    12  }
    13  
    14  func TestSetDefault(t *testing.T) {
    15  	intValues := []any{int(1), int32(1), uint16(1), uint64(1)}
    16  	for _, value := range intValues {
    17  		var tmp int16
    18  		SetDefault(&tmp, value)
    19  		assert.Equal(t, int16(1), tmp)
    20  	}
    21  
    22  	var ptr *testObject
    23  	var tmp = &testObject{A: 1, B: "b"}
    24  	SetDefault(&ptr, tmp)
    25  	assert.Equal(t, testObject{A: 1, B: "b"}, *ptr)
    26  	assert.Equal(t, tmp, ptr)
    27  }
    28  
    29  func TestSetDefault_ShouldPanic(t *testing.T) {
    30  	var ptr *testObject
    31  	var tmp = &testObject{A: 1, B: "b"}
    32  
    33  	err := Safe(func() {
    34  		SetDefault(ptr, tmp)
    35  	})()
    36  	assert.NotNil(t, err)
    37  	assert.Contains(t, err.Error(), "SetDefault")
    38  	assert.Contains(t, err.Error(), "must be a non-nil pointer")
    39  }
    40  
    41  func TestCaller(t *testing.T) {
    42  	name, file, line := Caller(0)
    43  	assert.Equal(t, "easy.TestCaller", name)
    44  	assert.Equal(t, "easy/utils_test.go", file)
    45  	assert.Equal(t, 42, line)
    46  }
    47  
    48  func TestCallerName(t *testing.T) {
    49  	name := CallerName()
    50  	assert.Equal(t, "easy.TestCallerName", name)
    51  }
    52  
    53  func TestSingleJoin(t *testing.T) {
    54  	text := []string{"a", "b..", "..c"}
    55  	got := SingleJoin("..", text...)
    56  	want := "a..b..c"
    57  	assert.Equal(t, want, got)
    58  }
    59  
    60  func TestSlashJoin(t *testing.T) {
    61  	got0 := SlashJoin()
    62  	assert.Equal(t, "", got0)
    63  
    64  	path1 := []string{"/a", "b", "c.png"}
    65  	want1 := "/a/b/c.png"
    66  	got1 := SlashJoin(path1...)
    67  	assert.Equal(t, want1, got1)
    68  
    69  	path2 := []string{"/a/", "b/", "/c.png"}
    70  	want2 := "/a/b/c.png"
    71  	got2 := SlashJoin(path2...)
    72  	assert.Equal(t, want2, got2)
    73  }