Skip to content

Commit af50297

Browse files
authored
Merge pull request #388 from voldyman/ill-be-back
Added /back and tests for all away commands
2 parents fb73ace + c3dccfd commit af50297

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

chat/command.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,19 @@ func InitCommands(c *Commands) {
478478
},
479479
})
480480

481+
c.Add(Command{
482+
Prefix: "/back",
483+
Help: "Clear away status.",
484+
Handler: func(room *Room, msg message.CommandMsg) error {
485+
isAway, _, _ := msg.From().GetAway()
486+
if isAway {
487+
msg.From().SetAway("")
488+
room.Send(message.NewEmoteMsg("is back.", msg.From()))
489+
}
490+
return nil
491+
},
492+
})
493+
481494
c.Add(Command{
482495
Op: true,
483496
Prefix: "/mute",

chat/command_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package chat
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/shazow/ssh-chat/chat/message"
8+
)
9+
10+
func TestAwayCommands(t *testing.T) {
11+
cmds := &Commands{}
12+
InitCommands(cmds)
13+
14+
room := NewRoom()
15+
go room.Serve()
16+
defer room.Close()
17+
18+
// steps are order dependent
19+
// User can be "away" or "not away" using 3 commands "/away [msg]", "/away", "/back"
20+
// 2^3 possible cases, run all and verify state at the end
21+
type step struct {
22+
// input
23+
Msg string
24+
25+
// expected output
26+
IsUserAway bool
27+
AwayMessage string
28+
}
29+
awayStep := step{"/away snorkling", true, "snorkling"}
30+
notAwayStep := step{"/away", false, ""}
31+
backStep := step{"/back", false, ""}
32+
33+
steps := []step{awayStep, notAwayStep, backStep}
34+
cases := [][]int{
35+
{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0},
36+
}
37+
for _, c := range cases {
38+
t.Run(fmt.Sprintf("Case: %d, %d, %d", c[0], c[1], c[2]), func(t *testing.T) {
39+
40+
u := message.NewUser(message.SimpleID("shark"))
41+
42+
for _, s := range []step{steps[c[0]], steps[c[1]], steps[c[2]]} {
43+
msg, _ := message.NewPublicMsg(s.Msg, u).ParseCommand()
44+
45+
cmds.Run(room, *msg)
46+
47+
isAway, _, awayMsg := u.GetAway()
48+
if isAway != s.IsUserAway {
49+
t.Fatalf("expected user away state '%t' not equals to actual '%t' after message '%s'", s.IsUserAway, isAway, s.Msg)
50+
}
51+
if awayMsg != s.AwayMessage {
52+
t.Fatalf("expected user away message '%s' not equal to actual '%s' after message '%s'", s.AwayMessage, awayMsg, s.Msg)
53+
}
54+
}
55+
56+
})
57+
}
58+
}

0 commit comments

Comments
 (0)