Skip to content

Commit be98d0c

Browse files
authored
Merge pull request #5 from dnnrly/bugfix/unique-filters
Bugfix/unique filters
2 parents 6f560d6 + 2e164ff commit be98d0c

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

cmd/root.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ var rootCmd = &cobra.Command{
1717
Short: "Command line access to HTTP references",
1818
Long: `This displays useful information related to HTTP.
1919
20+
It will prefer exact matches where there are mutliple entries matching
21+
the filter (e.g. Accept and Accept-Language). If you want to match
22+
everything with the same prefix then you can use * as a wildcard suffix,
23+
for example:
24+
httpref 'Accept*'
25+
2026
Most of the content comes from the Mozilla developer
2127
documentation (https://developer.mozilla.org/en-US/docs/Web/HTTP)
2228
and is copyright Mozilla and individual contributors. See
@@ -59,7 +65,7 @@ func root(cmd *cobra.Command, args []string) error {
5965
func printResults(results httpref.References) {
6066
switch len(results) {
6167
case 0:
62-
fmt.Fprintf(os.Stderr, "Name note recognised\n")
68+
fmt.Fprintf(os.Stderr, "Filter not found any results\n")
6369
os.Exit(1)
6470
case 1:
6571
fmt.Printf("%s - %s\n\n%s\n", results[0].Name, results[0].Summary, results[0].Description)

httpref.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ type References []Reference
1414
func (r References) ByName(code string) References {
1515
results := References{}
1616

17+
wildcard := strings.HasSuffix(code, "*")
18+
code = strings.ReplaceAll(code, "*", "")
19+
1720
for _, v := range r {
21+
if !wildcard && v.Name == code {
22+
return References{v}
23+
}
24+
1825
if strings.HasPrefix(v.Name, code) {
1926
results = append(results, v)
2027
}

httpref_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ func Test_Anything(t *testing.T) {
88
}
99

1010
func TestReferences_ByName(t *testing.T) {
11+
Statuses = append(Statuses, Reference{Name: "501-extended"})
1112
type args struct {
1213
code string
1314
}
@@ -18,6 +19,7 @@ func TestReferences_ByName(t *testing.T) {
1819
{name: "1", want: 5},
1920
{name: "40", want: 10},
2021
{name: "501", want: 1},
22+
{name: "501*", want: 2},
2123
}
2224

2325
for _, tt := range tests {

test/basic.bats

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,21 @@ BIN=./httpref
2626
[ $status -eq 0 ]
2727
}
2828

29+
@test "Finds unique entry on exact match on root" {
30+
run ${BIN} Accept
31+
[ $status -eq 0 ]
32+
[ "$(echo $output | grep -c '/docs/Web/HTTP/Headers/Accept$')" -eq 1 ]
33+
}
34+
35+
@test "Wildcard matching works on root" {
36+
run ${BIN} 'Accept*'
37+
[ $status -eq 0 ]
38+
[ "$(echo $output | grep -c '/docs/Web/HTTP/Headers/Accept$')" -eq 0 ]
39+
}
40+
41+
@test "Finds unique entry on exact match on headers" {
42+
run ${BIN} headers Accept
43+
[ $status -eq 0 ]
44+
[ "$(echo $output | grep -c '/docs/Web/HTTP/Headers/Accept$')" -eq 1 ]
45+
}
46+

0 commit comments

Comments
 (0)