mutator

package
v2.7.6 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package mutator defines the Mutator function type and the registration mechanism used by all built-in and third-party mutation operators.

Implementing a custom operator requires three steps:

  1. Write a function with the Mutator signature.
  2. Call Register from an init() function so it is available at startup.
  3. Import your package (blank import) in a fork of cmd/go-mutesting/main.go.

Example:

func init() {
    mutator.Register("mypkg/flip-sign", flipSign)
}

func flipSign(_ *types.Package, _ *types.Info, node ast.Node) []mutator.Mutation {
    n, ok := node.(*ast.UnaryExpr)
    if !ok || n.Op != token.SUB {
        return nil
    }
    return []mutator.Mutation{
        {Change: func() { n.Op = token.ADD }, Reset: func() { n.Op = token.SUB }},
    }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func List

func List() []string

List returns a list of all registered mutator names.

func Register

func Register(name string, mutator Mutator)

Register registers a mutator instance function with the given name.

Types

type Mutation

type Mutation struct {
	// Position is the source position of the original syntax changed by this
	// mutation. MutateWalkWithPositions falls back to the visited node position
	// when it is unset, preserving compatibility with third-party mutators.
	Position token.Pos
	// Change is called before executing the exec command.
	Change func()
	// Reset is called after executing the exec command.
	Reset func()
}

Mutation defines the behavior of one mutation

type Mutator

type Mutator func(pkg *types.Package, info *types.Info, node ast.Node) []Mutation

Mutator defines a mutator for mutation testing by returning a list of possible mutations for the given node.

func New

func New(name string) (Mutator, error)

New returns a new mutator instance given the registered name of the mutator. The error return argument is not nil, if the name does not exist in the registered mutator list.

Directories

Path Synopsis
Package composite holds mutators that operate on composite literals (struct, map, and keyed array/slice literals).
Package composite holds mutators that operate on composite literals (struct, map, and keyed array/slice literals).

Jump to

Keyboard shortcuts

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