-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.go
87 lines (70 loc) · 2.25 KB
/
test.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package oozetesting
import (
"os"
"path"
"testing"
"github.com/gtramontina/ooze/internal/gomutatedfile"
"github.com/gtramontina/ooze/internal/gosourcefile"
"github.com/gtramontina/ooze/internal/gotextdiff"
"github.com/gtramontina/ooze/viruses"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Mutations map[string]struct {
SourceFileName string
MutantFileNames []string
}
type Scenarios struct {
virusName string
virus viruses.Virus
mutations Mutations
}
func NewScenarios(virusName string, virus viruses.Virus, mutations Mutations) *Scenarios {
return &Scenarios{
virusName: virusName,
virus: virus,
mutations: mutations,
}
}
func Run(t *testing.T, scenes *Scenarios) {
t.Helper()
for name, testcase := range scenes.mutations {
source, err := os.ReadFile(path.Join("testdata", testcase.SourceFileName))
assert.NoError(t, err)
expectedMutatedFiles := []*gomutatedfile.GoMutatedFile{}
for _, mutantFileName := range testcase.MutantFileNames {
mutant, err := os.ReadFile(path.Join("testdata", mutantFileName))
assert.NoError(t, err)
mutatedFile := gomutatedfile.New(scenes.virusName, testcase.SourceFileName, source, mutant)
expectedMutatedFiles = append(expectedMutatedFiles, mutatedFile)
}
t.Run(name, func(t *testing.T) {
actualMutatedFiles := mutate(
scenes.virus,
gosourcefile.New(testcase.SourceFileName, source),
)
require.Equal(t,
len(expectedMutatedFiles),
len(actualMutatedFiles),
"Expected %d mutated files; got %d", len(expectedMutatedFiles), len(actualMutatedFiles),
)
for i, actualMutatedFile := range actualMutatedFiles {
expectedMutatedFile := expectedMutatedFiles[i]
assert.Equal(t,
expectedMutatedFile,
actualMutatedFile,
"Actual and expected (filename %s) mutants are not equal", testcase.MutantFileNames[i])
if t.Failed() {
t.Logf("Mutated filed diff:\n%s", actualMutatedFile.Diff(gotextdiff.New()))
}
}
})
}
}
func mutate(virus viruses.Virus, source *gosourcefile.GoSourceFile) []*gomutatedfile.GoMutatedFile {
mutatedFiles := []*gomutatedfile.GoMutatedFile{}
for _, infectedFile := range source.Incubate(virus) {
mutatedFiles = append(mutatedFiles, infectedFile.Mutate())
}
return mutatedFiles
}