aboutsummaryrefslogtreecommitdiff
path: root/internal/testing/sample/sample.go
diff options
context:
space:
mode:
authorEthan Lee <ethanalee@google.com>2026-04-06 21:37:38 +0000
committerGopher Robot <gobot@golang.org>2026-04-08 11:49:01 -0700
commit0ef8af41d6814a34f239b02ab621c4cfcb8c0019 (patch)
tree9803564ec92e6cbe5ab8cf72bb2ff5a1cf197093 /internal/testing/sample/sample.go
parent372618454cdb62e4cbaab1fd14c58f2faf5db80a (diff)
downloadgo-x-pkgsite-0ef8af41d6814a34f239b02ab621c4cfcb8c0019.tar.xz
internal/api: add examples parameter to PackageParams
- Enable conditional population of examples by introducing a new examples parameter. Change-Id: I53314344a414c41b423185c115e600ec8b63e6bf Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/763282 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ethan Lee <ethanalee@google.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> kokoro-CI: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'internal/testing/sample/sample.go')
-rw-r--r--internal/testing/sample/sample.go26
1 files changed, 21 insertions, 5 deletions
diff --git a/internal/testing/sample/sample.go b/internal/testing/sample/sample.go
index f08d3bfa..5c6af994 100644
--- a/internal/testing/sample/sample.go
+++ b/internal/testing/sample/sample.go
@@ -379,13 +379,29 @@ func constructFullPath(modulePath, suffix string) string {
// Documentation returns a Documentation value for the given Go source.
// It panics if there are errors parsing or encoding the source.
func Documentation(goos, goarch, fileContents string) *internal.Documentation {
+ return documentation(goos, goarch, map[string]string{"sample.go": fileContents})
+}
+
+// DocumentationWithExamples returns a Documentation value for the given package
+// documentation and example code.
+// It panics if there are errors parsing or encoding the source.
+func DocumentationWithExamples(goos, goarch, pkgDoc, exampleCode string) *internal.Documentation {
+ return documentation(goos, goarch, map[string]string{
+ "pkg.go": "// Package pkg is a package.\npackage pkg\n" + pkgDoc,
+ "pkg_test.go": "package pkg\n" + exampleCode,
+ })
+}
+
+func documentation(goos, goarch string, files map[string]string) *internal.Documentation {
fset := token.NewFileSet()
- pf, err := parser.ParseFile(fset, "sample.go", fileContents, parser.ParseComments)
- if err != nil {
- panic(err)
- }
docPkg := godoc.NewPackage(fset, nil)
- docPkg.AddFile(pf, true)
+ for name, contents := range files {
+ pf, err := parser.ParseFile(fset, name, contents, parser.ParseComments)
+ if err != nil {
+ panic(err)
+ }
+ docPkg.AddFile(pf, true)
+ }
src, err := docPkg.Encode(context.Background())
if err != nil {
panic(err)