Add tests for role sorting
MODIFY
discordutil/users_test.go
discordutil/users_test.go
package discordutil
import (
+ "reflect"
"testing"
"github.com/Bios-Marcel/discordgo"
@@ -143,3 +144,135 @@ func TestGetMemberName(t *testing.T) {
})
}
}
+
+func TestSortUserRoles(t *testing.T) {
+ type args struct {
+ roles []string
+ guildRoles []*discordgo.Role
+ }
+ tests := []struct {
+ name string
+ args args
+ expected []string
+ }{
+ {
+ name: "empty",
+ args: args{
+ roles: []string{},
+ guildRoles: []*discordgo.Role{},
+ },
+ expected: []string{},
+ }, {
+ name: "one role without discord roles in the state",
+ args: args{
+ roles: []string{"lol"},
+ guildRoles: []*discordgo.Role{},
+ },
+ expected: []string{"lol"},
+ }, {
+ name: "two roles without discord roles in the state",
+ args: args{
+ roles: []string{"a", "b"},
+ guildRoles: []*discordgo.Role{},
+ },
+ expected: []string{"a", "b"},
+ }, {
+ name: "one role that's in the state",
+ args: args{
+ roles: []string{"a"},
+ guildRoles: []*discordgo.Role{
+ &discordgo.Role{ID: "a"},
+ },
+ },
+ expected: []string{"a"},
+ }, {
+ name: "two roles that's in the state",
+ args: args{
+ roles: []string{"a", "b"},
+ guildRoles: []*discordgo.Role{
+ &discordgo.Role{
+ ID: "a",
+ Position: 0,
+ },
+ &discordgo.Role{
+ ID: "b",
+ Position: 1,
+ },
+ },
+ },
+ expected: []string{"b", "a"},
+ }, {
+ name: "three roles that's in the state",
+ args: args{
+ roles: []string{"a", "b", "c"},
+ guildRoles: []*discordgo.Role{
+ &discordgo.Role{
+ ID: "a",
+ Position: 0,
+ },
+ &discordgo.Role{
+ ID: "b",
+ Position: 1,
+ },
+ &discordgo.Role{
+ ID: "c",
+ Position: 2,
+ },
+ },
+ },
+ expected: []string{"c", "b", "a"},
+ }, {
+ name: "three roles that's in the state; already correct",
+ args: args{
+ roles: []string{"a", "b", "c"},
+ guildRoles: []*discordgo.Role{
+ &discordgo.Role{
+ ID: "a",
+ Position: 4,
+ },
+ &discordgo.Role{
+ ID: "b",
+ Position: 2,
+ },
+ &discordgo.Role{
+ ID: "c",
+ Position: 0,
+ },
+ },
+ },
+ expected: []string{"a", "b", "c"},
+ }, {
+ name: "roles with same positions should be preserved in order",
+ args: args{
+ roles: []string{"a", "b", "c", "d"},
+ guildRoles: []*discordgo.Role{
+ &discordgo.Role{
+ ID: "a",
+ Position: 0,
+ },
+ &discordgo.Role{
+ ID: "b",
+ Position: 2,
+ },
+ &discordgo.Role{
+ ID: "c",
+ Position: 2,
+ },
+ &discordgo.Role{
+ ID: "d",
+ Position: 4,
+ },
+ },
+ },
+ expected: []string{"d", "b", "c", "a"},
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ SortUserRoles(tt.args.roles, tt.args.guildRoles)
+ if !reflect.DeepEqual(tt.args.roles, tt.expected) {
+ t.Errorf("Expected %v, but was %v", tt.expected, tt.args.roles)
+ }
+ })
+ }
+}

Add tests for role sorting