forked from avito-tech/go-mutesting
/
mutator.go
50 lines (38 loc) · 1.23 KB
/
mutator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package mutator
import (
"fmt"
"go/ast"
"go/types"
"sort"
)
// Mutator defines a mutator for mutation testing by returning a list of possible mutations for the given node.
type Mutator func(pkg *types.Package, info *types.Info, node ast.Node) []Mutation
var mutatorLookup = make(map[string]Mutator)
// 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.
func New(name string) (Mutator, error) {
mutator, ok := mutatorLookup[name]
if !ok {
return nil, fmt.Errorf("unknown mutator %q", name)
}
return mutator, nil
}
// List returns a list of all registered mutator names.
func List() []string {
keyMutatorLookup := make([]string, 0, len(mutatorLookup))
for key := range mutatorLookup {
keyMutatorLookup = append(keyMutatorLookup, key)
}
sort.Strings(keyMutatorLookup)
return keyMutatorLookup
}
// Register registers a mutator instance function with the given name.
func Register(name string, mutator Mutator) {
if mutator == nil {
panic("mutator function is nil")
}
if _, ok := mutatorLookup[name]; ok {
panic(fmt.Sprintf("mutator %q already registered", name))
}
mutatorLookup[name] = mutator
}