github.com/0chain/gosdk@v1.17.11/core/pathutil/path_test.go (about)

     1  package pathutil
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestSplit(t *testing.T) {
    10  
    11  	tests := []struct {
    12  		name string
    13  		path string
    14  		dir  string
    15  		file string
    16  	}{
    17  		{
    18  			name: "empty",
    19  			path: "",
    20  			dir:  "",
    21  			file: "",
    22  		},
    23  		{
    24  			name: "only file",
    25  			path: "file",
    26  			dir:  "",
    27  			file: "file",
    28  		},
    29  		{
    30  			name: "only dir",
    31  			path: "/dir/",
    32  			dir:  "/dir",
    33  			file: "",
    34  		},
    35  		{
    36  			name: "only root",
    37  			path: "/",
    38  			dir:  "/",
    39  			file: "",
    40  		},
    41  	}
    42  
    43  	for _, test := range tests {
    44  		t.Run(test.name, func(t *testing.T) {
    45  			dir, file := Split(test.path)
    46  			require.Equal(t, test.dir, dir)
    47  			require.Equal(t, test.file, file)
    48  		})
    49  	}
    50  }
    51  
    52  func TestJoin(t *testing.T) {
    53  	tests := []struct {
    54  		name string
    55  		elem []string
    56  		path string
    57  	}{
    58  		{name: "empty", elem: []string{"", ""}, path: ""},
    59  		{name: "only slash", elem: []string{"/", "/"}, path: "/"},
    60  		{name: "multiple slashes", elem: []string{"/", "/images"}, path: "/images"},
    61  		{name: "one path without slash", elem: []string{"only_path"}, path: "/only_path"},
    62  		{name: "multiple paths without slash", elem: []string{"path1", "path2"}, path: "/path1/path2"},
    63  		{name: "multiple paths", elem: []string{"path1", "path2", "/path3", "path4/"}, path: "/path1/path2/path3/path4"},
    64  	}
    65  
    66  	for _, test := range tests {
    67  		t.Run(test.name, func(t *testing.T) {
    68  			path := Join(test.elem...)
    69  
    70  			require.Equal(t, test.path, path)
    71  		})
    72  	}
    73  }