Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Condense Entries unit tests with helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
bboreham committed Nov 2, 2015
1 parent 18f51a2 commit 963b6c5
Showing 1 changed file with 17 additions and 59 deletions.
76 changes: 17 additions & 59 deletions nameserver/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,22 @@ func TestAdd(t *testing.T) {
require.Equal(t, entries, expected)
}

func TestMerge(t *testing.T) {
e1 := Entries{
Entry{Hostname: "A"},
Entry{Hostname: "C"},
Entry{Hostname: "D"},
Entry{Hostname: "F"},
func makeEntries(values string) Entries {
entries := make(Entries, len(values))
for i, c := range values {
entries[i] = Entry{Hostname: string(c)}
}
return entries
}

e2 := Entries{
Entry{Hostname: "B"},
Entry{Hostname: "E"},
Entry{Hostname: "F"},
}
func TestMerge(t *testing.T) {
e1 := makeEntries("ACDF")
e2 := makeEntries("BEF")

diff := e1.merge(e2)
expectedDiff := Entries{
Entry{Hostname: "B"},
Entry{Hostname: "E"},
}
require.Equal(t, expectedDiff, diff)

expected := Entries{
Entry{Hostname: "A"},
Entry{Hostname: "B"},
Entry{Hostname: "C"},
Entry{Hostname: "D"},
Entry{Hostname: "E"},
Entry{Hostname: "F"},
}
require.Equal(t, expected, e1)
require.Equal(t, makeEntries("BE"), diff)
require.Equal(t, makeEntries("ABCDEF"), e1)

diff = e1.merge(e1)
require.Equal(t, Entries{}, diff)
Expand All @@ -85,10 +71,7 @@ func TestTombstone(t *testing.T) {
defer func() { now = oldNow }()
now = func() int64 { return 1234 }

es := Entries{
Entry{Hostname: "A"},
Entry{Hostname: "B"},
}
es := makeEntries("AB")

es.tombstone(router.UnknownPeerName, func(e *Entry) bool {
return e.Hostname == "B"
Expand All @@ -101,18 +84,12 @@ func TestTombstone(t *testing.T) {
}

func TestDelete(t *testing.T) {
es := Entries{
Entry{Hostname: "A"},
Entry{Hostname: "B"},
}
es := makeEntries("AB")

es.filter(func(e *Entry) bool {
return e.Hostname != "A"
})
expected := Entries{
Entry{Hostname: "B"},
}
require.Equal(t, expected, es)
require.Equal(t, makeEntries("B"), es)
}

func TestLookup(t *testing.T) {
Expand All @@ -132,29 +109,10 @@ func TestLookup(t *testing.T) {
}

func TestGossipDataMerge(t *testing.T) {
g1 := GossipData{Entries: Entries{
Entry{Hostname: "A"},
Entry{Hostname: "c"},
Entry{Hostname: "D"},
Entry{Hostname: "f"},
}}

g2 := GossipData{Entries: Entries{
Entry{Hostname: "B"},
Entry{Hostname: "E"},
Entry{Hostname: "f"},
}}
g1 := GossipData{Entries: makeEntries("AcDf")}
g2 := GossipData{Entries: makeEntries("BEf")}

g1.Merge(&g2)

expected := GossipData{Entries: Entries{
Entry{Hostname: "A"},
Entry{Hostname: "B"},
Entry{Hostname: "c"},
Entry{Hostname: "D"},
Entry{Hostname: "E"},
Entry{Hostname: "f"},
}}

require.Equal(t, expected, g1)
require.Equal(t, GossipData{Entries: makeEntries("ABcDEf")}, g1)
}

0 comments on commit 963b6c5

Please sign in to comment.