summaryrefslogtreecommitdiff
path: root/src/go/doc/example.go
Commit message (Collapse)AuthorAgeFilesLines
* go/doc: avoid panic on references to functions with no bodyNorman B. Lancaster2021-03-301-1/+4
| | | | | | | | | | | | | | | | | | This change guards a call to ast.Inspect with a nil check on the first argument. This avoids a panic when inspecting a reference to a function with a nil body. This can only happen when a function body is defined outside Go. Fixes #42706 Change-Id: I91bc607b24b6224920c24cfd07e76ce7737a98d4 GitHub-Last-Rev: 08072b9ce5c1fd4ee77eba6f1acc0a84e838ad7b GitHub-Pull-Request: golang/go#43011 Reviewed-on: https://go-review.googlesource.com/c/go/+/275516 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Trust: Daniel Martí <mvdan@mvdan.cc> Trust: Emmanuel Odeke <emmanuel@orijtech.com> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Go Bot <gobot@golang.org>
* go/doc: support examples on methods from embedded unexported typesJonathan Amsterdam2020-08-211-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In type T1 struct { t2 } type t2 int func (t2) M() T1 has method M because it embeds t2, which has M. Classify the example func ExampleT1_M with T1 instead of ignoring it, as is done currently. There is no other way to provide an example for such a method, since its original type is unexported. Continue to ignore examples on methods from embedded types that are exported, unless in AllMethods mode. Examples for those methods could be written on the original type. The change involves removing a check in classifyExamples. The check isn't necessary to get the above behavior because reader.collectEmbeddedMethods and sortedFuncs already generate the appropriate list of methods. For #40172. Change-Id: Ibe7d965ecba6426466184e6e6655fc05989e9caf Reviewed-on: https://go-review.googlesource.com/c/go/+/249557 Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
* go/doc: fix detection of whole file examplesGregory Petrosyan2020-04-151-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | After CL 211357 (commit 499dc1c), hasTests and numDecl were not updated properly for function declarations with parameters, which affected the whole file example detection logic. This caused examples like package foo_test func Foo(x int) { } func Example() { fmt.Println("Hello, world!") // Output: Hello, world! } to not be detected as whole file ones. Change-Id: I9ebd47e52d7ee9d91eb6f8e0257511de69b2a402 GitHub-Last-Rev: cc71c31124f6e3514f4e33ac7b169eca74c8bcb7 GitHub-Pull-Request: golang/go#37730 Reviewed-on: https://go-review.googlesource.com/c/go/+/222477 Reviewed-by: Agniva De Sarker <agniva.quicksilver@gmail.com> Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Agniva De Sarker <agniva.quicksilver@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* src/go/doc: in Examples, check for len of params.List, not nilnessMichael Matloob2019-12-171-1/+1
| | | | | | | | | | | | This makes the check the same as the one in the tests vet check. It's safer to check the number of arguments rather than for a nil slice. Change-Id: I8e04e9c612573f334770c1c4245238649656c6e2 Reviewed-on: https://go-review.googlesource.com/c/go/+/211598 Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
* go/doc: ignore example functions with argumentsMichael Matloob2019-12-161-0/+3
| | | | | | | | | | | | | | | | | | | | | | An Example function with arguments is not a valid example to be run with go test. Don't return those functions from Examples. This means that some functions that were previously showing up in Examples will no longer show up. But those functions were not being tested properly so the fact that they were showing up is misleading. This fixes an issue where a confusing compiler error was showing up when running go test on a file with an invalid example. While that issue could have been fixed by returning an error, this is more consistent with the behavior of go/doc.Examples, and the tests checker in vet will catch this issue. Fixes #35284 Change-Id: I2101a7d19f38522ef9c2e50967f9cfb30d28c730 Reviewed-on: https://go-review.googlesource.com/c/go/+/211357 Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
* go/doc: add NewFromFiles with support for classifying examplesDmitri Shuralyov2019-11-121-5/+106
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This CL is based on work started by Joe Tsai in CL 94855. It's rebased on top of the latest master branch, and addresses various code review comments and findings from attempting to use the original CL in practice. The testing package documents a naming convention for examples so that documentation tools can associate them with: • a package (Example or Example_suffix) • a function F (ExampleF or ExampleF_suffix) • a type T (ExampleT or ExampleT_suffix) • a method T.M (ExampleT_M or ExampleT_M_suffix) This naming convention is in widespread use and enforced via existing go vet checks. This change adds first-class support for classifying examples to go/doc, the package responsible for computing package documentation from Go AST. There isn't a way to supply test files to New that works well. External test files may have a package name with "_test" suffix, so ast.NewPackage may end up using the wrong package name if given test files. A workaround is to add test files to *ast.Package.Files after it is returned from ast.NewPackage: pkg, _ := ast.NewPackage(fset, goFiles, ...) for name, f := range testGoFiles { pkg.Files[name] = f } p := doc.New(pkg, ...) But that is not a good API. After nearly 8 years, a new entry-point is added to the go/doc package, the function NewFromFiles. It accepts a Go package in the form of a list of parsed Go files (including _test.go files) and an import path. The caller is responsible with filtering out files based on build constraints, as was the case before with New. NewFromFiles computes package documentation from .go files, extracts examples from _test.go files and classifies them. Examples fields are added to Package, Type, and Func. They are documented to only be populated with examples found in _test.go files provided to NewFromFiles. The new behavior is: 1. NewFromFiles computes package documentation from provided parsed .go files. It extracts examples from _test.go files. 2. It assigns each Example to corresponding Package, Type, or Func. 3. It sets the Suffix field in each example to the suffix. 4. Malformed examples are skipped. This change implements behavior that matches the current behavior of existing godoc-like tools, and will enable them to rely on the logic in go/doc instead of reimplementing it themselves. Fixes #23864 Change-Id: Iae834f2ff92fbd1c93a9bb7c2bf47d619bee05cf Reviewed-on: https://go-review.googlesource.com/c/go/+/204830 Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* cmd/go: avoid compiling most regexes at initDaniel Martí2019-02-271-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | These regexes are all related to commands like get and build, so they're unnecessary for simpler commands like env. In particular, we need env to be fast, since libraries like go/packages call it early and often. Some external Go tools are interactive, so milliseconds matter. lazyregexp eagerly compiles the patterns when running from within a test binary, so there's no longer any need to do that as part of non-test binaries. Picking up the low-hanging fruit spotted by 'perf record' shaves off well over a full millisecond off the benchmark on my laptop: name old time/op new time/op delta ExecGoEnv-8 4.92ms ± 1% 3.81ms ± 0% -22.52% (p=0.004 n=6+5) This CL required adding a few more methods to the lazy regexp wrapper. Updates #29382. Change-Id: I22417ab6258f7437a2feea0d25ceb2bb4d735a15 Reviewed-on: https://go-review.googlesource.com/c/155540 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* go/doc: handle Examples with no bodyAgniva De Sarker2018-12-161-0/+3
| | | | | | | | | | Fixes #29271 Change-Id: Iff6a16c659ad6ec1b4d9559fcbcd40196086c60e Reviewed-on: https://go-review.googlesource.com/c/154380 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* go/doc: disable playground for examples that use syscall/jsRichard Musiol2018-11-201-0/+5
| | | | | | | | | | | | The playground is not using GOOS=js, so it is not able to use the package syscall/js. Examples that depend on syscall/js should not show a "Run" button. Fixes #28526. Change-Id: I8b2fcdd0c0ee517a5c3864bf459f813129542389 Reviewed-on: https://go-review.googlesource.com/c/148918 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* go/doc: inspect function signature for building playground examplesYury Smolsky2018-11-051-0/+12
| | | | | | | | | | | | | | | | | | | | | This documentation example was broken: https://golang.org/pkg/image/png/#example_Decode. It did not have the "io" package imported, The package was referenced in the result type of the function. The "playExample" function did not inspect the result types of declared functions. This CL adds inspecting of parameters and result types of functions. Fixes #28492 Updates #9679 Change-Id: I6d8b11bad2db8ea8ba69039cfaa914093bdd5132 Reviewed-on: https://go-review.googlesource.com/c/146118 Run-TryBot: Yury Smolsky <yury@smolsky.by> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* go/doc: skip examples with no bodyMuhammad Falak R Wani2018-10-091-0/+3
| | | | | | | | | | Fixes #28044 Change-Id: I0052e078dd34dc3546204416bcc5a99e3146c535 Reviewed-on: https://go-review.googlesource.com/c/140317 Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* doc.Example should not worry about unresolved blank identifiersMostyn Bramley-Moore2018-08-081-1/+1
| | | | | | | | | | | | | | | | | | | | | | | https://golang.org/pkg/bufio/#example_Scanner_custom is not directly runnable in the playground via godoc, but if I copy+paste the code into https://play.golang.org/ then it runs just fine. This seems to be due to the blank identifier being considered unresolved in the following line in the example: _, err = strconv.ParseInt(string(token), 10, 32) But that's the whole point of blank identifiers- they're not supposed to be resolved. So let's skip adding the blank identifier to doc.playExample's unresolved map. Fixes #26447 Change-Id: I52bc7d99be1d14a61dc012d10c18349d52ba4c51 GitHub-Last-Rev: 9172e9dc1378b0f37f96fc2e1ade4dda9d848398 GitHub-Pull-Request: golang/go#26448 Reviewed-on: https://go-review.googlesource.com/124775 Reviewed-by: Robert Griesemer <gri@golang.org>
* go/doc: do not treat methods as test functionsRoger Peppe2018-07-311-1/+1
| | | | | | | | | | | | | | | The example code was treating a method starting with Test as a test function when considering whether to produce a whole-file example or not. As a method can never be a test function, this isn't correct. Change-Id: Idd8ec9eaf0904af076e941d7fe7d967f6b7eef78 Reviewed-on: https://go-review.googlesource.com/125257 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Andrew Bonventre <andybons@golang.org> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org>
* go/doc: make examples that depend on top-level decls playableHiroshi Ioka2018-06-271-30/+92
| | | | | | | | | | | | | | | | | | | | | | Currently, the following example cannot run in playground: func a() { fmt.Println("A") } func ExampleA() { a() } This CL solves it. Fixes #23095 Change-Id: I5a492ff886a743f20cb4ae646e8453bde9c5f0da Reviewed-on: https://go-review.googlesource.com/83615 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* go/*: use sort.Slice to simplify some codeDaniel Martí2017-09-121-7/+4
| | | | | | | | | | | | | Skip the ones that have multiple uses for now. Also had to rename the importComment variable as it shadowed the top-level func by the same name. Change-Id: I796285aa7b4fdf2c39e652666390427d37b063ee Reviewed-on: https://go-review.googlesource.com/63150 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* testing: implement 'Unordered Output' in Examples.Brady Catherman2016-03-091-12/+18
| | | | | | | | | | | | | | | | | Adds a type of output to Examples that allows tests to have unordered output. This is intended to help clarify when the output of a command will produce a fixed return, but that return might not be in an constant order. Examples where this is useful would be documenting the rand.Perm() call, or perhaps the (os.File).Readdir(), both of which can not guarantee order, but can guarantee the elements of the output. Fixes #10149 Change-Id: Iaf0cf1580b686afebd79718ed67ea744f5ed9fc5 Reviewed-on: https://go-review.googlesource.com/19280 Reviewed-by: Andrew Gerrand <adg@golang.org>
* build: move package sources from src/pkg to srcRuss Cox2014-09-081-0/+355
Preparation was in CL 134570043. This CL contains only the effect of 'hg mv src/pkg/* src'. For more about the move, see golang.org/s/go14nopkg.