Skip to content

Commit

Permalink
Refactored channel into separate file and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
insomniacslk committed Apr 4, 2018
1 parent 730220f commit 9ecfa13
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 38 deletions.
39 changes: 39 additions & 0 deletions irc_channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

// Channel represents an IRC channel. It maps to Slack's groups and channels.
// Private messages are handled differently.
type Channel struct {
Members []string
Topic string
// Slack groups are different from channels. Here I try to uniform them for
// IRC, but I still need to know which is which to use the right API calls.
IsGroup bool
}

// MembersDiff compares the members of this channel with another members list
// and return a slice of members who joined and a slice of members who left.
func (c Channel) MembersDiff(otherMembers []string) ([]string, []string) {
var membersMap = map[string]bool{}
for _, m := range c.Members {
membersMap[m] = true
}
var otherMembersMap = map[string]bool{}
for _, m := range otherMembers {
otherMembersMap[m] = true
}

added := make([]string, 0)
for _, m := range otherMembers {
if _, ok := membersMap[m]; !ok {
added = append(added, m)
}
}

removed := make([]string, 0)
for _, m := range c.Members {
if _, ok := otherMembersMap[m]; !ok {
removed = append(removed, m)
}
}
return added, removed
}
27 changes: 27 additions & 0 deletions irc_channel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"testing"
)

func TestMembersDiffEmpty(t *testing.T) {
c := Channel{Members: []string{}}
a, r := c.MembersDiff([]string{})
if len(a) != 0 {
t.Fatalf("Added members: %v; want empty list", a)
}
if len(r) != 0 {
t.Fatalf("Removed members: %v; want empty list", r)
}
}

func TestMembersDiffNonEmpty(t *testing.T) {
c := Channel{Members: []string{"removed1"}}
a, r := c.MembersDiff([]string{"added1"})
if !(len(a) == 1 && a[0] == "added1") {
t.Fatalf("Added members: %v; want: %v", a, []string{"added1"})
}
if !(len(r) == 1 && r[0] == "removed1") {
t.Fatalf("Removed members: %v; want: %v", a, []string{"removed1"})
}
}
38 changes: 0 additions & 38 deletions irc_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,6 @@ import (
"github.com/nlopes/slack"
)

// Channel represents an IRC channel. It maps to Slack's groups and channels.
// Private messages are handled differently.
type Channel struct {
Members []string
Topic string
// Slack groups are different from channels. Here I try to uniform them for
// IRC, but I still need to know which is which to use the right API calls.
IsGroup bool
}

// MembersDiff compares the members of this channel with another members list
// and return a slice of members who joined and a slice of members who left.
func (c Channel) MembersDiff(otherMembers []string) ([]string, []string) {
var membersMap = map[string]bool{}
for _, m := range c.Members {
membersMap[m] = true
}
var otherMembersMap = map[string]bool{}
for _, m := range otherMembers {
otherMembersMap[m] = true
}

added := make([]string, 0)
for _, m := range otherMembers {
if _, ok := membersMap[m]; !ok {
added = append(added, m)
}
}

removed := make([]string, 0)
for _, m := range c.Members {
if _, ok := otherMembersMap[m]; !ok {
removed = append(removed, m)
}
}
return added, removed
}

// IrcContext holds the client context information
type IrcContext struct {
Conn *net.TCPConn
Expand Down

0 comments on commit 9ecfa13

Please sign in to comment.