Add tests for ParseTargets

Still need tests with actual alias map
This commit is contained in:
eenblam 2018-08-31 15:01:44 -07:00
commit 7cca7b9d93

View file

@ -35,15 +35,52 @@ func TestNormalizeMac(t *testing.T) {
// TODO: refactor to parse targets with an actual alias map // TODO: refactor to parse targets with an actual alias map
func TestParseTargets(t *testing.T) { func TestParseTargets(t *testing.T) {
ips, macs, err := ParseTargets("192.168.1.2, 192.168.1.3", &Aliases{}) cases := []struct {
if err != nil { Name string
t.Error("ips:", ips, "macs:", macs, "err:", err) InputTargets string
InputAliases *Aliases
ExpectedIPCount int
ExpectedMACCount int
ExpectedError bool
}{
// Not sure how to trigger sad path where macParser.FindAllString()
// finds a MAC but net.ParseMac() fails on the result.
{
"empty target string causes empty return",
"",
&Aliases{},
0,
0,
false,
},
{
"MACs are parsed",
"192.168.1.2, 192.168.1.3, 5c:00:0b:90:a9:f0, 6c:00:0b:90:a9:f0",
&Aliases{},
2,
2,
false,
},
} }
if len(ips) != 2 { for _, test := range cases {
t.Fatalf("expected '%d', got '%d'", 2, len(ips)) t.Run(test.Name, func(t *testing.T) {
} ips, macs, err := ParseTargets(test.InputTargets, test.InputAliases)
if len(macs) != 0 { if err != nil && !test.ExpectedError {
t.Fatalf("expected '%d', got '%d'", 0, len(macs)) t.Errorf("unexpected error: %s", err)
}
if err == nil && test.ExpectedError {
t.Error("Expected error, but got none")
}
if test.ExpectedError {
return
}
if len(ips) != test.ExpectedIPCount {
t.Errorf("Wrong number of IPs. Got %v for targets %s", ips, test.InputTargets)
}
if len(macs) != test.ExpectedMACCount {
t.Errorf("Wrong number of MACs. Got %v for targets %s", macs, test.InputTargets)
}
})
} }
} }