From 963b6c51b2d661686c6e4814ff817769f4cb024e Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 30 Oct 2015 17:48:22 +0000 Subject: [PATCH] Condense Entries unit tests with helper function --- nameserver/entry_test.go | 76 +++++++++------------------------------- 1 file changed, 17 insertions(+), 59 deletions(-) diff --git a/nameserver/entry_test.go b/nameserver/entry_test.go index 4a84d14304..dd0500570d 100644 --- a/nameserver/entry_test.go +++ b/nameserver/entry_test.go @@ -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) @@ -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" @@ -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) { @@ -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) }