Adding impl for quiet mode with tests.

This commit is contained in:
Nick Presta 2015-01-03 01:28:48 -05:00
parent 362d0fdffd
commit a2ee2000bb
4 changed files with 139 additions and 0 deletions

View File

@ -85,6 +85,12 @@ func (ch *Channel) HandleMsg(m Message) {
// Skip
return
}
if _, ok := m.(*AnnounceMsg); ok {
if user.Config.Quiet {
// Skip
return
}
}
err := user.Send(m)
if err != nil {
ch.Leave(user)

View File

@ -57,6 +57,113 @@ func TestChannelJoin(t *testing.T) {
}
}
func TestChannelDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) {
u := NewUser("foo")
u.Config = UserConfig{
Quiet: true,
}
ch := NewChannel()
defer ch.Close()
err := ch.Join(u)
if err != nil {
t.Fatal(err)
}
// Drain the initial Join message
<-ch.broadcast
go func() {
for msg := range u.msg {
if _, ok := msg.(*AnnounceMsg); ok {
t.Errorf("Got unexpected `%T`", msg)
}
}
}()
// Call with an AnnounceMsg and all the other types
// and assert we received only non-announce messages
ch.HandleMsg(NewAnnounceMsg("Ignored"))
// Assert we still get all other types of messages
ch.HandleMsg(NewEmoteMsg("hello", u))
ch.HandleMsg(NewSystemMsg("hello", u))
ch.HandleMsg(NewPrivateMsg("hello", u, u))
ch.HandleMsg(NewPublicMsg("hello", u))
}
func TestChannelQuietToggleBroadcasts(t *testing.T) {
u := NewUser("foo")
u.Config = UserConfig{
Quiet: true,
}
ch := NewChannel()
defer ch.Close()
err := ch.Join(u)
if err != nil {
t.Fatal(err)
}
// Drain the initial Join message
<-ch.broadcast
u.ToggleQuietMode()
expectedMsg := NewAnnounceMsg("Ignored")
ch.HandleMsg(expectedMsg)
msg := <-u.msg
if _, ok := msg.(*AnnounceMsg); !ok {
t.Errorf("Got: `%T`; Expected: `%T`", msg, expectedMsg)
}
u.ToggleQuietMode()
ch.HandleMsg(NewAnnounceMsg("Ignored"))
ch.HandleMsg(NewSystemMsg("hello", u))
msg = <-u.msg
if _, ok := msg.(*AnnounceMsg); ok {
t.Errorf("Got unexpected `%T`", msg)
}
}
func TestQuietToggleDisplayState(t *testing.T) {
var expected, actual []byte
s := &MockScreen{}
u := NewUser("foo")
ch := NewChannel()
go ch.Serve()
defer ch.Close()
err := ch.Join(u)
if err != nil {
t.Fatal(err)
}
// Drain the initial Join message
<-ch.broadcast
ch.Send(ParseInput("/quiet", u))
u.ConsumeOne(s)
expected = []byte("-> Quiet mode is toggled ON" + Newline)
s.Read(&actual)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
}
ch.Send(ParseInput("/quiet", u))
u.ConsumeOne(s)
expected = []byte("-> Quiet mode is toggled OFF" + Newline)
s.Read(&actual)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Got: `%s`; Expected: `%s`", actual, expected)
}
}
func TestChannelNames(t *testing.T) {
var expected, actual []byte
@ -72,6 +179,7 @@ func TestChannelNames(t *testing.T) {
t.Fatal(err)
}
// Drain the initial Join message
<-ch.broadcast
ch.Send(ParseInput("/names", u))

View File

@ -196,6 +196,24 @@ func InitCommands(c *Commands) {
},
})
c.Add(Command{
Prefix: "/quiet",
Help: "Silence announcement-type messages (join, part, rename, etc).",
Handler: func(channel *Channel, msg CommandMsg) error {
u := msg.From()
u.ToggleQuietMode()
var body string
if u.Config.Quiet {
body = "Quiet mode is toggled ON"
} else {
body = "Quiet mode is toggled OFF"
}
channel.Send(NewSystemMsg(body, u))
return nil
},
})
c.Add(Command{
Op: true,
Prefix: "/op",

View File

@ -59,6 +59,11 @@ func (u *User) SetName(name string) {
u.SetColorIdx(rand.Int())
}
// ToggleQuietMode will toggle whether or not quiet mode is enabled
func (u *User) ToggleQuietMode() {
u.Config.Quiet = !u.Config.Quiet
}
// SetColorIdx will set the colorIdx to a specific value, primarily used for
// testing.
func (u *User) SetColorIdx(idx int) {
@ -122,6 +127,7 @@ func (u *User) Send(m Message) error {
type UserConfig struct {
Highlight bool
Bell bool
Quiet bool
Theme *Theme
}
@ -132,6 +138,7 @@ func init() {
DefaultUserConfig = &UserConfig{
Highlight: true,
Bell: false,
Quiet: false,
}
// TODO: Seed random?