github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/path/join_test.go (about)

     1  package path_test
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/lmorg/murex/test/count"
     9  	"github.com/lmorg/murex/utils/consts"
    10  	"github.com/lmorg/murex/utils/home"
    11  	"github.com/lmorg/murex/utils/json"
    12  	"github.com/lmorg/murex/utils/path"
    13  )
    14  
    15  func TestJoin(t *testing.T) {
    16  	slash := consts.PathSlash
    17  
    18  	tests := []struct {
    19  		Slice    []string
    20  		Expected string
    21  	}{
    22  		{
    23  			Slice:    []string{slash},
    24  			Expected: slash,
    25  		},
    26  		{
    27  			Slice:    []string{"foo", "bar"},
    28  			Expected: fmt.Sprintf("foo%sbar", slash),
    29  		},
    30  		{
    31  			Slice:    []string{"/", "foo", "bar"},
    32  			Expected: fmt.Sprintf("%sfoo%sbar", slash, slash),
    33  		},
    34  		{
    35  			Slice:    []string{".", "foo", "bar"},
    36  			Expected: fmt.Sprintf(".%sfoo%sbar", slash, slash),
    37  		},
    38  		{
    39  			Slice:    strings.Split(home.MyDir, slash),
    40  			Expected: home.MyDir + slash,
    41  		},
    42  		{
    43  			Slice:    strings.Split(home.MyDir, slash)[1:],
    44  			Expected: home.MyDir[1:],
    45  		},
    46  	}
    47  
    48  	count.Tests(t, len(tests))
    49  
    50  	for i, test := range tests {
    51  		actual := path.Join(test.Slice)
    52  
    53  		if test.Expected != actual {
    54  			t.Errorf("expected != actual in test %d", i)
    55  			t.Logf("  Slice:    %s", json.LazyLogging(test.Slice))
    56  			t.Logf("  Expected: '%s'", test.Expected)
    57  			t.Logf("  Actual:   '%s'", actual)
    58  		}
    59  	}
    60  }