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

     1  package alter
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lmorg/murex/test/count"
     7  )
     8  
     9  type test struct {
    10  	path     string
    11  	expected []string
    12  }
    13  
    14  func TestSplitPath(t *testing.T) {
    15  	tests := []test{
    16  		{
    17  			path:     "/",
    18  			expected: []string{""},
    19  		},
    20  
    21  		{
    22  			path:     "/a/b/c",
    23  			expected: []string{"a", "b", "c"},
    24  		},
    25  		{
    26  			path:     "/aaa/bbb/ccc",
    27  			expected: []string{"aaa", "bbb", "ccc"},
    28  		},
    29  		{
    30  			path:     "/1/2/3",
    31  			expected: []string{"1", "2", "3"},
    32  		},
    33  
    34  		{
    35  			path:     ".a.b.c",
    36  			expected: []string{"a", "b", "c"},
    37  		},
    38  		{
    39  			path:     ".aaa.bbb.ccc",
    40  			expected: []string{"aaa", "bbb", "ccc"},
    41  		},
    42  		{
    43  			path:     ".1.2.3",
    44  			expected: []string{"1", "2", "3"},
    45  		},
    46  
    47  		{
    48  			path:     "-a-b-c",
    49  			expected: []string{"a", "b", "c"},
    50  		},
    51  		{
    52  			path:     "-aaa-bbb-ccc",
    53  			expected: []string{"aaa", "bbb", "ccc"},
    54  		},
    55  		{
    56  			path:     "-1-2-3",
    57  			expected: []string{"1", "2", "3"},
    58  		},
    59  
    60  		{
    61  			path:     "1a1b1c",
    62  			expected: []string{"a", "b", "c"},
    63  		},
    64  		{
    65  			path:     "1aaa1bbb1ccc",
    66  			expected: []string{"aaa", "bbb", "ccc"},
    67  		},
    68  		{
    69  			path:     "111213",
    70  			expected: []string{"", "", "2", "3"},
    71  		},
    72  	}
    73  
    74  	count.Tests(t, len(tests))
    75  
    76  	for i := range tests {
    77  		split, err := SplitPath(tests[i].path)
    78  		if err != nil {
    79  			t.Error("SplitPath raised an error")
    80  			t.Logf("  index:    %d", i)
    81  			t.Logf("  path:     %s", tests[i].path)
    82  			t.Logf("  expected: %v (%d)", tests[i].expected, len(tests[i].expected))
    83  			t.Logf("  actual:   %v (%d)", split, len(split))
    84  		}
    85  
    86  		if len(split) != len(tests[i].expected) {
    87  			t.Error("SplitPath returned a different length slice to expected")
    88  			t.Logf("  index:    %d", i)
    89  			t.Logf("  path:     %s", tests[i].path)
    90  			t.Logf("  expected: %v (%d)", tests[i].expected, len(tests[i].expected))
    91  			t.Logf("  actual:   %v (%d)", split, len(split))
    92  		}
    93  
    94  		for j := range split {
    95  			if split[j] != tests[i].expected[j] {
    96  				t.Error("SplitPath returned a different slice to expected")
    97  				t.Logf("  index:      %d", i)
    98  				t.Logf("  path:       %s", tests[i].path)
    99  				t.Logf("  expected:   %v (%d)", tests[i].expected, len(tests[i].expected))
   100  				t.Logf("  actual:     %v (%d)", split, len(split))
   101  				t.Logf(`  diff value: "%s" != "%s"`, split[j], tests[i].expected[j])
   102  				t.Logf(`  expected:   "%s"`, tests[i].expected[j])
   103  				t.Logf(`  actual:     "%s"`, split[j])
   104  				t.Logf("  diff index: %d", j)
   105  			}
   106  		}
   107  
   108  	}
   109  }