github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/examples/boilerplate/slice_namesbyRole_old_test.go (about)

     1  package boilerplate
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func Test_NamesByRole_Old(t *testing.T) {
    11  
    12  	var namesByRole = map[string][]string{}
    13  	add := func(role string, u User) {
    14  		namesByRole[role] = append(namesByRole[role], u.Name())
    15  	}
    16  	for _, u := range users {
    17  		if roles := u.Roles(); len(roles) == 0 {
    18  			add("", u)
    19  		} else {
    20  			for _, r := range roles {
    21  				add(strings.ToLower(r.Name()), u)
    22  			}
    23  		}
    24  	}
    25  	//map[:[Tom] admin:[Bob] manager:[Bob Alice]]
    26  
    27  	assert.Equal(t, namesByRole[""], []string{"Tom"})
    28  	assert.Equal(t, namesByRole["manager"], []string{"Bob", "Alice"})
    29  	assert.Equal(t, namesByRole["admin"], []string{"Bob"})
    30  
    31  }