-
Notifications
You must be signed in to change notification settings - Fork 1
/
memorablePasswordUtils_test.go
170 lines (153 loc) · 5.09 KB
/
memorablePasswordUtils_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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"strings"
"testing"
)
//func TestRandomYear(t *testing.T) {
// rand.Seed(time.Now().UnixNano())
// minYear := 0
// maxYear := 2000
//
// // Run the test multiple times to check for randomness
// for i := 0; i < 100; i++ {
// randomYearStr := RandomYearOrFloat()
// randomYear, err := strconv.Atoi(randomYearStr)
//
// if err != nil {
// t.Errorf("RandomYearOrFloat() returned an invalid number: %s", randomYearStr)
// }
//
// if randomYear < minYear || randomYear > maxYear {
// t.Errorf("RandomYearOrFloat() returned a number out of range: %d (expected between %d and %d)", randomYear, minYear, maxYear)
// }
// }
//}
//func TestMemorableTransformFive(t *testing.T) {
// tests := []struct {
// name string
// requestedPasswordLength int
// expectRandomUnit bool
// }{
// {"length 24", 24, false},
// {"length 25", 25, true},
// }
//
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// memorablePassword := memorableTransformFive("", tt.requestedPasswordLength)
//
// fmt.Println(memorablePassword)
//
// // Test if password matches the pattern: {Degrading-1729-Earshot}
// // https://regex101.com/r/vsmcXd/2
// assert.Regexp(t, "^[\\[\\{\\(\\<][A-Za-z]+-\\d{1,4}-[A-Za-z]+[\\]\\}\\)\\>]$", memorablePassword)
//
// splitPassword := strings.Split(memorablePassword, "-")
//
// // Test for word, dash, word
// if len(splitPassword) != 3 {
// t.Errorf("memorableTransformFive() returned password with incorrect format: got %s", memorablePassword)
// }
//
// // Create a different slice to test this, so we don't modify the original password
// splitPasswordStripBracketsLeft := splitPassword
//
// // This for loop iterates through each element of the splitPassword slice, applying
// // the strings.TrimLeftFunc function to remove non-alphabetic characters from the
// // beginning of each string, and updates the slice with the modified strings.
// for i, passwordPart := range splitPassword {
// splitPasswordStripBracketsLeft[i] = strings.TrimLeftFunc(passwordPart, func(r rune) bool {
// return !unicode.IsLetter(r)
// })
// }
//
// if !isCapitalized(splitPasswordStripBracketsLeft[0]) {
// t.Errorf("memorableTransformFive() failed to capitalize the first word: got %s", splitPassword[0])
// }
//
// if !isCapitalized(splitPasswordStripBracketsLeft[2]) {
// t.Errorf("memorableTransformFive() failed to capitalize the second word: got %s", splitPassword[2])
// }
//
// })
// }
//}
func TestCapitalizeFirstLetter(t *testing.T) {
tests := []struct {
input string
output string
}{
{"hello", "Hello"},
{"world", "World"},
{"capitalize", "Capitalize"},
{"test123", "Test123"},
{"", ""},
}
for _, test := range tests {
capitalized := capitalizeFirstLetter(test.input)
if capitalized != test.output {
t.Errorf("capitalizeFirstLetter() failed for input %s: expected %s, got %s", test.input, test.output, capitalized)
}
}
}
func TestCapitalizeFirstLetterIdempotent(t *testing.T) {
tests := []string{
"Hello",
"World",
"Capitalize",
"Test123",
"",
}
for _, input := range tests {
capitalized := capitalizeFirstLetter(input)
if capitalized != input {
t.Errorf("capitalizeFirstLetter() is not idempotent for input %s: expected %s, got %s", input, input, capitalized)
}
}
}
func TestCapitalizeFirstLetterOnlyFirstRune(t *testing.T) {
input := "gödel"
expected := "Gödel"
capitalized := capitalizeFirstLetter(input)
if capitalized != expected {
t.Errorf("capitalizeFirstLetter() failed for input %s: expected %s, got %s", input, expected, capitalized)
}
}
func TestAppendRandomUnit(t *testing.T) {
//rand.Seed(time.Now().UnixNano())
units := []string{
"Mhz", "Ghz", "Mbps", "Mph", "Gbps", "Kbps", "inches", "feet", "miles",
"Hz", "kHz", "THz", "nm", "mm", "cm", "m", "km", "yd", "mi", "nmi",
"liters", "gallons", "pints", "quarts", "milliliters", "cubic meters",
"grams", "kilograms", "pounds", "ounces", "tons", "tonnes",
"seconds", "minutes", "hours", "days", "weeks", "months", "years",
"degrees Celsius", "degrees Fahrenheit", "Kelvin",
"pascals", "bars", "atmospheres", "mmHg", "torr",
"volts", "amperes", "watts", "ohms", "farads", "henrys", "teslas", "webers",
"Joules", "calories", "BTU", "ergs", "electronvolts",
"lumens", "candelas", "lux", "foot-candles",
"bits", "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB",
"dpi", "ppi", "pt", "em", "rem", "px",
}
testNumbers := []string{"42", "123", "0", "1000"}
// Run the test multiple times to check for randomness
for i := 0; i < 100; i++ {
for _, testNumber := range testNumbers {
result := appendRandomUnit(testNumber)
if !strings.HasPrefix(result, testNumber) {
t.Errorf("appendRandomUnit() result does not start with the input number: got %s, expected prefix %s", result, testNumber)
}
appendedUnit := strings.TrimPrefix(result, testNumber)
unitFound := false
for _, unit := range units {
if unit == appendedUnit {
unitFound = true
break
}
}
if !unitFound {
t.Errorf("appendRandomUnit() appended an invalid unit: got %s", appendedUnit)
}
}
}
}