gotestast

package
v1.27.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	GEN_TESTSUITE_FILE   = regexp.MustCompile(`^// Code generated by "gotest \(github\.com/mvrahden/go-test\)"; DO NOT EDIT\.(?:$|\n)`)
	IS_TEST_SUITE        = &regexpW{regexp2.MustCompile(`^(?!ƒƒ_GOTEST_|_)(?:X_|F_)?.+TestSuite$`, regexp2.ECMAScript)}
	IS_TEST_SUITE_METHOD = &regexpW{regexp2.MustCompile(`^(?:BeforeAll|AfterAll|BeforeEach|AfterEach|SuiteConfig|SuiteGuard|(?:X_|F_)?Test.+)$`, regexp2.ECMAScript)}
	IS_BEFORE_ALL        = regexp.MustCompile(`^BeforeAll$`)
	IS_AFTER_ALL         = regexp.MustCompile(`^AfterAll$`)
	IS_BEFORE_EACH       = regexp.MustCompile(`^BeforeEach$`)
	IS_AFTER_EACH        = regexp.MustCompile(`^AfterEach$`)
	IS_TEST_CASE         = &regexpW{regexp2.MustCompile(`^(?:X_|F_)?Test.+$`, regexp2.ECMAScript)} // matches all test cases
	IS_TEST_CASE_ASYNC   = regexp.MustCompile(`^(?:X_|F_)?Test.+Async$`)                           // matches all test cases with async suffix
)

Functions

func ClassifyLocalFields added in v1.4.0

func ClassifyLocalFields(f *FixtureSpec) map[string]bool

ClassifyLocalFields analyzes a Hydrate method's AST to determine which exported fixture fields are assigned (directly or via one-level-deep receiver method calls). Fields assigned in Hydrate are "local" — they are excluded from JSON serialization and reconstructed in the test process.

func ClassifyLocalFieldsRaw added in v1.6.0

func ClassifyLocalFieldsRaw(hydrateDecl *ast.FuncDecl, fixtureName string, syntax []*ast.File, info *types.Info) map[string]bool

ClassifyLocalFieldsRaw performs the same analysis as ClassifyLocalFields but accepts raw inputs instead of a FixtureSpec. This supports cross-package fixture resolution where no FixtureSpec exists.

func DetermineFixtureHarness

func DetermineFixtureHarness(n ast.Node, pkg *packages.Package, f *FixtureSpec) (token.Pos, error)

DetermineFixtureHarness inspects a FuncDecl AST node to see if it is a lifecycle method (BeforeAll, AfterAll, BeforeEach, AfterEach) on the given fixture spec. It validates the method signature according to the fixture kind and populates the corresponding field on the FixtureSpec.

func DetermineTestSuiteHarness

func DetermineTestSuiteHarness(n ast.Node, pkg *packages.Package, s *TestSuiteSpec) (token.Pos, error)

func FindMethodDecl added in v1.25.0

func FindMethodDecl(pkg *packages.Package, receiverName, methodName string) *ast.FuncDecl

FindMethodDecl finds the FuncDecl for a named method on a receiver type within the given package's syntax.

func ReceiverTypeName added in v1.25.0

func ReceiverTypeName(expr ast.Expr) string

ReceiverTypeName extracts the type name from a receiver expression, unwrapping pointer and generic type wrappers.

func SubtestName added in v1.27.0

func SubtestName(s string) string

SubtestName applies the same rewriting the testing package uses for subtest names. A statically derived name that does not match the runtime one byte for byte would produce two tree entries for one behavior, so this is the contract between the two.

func ValidateContextConsistency added in v1.10.0

func ValidateContextConsistency(ts *TestSuiteSpec) error

ValidateContextConsistency checks that all methods in the suite agree on whether they accept a context parameter, and that context types match BeforeEach's return type.

func ValidateFixtureConsistency added in v1.16.0

func ValidateFixtureConsistency(f *FixtureSpec) (token.Pos, error)

ValidateFixtureConsistency checks post-collection invariants on a fixture spec.

Types

type Behavior added in v1.27.0

type Behavior struct {
	// Name is the subtest segment go test will produce: the same sanitisation
	// the testing package applies, plus the "#01" it appends to a name that
	// repeats among its siblings. A description containing a slash is split
	// into one Behavior per level, because that is what go test does with it.
	// All of this has to match byte for byte, because it is how a
	// statically-known behavior is reconciled with the one observed at run time.
	Name     string
	Display  string
	Kind     BehaviorKind
	Line     int
	Children []*Behavior
}

type BehaviorKind added in v1.27.0

type BehaviorKind int
const (
	// BehaviorWhen is a `t.When(...)` block: a grouping node with children.
	BehaviorWhen BehaviorKind = iota
	// BehaviorIt is a `t.It(...)` block: a leaf behavior.
	BehaviorIt
	// BehaviorEach is one row of a `gotest.Each` table, named from the entry's
	// Desc or Name field exactly as the runtime names it.
	BehaviorEach
)

type FixtureKind

type FixtureKind int

FixtureKind distinguishes package-scoped fixtures from shared (cross-package) fixtures.

const (
	FixtureKindUnknown FixtureKind = iota
	PackageFixture
	SharedFixture
)

type FixtureSpec

type FixtureSpec struct {
	Kind FixtureKind

	Config        *types.Func // FixtureConfig()/SharedFixtureConfig() method, may be nil
	BeforeAll     *types.Func
	AfterAll      *types.Func   // may be nil
	BeforeEach    *types.Func   // may be nil
	AfterEach     *types.Func   // may be nil
	Hydrate       *types.Func   // shared fixtures only, may be nil
	Dehydrate     *types.Func   // shared fixtures only, may be nil
	HydrateDecl   *ast.FuncDecl // AST for Hydrate body analysis, may be nil
	ParentFixture *FixtureSpec  // pointer to parent fixture (via embedding), may be nil
	// contains filtered or unexported fields
}

FixtureSpec describes a fixture type identified by naming convention.

func DetermineFixture

func DetermineFixture(n ast.Node, pkg *packages.Package) (*FixtureSpec, error)

DetermineFixture inspects an AST node for a struct type whose name ends in "SharedFixture" (→ SharedFixture kind) or "Fixture" (→ PackageFixture kind). Returns nil if the type name doesn't match either suffix, or an error if the matching type is not a struct.

func NewFixtureSpecForTest

func NewFixtureSpecForTest(name string, kind FixtureKind) *FixtureSpec

NewFixtureSpecForTest creates a minimal FixtureSpec for use in unit tests. It sets only the Kind and ts fields (so Identifier() works).

func NewFixtureSpecForTestWithPkg

func NewFixtureSpecForTestWithPkg(name string, kind FixtureKind, pkgPath string) *FixtureSpec

NewFixtureSpecForTestWithPkg creates a minimal FixtureSpec for use in unit tests, including a package path so that PackagePath() works.

func (*FixtureSpec) ExportedFieldNames added in v1.4.0

func (f *FixtureSpec) ExportedFieldNames() []string

ExportedFieldNames returns the names of all exported fields on the fixture struct.

func (*FixtureSpec) Identifier

func (f *FixtureSpec) Identifier() string

Identifier returns the fixture type name.

func (*FixtureSpec) PackageName

func (f *FixtureSpec) PackageName() string

PackageName returns the package name of the fixture type.

func (*FixtureSpec) PackagePath

func (f *FixtureSpec) PackagePath() string

PackagePath returns the import path of the package that defines this fixture.

func (*FixtureSpec) PackageSyntax added in v1.4.0

func (f *FixtureSpec) PackageSyntax() []*ast.File

PackageSyntax returns the parsed AST files for the fixture's package.

func (*FixtureSpec) PackageTypesInfo added in v1.4.0

func (f *FixtureSpec) PackageTypesInfo() *types.Info

PackageTypesInfo returns the type-checking results for the fixture's package.

func (*FixtureSpec) StructType

func (f *FixtureSpec) StructType() *types.Struct

StructType returns the underlying *types.Struct for field inspection.

type MethodSpec added in v1.27.0

type MethodSpec struct {
	Behaviors []*Behavior
	Complete  bool
	Notes     []string
}

MethodSpec is the behavior tree of a single test method. Complete reports whether the tree is exhaustive: when false, the method declares behaviors this walker cannot see, and Notes says which construct defeated it.

type SkippedTestCases

type SkippedTestCases map[*TestSuiteSpec][]*TestSuiteMethod

type SkippedTestSuites

type SkippedTestSuites []*TestSuiteSpec

type TestSuiteHarness

type TestSuiteHarness struct {
	BeforeAll      *TestSuiteMethod
	BeforeEach     *TestSuiteMethod
	AfterAll       *TestSuiteMethod
	AfterEach      *TestSuiteMethod
	TestCases      []*TestSuiteMethod
	Config         *types.Func // SuiteConfig() method, may be nil
	Guard          *types.Func // SuiteGuard() method, may be nil
	ConfigParallel bool
	// ConfigExclusive mirrors SuiteConfig.Exclusive, resolved statically so
	// the runner can schedule the suite outside the parallel bulk.
	ConfigExclusive bool
}

type TestSuiteMethod

type TestSuiteMethod struct {
	// contains filtered or unexported fields
}

func (*TestSuiteMethod) ContextTypeName added in v1.10.0

func (m *TestSuiteMethod) ContextTypeName(pkg *types.Package) string

func (*TestSuiteMethod) HasContextParam added in v1.10.0

func (m *TestSuiteMethod) HasContextParam() bool

func (*TestSuiteMethod) HasReturn added in v1.10.0

func (m *TestSuiteMethod) HasReturn() bool

func (*TestSuiteMethod) Identifier

func (m *TestSuiteMethod) Identifier() string

Identifier returns the test methods identifier.

FOR RENDERING

func (*TestSuiteMethod) IsAsync added in v1.26.0

func (m *TestSuiteMethod) IsAsync() bool

func (*TestSuiteMethod) IsExcluded added in v1.3.0

func (m *TestSuiteMethod) IsExcluded() bool

func (*TestSuiteMethod) IsFocused added in v1.3.0

func (m *TestSuiteMethod) IsFocused() bool

func (*TestSuiteMethod) Pos added in v1.3.0

func (m *TestSuiteMethod) Pos() token.Pos

func (*TestSuiteMethod) ReturnTypeName added in v1.10.0

func (m *TestSuiteMethod) ReturnTypeName(pkg *types.Package) string

func (*TestSuiteMethod) UsesStdlibT

func (m *TestSuiteMethod) UsesStdlibT() bool

UsesStdlibT returns true if the method's parameter is *testing.T instead of *gotest.T.

type TestSuiteSpec

type TestSuiteSpec struct {
	// contains filtered or unexported fields
}

func DetermineTestSuite

func DetermineTestSuite(n ast.Node, pkg *packages.Package) (*TestSuiteSpec, token.Pos, error)

func NewTestSuiteSpecForTest added in v1.16.0

func NewTestSuiteSpecForTest(name, pkgName string, isGenericAlias bool) *TestSuiteSpec

NewTestSuiteSpecForTest creates a minimal TestSuiteSpec for use in unit tests.

func (*TestSuiteSpec) AfterAll

func (ts *TestSuiteSpec) AfterAll() *TestSuiteMethod

AfterAll returns the method pointer.

FOR RENDERING

func (*TestSuiteSpec) AfterEach

func (ts *TestSuiteSpec) AfterEach() *TestSuiteMethod

AfterEach returns the method pointer.

FOR RENDERING

func (*TestSuiteSpec) BeforeAll

func (ts *TestSuiteSpec) BeforeAll() *TestSuiteMethod

BeforeAll returns the method pointer.

FOR RENDERING

func (*TestSuiteSpec) BeforeEach

func (ts *TestSuiteSpec) BeforeEach() *TestSuiteMethod

BeforeEach returns the method pointer.

FOR RENDERING

func (*TestSuiteSpec) BehaviorsOf added in v1.27.0

func (ts *TestSuiteSpec) BehaviorsOf(m *TestSuiteMethod) MethodSpec

BehaviorsOf walks a test method body and returns the behaviors it declares.

func (*TestSuiteSpec) ContextTypeName added in v1.10.0

func (ts *TestSuiteSpec) ContextTypeName() string

ContextTypeName returns the rendered type name of the BeforeEach return type.

FOR RENDERING

func (*TestSuiteSpec) ContextTypePkgPath added in v1.10.0

func (ts *TestSuiteSpec) ContextTypePkgPath() string

ContextTypePkgPath returns the import path of the package containing the context type, or empty string if the type is in the same package.

FOR RENDERING

func (*TestSuiteSpec) FileSet added in v1.3.0

func (ts *TestSuiteSpec) FileSet() *token.FileSet

func (*TestSuiteSpec) Fixture

func (ts *TestSuiteSpec) Fixture() *FixtureSpec

Fixture returns the fixture spec embedded in this test suite, or nil.

FOR RENDERING

func (*TestSuiteSpec) FixtureFieldName added in v1.6.0

func (ts *TestSuiteSpec) FixtureFieldName() string

FixtureFieldName returns the field name used in the suite struct for the fixture pointer.

func (*TestSuiteSpec) FullIdentifier

func (ts *TestSuiteSpec) FullIdentifier() string

FullIdentifier returns the Types Name plus it's package identifier with dot-notation.

FOR RENDERING

func (*TestSuiteSpec) HasConfig added in v1.4.0

func (ts *TestSuiteSpec) HasConfig() bool

HasConfig returns true when the suite defines a SuiteConfig() marker method.

FOR RENDERING

func (*TestSuiteSpec) HasGuard added in v1.10.0

func (ts *TestSuiteSpec) HasGuard() bool

HasGuard returns true when the suite defines a SuiteGuard() marker method.

FOR RENDERING

func (*TestSuiteSpec) HasReturningBeforeEach added in v1.10.0

func (ts *TestSuiteSpec) HasReturningBeforeEach() bool

HasReturningBeforeEach returns true when BeforeEach is defined and returns a context value.

FOR RENDERING

func (*TestSuiteSpec) Identifier

func (ts *TestSuiteSpec) Identifier() string

Identifier returns the Types Name.

FOR RENDERING

func (*TestSuiteSpec) IsExcluded added in v1.3.0

func (ts *TestSuiteSpec) IsExcluded() bool

func (*TestSuiteSpec) IsExclusive added in v1.27.0

func (ts *TestSuiteSpec) IsExclusive() bool

IsExclusive returns true when SuiteConfig has Exclusive: true — the runner then dispatches this suite strictly alone, after the parallel bulk.

func (*TestSuiteSpec) IsFocused added in v1.3.0

func (ts *TestSuiteSpec) IsFocused() bool

func (*TestSuiteSpec) IsGenericAlias added in v1.16.0

func (ts *TestSuiteSpec) IsGenericAlias() bool

IsGenericAlias returns true if this suite is a type alias of a generic type.

func (*TestSuiteSpec) IsMethodParallel added in v1.10.0

func (ts *TestSuiteSpec) IsMethodParallel() bool

IsMethodParallel returns true when SuiteConfig has Parallel: true.

FOR RENDERING

func (*TestSuiteSpec) IsPxTestSuite

func (ts *TestSuiteSpec) IsPxTestSuite() bool

IsPxTestSuite gives info about the test suites declarated package.

FOR RENDERING

func (*TestSuiteSpec) Package added in v1.5.0

func (ts *TestSuiteSpec) Package() *packages.Package

Package returns the underlying *packages.Package.

func (*TestSuiteSpec) PackageName

func (ts *TestSuiteSpec) PackageName() string

Identifier returns the Types Name.

FOR RENDERING

func (*TestSuiteSpec) Pos added in v1.3.0

func (ts *TestSuiteSpec) Pos() token.Pos

func (*TestSuiteSpec) SetFixture

func (ts *TestSuiteSpec) SetFixture(f *FixtureSpec)

SetFixture sets the fixture spec for this test suite.

func (*TestSuiteSpec) SetFixtureFieldName added in v1.6.0

func (ts *TestSuiteSpec) SetFixtureFieldName(name string)

SetFixtureFieldName sets the field name used in the suite struct for the fixture pointer.

func (*TestSuiteSpec) StructType

func (ts *TestSuiteSpec) StructType() *types.Struct

StructType returns the underlying *types.Struct for field inspection.

func (*TestSuiteSpec) TestCases

func (ts *TestSuiteSpec) TestCases() []*TestSuiteMethod

TestCases returns the sequential test cases slice.

FOR RENDERING

func (*TestSuiteSpec) TypeSpecPos added in v1.5.0

func (ts *TestSuiteSpec) TypeSpecPos() token.Pos

TypeSpecPos returns the token position of the test suite type name.

type TestSuiteSpecSet

type TestSuiteSpecSet []*TestSuiteSpec

func (TestSuiteSpecSet) ReduceToEffectiveSet

Jump to

Keyboard shortcuts

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