srctesting

package
v1.19.0-beta2 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 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

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

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

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

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

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

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

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

func New(t *testing.T) *Fixture

New creates a fresh Fixture.

func (*Fixture) Check

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

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

Import implements types.Importer.

func (*Fixture) Parse

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

Parse source from the string and return complete AST.

type Source

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