Documentation
¶
Overview ¶
Package parser scans a package of go source to extract testable examples.
The outcome of the parser is an index of testable examples by exported function or type.
Provided testable examples are a structure that may be rendered as go code.
This package is freely inspired by go team's pkgsite tool (github.com/golang/pkgsite).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Examples ¶
type Examples map[string][]TestableExample
Examples is an index of TestableExample.
Keys are exported symbol names (not fully qualified names).
Each key may have 1 or several examples attached to it.
Example:
for function {package}.MyFunction, the key is "MyFunction".
type Extractor ¶
type Extractor struct {
// contains filtered or unexported fields
}
Extractor explores a go package (including test code) and looks for testable examples.
type Option ¶
type Option func(*Extractor)
Option configures the Extractor.
func WithBuildTags ¶
WithBuildTags sets build tags for package loading (e.g. "integrationtest").
func WithWorkDir ¶
WithWorkDir sets the working directory for package resolution.
type TestableExample ¶
type TestableExample struct {
// Name is the full example name after the "Example" prefix.
//
// For ExampleEqual, Name is "Equal".
// For ExampleEqual_basic, Name is "Equal_basic".
Name string
// Suffix is the example suffix, without leading '_'.
//
// For ExampleEqual, Suffix is "".
// For ExampleEqual_basic, Suffix is "basic".
Suffix string
// Doc is the doc comment on the example function.
Doc string
// Output is the expected output string (from "// Output:" comments).
Output string
// WholeFile indicates this is a whole-file example with supporting
// declarations (types, helper functions) outside the example function.
WholeFile bool
// contains filtered or unexported fields
}
TestableExample describes a go testable example and knows how to render as formatted go code.
func (TestableExample) Render ¶
func (x TestableExample) Render() string
Render the example as a formatted go code snippet.
For whole-file examples, the complete file is rendered (minus package and imports), preserving all supporting declarations.
For regular examples, only the function body is rendered.
In both cases, the code is formatted with format.Source.