parser

package
v2.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

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.

func New

func New(pkg string, opts ...Option) *Extractor

New Extractor for a given source package to be scanned.

pkg is an import path or a relative pattern (e.g., "./assert") resolved from the working directory.

func (*Extractor) Parse

func (e *Extractor) Parse() (Examples, error)

Parse source code including test code.

Builds an index of exported symbols (functions and types).

Attaches all identified testable examples to the index.

It may fail if the code doesn't compile.

type Option

type Option func(*Extractor)

Option configures the Extractor.

func WithBuildTags

func WithBuildTags(tags ...string) Option

WithBuildTags sets build tags for package loading (e.g. "integrationtest").

func WithWorkDir

func WithWorkDir(dir string) Option

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL