github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/pkg/avatar/service_test.go (about)

     1  package avatar
     2  
     3  import (
     4  	"math/rand"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestGetInitials(t *testing.T) {
    13  	assert.Equal(t, "?", getInitials("  "))
    14  	assert.Equal(t, "P", getInitials("Pierre"))
    15  	assert.Equal(t, "FP", getInitials("François Pignon"))
    16  	assert.Equal(t, "П", getInitials("Пьер"))
    17  	assert.Equal(t, "ÉC", getInitials("Éric de la Composition"))
    18  }
    19  
    20  func TestGetColor(t *testing.T) {
    21  	rand.Seed(time.Now().UnixNano())
    22  
    23  	assert.Equal(t, colors[0], getColor(""))
    24  
    25  	for i := 0; i < 100; i++ {
    26  		str := randomString()
    27  		color := getColor(str)
    28  		assert.Len(t, color, 7)
    29  		assert.Equal(t, "#", color[0:1])
    30  	}
    31  }
    32  
    33  var chars = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ" +
    34  	"abcdefghijklmnopqrstuvwxyzåäö" +
    35  	"0123456789")
    36  
    37  func randomString() string {
    38  	length := 1 + rand.Intn(16)
    39  	var b strings.Builder
    40  	for i := 0; i < length; i++ {
    41  		b.WriteRune(chars[rand.Intn(len(chars))])
    42  	}
    43  	return b.String()
    44  }