-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutator_pick_test.go
96 lines (87 loc) · 2.65 KB
/
mutator_pick_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
package mutator
import (
"testing"
)
// Helper function to count the number of picked characters
func countPickedChars(original, picked string) int {
// Picked characters should be equal to the length of the picked string
return len(picked)
}
// Test for pickChars
func TestPickChars(t *testing.T) {
tests := []struct {
name string
input string
mutationType MutationType
expectedCount int // Expected number of picked characters
expectedChars func(rune) bool // Function to validate picked characters
}{
{
name: "Pick Letters Only",
input: "a1!b2@c3#d4$e5%",
mutationType: NewPickMutation(3, 1.0, "letters"),
expectedCount: 3,
expectedChars: isLetter,
},
{
name: "Pick Numbers Only",
input: "a1!b2@c3#d4$e5%",
mutationType: NewPickMutation(2, 1.0, "numbers"),
expectedCount: 2,
expectedChars: isNumber,
},
{
name: "Pick Special Characters Only",
input: "a1!b2@c3#d4$e5%",
mutationType: NewPickMutation(4, 1.0, "special"),
expectedCount: 4,
expectedChars: isSpecialChar,
},
{
name: "Pick Mixed Valid Characters",
input: "a1!b2@c3#d4$e5%",
mutationType: NewPickMutation(5, 1.0, "letters,numbers,special"),
expectedCount: 5,
expectedChars: func(r rune) bool {
return isLetter(r) || isNumber(r) || isSpecialChar(r)
},
},
{
name: "Pick More Than Available",
input: "abc",
mutationType: NewPickMutation(5, 1.0, "letters"),
expectedCount: 3, // Since we only have 3 letters available
expectedChars: isLetter,
},
{
name: "Pick No Valid Characters",
input: "123456!",
mutationType: NewPickMutation(2, 1.0, "special"),
expectedCount: 1, // No letters to pick
expectedChars: isSpecialChar,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Initialize Mutator with a default configuration
mutator := New(&Config{
MutationTypes: []MutationType{tt.mutationType},
})
inputRunes := []rune(tt.input)
result := mutator.pickChars(inputRunes, tt.mutationType)
resultStr := string(result)
// Count the number of picked characters
pickedCount := countPickedChars(tt.input, resultStr)
// fmt.Println(tt.expectedCount, pickedCount)
if pickedCount != tt.expectedCount {
t.Errorf("pickChars() picked %d characters, want %d", pickedCount, tt.expectedCount)
}
// Check if all characters in result are valid according to the mutationType
for _, r := range resultStr {
if !tt.expectedChars(r) {
t.Errorf("pickChars() result contains invalid character: %c", r)
}
}
})
}
}