podterminal/utils/randomString_test.go

63 lines
1.2 KiB
Go
Raw Normal View History

2024-01-27 11:20:40 +01:00
package utils
import (
"math"
"testing"
)
func TestRandomString(t *testing.T) {
t.Run("Test valid length", func(t *testing.T) {
length := 10
result, err := RandomString(length)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
if len(result) != length {
t.Errorf("Expected a string of length %d, but got: %d", length, len(result))
}
})
t.Run("Test zero length", func(t *testing.T) {
length := 0
result, err := RandomString(length)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
if len(result) != length {
t.Errorf("Expected an empty string, but got: %s", result)
}
})
t.Run("Test negative length", func(t *testing.T) {
length := -10
result, err := RandomString(length)
if err == nil {
t.Errorf("Expected an error, but got none")
}
if result != "" {
t.Errorf("Expected an empty string, but got: %s", result)
}
})
t.Run("Test max Int32 Length", func(t *testing.T) {
length := math.MaxInt32
result, err := RandomString(length)
if err != nil {
t.Errorf("Expected no error, but got: %v", err)
}
if len(result) != length {
t.Errorf("Expected an empty string, but got: %s", result)
}
})
}