Skip to content

Commit

Permalink
Handle 'set_x_for' commands without any params
Browse files Browse the repository at this point in the history
  • Loading branch information
col committed Jul 16, 2018
1 parent 2652628 commit e0ebeef
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion domain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func (c Command) FirstParam() string {
}

func (c Command) ParamsStringExceptFirst() string {
return strings.Join(c.Params[1:], " ")
if len(c.Params) > 1 {
return strings.Join(c.Params[1:], " ")
}
return ""
}

type User struct {
Expand Down
3 changes: 3 additions & 0 deletions whosinbot/whos_in_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func (b *WhosInBot) handleResponse(command domain.Command, status string) (*doma
}

func (b *WhosInBot) handleResponseFor(command domain.Command, status string) (*domain.Response, error) {
if len(command.FirstParam()) == 0 {
return &domain.Response{ChatID: command.ChatID, Text: "Please provide the persons first name."}, nil
}
return b.setAttendanceFor(command, command.FirstParam(), status, command.ParamsStringExceptFirst())
}

Expand Down
21 changes: 21 additions & 0 deletions whosinbot/whos_in_bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ func TestSetInForWithReasonWhenRollCallExist(t *testing.T) {
assertBotResponse(t, response, err, 123, "1. JohnSmith (sample reason)", nil)
}

func TestSetInForWithoutANameWhenRollCallExist(t *testing.T) {
setUp()
mockDataStore.rollCall = &domain.RollCall{ChatID: 123}
response, err := bot.HandleCommand(command("set_in_for", []string{}))
assertBotResponse(t, response, err, 123, "Please provide the persons first name.", nil)
}

func TestSetOutForWhenRollCallDoesNotExist(t *testing.T) {
setUp()
response, err := bot.HandleCommand(command("set_out_for", []string{"JohnSmith"}))
Expand All @@ -381,6 +388,13 @@ func TestSetOutForWithReasonWhenRollCallExist(t *testing.T) {
assertBotResponse(t, response, err, 123, "Out\n - JohnSmith (sample reason)", nil)
}

func TestSetOutForWithoutANameWhenRollCallExist(t *testing.T) {
setUp()
mockDataStore.rollCall = &domain.RollCall{ChatID: 123}
response, err := bot.HandleCommand(command("set_out_for", []string{}))
assertBotResponse(t, response, err, 123, "Please provide the persons first name.", nil)
}

func TestSetMaybeForWhenRollCallDoesNotExist(t *testing.T) {
setUp()
response, err := bot.HandleCommand(command("set_maybe_for", []string{"JohnSmith"}))
Expand All @@ -401,6 +415,13 @@ func TestSetMaybeForWithReasonWhenRollCallExist(t *testing.T) {
assertBotResponse(t, response, err, 123, "Maybe\n - JohnSmith (sample reason)", nil)
}

func TestSetMaybeForWithoutANameWhenRollCallExist(t *testing.T) {
setUp()
mockDataStore.rollCall = &domain.RollCall{ChatID: 123}
response, err := bot.HandleCommand(command("set_maybe_for", []string{}))
assertBotResponse(t, response, err, 123, "Please provide the persons first name.", nil)
}

// Test Helpers
func command(name string, params []string) domain.Command {
return domain.Command{
Expand Down

0 comments on commit e0ebeef

Please sign in to comment.