-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnokpp_test.go
273 lines (220 loc) · 6.53 KB
/
rnokpp_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package rnokpp_test
import (
"fmt"
"math/rand"
"testing"
"time"
"github.com/fre5h/rnokpp"
)
const reset = "\033[0m"
const red = "\033[31m"
const green = "\033[32m"
const yellow = "\033[33m"
func TestGetDetails(t *testing.T) {
var testVariantsForTestGetDetails = []struct {
rnokpp string
valid bool
gender rnokpp.Gender
date string
}{
{
rnokpp: "3652504575",
valid: true,
gender: rnokpp.Male,
date: "01.01.2000",
},
}
for _, testVariant := range testVariantsForTestGetDetails {
result, _ := rnokpp.GetDetails(testVariant.rnokpp)
if result.Valid != testVariant.valid {
t.Error("Invalid", result.Valid, testVariant.valid)
}
if result.Gender != testVariant.gender {
t.Error("Different gender", result.Gender, testVariant.gender)
}
if result.Birthday.Format("02.01.2006") != testVariant.date {
t.Error("Different birthday", result.Birthday.Format("02.01.2006"), testVariant.date)
}
}
}
func TestGetDetailsWithErrors(t *testing.T) {
if _, err := rnokpp.GetDetails("1234567890+"); err == nil {
t.Error("Expected error for a string longer than 10 symbols")
}
if _, err := rnokpp.GetDetails("123456789"); err == nil {
t.Error("Expected error for a string smaller than 10 symbols")
}
var testVariantsForTestGetDetailsForNonDigitsInString = []string{
"123456789X",
" ",
"ABCDEFGHIJ",
" 234567890",
"123456789 ",
}
for _, invalidRnokpp := range testVariantsForTestGetDetailsForNonDigitsInString {
if _, err := rnokpp.GetDetails(invalidRnokpp); err == nil {
t.Error("Expected error for non digits in string")
}
}
}
func TestIsMale(t *testing.T) {
var err error
var isMale bool
isMale, err = rnokpp.IsMale("3652504575") // male RNOKPP
if isMale == false || err != nil {
t.Errorf("RNOKPP %s%s%s expected to be a valid for male, but it is not", yellow, "3652504575", reset)
}
isMale, err = rnokpp.IsMale("3068208400") // female RNOKPP
if isMale == true || err != nil {
t.Errorf("RNOKPP %s%s%s expected to be a invalid for male, but it is not", yellow, "3068208400", reset)
}
_, err = rnokpp.IsMale("invalid") // invalid RNOKPP
if err == nil {
t.Error("Expected error for invalid RNOKPP")
}
}
func TestIsFemale(t *testing.T) {
var err error
var isFemale bool
isFemale, err = rnokpp.IsFemale("3652504575") // male RNOKPP
if isFemale == true || err != nil {
t.Errorf("RNOKPP %s%s%s expected to be a valid for female, but it is not", yellow, "3652504575", reset)
}
isFemale, err = rnokpp.IsFemale("3068208400") // female RNOKPP
if isFemale == false || err != nil {
t.Errorf("RNOKPP %s%s%s expected to be a invalid for female, but it is not", yellow, "3068208400", reset)
}
_, err = rnokpp.IsFemale("invalid") // invalid RNOKPP
if err == nil {
t.Error("Expected error for invalid RNOKPP")
}
}
func TestGetGender(t *testing.T) {
if gender, _ := rnokpp.GetGender("3652504575"); *gender != rnokpp.Male {
t.Error("Expected gender Male for RNOKPP 3652504575")
}
if gender, _ := rnokpp.GetGender("3068208400"); *gender != rnokpp.Female {
t.Error("Expected gender Female for RNOKPP 3068208400")
}
if gender, err := rnokpp.GetGender("invalid"); gender != nil || err == nil {
t.Error("Expected error invalid RNOKPP")
}
}
func TestGenerateRnokpp(t *testing.T) {
oldDate, _ := time.Parse("02.01.2006", "31.12.1899")
if _, err := rnokpp.GenerateRnokpp(oldDate, rnokpp.Male); err == nil {
t.Error("Expected error for too old date")
}
tomorrow := time.Now().AddDate(0, 0, 1)
if _, err := rnokpp.GenerateRnokpp(tomorrow, rnokpp.Male); err == nil {
t.Error("Expected error for too old date")
}
for i := 0; i < 100; i++ {
randomTime := rand.Int63n(time.Now().Unix()-94608000) + 94608000
birthday := time.Unix(randomTime, 0)
generatedRnokpp, _ := rnokpp.GenerateRnokpp(birthday, rnokpp.Male)
if !rnokpp.IsValid(generatedRnokpp) {
t.Errorf("Generated RNOKPP is not valid %s\"%s\"%s", red, generatedRnokpp, reset)
}
}
}
func TestGenerateRandomRnokpp(t *testing.T) {
for i := 0; i < 100; i++ {
generatedRnokpp, _ := rnokpp.GenerateRandomRnokpp()
if !rnokpp.IsValid(generatedRnokpp) {
t.Errorf("Generated random RNOKPP is not valid %s\"%s\"%s", red, generatedRnokpp, reset)
}
}
}
func TestGenerateRandomRnokppN(t *testing.T) {
generatedRnokpps, _ := rnokpp.GenerateRandomRnokppN(100)
for _, generatedRnokpp := range generatedRnokpps {
if !rnokpp.IsValid(generatedRnokpp) {
t.Errorf("Generated random RNOKPP is not valid %s\"%s\"%s", red, generatedRnokpp, reset)
}
}
_, err := rnokpp.GenerateRandomRnokppN(0)
if err == nil {
t.Errorf("Generation of RNOKPPs with non positiv number should not be possible")
}
}
func ExampleIsValid() {
fmt.Print(rnokpp.IsValid("3652504575"), rnokpp.IsValid("1234567890"))
// Output:
// true false
}
func ExampleGetDetails() {
details, _ := rnokpp.GetDetails("3652504575")
fmt.Print(details)
// Output:
// valid, male, 01.01.2000
}
func ExampleGetGender() {
result1, _ := rnokpp.GetGender("3652504575")
fmt.Println(result1)
result2, _ := rnokpp.GetGender("3068208400")
fmt.Println(result2)
// Output:
// male
// female
}
func ExampleIsMale() {
result1, err1 := rnokpp.IsMale("3652504575") // male RNOKPP
if err1 == nil {
fmt.Println(result1)
}
result2, err2 := rnokpp.IsMale("3068208400") // female RNOKPP
if err2 == nil {
fmt.Println(result2)
}
// Output:
// true
// false
}
func ExampleIsFemale() {
result1, err1 := rnokpp.IsFemale("3652504575") // male RNOKPP
if err1 == nil {
fmt.Println(result1)
}
result2, err2 := rnokpp.IsFemale("3068208400") // female RNOKPP
if err2 == nil {
fmt.Println(result2)
}
// Output:
// false
// true
}
func ExampleGenerateRnokpp() {
birthday, _ := time.Parse("02.01.2006", "01.01.2000")
// string with valid random RNOKPP for male
if generatedRnokppMale, err := rnokpp.GenerateRnokpp(birthday, rnokpp.Male); err == nil {
fmt.Println(rnokpp.IsValid(generatedRnokppMale))
}
// string with valid random RNOKPP for female
if generatedRnokppFemale, err := rnokpp.GenerateRnokpp(birthday, rnokpp.Female); err == nil {
fmt.Println(rnokpp.IsValid(generatedRnokppFemale))
}
// Output:
// true
// true
}
func ExampleGenerateRandomRnokpp() {
if generatedRnokpp, err := rnokpp.GenerateRandomRnokpp(); err == nil {
fmt.Println(rnokpp.IsValid(generatedRnokpp))
}
// Output:
// true
}
func ExampleGenerateRandomRnokppN() {
if generatedRnokpps, err := rnokpp.GenerateRandomRnokppN(5); err == nil {
for _, generatedRnokpp := range generatedRnokpps {
fmt.Println(rnokpp.IsValid(generatedRnokpp))
}
}
// Output:
// true
// true
// true
// true
// true
}