-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers_test.go
103 lines (91 loc) · 1.99 KB
/
helpers_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package evo
import (
"errors"
"testing"
)
func TestMutatorsMutate(t *testing.T) {
var cases = []struct {
Desc string
HasError bool
Expected []bool
Mutators []Mutator
}{
{
Desc: "no errors, no structural",
HasError: false,
Expected: []bool{true, true},
Mutators: []Mutator{
&mockNonstructural{},
&mockNonstructural{},
},
},
{
Desc: "has errors",
HasError: true,
Expected: []bool{true, true},
Mutators: []Mutator{
&mockNonstructural{HasError: true},
&mockNonstructural{},
},
},
{
Desc: "has structural, second mutator not called",
HasError: false,
Expected: []bool{true, false},
Mutators: []Mutator{
&mockStructural{},
&mockNonstructural{},
},
},
}
for _, c := range cases {
t.Run(c.Desc, func(t *testing.T) {
// Build a new collection
mut := Mutators(c.Mutators)
// Mutate a genome
err := mut.Mutate(&Genome{})
if c.HasError {
if err == nil {
t.Error("expected error not found")
}
return
}
if err != nil {
t.Errorf("error not expected: %v", err)
}
// Check that right mutators were called
for i := 0; i < len(c.Mutators); i++ {
act := c.Mutators[i].(callable).Called()
if act != c.Expected[i] {
t.Errorf("incorrect called value for mutator %d: expected %t, actual %t", i, c.Expected, act)
}
}
})
}
}
type mockStructural struct {
called bool
HasError bool
}
func (m *mockStructural) Mutate(g *Genome) error {
m.called = true
if m.HasError {
return errors.New("mock structural error")
}
g.Encoded.Nodes = append(g.Encoded.Nodes, Node{})
return nil
}
func (m *mockStructural) Called() bool { return m.called }
type mockNonstructural struct {
called bool
HasError bool
}
func (m *mockNonstructural) Mutate(g *Genome) error {
m.called = true
if m.HasError {
return errors.New("mock nonstructural error")
}
return nil
}
func (m *mockNonstructural) Called() bool { return m.called }
type callable interface{ Called() bool }