Skip to content

Commit 9d517c1

Browse files
committed
nat: TestParsePortRangeToInt: reduce overlap with TestParsePortRange
ParsePortRangeToInt is a shallow wrapper around ParsePortRange, except for returning int's, and accepting empty values. Other cases are covered by TestParsePortRange. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 62c7de1 commit 9d517c1

1 file changed

Lines changed: 13 additions & 43 deletions

File tree

nat/nat_test.go

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -81,53 +81,23 @@ func TestParsePort(t *testing.T) {
8181
}
8282
}
8383

84+
// TestParsePortRangeToInt tests behavior that's specific to [ParsePortRangeToInt],
85+
// which is a shallow wrapper around [ParsePortRange], except for returning int's,
86+
// and accepting empty values. Other cases are covered by [TestParsePortRange].
8487
func TestParsePortRangeToInt(t *testing.T) {
85-
var (
86-
begin int
87-
end int
88-
err error
89-
)
90-
91-
type TestRange struct {
92-
Range string
93-
Begin int
94-
End int
95-
}
96-
validRanges := []TestRange{
97-
{"1234", 1234, 1234},
98-
{"1234-1234", 1234, 1234},
99-
{"1234-1235", 1234, 1235},
100-
{"8000-9000", 8000, 9000},
101-
{"0", 0, 0},
102-
{"0-0", 0, 0},
88+
_, _, err := ParsePortRangeToInt("")
89+
if err != nil {
90+
t.Error(err)
10391
}
104-
105-
for _, r := range validRanges {
106-
begin, end, err = ParsePortRangeToInt(r.Range)
107-
108-
if err != nil || begin != r.Begin {
109-
t.Fatalf("Parsing port range '%s' did not succeed. Expected begin %d, got %d", r.Range, r.Begin, begin)
110-
}
111-
if err != nil || end != r.End {
112-
t.Fatalf("Parsing port range '%s' did not succeed. Expected end %d, got %d", r.Range, r.End, end)
113-
}
92+
begin, end, err := ParsePortRangeToInt("8000-9000")
93+
if err != nil {
94+
t.Error(err)
11495
}
115-
116-
invalidRanges := []string{
117-
"asdf",
118-
"1asdf",
119-
"9000-8000",
120-
"9000-",
121-
"-8000",
122-
"-8000-",
96+
if expBegin := 8000; begin != 8000 {
97+
t.Errorf("expected begin %d, got %d", expBegin, begin)
12398
}
124-
125-
for _, r := range invalidRanges {
126-
begin, end, err = ParsePortRangeToInt(r)
127-
128-
if err == nil || begin != 0 || end != 0 {
129-
t.Fatalf("Parsing port range '%s' succeeded", r)
130-
}
99+
if expEnd := 9000; end != expEnd {
100+
t.Errorf("expected end %d, got %d", expEnd, end)
131101
}
132102
}
133103

0 commit comments

Comments
 (0)