Commit cb7d6e37 authored by Volker Dobler's avatar Volker Dobler Committed by Andrew Gerrand

godoc: Allow examples for methods.

An example for a method M() of type T can be written as
func ExampleT_M() { ... }.
To differentiate between multiple examples for one function, type or
method a suffix with a lowercase start may be appended to the name
of the example function, e.g. ExampleFoo_basicUsage.

Fixes #2465.

R=golang-dev, adg, r, rsc, duperray.olivier, r
CC=golang-dev
https://golang.org/cl/5440100
parent 9834a25d
...@@ -26,6 +26,8 @@ import ( ...@@ -26,6 +26,8 @@ import (
"strings" "strings"
"text/template" "text/template"
"time" "time"
"unicode"
"unicode/utf8"
) )
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
...@@ -482,14 +484,24 @@ func comment_textFunc(comment, indent, preIndent string) string { ...@@ -482,14 +484,24 @@ func comment_textFunc(comment, indent, preIndent string) string {
return buf.String() return buf.String()
} }
func startsWithUppercase(s string) bool {
r, _ := utf8.DecodeRuneInString(s)
return unicode.IsUpper(r)
}
func example_htmlFunc(funcName string, examples []*doc.Example, fset *token.FileSet) string { func example_htmlFunc(funcName string, examples []*doc.Example, fset *token.FileSet) string {
var buf bytes.Buffer var buf bytes.Buffer
for _, eg := range examples { for _, eg := range examples {
// accept Foo or Foo_.* for funcName == Foo
name := eg.Name name := eg.Name
if i := strings.Index(name, "_"); i >= 0 {
name = name[:i] // strip lowercase braz in Foo_braz or Foo_Bar_braz from name
// while keeping uppercase Braz in Foo_Braz
if i := strings.LastIndex(name, "_"); i != -1 {
if i < len(name)-1 && !startsWithUppercase(name[i+1:]) {
name = name[:i]
}
} }
if name != funcName { if name != funcName {
continue continue
} }
......
...@@ -35,8 +35,15 @@ os.Stdout and os.Stderr is compared against their doc comment. ...@@ -35,8 +35,15 @@ os.Stdout and os.Stderr is compared against their doc comment.
fmt.Println("The output of this example function.") fmt.Println("The output of this example function.")
} }
Multiple example functions may be provided for a given name XXX if they are The following naming conventions are used to declare examples for a function F,
discriminated by a distinct suffix starting with "_", such as ExampleXXX_2. a type T and method M on type T:
func ExampleF() { ... } and func ExampleF_suffix() { ... }
func ExampleT() { ... } and func ExampleT_suffix() { ... }
func ExampleT_M() { ... } and func ExampleT_M_suffix() { ... }
Multiple example functions may be provided by appending a distinct suffix
to the name. The suffix must start with a lowercase letter.
Example functions without doc comments are compiled but not executed. Example functions without doc comments are compiled but not executed.
See the documentation of the testing package for more information. See the documentation of the testing package for more information.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment