func (s *ListSuite) Test_Peers_sortsByNameForPresentation(c *g.C) { expectedPeers := []*Peer{ &Peer{ Jid: "ba", Name: "ab", }, &Peer{ Jid: "ac", Name: "", }, &Peer{ Jid: "aa", Name: "bb", }, &Peer{ Jid: "aa", Name: "cb", }, } group := &Group{ peers: []*Peer{ expectedPeers[2], expectedPeers[0], expectedPeers[3], expectedPeers[1], }, } c.Assert(group.Peers(), g.DeepEquals, expectedPeers) }
func (s *ListSuite) Test_LatestError_setsLatestErrorWhenExists(c *g.C) { l := New() pp := &Peer{Jid: "*****@*****.**"} l.AddOrMerge(pp) l.LatestError("[email protected]/foo", "tow", "frou", "sxi") c.Assert(pp.LatestError, g.DeepEquals, &PeerError{"tow", "frou", "sxi"}) }
func (s *ListSuite) Test_Clear_clearsTheList(c *g.C) { l := New() l.AddOrMerge(&Peer{Jid: "*****@*****.**"}) l.Clear() c.Assert(len(l.peers), g.Equals, 0) }
func (s *PeerXmppSuite) Test_SetLatestError_setsLatestError(c *g.C) { p1 := &Peer{ Groups: toSet("one"), resources: toSet(), } p1.SetLatestError("oen", "tow", "there") c.Assert(p1.LatestError, g.DeepEquals, &PeerError{"oen", "tow", "there"}) }
func (s *PeerXmppSuite) Test_MergeWith_takesTheFirstGroupsIfExists(c *g.C) { p1 := &Peer{ Groups: toSet("one"), resources: toSet(), } p2 := &Peer{} c.Assert(fromSet(p1.MergeWith(p2).Groups)[0], g.Equals, "one") }
func (s *ListSuite) Test_SubscribeRequest_addsTheSubscribeID(c *g.C) { l := New() l.AddOrMerge(&Peer{Jid: "*****@*****.**"}) l.SubscribeRequest("[email protected]/hmm", "something", "") c.Assert(l.peers["*****@*****.**"].PendingSubscribeID, g.Equals, "something") l.SubscribeRequest("[email protected]/hmm2", "something3", "") c.Assert(l.peers["*****@*****.**"].PendingSubscribeID, g.Equals, "something3") }
func (s *PeerXmppSuite) Test_Dump_willDumpAllInfo(c *g.C) { p := &Peer{ Jid: "*****@*****.**", Name: "something", Subscription: "from", Groups: toSet("hello::bar"), resources: toSet(), } c.Assert(p.Dump(), g.Equals, "Peer{[email protected][something ()], subscription='from', status=''('') online=false, asked=false, pendingSubscribe='', belongsTo=''}") }
func (s *ListSuite) Test_ToSlice_createsASliceOfTheContentSortedAlphabetically(c *g.C) { l := New() l.AddOrMerge(&Peer{Jid: "*****@*****.**"}) l.AddOrMerge(&Peer{Jid: "*****@*****.**"}) l.AddOrMerge(&Peer{Jid: "*****@*****.**"}) c.Assert(l.ToSlice(), g.DeepEquals, []*Peer{ &Peer{Jid: "*****@*****.**"}, &Peer{Jid: "*****@*****.**"}, &Peer{Jid: "*****@*****.**"}, }) }
func (s *PeerXmppSuite) Test_PeerWithState_createsANewPeer(c *g.C) { p := PeerWithState("[email protected]/1234", "hmm", "no", "", "") c.Assert(p.Jid, g.Equals, "*****@*****.**") c.Assert(p.Name, g.Equals, "") c.Assert(p.Status, g.Equals, "hmm") c.Assert(p.StatusMsg, g.Equals, "no") }
func (s *ListSuite) Test_IterAll_willIterateOverAllTheListsGivenAndYieldTheirPeers(c *g.C) { l := New() l2 := New() pp := &Peer{Jid: "*****@*****.**"} pp2 := &Peer{Jid: "*****@*****.**"} l.AddOrMerge(pp) l2.AddOrMerge(pp2) result := []*Peer{} IterAll(func(_ int, p *Peer) { result = append(result, p) }, l, l2) c.Assert(result, g.DeepEquals, []*Peer{pp2, pp}) }
func (s *ListSuite) Test_GetPendingSubscribe_returnsThePendingSubscribeIfExists(c *g.C) { l := New() l.AddOrMerge(&Peer{Jid: "*****@*****.**", PendingSubscribeID: "foo"}) l.AddOrMerge(&Peer{Jid: "*****@*****.**"}) v, k := l.GetPendingSubscribe("*****@*****.**") c.Assert(k, g.Equals, false) v, k = l.GetPendingSubscribe("[email protected]/bar") c.Assert(k, g.Equals, true) c.Assert(v, g.Equals, "foo") v, k = l.GetPendingSubscribe("[email protected]/bar") c.Assert(k, g.Equals, false) c.Assert(v, g.Equals, "") }
func (s *ListSuite) Test_Unsubscribed_whenExist(c *g.C) { l := New() l.AddOrMerge(&Peer{Jid: "*****@*****.**", Subscription: "both", Asked: true, PendingSubscribeID: "foo"}) l.AddOrMerge(&Peer{Jid: "*****@*****.**", Subscription: "to"}) l.AddOrMerge(&Peer{Jid: "*****@*****.**", Subscription: "from"}) l.Unsubscribed("[email protected]/123") c.Assert(l.peers["*****@*****.**"].Subscription, g.Equals, "from") c.Assert(l.peers["*****@*****.**"].Asked, g.Equals, false) c.Assert(l.peers["*****@*****.**"].PendingSubscribeID, g.Equals, "") l.Unsubscribed("[email protected]/123") c.Assert(l.peers["*****@*****.**"].Subscription, g.Equals, "none") l.Unsubscribed("[email protected]/123") c.Assert(l.peers["*****@*****.**"].Subscription, g.Equals, "from") }
func (s *PeerXmppSuite) Test_toEntry_ReturnsAnEntryWithTheInformation(c *g.C) { p := &Peer{ Jid: "*****@*****.**", Name: "something", Subscription: "from", Groups: toSet("hello::bar"), resources: toSet(), } c.Assert(p.ToEntry().Jid, g.Equals, "*****@*****.**") c.Assert(p.ToEntry().Name, g.Equals, "something") c.Assert(p.ToEntry().Subscription, g.Equals, "from") c.Assert(p.ToEntry().Group, g.DeepEquals, []string{"hello::bar"}) }
func (s *ListSuite) Test_Remove_doesNothingWhenAskedToRemoveEntryNotInList(c *g.C) { l := New() l.peers["*****@*****.**"] = &Peer{} res, rem := l.Remove("*****@*****.**") c.Assert(rem, g.Equals, false) c.Assert(res, g.IsNil) c.Assert(len(l.peers), g.Equals, 1) }
func (s *ListSuite) Test_AddOrMerge_addsTheEntryIfNotInTheList(c *g.C) { l := New() p := &Peer{Jid: "somewhere", Name: "something"} res := l.AddOrMerge(p) c.Assert(res, g.Equals, true) c.Assert(len(l.peers), g.Equals, 1) c.Assert(l.peers["somewhere"], g.Equals, p) }
func (s *PeerXmppSuite) Test_PeerFrom_returnsANewPeerWithTheSameInformation(c *g.C) { re := data.RosterEntry{ Jid: "*****@*****.**", Subscription: "from", Name: "someone", Group: []string{ "onegroup", "twogroup", }, } p := PeerFrom(re, "", "", nil) c.Assert(p.Jid, g.Equals, "*****@*****.**") c.Assert(p.Subscription, g.Equals, "from") c.Assert(p.Name, g.Equals, "someone") c.Assert(p.Groups, g.DeepEquals, toSet("onegroup", "twogroup")) }
func (s *ListSuite) Test_Remove_removesAnEntryIfInTheList(c *g.C) { l := New() l.peers["*****@*****.**"] = &Peer{} l.peers["*****@*****.**"] = &Peer{Name: "me"} res, rem := l.Remove("[email protected]/somewhere") c.Assert(rem, g.Equals, true) c.Assert(res.Name, g.Equals, "me") c.Assert(len(l.peers), g.Equals, 1) }
func (s *ListSuite) Test_PeerBecameUnavailable_setsTheOfflineState(c *g.C) { l := New() l.AddOrMerge(&Peer{Jid: "*****@*****.**", Online: true}) res := l.PeerBecameUnavailable("[email protected]/foo") c.Assert(res, g.Equals, false) res = l.PeerBecameUnavailable("[email protected]/foo2") c.Assert(res, g.Equals, true) c.Assert(l.peers["*****@*****.**"].Online, g.Equals, false) }
func (s *ListSuite) Test_AddOrReplace_mergesTheEntriesIfInTheList(c *g.C) { l := New() p1 := &Peer{Jid: "somewhere", Name: "something", Groups: toSet("hello"), Subscription: "from"} l.peers["somewhere"] = p1 p2 := &Peer{Jid: "somewhere", Name: "something2", Groups: toSet("goodbye")} res := l.AddOrMerge(p2) c.Assert(res, g.Equals, false) c.Assert(len(l.peers), g.Equals, 1) c.Assert(*l.peers["somewhere"], g.DeepEquals, Peer{Jid: "somewhere", Name: "something2", Groups: toSet("goodbye"), Subscription: "from", resources: toSet()}) }
func (s *ListSuite) Test_StateOf_returnsState(c *g.C) { l := New() l.AddOrMerge(&Peer{Jid: "*****@*****.**", Status: "bla", StatusMsg: "hmm"}) st, sm, k := l.StateOf("*****@*****.**") c.Assert(k, g.Equals, false) st, sm, k = l.StateOf("[email protected]/aha") c.Assert(k, g.Equals, true) c.Assert(st, g.Equals, "bla") c.Assert(sm, g.Equals, "hmm") }
func (s *ListSuite) Test_Iter_yieldsEachEntry(c *g.C) { l := New() l.AddOrMerge(&Peer{Jid: "*****@*****.**"}) l.AddOrMerge(&Peer{Jid: "*****@*****.**"}) l.AddOrMerge(&Peer{Jid: "*****@*****.**"}) called := 0 l.Iter(func(ix int, p *Peer) { called++ switch ix { case 0: c.Assert(p, g.DeepEquals, &Peer{Jid: "*****@*****.**"}) case 1: c.Assert(p, g.DeepEquals, &Peer{Jid: "*****@*****.**"}) case 2: c.Assert(p, g.DeepEquals, &Peer{Jid: "*****@*****.**"}) } }) c.Assert(called, g.DeepEquals, 3) }
func (s *ListSuite) Test_PeerPresenceUpdate_sometimesUpdatesNonExistantPeers(c *g.C) { l := New() res := l.PeerPresenceUpdate("[email protected]/hmm", "hello", "goodbye", "") c.Assert(res, g.Equals, true) c.Assert(l.peers["*****@*****.**"].Status, g.Equals, "hello") c.Assert(l.peers["*****@*****.**"].StatusMsg, g.Equals, "goodbye") res = l.PeerPresenceUpdate("[email protected]/hmm", "xa", "goodbye", "") c.Assert(res, g.Equals, false) c.Assert(l.peers["*****@*****.**"], g.IsNil) res = l.PeerPresenceUpdate("[email protected]/hmm", "away", "goodbye", "") c.Assert(res, g.Equals, false) c.Assert(l.peers["*****@*****.**"], g.IsNil) }
func (s *PeerXmppSuite) Test_NameForPresentation_returnsTheNameIfItExistsButJidOtherwise(c *g.C) { c.Assert((&Peer{Name: "foo", Jid: "*****@*****.**"}).NameForPresentation(), g.Equals, "foo") c.Assert((&Peer{Jid: "*****@*****.**"}).NameForPresentation(), g.Equals, "*****@*****.**") c.Assert((&Peer{Jid: "*****@*****.**", Name: "Name", Nickname: "Nick"}).NameForPresentation(), g.Equals, "Nick") }
func (s *PeerXmppSuite) Test_PeerWithPendingSubscribe_createsNewPeer(c *g.C) { p := peerWithPendingSubscribe("[email protected]/1234", "223434", "") c.Assert(p.Jid, g.Equals, "*****@*****.**") c.Assert(p.Name, g.Equals, "") c.Assert(p.PendingSubscribeID, g.Equals, "223434") }
func (s *ListSuite) Test_New_returnsANewList(c *g.C) { l := New() c.Assert(l, g.Not(g.IsNil)) c.Assert(l.peers, g.Not(g.IsNil)) }
func (s *ListSuite) Test_Subscribed_whenDoesntExist(c *g.C) { l := New() l.Subscribed("*****@*****.**") c.Assert(len(l.peers), g.Equals, 0) }
func (s *PeerXmppSuite) Test_fromSet_returnsTheKeysInTheSet(c *g.C) { c.Assert(fromSet(toSet("helo")), g.DeepEquals, []string{"helo"}) c.Assert(fromSet(toSet()), g.DeepEquals, []string{}) c.Assert(len(fromSet(toSet("helo1", "helo2", "helo1"))), g.DeepEquals, 2) }
func (s *ListSuite) Test_RemovePendingSubscribe_removesThePendingSubscribe(c *g.C) { l := New() l.AddOrMerge(&Peer{Jid: "*****@*****.**", PendingSubscribeID: "foo"}) l.AddOrMerge(&Peer{Jid: "*****@*****.**"}) v, k := l.RemovePendingSubscribe("*****@*****.**") c.Assert(k, g.Equals, false) v, k = l.RemovePendingSubscribe("[email protected]/bar") c.Assert(k, g.Equals, true) c.Assert(v, g.Equals, "foo") c.Assert(l.peers["*****@*****.**"].PendingSubscribeID, g.Equals, "") v, k = l.RemovePendingSubscribe("[email protected]/bar") c.Assert(k, g.Equals, false) c.Assert(v, g.Equals, "") }
func (s *ListSuite) Test_PeerPresenceUpdate_updatesPreviouslyKnownPeer(c *g.C) { l := New() l.AddOrMerge(&Peer{Jid: "*****@*****.**", Online: false, resources: toSet()}) l.AddOrMerge(&Peer{Jid: "*****@*****.**", Online: true, Status: "dnd", StatusMsg: "working", resources: toSet()}) res := l.PeerPresenceUpdate("[email protected]/hmm", "hello", "goodbye", "") c.Assert(res, g.Equals, true) c.Assert(l.peers["*****@*****.**"].Status, g.Equals, "hello") c.Assert(l.peers["*****@*****.**"].StatusMsg, g.Equals, "goodbye") c.Assert(l.peers["*****@*****.**"].Online, g.Equals, true) res = l.PeerPresenceUpdate("[email protected]/hmm", "dnd", "working", "") c.Assert(res, g.Equals, false) c.Assert(l.peers["*****@*****.**"].Status, g.Equals, "dnd") c.Assert(l.peers["*****@*****.**"].StatusMsg, g.Equals, "working") c.Assert(l.peers["*****@*****.**"].Online, g.Equals, true) }