add more test cases and fix typo

Signed-off-by: Tim Ramlot <42113979+inteon@users.noreply.github.com>
This commit is contained in:
Tim Ramlot 2023-09-22 12:44:52 +02:00
parent 860df2294b
commit eac230f93e
No known key found for this signature in database
GPG Key ID: 47428728E0C2878D
2 changed files with 21 additions and 1 deletions

View File

@ -82,7 +82,7 @@ func ComputeSecureUniqueDeterministicNameFromData(fullName string, maxNameLength
return fmt.Sprintf("%s-%08x", prefix, hashResult), nil
}
// DNSSafeShortenToNCharacters shortens the input string to 52 chars and ensures the last char is an alpha-numeric character.
// DNSSafeShortenToNCharacters shortens the input string to N chars and ensures the last char is an alpha-numeric character.
func DNSSafeShortenToNCharacters(in string, maxLength int) string {
var alphaNumeric = regexp.MustCompile(`[a-zA-Z\d]`)

View File

@ -122,6 +122,26 @@ func TestDNSSafeShortenToNCharacters(t *testing.T) {
}
tests := []testcase{
{
in: "aaaaaaaaaaaaaaa",
maxLength: 0,
expOut: "",
},
{
in: "aa-----aaaa",
maxLength: 5,
expOut: "aa",
},
{
in: "aa11111aaaa",
maxLength: 5,
expOut: "aa111",
},
{
in: "aaAAAAAaaaa",
maxLength: 5,
expOut: "aaAAA",
},
{
in: "aaaaaaaaaaaaaaa",
maxLength: 3,