这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"},
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change. It says what we're testing more succinctly.

}

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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to ask if this test is necessary BUT I think it actually specifies the contract for naming port mappings. Well spotted!

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