srctesting

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: BSD-2-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package srctesting contains common helpers for unit testing source code analysis and transformation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format added in v1.20.0

func Format(t *testing.T, fset *token.FileSet, node any) string

Format AST node into a string.

The node type must be *ast.File, *printer.CommentedNode, []ast.Decl, []ast.Stmt, or assignment-compatible to ast.Expr, ast.Decl, ast.Spec, or ast.Stmt.

func GetNodeAtLineNo added in v1.20.0

func GetNodeAtLineNo[N ast.Node](file *ast.File, fSet *token.FileSet, lineNo int) N

GetNodeAtLineNo returns the first node of type N that starts on the given line in the given file. This helps lookup nodes that aren't named but are needed by a specific test.

func LookupObj added in v1.20.0

func LookupObj(pkg *types.Package, name string) types.Object

LookupObj returns a top-level object with the given name.

Methods can be referred to as RecvTypeName.MethodName.

func ParseDecl added in v1.20.0

func ParseDecl(t *testing.T, src string) ast.Decl

ParseDecl parses source with a single declaration and returns that declaration AST.

Fails the test if there isn't exactly one declaration in the source.

func ParseFuncDecl

func ParseFuncDecl(t *testing.T, src string) *ast.FuncDecl

ParseFuncDecl parses source with a single function defined and returns the function AST.

Fails the test if there isn't exactly one function declared in the source.

func ParseSources added in v1.20.0

func ParseSources(t *testing.T, sourceFiles []Source, auxFiles []Source) *packages.Package

ParseSources parses the given source files and returns the root package that contains the given source files.

The source file should all be from the same package as the files for the root package. At least one source file must be given. The root package's path will be `command-line-arguments`.

The auxiliary files can be for different packages but should have paths added to the source name so that they can be grouped together by package. To import an auxiliary package, the path should be prepended by `github.com/gopherjs/gopherjs/compiler`.

func ParseSpec added in v1.20.0

func ParseSpec(t *testing.T, src string) ast.Spec

ParseSpec parses source with a single declaration containing a single specification and returns that specification AST.

Fails the test if there isn't exactly one declaration and one specification in the source.

Types

type Fixture added in v1.20.0

type Fixture struct {
	T        *testing.T
	FileSet  *token.FileSet
	Info     *types.Info
	Packages map[string]*types.Package
}

Fixture provides utilities for parsing and type checking Go code in tests.

func New added in v1.20.0

func New(t *testing.T) *Fixture

New creates a fresh Fixture.

func (*Fixture) Check added in v1.20.0

func (f *Fixture) Check(importPath string, files ...*ast.File) (*types.Info, *types.Package)

Check type correctness of the provided AST.

Fails the test if type checking fails. Provided AST is expected not to have any imports. If f.Info is nil, it will create a new types.Info instance to store type checking results and return it, otherwise f.Info is used.

func (*Fixture) Import added in v1.20.0

func (f *Fixture) Import(path string) (*types.Package, error)

Import implements types.Importer.

func (*Fixture) Parse added in v1.20.0

func (f *Fixture) Parse(name, src string) *ast.File

Parse source from the string and return complete AST.

type Source added in v1.20.0

type Source struct {
	Name     string
	Contents []byte
}

Jump to

Keyboard shortcuts

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