Skip to content

Commit

Permalink
Merge pull request #17 from jan-carreras/#16-in-range-returns-incorre…
Browse files Browse the repository at this point in the history
…ct-registered-port

#16 in range returns incorrect registered port
  • Loading branch information
dnnrly authored Aug 15, 2020
2 parents 45373e1 + 0154f7d commit 62977bf
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 61 deletions.
6 changes: 1 addition & 5 deletions httpref.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,9 @@ func (r References) ByName(code string) References {
// InRange attempts to find a numeric in a range in the Name field
func (r References) InRange(code string) References {
for _, v := range r {
if v.Name == code {
return References{v}
}

if strings.Contains(v.Name, "-") {
parts := strings.Split(v.Name, "-")
if code >= parts[0] || code <= parts[len(parts)-1] {
if code >= parts[0] && code <= parts[len(parts)-1] {
return References{v}
}
}
Expand Down
39 changes: 32 additions & 7 deletions httpref_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package httpref

import (
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -29,20 +30,34 @@ func TestReferences_ByName(t *testing.T) {
}

func TestReferences_InRange(t *testing.T) {
tests := []string{
"19150",
"16406",
"5988",
"5989",
tests := []struct {
having string
expect string
}{
{having: "19150", expect: "10000-20000"},
{having: "16406", expect: "10000-20000"},
{having: "5988", expect: "5988-5989"},
{having: "5989", expect: "5988-5989"},
}

for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
if got := RegisteredPorts.InRange(tt); len(got) != 1 {
t.Run(tt.having, func(t *testing.T) {
got := RegisteredPorts.InRange(tt.having)
if len(got) != 1 {
t.Errorf("References.InRange() = %v, want %v", len(got), 1)
}
if got[0].Name != tt.expect {
t.Errorf("References.InRange()[0] = %v, want %v", got[0].Name, tt.expect)
}
})
}

t.Run("Invalid port should not return anything", func(t *testing.T) {
got := RegisteredPorts.InRange("70000") // Invalid port, should not return anything
if len(got) != 0 {
t.Errorf("References.InRange() = %v, want %v", len(got), 0)
}
})
}

func TestReference_SummarizeContainsCorrectParts(t *testing.T) {
Expand Down Expand Up @@ -86,3 +101,13 @@ func TestReferences_Titles(t *testing.T) {
n := Statuses.Titles()
assert.Equal(t, 5, len(n))
}

func TestPortsConsistencyValidation(t *testing.T) {
ports := append(WellKnownPorts[1:], RegisteredPorts[1:]...)
var validRange = regexp.MustCompile(`^\d+(-\d+)?$`)
for _, port := range ports {
if !validRange.MatchString(port.Name) {
t.Errorf("Invalid port format: %v", port.Name)
}
}
}
Loading

0 comments on commit 62977bf

Please sign in to comment.