From 7a5ed989765345f3a944e553cc27b561b058af87 Mon Sep 17 00:00:00 2001 From: Chris Bednarski Date: Tue, 24 Feb 2015 23:06:30 -0800 Subject: [PATCH] Added tests for Equal() EqualIp() and IsValid() --- hostname_test.go | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/hostname_test.go b/hostname_test.go index 7f23889..1c20da0 100644 --- a/hostname_test.go +++ b/hostname_test.go @@ -2,11 +2,11 @@ package hostess_test import ( "github.com/cbednarski/hostess" + "net" "testing" ) func TestHostname(t *testing.T) { - h := hostess.Hostname{} h.Domain = domain h.Ip = ip @@ -23,6 +23,48 @@ func TestHostname(t *testing.T) { } } +func TestEqual(t *testing.T) { + a := hostess.NewHostname("localhost", "127.0.0.1", true) + b := hostess.NewHostname("localhost", "127.0.0.1", false) + c := hostess.NewHostname("localhost", "127.0.1.1", false) + + if !a.Equal(b) { + t.Errorf("%s and %s should be equal", a, b) + } + if a.Equal(c) { + t.Errorf("%s and %s should not be equal", a, c) + } +} + +func TestEqualIp(t *testing.T) { + a := hostess.NewHostname("localhost", "127.0.0.1", true) + c := hostess.NewHostname("localhost", "127.0.1.1", false) + ip := net.ParseIP("127.0.0.1") + + if !a.EqualIp(ip) { + t.Errorf("%s and %s should be equal", a.Ip, ip) + } + if a.EqualIp(c.Ip) { + t.Errorf("%s and %s should not be equal", a.Ip, c.Ip) + } +} + +func TestIsValid(t *testing.T) { + a := hostess.NewHostname("localhost", "127.0.0.1", true) + d := hostess.NewHostname("", "127.0.0.1", true) + e := hostess.NewHostname("localhost", "localhost", true) + + if !a.IsValid() { + t.Errorf("%s should be a valid hostname", a) + } + if d.IsValid() { + t.Errorf("%s should be invalid because the name is blank", d) + } + if e.IsValid() { + t.Errorf("%s should be invalid because the ip is malformed", e) + } +} + func TestFormatHostname(t *testing.T) { hostname := &hostess.Hostname{domain, ip, enabled, false}