tests/search: add test for default search mode
Tests are added to ensure that the heuristics we use for determined the
search mode are correct.
Change-Id: Ic0b528e19a6f43e68a43215d25e8cbec9df20814
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/346535
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
diff --git a/tests/search/importedby.txt b/tests/search/importedby.txt
index 6b3839d..23b83dd 100644
--- a/tests/search/importedby.txt
+++ b/tests/search/importedby.txt
@@ -6,3 +6,5 @@
math/big, 88393
github.com/go-openapi/strfmt, 11101
gopkg.in/foo.v1, 1
+github.com/julieqiu/api-demo, 0
+github.com/beego/bee/cmd/commands/api, 476
diff --git a/tests/search/main.go b/tests/search/main.go
index 881d6be..72575f4 100755
--- a/tests/search/main.go
+++ b/tests/search/main.go
@@ -53,6 +53,7 @@
)
var testFiles = []string{
+ "tests/search/scripts/default.txt",
"tests/search/scripts/symbolsearch.txt",
}
@@ -122,7 +123,7 @@
}
if want.symbol != got.SymbolName || want.pkg != got.PackagePath || st.mode != searchPage.SearchMode {
output = append(output,
- fmt.Sprintf("query %s, mismatch result %d:\n\twant: %q %q [%q]\n\t got: %q %q [%q]\n",
+ fmt.Sprintf("query %s, mismatch result %d:\n\twant: %q %q [m=%q]\n\t got: %q %q [m=%q]\n",
st.query, i+1,
want.pkg, want.symbol, st.mode,
got.PackagePath, got.SymbolName, searchPage.SearchMode))
@@ -176,7 +177,7 @@
// have passed a test case result, another newline, or a comment,
// otherwise this file can't be valid.
if curr != posNewline && curr != posResult {
- return nil, fmt.Errorf("invalid syntax on line %d: %q", num, line)
+ return nil, fmt.Errorf("invalid syntax on line %d (%q): %q", num, filename, line)
}
if curr == posResult {
// This is the first time that we have seen a newline for this
@@ -214,12 +215,18 @@
// an expected search result.
curr = posResult
parts := strings.Split(line, " ")
- if len(parts) != 2 {
- return nil, fmt.Errorf("invalid syntax on line %d: %q", num, line)
- }
- r := &searchResult{
- symbol: parts[0],
- pkg: parts[1],
+ r := &searchResult{}
+ if test.mode == "symbol" {
+ if len(parts) != 2 {
+ return nil, fmt.Errorf("invalid syntax on line %d (%q): %q (want symbol result)", num, filename, line)
+ }
+ r.symbol = parts[0]
+ r.pkg = parts[1]
+ } else {
+ if len(parts) != 1 {
+ return nil, fmt.Errorf("invalid syntax on line %d (%q): %q (want package result)", num, filename, line)
+ }
+ r.pkg = parts[0]
}
test.results = append(test.results, r)
default:
diff --git a/tests/search/scripts/default.txt b/tests/search/scripts/default.txt
new file mode 100644
index 0000000..8d76491
--- /dev/null
+++ b/tests/search/scripts/default.txt
@@ -0,0 +1,15 @@
+# Copyright 2021 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+#
+# This file contains test scripts for queries and their default search mode.
+
+github.com defaults to package mode
+[package] github.com
+github.com/go-openapi/strfmt
+github.com/beego/bee/cmd/commands/api
+github.com/julieqiu/api-demo/tar
+
+gopkg.in search defaults to package mode
+[package] gopkg.in
+gopkg.in/foo.v1