-
Notifications
You must be signed in to change notification settings - Fork 7
/
name_test.go
165 lines (147 loc) · 6.69 KB
/
name_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
package handy
import "testing"
func TestCheckPersonName(t *testing.T) {
type TestStructForCheckPersonName struct {
summary string
name string
acceptEmpty bool
expectedOutput uint8
}
testlist := []TestStructForCheckPersonName{
{"Only two letters", "T S", false, CheckPersonNameResultTooSimple},
{"only four letters", "AB CD", false, CheckPersonNameResultTooSimple},
{"five letters with non-ascii runes", "ça vá", false, CheckPersonNameResultTooSimple},
{"mixing letters and numbers", "W0RDS W1TH NUMB3RS", false, CheckPersonNameResultPolluted},
{"Sending and accepting empty string", "", true, CheckPersonNameResultOK},
{"Sending spaces-only string and accepting empty", " ", true, CheckPersonNameResultOK},
{"Sending but not accepting empty string", " ", false, CheckPersonNameResultTooShort},
{"Sending spaces-only string and refusing empty", " ", false, CheckPersonNameResultTooShort},
{"Sending numbers, expecting false", " 5454 ", true, CheckPersonNameResultPolluted},
{"OneWorded string", "ONEWORD", false, CheckPersonNameResultTooFewWords},
{"Minimum acceptable", "AB CDE", false, CheckPersonNameResultOK},
{"Non-ascii stuff", "ÑÔÑÀSÇÏÏ ÇÃO ÀË", false, CheckPersonNameResultOK},
{"Words with symbols. Expecting true", "WORDS-WITH SYMBOLS'", false, CheckPersonNameResultOK},
{"Words with symbols. Expecting false", "WORDS WITH SYMBOLS`", false, CheckPersonNameResultPolluted},
{"less than two letters", "a", false, CheckPersonNameResultTooFewWords},
{"Sending numbers, expecting false", "5454", false, CheckPersonNameResultPolluted},
}
for _, tst := range testlist {
t.Run(tst.summary, func(t *testing.T) {
tr := CheckPersonName(tst.name, tst.acceptEmpty)
if tr != tst.expectedOutput {
t.Errorf("Test has failed!\n\tName: %s\n\tAcceptEmpty: %t, \n\tExpected: %d, \n\tGot: %d,", tst.name, tst.acceptEmpty, tst.expectedOutput, tr)
}
})
}
}
//// StringSlicesAreEqual compares two string slices and returns true if they have the same elements, in same order
//func StringSlicesAreEqual(x, y []string) bool {
// if ((x == nil) != (y == nil)) || (len(x) != len(y)) {
// return false
// }
//
// for i := range y {
// if x[i] != y[i] {
// return false
// }
// }
//
// return true
//}
func TestNameFirstAndLast(t *testing.T) {
type TestNameFirstAndLastStruct struct {
summary string
name string
transformFlags uint
expectedOutputS string
}
testlist := []TestNameFirstAndLastStruct{
{"Only two letters", "x Y", TransformNone, `x Y`},
{"one word name", "namë", TransformNone, `namë`},
{"all non-ascii runes", "çá öáã àÿ", TransformNone, `çá àÿ`},
{"all non-ascii runes to upper", "çá öáã àÿ", TransformFlagUpperCase, `ÇÁ ÀŸ`},
{"mixing letters and numbers and then filtering digits off", "W0RDS W1TH NUMB3RS", TransformFlagRemoveDigits, `WRDS NUMBRS`},
{"empty string", "", TransformNone, ``},
{"only spaces", " ", TransformNone, ``},
{"with spaces and tabs", " FIRST NAME - MIDDLENAME LAST ", TransformNone, `FIRST LAST`},
{"last name single rune", "NAME X", TransformNone, `NAME X`},
{"only symbols", "5454#@$", TransformNone, `5454#@$`},
{"single letter", "x", TransformNone, `x`},
{"only spaces empty return", " ", TransformNone, ``},
{"regular name to upper", "name lastname", TransformFlagUpperCase, `NAME LASTNAME`},
{"regular name to title", "name LASTNAME", TransformFlagTitleCase, `Name Lastname`},
{"REGULAR Name to lOwEr", "name LASTNAME", TransformFlagLowerCase, `name lastname`},
}
for _, tst := range testlist {
t.Run(tst.summary, func(t *testing.T) {
s := NameFirstAndLast(tst.name, tst.transformFlags)
if s != tst.expectedOutputS {
t.Errorf(`[%s] Test has failed! Given name: "%s", Expected string: "%s", Got: "%s"`, tst.summary, tst.name, tst.expectedOutputS, s)
}
})
}
}
func TestNameFirst(t *testing.T) {
type TestNameFirstStruct struct {
summary string
name string
transformFlags uint
expectedOutputS string
}
testlist := []TestNameFirstStruct{
{"Only two letters", "x Y", TransformNone, `x`},
{"one word name", "namë", TransformNone, `namë`},
{"all non-ascii runes", "çá öáã àÿ", TransformNone, `çá`},
{"all non-ascii runes to upper", "çá öáã àÿ", TransformFlagUpperCase, `ÇÁ`},
{"mixing letters and numbers and then filtering digits off", "W0RDS W1TH NUMB3RS", TransformFlagRemoveDigits, `WRDS`},
{"empty string", "", TransformNone, ``},
{"only spaces", " ", TransformNone, ``},
{"with spaces and tabs", " FIRST NAME - MIDDLENAME LAST ", TransformNone, `FIRST`},
{"last name single rune", "NAME X", TransformNone, `NAME`},
{"only symbols", "5454#@$", TransformNone, `5454#@$`},
{"single letter", "x", TransformNone, `x`},
{"only spaces empty return", " ", TransformNone, ``},
{"regular name to upper", "name lastname", TransformFlagUpperCase, `NAME`},
{"regular name to title", "name LASTNAME", TransformFlagTitleCase, `Name`},
{"REGULAR Name to lOwEr", "name LASTNAME", TransformFlagLowerCase, `name`},
}
for _, tst := range testlist {
t.Run(tst.summary, func(t *testing.T) {
s := NameFirst(tst.name, tst.transformFlags)
if s != tst.expectedOutputS {
t.Errorf(`[%s] Test has failed! Given name: "%s", Expected string: "%s", Got: "%s"`, tst.summary, tst.name, tst.expectedOutputS, s)
}
})
}
}
func TestNameInitials(t *testing.T) {
type tStruct struct {
summary string
name string
transformFlags uint
expectedOutput string
}
testlist := []tStruct{
{`simplest 2 words name`, `miguel pragier`, TransformNone, `m p`},
{`3 words name separated`, `ivan alexandrovitch kleshtakov`, TransformNone, `i a k`},
{`3 words with unicode`, `Ívän Âlexandrovitch Çzelyatchenko`, TransformNone, `Í Â Ç`},
{`3 words with unicode title-case`, `ívän âlexandrovitch çzelyatchenko`, TransformFlagTitleCase, `Í Â Ç`},
{`empty string`, ``, TransformNone, ``},
{`dot`, `.`, TransformNone, `.`},
{`spaces and tabs`, " \t\t \n", TransformNone, ``},
{`name with tabs`, "richard\t\tstallmann", TransformNone, `r s`},
{`noble name with 1`, `dom pedro 1`, TransformNone, `d p 1`},
{`noble name with I uppercase`, `dom pedro I`, TransformFlagUpperCase, `D P I`},
{`3 letters`, `x y z`, TransformNone, `x y z`},
{`one word`, `asingleword`, TransformNone, `a`},
{`comma separators`, `name,with,comma,separators`, TransformNone, `n`},
}
for _, tst := range testlist {
t.Run(tst.summary, func(t *testing.T) {
s := NameInitials(tst.name, tst.transformFlags)
if s != tst.expectedOutput {
t.Errorf(`[%s] Test has failed! Given name: "%s", Expected string: "%s", Got: "%s"`, tst.summary, tst.name, tst.expectedOutput, s)
}
})
}
}