Skip to content

Commit

Permalink
Merge pull request #5 from dnnrly/bugfix/unique-filters
Browse files Browse the repository at this point in the history
Bugfix/unique filters
  • Loading branch information
dnnrly authored Jan 12, 2020
2 parents 6f560d6 + 2e164ff commit be98d0c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ var rootCmd = &cobra.Command{
Short: "Command line access to HTTP references",
Long: `This displays useful information related to HTTP.
It will prefer exact matches where there are mutliple entries matching
the filter (e.g. Accept and Accept-Language). If you want to match
everything with the same prefix then you can use * as a wildcard suffix,
for example:
httpref 'Accept*'
Most of the content comes from the Mozilla developer
documentation (https://developer.mozilla.org/en-US/docs/Web/HTTP)
and is copyright Mozilla and individual contributors. See
Expand Down Expand Up @@ -59,7 +65,7 @@ func root(cmd *cobra.Command, args []string) error {
func printResults(results httpref.References) {
switch len(results) {
case 0:
fmt.Fprintf(os.Stderr, "Name note recognised\n")
fmt.Fprintf(os.Stderr, "Filter not found any results\n")
os.Exit(1)
case 1:
fmt.Printf("%s - %s\n\n%s\n", results[0].Name, results[0].Summary, results[0].Description)
Expand Down
7 changes: 7 additions & 0 deletions httpref.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ type References []Reference
func (r References) ByName(code string) References {
results := References{}

wildcard := strings.HasSuffix(code, "*")
code = strings.ReplaceAll(code, "*", "")

for _, v := range r {
if !wildcard && v.Name == code {
return References{v}
}

if strings.HasPrefix(v.Name, code) {
results = append(results, v)
}
Expand Down
2 changes: 2 additions & 0 deletions httpref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ func Test_Anything(t *testing.T) {
}

func TestReferences_ByName(t *testing.T) {
Statuses = append(Statuses, Reference{Name: "501-extended"})
type args struct {
code string
}
Expand All @@ -18,6 +19,7 @@ func TestReferences_ByName(t *testing.T) {
{name: "1", want: 5},
{name: "40", want: 10},
{name: "501", want: 1},
{name: "501*", want: 2},
}

for _, tt := range tests {
Expand Down
18 changes: 18 additions & 0 deletions test/basic.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,21 @@ BIN=./httpref
[ $status -eq 0 ]
}

@test "Finds unique entry on exact match on root" {
run ${BIN} Accept
[ $status -eq 0 ]
[ "$(echo $output | grep -c '/docs/Web/HTTP/Headers/Accept$')" -eq 1 ]
}

@test "Wildcard matching works on root" {
run ${BIN} 'Accept*'
[ $status -eq 0 ]
[ "$(echo $output | grep -c '/docs/Web/HTTP/Headers/Accept$')" -eq 0 ]
}

@test "Finds unique entry on exact match on headers" {
run ${BIN} headers Accept
[ $status -eq 0 ]
[ "$(echo $output | grep -c '/docs/Web/HTTP/Headers/Accept$')" -eq 1 ]
}

0 comments on commit be98d0c

Please sign in to comment.