diff options
| author | Julie Qiu <julie@golang.org> | 2021-07-28 22:12:18 -0400 |
|---|---|---|
| committer | Julie Qiu <julie@golang.org> | 2021-07-30 18:56:50 +0000 |
| commit | f764a6ce1fcb6fc77bb96b501de3545c14bf3744 (patch) | |
| tree | 34e7712616a5b80867cc70511de8525bddee38c1 /internal/postgres | |
| parent | 3953453bea14f47f04a8059852c8498569dcd367 (diff) | |
| download | go-x-pkgsite-f764a6ce1fcb6fc77bb96b501de3545c14bf3744.tar.xz | |
internal/postgres/symbolsearch: add test for ParseInputType
An edge case with InputTypeTwoDots is also handled.
For golang/go#44142
Change-Id: I4547c6e237e589b13a0acc8692bfb064d514d22a
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/338171
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Diffstat (limited to 'internal/postgres')
| -rw-r--r-- | internal/postgres/symbolsearch/symbolsearch_test.go | 25 | ||||
| -rw-r--r-- | internal/postgres/symbolsearch/type.go | 8 |
2 files changed, 33 insertions, 0 deletions
diff --git a/internal/postgres/symbolsearch/symbolsearch_test.go b/internal/postgres/symbolsearch/symbolsearch_test.go index b8764196..b79ca273 100644 --- a/internal/postgres/symbolsearch/symbolsearch_test.go +++ b/internal/postgres/symbolsearch/symbolsearch_test.go @@ -22,6 +22,31 @@ func TestProcessArg(t *testing.T) { if diff := cmp.Diff(test.want, got); diff != "" { t.Errorf("mismatch (-want, +got):\n%s", diff) } + + } +} + +func TestParseInputType(t *testing.T) { + for _, test := range []struct { + name, q string + want InputType + }{ + {"no dot symbol name", "DB", InputTypeNoDot}, + {"one dot symbol name", "DB.Begin", InputTypeOneDot}, + {"one dot package dot symbol name", "sql.DB", InputTypeOneDot}, + {"two dots package name dot symbol name", "sql.DB.Begin", InputTypeTwoDots}, + {"two dots stdlib package path dot symbol name", "database/sql.DB.Begin", InputTypeTwoDots}, + {"multiword two words", "foo bar", InputTypeMultiWord}, + {"multiword three words", "foo bar baz", InputTypeMultiWord}, + {"two dots package path dot symbol name not supported", "github.com/foo/bar.DB", InputTypeNoMatch}, + {"three dots package path dot symbol name not supported", "github.com/foo/bar.DB.Begin", InputTypeNoMatch}, + } { + t.Run(test.name, func(t *testing.T) { + got := ParseInputType(test.q) + if got != test.want { + t.Errorf("ParseInputType(%q) = %q; want = %q", test.q, got, test.want) + } + }) } } diff --git a/internal/postgres/symbolsearch/type.go b/internal/postgres/symbolsearch/type.go index a65f0c32..73c9cb26 100644 --- a/internal/postgres/symbolsearch/type.go +++ b/internal/postgres/symbolsearch/type.go @@ -15,12 +15,20 @@ func ParseInputType(q string) InputType { if strings.ContainsAny(q, " \t\n") { return InputTypeMultiWord } + parts := strings.Split(q, ".") switch strings.Count(q, ".") { case 0: return InputTypeNoDot case 1: return InputTypeOneDot case 2: + if strings.Contains(parts[1], "/") { + // Example: if q = "github.com/foo/bar", parts[1] will be + // "com/foo/bar.DB". + // <package-path>.<symbol> is currently not a supported search when + // package path is not a standard library path. + return InputTypeNoMatch + } return InputTypeTwoDots default: return InputTypeNoMatch |
