github.com/vshn/k8ify@v1.1.2-0.20240502214202-6c9ed3ef0bf4/pkg/util/stringutils_test.go (about)

     1  package util_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/vshn/k8ify/pkg/util"
     8  )
     9  
    10  func TestSanitize(t *testing.T) {
    11  	assert.Equal(t, "foo", util.Sanitize("foo"))
    12  	assert.Equal(t, "feat-foo1", util.Sanitize("feat/foo1"))
    13  	assert.Equal(t, "test", util.Sanitize("1test"))
    14  	assert.Equal(t, "feat-foo1", util.Sanitize("/feat/foo1"))
    15  	assert.Equal(t, "leading", util.Sanitize("-leading"))
    16  	assert.Equal(t, "leading", util.Sanitize("0-leading"))
    17  	assert.Equal(t, "trailing", util.Sanitize("trailing-"))
    18  	assert.Equal(t, "trailing", util.Sanitize("trailing--"))
    19  	assert.Equal(t, "both", util.Sanitize("--both--"))
    20  }
    21  
    22  func TestSanitiziWithMinLength(t *testing.T) {
    23  	assert.Equal(t, "foobar", util.SanitizeWithMinLength("foobar", 6))
    24  	assert.Equal(t, "fr54k9", util.SanitizeWithMinLength("foo", 6))
    25  }
    26  
    27  func TestByteArrayToAlpha(t *testing.T) {
    28  	assert.Equal(t, "", util.ByteArrayToAlphaNum([]byte{}))
    29  	assert.Equal(t, "aa", util.ByteArrayToAlphaNum([]byte{0}))
    30  	assert.Equal(t, "ba", util.ByteArrayToAlphaNum([]byte{1}))
    31  	assert.Equal(t, "za", util.ByteArrayToAlphaNum([]byte{25}))
    32  	assert.Equal(t, "ab", util.ByteArrayToAlphaNum([]byte{26}))
    33  	assert.Equal(t, "zb", util.ByteArrayToAlphaNum([]byte{51}))
    34  	assert.Equal(t, "ac", util.ByteArrayToAlphaNum([]byte{52}))
    35  	assert.Equal(t, "bc", util.ByteArrayToAlphaNum([]byte{53}))
    36  	assert.Equal(t, "a0aa", util.ByteArrayToAlphaNum([]byte{2, 164}))
    37  	assert.Equal(t, "a9aa", util.ByteArrayToAlphaNum([]byte{3, 142}))
    38  	assert.Equal(t, "z9aa", util.ByteArrayToAlphaNum([]byte{3, 167}))
    39  	assert.Equal(t, "aaba", util.ByteArrayToAlphaNum([]byte{3, 168}))
    40  	assert.Equal(t, "faaaaaaaaaaaa", util.ByteArrayToAlphaNum([]byte{0, 0, 0, 0, 0, 0, 0, 5}))
    41  	assert.Equal(t, "aaabacad", util.ByteArrayToAlphaNum([]byte{39, 141, 108, 23, 160}))
    42  	maxHashValue := make([]byte, 64)
    43  	for i := 0; i < 64; i++ {
    44  		maxHashValue[i] = 255
    45  	}
    46  	assert.Equal(t, "vjyw9istw1eo0gijxmt8ogd0ittw5jzslzuqb70zyf4aes4kc2j913cu0sjpfva7hytf3gk486srdn7wpyrtoesukpqzoilrknub", util.ByteArrayToAlphaNum(maxHashValue))
    47  }