diff --git a/disroute_test.go b/disroute_test.go index 5e0f7f3..2320d7a 100644 --- a/disroute_test.go +++ b/disroute_test.go @@ -23,59 +23,40 @@ var ( } ) -func Test_RegisterAll_Errors(t *testing.T) { - r := disroute.New() - - // Testing for the case when cmds is empty - err := r.RegisterAll([]*disroute.Cmd{}) - if err != nil { - t.Errorf("Expected nil error, got %v", err) - } - - // Testing for the case when a cmd has no handler and no subcommands - err = r.RegisterAll([]*disroute.Cmd{{Path: "test"}}) - if err == nil { - t.Error("Expected error, got nil") - } - - // Testing for the case when a subcommand has no handler - err = r.RegisterAll([]*disroute.Cmd{{Path: "test", Options: []*disroute.CmdOption{{Type: disroute.TypeSubcommand}}}}) - if err == nil { - t.Error("Expected error, got nil") - } - - // Testing for the case when a subcommand group has a subcommand with nil handler - err = r.RegisterAll([]*disroute.Cmd{{Path: "test", Options: []*disroute.CmdOption{{Type: disroute.TypeSubcommandGroup, Options: []*disroute.CmdOption{{Type: disroute.TypeSubcommand}}}}}}) - if err == nil { - t.Error("Expected error, got nil") - } +type table struct { + cmds []*disroute.Cmd + interactions []*discordgo.InteractionCreate } -func Test_RegisterAll_SingleCmd(t *testing.T) { - r := disroute.New() - +func getSingleCmd(iType discordgo.InteractionType) table { cmds := []*disroute.Cmd{ { Path: "cmd", Handlers: disroute.Handlers{ - Cmd: EmptyHandler, + Cmd: EmptyHandler, + Autocomplete: EmptyHandler, }, }, } - err := r.RegisterAll(cmds) - if err != nil { - t.Errorf("Expected nil error, got %v", err) + i := []*discordgo.InteractionCreate{ + { + Interaction: &discordgo.Interaction{ + Type: iType, + Data: discordgo.ApplicationCommandInteractionData{ + Name: "cmd", + }, + }, + }, } - if len(r.GetAll()) != 1 { - t.Errorf("Expected 1 command, got %d", len(r.GetAll())) + return table{ + cmds: cmds, + interactions: i, } } -func Test_RegisterAll_Subcommands(t *testing.T) { - r := disroute.New() - +func getSubcommandCmd(iType discordgo.InteractionType) table { cmds := []*disroute.Cmd{ { Path: "sub", @@ -84,33 +65,45 @@ func Test_RegisterAll_Subcommands(t *testing.T) { Path: "cmd", Type: disroute.TypeSubcommand, Handlers: disroute.Handlers{ - Cmd: EmptyHandler, + Cmd: EmptyHandler, + Autocomplete: EmptyHandler, }, }, { Path: "cmd2", Type: disroute.TypeSubcommand, Handlers: disroute.Handlers{ - Cmd: EmptyHandler, + Cmd: EmptyHandler, + Autocomplete: EmptyHandler, }, }, }, }, } - - err := r.RegisterAll(cmds) - if err != nil { - t.Errorf("Expected nil error, got %v", err) + i := []*discordgo.InteractionCreate{ + { + Interaction: &discordgo.Interaction{ + Type: iType, + Data: discordgo.ApplicationCommandInteractionData{ + Name: "sub", + Options: []*discordgo.ApplicationCommandInteractionDataOption{ + { + Name: "cmd", + Type: discordgo.ApplicationCommandOptionSubCommand, + }, + }, + }, + }, + }, } - if len(r.GetAll()) != 2 { - t.Errorf("Expected 2 commands, got %d", len(r.GetAll())) + return table{ + cmds: cmds, + interactions: i, } } -func Test_RegisterAll_SubcommandGroups(t *testing.T) { - r := disroute.New() - +func getSubcommandGroupCmd(iType discordgo.InteractionType) table { cmds := []*disroute.Cmd{ { Path: "gr-sub", @@ -123,31 +116,58 @@ func Test_RegisterAll_SubcommandGroups(t *testing.T) { Path: "cmd", Type: disroute.TypeSubcommand, Handlers: disroute.Handlers{ - Cmd: EmptyHandler, + Cmd: EmptyHandler, + Autocomplete: EmptyHandler, }, }, { Path: "cmd2", Type: disroute.TypeSubcommand, Handlers: disroute.Handlers{ - Cmd: EmptyHandler, + Cmd: EmptyHandler, + Autocomplete: EmptyHandler, }, }, + { + Path: "non_subcommand", + Type: discordgo.ApplicationCommandOptionBoolean, + }, }, }, }, }, } - err := r.RegisterAll(cmds) - if err != nil { - t.Error(err) + i := []*discordgo.InteractionCreate{ + { + Interaction: &discordgo.Interaction{ + Type: iType, + Data: discordgo.ApplicationCommandInteractionData{ + Name: "gr-sub", + Options: []*discordgo.ApplicationCommandInteractionDataOption{ + { + Name: "gr", + Type: discordgo.ApplicationCommandOptionSubCommandGroup, + Options: []*discordgo.ApplicationCommandInteractionDataOption{ + { + Name: "cmd", + Type: discordgo.ApplicationCommandOptionSubCommand, + }, + }, + }, + }, + }, + }, + }, } -} -func Test_RegisterAll_MixedSubcommands(t *testing.T) { - r := disroute.New() + return table{ + cmds: cmds, + interactions: i, + } +} +func getMixedCmd(iType discordgo.InteractionType) table { cmds := []*disroute.Cmd{ { Path: "gr-sub", @@ -160,14 +180,16 @@ func Test_RegisterAll_MixedSubcommands(t *testing.T) { Path: "cmd", Type: disroute.TypeSubcommand, Handlers: disroute.Handlers{ - Cmd: EmptyHandler, + Cmd: EmptyHandler, + Autocomplete: EmptyHandler, }, }, { Path: "cmd2", Type: disroute.TypeSubcommand, Handlers: disroute.Handlers{ - Cmd: EmptyHandler, + Cmd: EmptyHandler, + Autocomplete: EmptyHandler, }, }, }, @@ -176,14 +198,132 @@ func Test_RegisterAll_MixedSubcommands(t *testing.T) { Path: "cmd", Type: disroute.TypeSubcommand, Handlers: disroute.Handlers{ - Cmd: EmptyHandler, + Cmd: EmptyHandler, + Autocomplete: EmptyHandler, }, }, }, }, } - err := r.RegisterAll(cmds) + i := []*discordgo.InteractionCreate{ + { + Interaction: &discordgo.Interaction{ + Type: iType, + Data: discordgo.ApplicationCommandInteractionData{ + Name: "gr-sub", + Options: []*discordgo.ApplicationCommandInteractionDataOption{ + { + Name: "cmd", + Type: discordgo.ApplicationCommandOptionSubCommand, + }, + }, + }, + }, + }, + { + Interaction: &discordgo.Interaction{ + Type: iType, + Data: discordgo.ApplicationCommandInteractionData{ + Name: "gr-sub", + Options: []*discordgo.ApplicationCommandInteractionDataOption{ + { + Name: "gr", + Type: discordgo.ApplicationCommandOptionSubCommandGroup, + Options: []*discordgo.ApplicationCommandInteractionDataOption{ + { + Name: "cmd", + Type: discordgo.ApplicationCommandOptionSubCommand, + }, + }, + }, + }, + }, + }, + }, + } + + return table{ + cmds: cmds, + interactions: i, + } +} + +func Test_RegisterAll_Errors(t *testing.T) { + r := disroute.New() + + // Testing for the case when cmds is empty + err := r.RegisterAll([]*disroute.Cmd{}) + if err != nil { + t.Errorf("Expected nil error, got %v", err) + } + + // Testing for the case when a cmd has no handler and no subcommands + err = r.RegisterAll([]*disroute.Cmd{{Path: "test"}}) + if err == nil { + t.Error("Expected error, got nil") + } + + // Testing for the case when a subcommand has no handler + err = r.RegisterAll([]*disroute.Cmd{{Path: "test", Options: []*disroute.CmdOption{{Type: disroute.TypeSubcommand}}}}) + if err == nil { + t.Error("Expected error, got nil") + } + + // Testing for the case when a subcommand group has a subcommand with nil handler + err = r.RegisterAll([]*disroute.Cmd{{Path: "test", Options: []*disroute.CmdOption{{Type: disroute.TypeSubcommandGroup, Options: []*disroute.CmdOption{{Type: disroute.TypeSubcommand}}}}}}) + if err == nil { + t.Error("Expected error, got nil") + } +} + +func Test_RegisterAll_SingleCmd(t *testing.T) { + r := disroute.New() + + cases := getSingleCmd(0) + + err := r.RegisterAll(cases.cmds) + if err != nil { + t.Errorf("Expected nil error, got %v", err) + } + + if len(r.GetAll()) != 1 { + t.Errorf("Expected 1 command, got %d", len(r.GetAll())) + } +} + +func Test_RegisterAll_Subcommands(t *testing.T) { + r := disroute.New() + + cases := getSubcommandCmd(0) + + err := r.RegisterAll(cases.cmds) + if err != nil { + t.Errorf("Expected nil error, got %v", err) + } + + if len(r.GetAll()) != 2 { + t.Errorf("Expected 2 commands, got %d", len(r.GetAll())) + } +} + +func Test_RegisterAll_SubcommandGroups(t *testing.T) { + r := disroute.New() + + cases := getSubcommandGroupCmd(0) + + err := r.RegisterAll(cases.cmds) + if err != nil { + t.Error(err) + } +} + +func Test_RegisterAll_MixedSubcommands(t *testing.T) { + r := disroute.New() + + cases := getMixedCmd(0) + + err := r.RegisterAll(cases.cmds) if err != nil { t.Error(err) } @@ -271,125 +411,53 @@ func Test_FindAndExecute_Errors(t *testing.T) { func Test_FindAndExecute_SingleCmd(t *testing.T) { r := disroute.New() - cmds := []*disroute.Cmd{ - { - Path: "cmd", - Handlers: disroute.Handlers{ - Cmd: EmptyHandler, - }, - }, - } + cases := getSingleCmd(disroute.TypeCommand) - err := r.RegisterAll(cmds) + err := r.RegisterAll(cases.cmds) if err != nil { t.Errorf("Expected nil error, got %v", err) } - _, err = r.FindAndExecute(&discordgo.InteractionCreate{ - Interaction: &discordgo.Interaction{ - Type: discordgo.InteractionApplicationCommand, - Data: discordgo.ApplicationCommandInteractionData{ - Name: "cmd", - }, - }, - }) - if err != nil { - t.Error("Expected nil error, got", err) + for _, i := range cases.interactions { + _, err = r.FindAndExecute(i) + if err != nil { + t.Error("Expected nil error, got", err) + } } } func Test_FindAndExecute_Subcommand(t *testing.T) { r := disroute.New() - cmds := []*disroute.Cmd{ - { - Path: "cmd3", - Options: []*disroute.CmdOption{ - { - Path: "sub", - Type: disroute.TypeSubcommand, - Handlers: disroute.Handlers{ - Cmd: EmptyHandler, - }, - }, - }, - }, - } + cases := getSubcommandCmd(disroute.TypeCommand) - err := r.RegisterAll(cmds) + err := r.RegisterAll(cases.cmds) if err != nil { t.Errorf("Expected nil error, got %v", err) } - _, err = r.FindAndExecute(&discordgo.InteractionCreate{ - Interaction: &discordgo.Interaction{ - Type: discordgo.InteractionApplicationCommand, - Data: discordgo.ApplicationCommandInteractionData{ - Name: "cmd3", - Options: []*discordgo.ApplicationCommandInteractionDataOption{ - { - Name: "sub", - Type: discordgo.ApplicationCommandOptionSubCommand, - }, - }, - }, - }, - }) - if err != nil { - t.Error("Expected nil error, got", err) + for _, i := range cases.interactions { + _, err = r.FindAndExecute(i) + if err != nil { + t.Error("Expected nil error, got", err) + } } } func Test_FindAndExecute_SubcommandGroup(t *testing.T) { r := disroute.New() - cmds := []*disroute.Cmd{ - { - Path: "cmd4", - Options: []*disroute.CmdOption{ - { - Path: "gr", - Type: disroute.TypeSubcommandGroup, - Options: []*disroute.CmdOption{ - { - Path: "sub2", - Type: disroute.TypeSubcommand, - Handlers: disroute.Handlers{ - Cmd: EmptyHandler, - }, - }, - }, - }, - }, - }, - } + cases := getSubcommandGroupCmd(disroute.TypeCommand) - err := r.RegisterAll(cmds) + err := r.RegisterAll(cases.cmds) if err != nil { t.Errorf("Expected nil error, got %v", err) } - _, err = r.FindAndExecute(&discordgo.InteractionCreate{ - Interaction: &discordgo.Interaction{ - Type: discordgo.InteractionApplicationCommand, - Data: discordgo.ApplicationCommandInteractionData{ - Name: "cmd4", - Options: []*discordgo.ApplicationCommandInteractionDataOption{ - { - Name: "gr", - Type: discordgo.ApplicationCommandOptionSubCommandGroup, - Options: []*discordgo.ApplicationCommandInteractionDataOption{ - { - Name: "sub2", - Type: discordgo.ApplicationCommandOptionSubCommand, - }, - }, - }, - }, - }, - }, - }) - if err != nil { - t.Error("Expected nil error, got", err) + for _, i := range cases.interactions { + _, err = r.FindAndExecute(i) + if err != nil { + t.Error("Expected nil error, got", err) + } } } func Test_FindAndAutocomplete_Errors(t *testing.T) { @@ -470,127 +538,52 @@ func Test_FindAndAutocomplete_Errors(t *testing.T) { func Test_FindAndAutocomplete_SingleCmd(t *testing.T) { r := disroute.New() - cmds := []*disroute.Cmd{ - { - Path: "cmd", - Handlers: disroute.Handlers{ - Cmd: EmptyHandler, - Autocomplete: EmptyHandler, - }, - }, - } + cases := getSingleCmd(disroute.TypeCommandAutocompletion) - err := r.RegisterAll(cmds) + err := r.RegisterAll(cases.cmds) if err != nil { t.Errorf("Expected nil error, got %v", err) } - _, err = r.FindAndAutocomplete(&discordgo.InteractionCreate{ - Interaction: &discordgo.Interaction{ - Type: discordgo.InteractionApplicationCommandAutocomplete, - Data: discordgo.ApplicationCommandInteractionData{ - Name: "cmd", - }, - }, - }) - if err != nil { - t.Error("Expected nil error, got", err) + for _, i := range cases.interactions { + _, err = r.FindAndAutocomplete(i) + if err != nil { + t.Error("Expected nil error, got", err) + } } } func Test_FindAndAutocomplete_Subcommand(t *testing.T) { r := disroute.New() - cmds := []*disroute.Cmd{ - { - Path: "cmd3", - Options: []*disroute.CmdOption{ - { - Path: "sub", - Type: disroute.TypeSubcommand, - Handlers: disroute.Handlers{ - Cmd: EmptyHandler, - Autocomplete: EmptyHandler, - }, - }, - }, - }, - } + cases := getSubcommandCmd(disroute.TypeCommandAutocompletion) - err := r.RegisterAll(cmds) + err := r.RegisterAll(cases.cmds) if err != nil { t.Errorf("Expected nil error, got %v", err) } - _, err = r.FindAndAutocomplete(&discordgo.InteractionCreate{ - Interaction: &discordgo.Interaction{ - Type: discordgo.InteractionApplicationCommandAutocomplete, - Data: discordgo.ApplicationCommandInteractionData{ - Name: "cmd3", - Options: []*discordgo.ApplicationCommandInteractionDataOption{ - { - Name: "sub", - Type: discordgo.ApplicationCommandOptionSubCommand, - }, - }, - }, - }, - }) - if err != nil { - t.Error("Expected nil error, got", err) + for _, i := range cases.interactions { + _, err = r.FindAndAutocomplete(i) + if err != nil { + t.Error("Expected nil error, got", err) + } } } func Test_FindAndAutocomplete_SubcommandGroup(t *testing.T) { r := disroute.New() - cmds := []*disroute.Cmd{ - { - Path: "cmd4", - Options: []*disroute.CmdOption{ - { - Path: "gr", - Type: disroute.TypeSubcommandGroup, - Options: []*disroute.CmdOption{ - { - Path: "sub2", - Type: disroute.TypeSubcommand, - Handlers: disroute.Handlers{ - Cmd: EmptyHandler, - Autocomplete: EmptyHandler, - }, - }, - }, - }, - }, - }, - } + cases := getSubcommandGroupCmd(disroute.TypeCommandAutocompletion) - err := r.RegisterAll(cmds) + err := r.RegisterAll(cases.cmds) if err != nil { t.Errorf("Expected nil error, got %v", err) } - _, err = r.FindAndAutocomplete(&discordgo.InteractionCreate{ - Interaction: &discordgo.Interaction{ - Type: discordgo.InteractionApplicationCommandAutocomplete, - Data: discordgo.ApplicationCommandInteractionData{ - Name: "cmd4", - Options: []*discordgo.ApplicationCommandInteractionDataOption{ - { - Name: "gr", - Type: discordgo.ApplicationCommandOptionSubCommandGroup, - Options: []*discordgo.ApplicationCommandInteractionDataOption{ - { - Name: "sub2", - Type: discordgo.ApplicationCommandOptionSubCommand, - }, - }, - }, - }, - }, - }, - }) - if err != nil { - t.Error("Expected nil error, got", err) + for _, i := range cases.interactions { + _, err = r.FindAndAutocomplete(i) + if err != nil { + t.Error("Expected nil error, got", err) + } } }