Skip to content

Commit

Permalink
Merge pull request #110 from bmf-san/feature/add-tests-for-increasing…
Browse files Browse the repository at this point in the history
…-coverage

Add tests for increasing coverage
  • Loading branch information
bmf-san authored Jul 14, 2023
2 parents 047ef05 + b3e16a1 commit 0a39cf5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
11 changes: 1 addition & 10 deletions trie.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package goblin

import (
"fmt"
"net/http"
"path"
"regexp"
Expand Down Expand Up @@ -164,10 +163,7 @@ type regCache struct {
func (rc *regCache) getReg(ptn string) (*regexp.Regexp, error) {
v, ok := rc.s.Load(ptn)
if ok {
reg, ok := v.(*regexp.Regexp)
if !ok {
return nil, fmt.Errorf("the value of %q is wrong", ptn)
}
reg, _ := v.(*regexp.Regexp)
return reg, nil
}
reg, err := regexp.Compile(ptn)
Expand All @@ -185,10 +181,6 @@ func (t *tree) Search(path string) (*action, Params, error) {
path = cleanPath(path)
curNode := t.node

if path == "/" && curNode.action == nil {
return nil, nil, ErrNotFound
}

path = removeTrailingSlash(path)

cnt := strings.Count(path, "/")
Expand Down Expand Up @@ -217,7 +209,6 @@ func (t *tree) Search(path string) (*action, Params, error) {
// no matching path was found.
return nil, nil, ErrNotFound
}
break
}

nextNode := curNode.getChild(l)
Expand Down
65 changes: 65 additions & 0 deletions trie_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package goblin

import (
"fmt"
"net/http"
"reflect"
"regexp"
"testing"
)

Expand Down Expand Up @@ -703,6 +705,7 @@ func TestSearchRegexp(t *testing.T) {
fooBarHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
fooBarIDHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
fooBarIDNameHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
bazInvalidIDHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

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

cases := []caseWithFailure{
{
Expand Down Expand Up @@ -883,6 +887,14 @@ func TestSearchRegexp(t *testing.T) {
},
},
},
{
hasError: true,
item: &item{
path: "/baz/1",
},
expectedAction: nil,
expectedParams: Params{},
},
}

testWithFailure(t, tree, cases)
Expand Down Expand Up @@ -1010,6 +1022,59 @@ func testWithFailure(t *testing.T, tree *tree, cases []caseWithFailure) {
}
}

func TestGetReg(t *testing.T) {
cases := []struct {
name string
ptn string
isCached bool
expectedReg *regexp.Regexp
expectedErr error
}{
{
name: "Valid - no cache",
ptn: `\d+`,
isCached: false,
expectedReg: regexp.MustCompile(`\d+`),
expectedErr: nil,
},
{
name: "Valid - cached",
ptn: `\d+`,
isCached: true,
expectedReg: regexp.MustCompile(`\d+`),
expectedErr: nil,
},
{
name: "Invalid - regexp compile error",
ptn: `[\d+`,
isCached: false,
expectedReg: nil,
expectedErr: fmt.Errorf("error parsing regexp: missing closing ]: `[\\d+`"),
},
}

cache := regCache{}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if c.isCached {
cache.s.Store(c.ptn, c.expectedReg)
}
reg, err := cache.getReg(c.ptn)

if !reflect.DeepEqual(reg, c.expectedReg) {
t.Errorf("actual:%v expected:%v", reg, c.expectedReg)
}

if err != nil {
if err.Error() != c.expectedErr.Error() {
t.Errorf("actual:%v expected:%v", err.Error(), c.expectedErr.Error())
}
}
})
}
}

func TestGetPattern(t *testing.T) {
cases := []struct {
actual string
Expand Down

0 comments on commit 0a39cf5

Please sign in to comment.