Documentation
¶
Overview ¶
Package srctesting contains common helpers for unit testing source code analysis and transformation.
Index ¶
- func Format(t *testing.T, fset *token.FileSet, node any) string
- func GetNodeAtLineNo[N ast.Node](file *ast.File, fSet *token.FileSet, lineNo int) N
- func LookupObj(pkg *types.Package, name string) types.Object
- func ParseDecl(t *testing.T, src string) ast.Decl
- func ParseFuncDecl(t *testing.T, src string) *ast.FuncDecl
- func ParseSources(t *testing.T, sourceFiles []Source, auxFiles []Source) *packages.Package
- func ParseSpec(t *testing.T, src string) ast.Spec
- type Fixture
- type Source
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Format ¶
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 ¶
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 ¶
LookupObj returns a top-level object with the given name.
Methods can be referred to as RecvTypeName.MethodName.
func ParseDecl ¶
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 ¶
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 ¶
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`.
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 (*Fixture) Check ¶
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.