Skip to content

Commit 0a39cf5

Browse files
authored
Merge pull request #110 from bmf-san/feature/add-tests-for-increasing-coverage
Add tests for increasing coverage
2 parents 047ef05 + b3e16a1 commit 0a39cf5

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

trie.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package goblin
22

33
import (
4-
"fmt"
54
"net/http"
65
"path"
76
"regexp"
@@ -164,10 +163,7 @@ type regCache struct {
164163
func (rc *regCache) getReg(ptn string) (*regexp.Regexp, error) {
165164
v, ok := rc.s.Load(ptn)
166165
if ok {
167-
reg, ok := v.(*regexp.Regexp)
168-
if !ok {
169-
return nil, fmt.Errorf("the value of %q is wrong", ptn)
170-
}
166+
reg, _ := v.(*regexp.Regexp)
171167
return reg, nil
172168
}
173169
reg, err := regexp.Compile(ptn)
@@ -185,10 +181,6 @@ func (t *tree) Search(path string) (*action, Params, error) {
185181
path = cleanPath(path)
186182
curNode := t.node
187183

188-
if path == "/" && curNode.action == nil {
189-
return nil, nil, ErrNotFound
190-
}
191-
192184
path = removeTrailingSlash(path)
193185

194186
cnt := strings.Count(path, "/")
@@ -217,7 +209,6 @@ func (t *tree) Search(path string) (*action, Params, error) {
217209
// no matching path was found.
218210
return nil, nil, ErrNotFound
219211
}
220-
break
221212
}
222213

223214
nextNode := curNode.getChild(l)

trie_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package goblin
22

33
import (
4+
"fmt"
45
"net/http"
56
"reflect"
7+
"regexp"
68
"testing"
79
)
810

@@ -703,6 +705,7 @@ func TestSearchRegexp(t *testing.T) {
703705
fooBarHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
704706
fooBarIDHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
705707
fooBarIDNameHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
708+
bazInvalidIDHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
706709

707710
tree.Insert(`/`, rootHandler, []middleware{first})
708711
tree.Insert(`/:*[(.+)]`, rootWildCardHandler, []middleware{first})
@@ -712,6 +715,7 @@ func TestSearchRegexp(t *testing.T) {
712715
tree.Insert(`/foo/bar`, fooBarHandler, []middleware{first})
713716
tree.Insert(`/foo/bar/:id`, fooBarIDHandler, []middleware{first})
714717
tree.Insert(`/foo/bar/:id/:name`, fooBarIDNameHandler, []middleware{first})
718+
tree.Insert(`/baz/:id[[\d+]`, bazInvalidIDHandler, []middleware{first})
715719

716720
cases := []caseWithFailure{
717721
{
@@ -883,6 +887,14 @@ func TestSearchRegexp(t *testing.T) {
883887
},
884888
},
885889
},
890+
{
891+
hasError: true,
892+
item: &item{
893+
path: "/baz/1",
894+
},
895+
expectedAction: nil,
896+
expectedParams: Params{},
897+
},
886898
}
887899

888900
testWithFailure(t, tree, cases)
@@ -1010,6 +1022,59 @@ func testWithFailure(t *testing.T, tree *tree, cases []caseWithFailure) {
10101022
}
10111023
}
10121024

1025+
func TestGetReg(t *testing.T) {
1026+
cases := []struct {
1027+
name string
1028+
ptn string
1029+
isCached bool
1030+
expectedReg *regexp.Regexp
1031+
expectedErr error
1032+
}{
1033+
{
1034+
name: "Valid - no cache",
1035+
ptn: `\d+`,
1036+
isCached: false,
1037+
expectedReg: regexp.MustCompile(`\d+`),
1038+
expectedErr: nil,
1039+
},
1040+
{
1041+
name: "Valid - cached",
1042+
ptn: `\d+`,
1043+
isCached: true,
1044+
expectedReg: regexp.MustCompile(`\d+`),
1045+
expectedErr: nil,
1046+
},
1047+
{
1048+
name: "Invalid - regexp compile error",
1049+
ptn: `[\d+`,
1050+
isCached: false,
1051+
expectedReg: nil,
1052+
expectedErr: fmt.Errorf("error parsing regexp: missing closing ]: `[\\d+`"),
1053+
},
1054+
}
1055+
1056+
cache := regCache{}
1057+
1058+
for _, c := range cases {
1059+
t.Run(c.name, func(t *testing.T) {
1060+
if c.isCached {
1061+
cache.s.Store(c.ptn, c.expectedReg)
1062+
}
1063+
reg, err := cache.getReg(c.ptn)
1064+
1065+
if !reflect.DeepEqual(reg, c.expectedReg) {
1066+
t.Errorf("actual:%v expected:%v", reg, c.expectedReg)
1067+
}
1068+
1069+
if err != nil {
1070+
if err.Error() != c.expectedErr.Error() {
1071+
t.Errorf("actual:%v expected:%v", err.Error(), c.expectedErr.Error())
1072+
}
1073+
}
1074+
})
1075+
}
1076+
}
1077+
10131078
func TestGetPattern(t *testing.T) {
10141079
cases := []struct {
10151080
actual string

0 commit comments

Comments
 (0)