-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored channel into separate file and added test
- Loading branch information
1 parent
730220f
commit 9ecfa13
Showing
3 changed files
with
66 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters