aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/go/doc/example.go
blob: d5b58d266436ed8247fd8c3abe44d637888eaa2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2011 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.

// Extract example functions from package ASTs.

package doc

import (
	"go/ast"
	"go/printer"
	"strings"
	"unicode"
	"unicode/utf8"
)

type Example struct {
	Name   string                 // name of the item being demonstrated
	Body   *printer.CommentedNode // code
	Output string                 // expected output
}

func Examples(pkg *ast.Package) []*Example {
	var examples []*Example
	for _, src := range pkg.Files {
		for _, decl := range src.Decls {
			f, ok := decl.(*ast.FuncDecl)
			if !ok {
				continue
			}
			name := f.Name.Name
			if !isTest(name, "Example") {
				continue
			}
			examples = append(examples, &Example{
				Name: name[len("Example"):],
				Body: &printer.CommentedNode{
					Node:     f.Body,
					Comments: src.Comments,
				},
				Output: f.Doc.Text(),
			})
		}
	}
	return examples
}

// isTest tells whether name looks like a test, example, or benchmark.
// It is a Test (say) if there is a character after Test that is not a
// lower-case letter. (We don't want Testiness.)
func isTest(name, prefix string) bool {
	if !strings.HasPrefix(name, prefix) {
		return false
	}
	if len(name) == len(prefix) { // "Test" is ok
		return true
	}
	rune, _ := utf8.DecodeRuneInString(name[len(prefix):])
	return !unicode.IsLower(rune)
}