|
| 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