-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutator_replace_test.go
121 lines (112 loc) · 3.08 KB
/
mutator_replace_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package mutator
import (
"testing"
)
// Helper function to count the number of replacements
func countReplacements(original, changed string) int {
// Count the number of characters replaced
replacements := 0
for i := range original {
if original[i] != changed[i] {
replacements++
}
}
return replacements
}
// Test for replaceChars
func TestReplaceChars(t *testing.T) {
mutator := New(&Config{
MutationTypes: []MutationType{
NewReplaceMutation(1, 1.0, "all"),
},
})
tests := []struct {
name string
input string
mutationType MutationType
expectedCount int // Expected number of replacements
}{
{
name: "Single Replacement - All",
input: "hello",
mutationType: NewReplaceMutation(1, 1.0, "all"),
expectedCount: 1,
},
{
name: "Multiple Replacements - All",
input: "hello",
mutationType: NewReplaceMutation(2, 1.0, "all"),
expectedCount: 2,
},
{
name: "More Replacements Than Characters - All",
input: "hello",
mutationType: NewReplaceMutation(5, 1.0, "all"),
expectedCount: 5, // In this case, all characters might be replaced
},
{
name: "Replace Letters Only",
input: "hello123!",
mutationType: NewReplaceMutation(2, 1.0, "letters"),
expectedCount: 2,
},
{
name: "Replace Numbers Only",
input: "hello123!",
mutationType: NewReplaceMutation(1, 1.0, "numbers"),
expectedCount: 1,
},
{
name: "Replace Special Characters Only",
input: "hello123!",
mutationType: NewReplaceMutation(1, 1.0, "special"),
expectedCount: 1,
},
{
name: "Replace Mixed Valid Characters",
input: "a1!b2@c3#d4$e5%",
mutationType: NewReplaceMutation(4, 1.0, "letters,numbers,special"),
expectedCount: 4,
},
{
name: "Replace No Valid Characters",
input: "123456",
mutationType: NewReplaceMutation(2, 1.0, "letters"),
expectedCount: 6, // No letters to replace
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inputRunes := []rune(tt.input)
original := string(inputRunes)
result := mutator.replaceChars(inputRunes, tt.mutationType)
resultStr := string(result)
// Count the number of replacements
replacementCount := countReplacements(original, resultStr)
if replacementCount == 0 {
t.Errorf("replaceChars() replaced %d characters, want %d", replacementCount, tt.expectedCount)
}
// Additional checks to ensure replacements are valid
for i, r := range resultStr {
if rune(original[i]) != r && !isValidReplacement(r, tt.mutationType) {
t.Errorf("replaceChars() replaced with invalid character: %c", r)
}
}
})
}
}
func isValidReplacement(r rune, mutationType MutationType) bool {
if mutationType.Any {
return true
}
if mutationType.CharSet != "" {
for _, char := range mutationType.CharSet {
if r == char {
return true
}
}
}
return (mutationType.Letters && isLetter(r)) ||
(mutationType.Numbers && isNumber(r)) ||
(mutationType.Special && isSpecialChar(r))
}