1package govalidator
2
3import (
4	"reflect"
5	"testing"
6)
7
8func TestContains(t *testing.T) {
9	t.Parallel()
10
11	var tests = []struct {
12		param1   string
13		param2   string
14		expected bool
15	}{
16		{"abacada", "", true},
17		{"abacada", "ritir", false},
18		{"abacada", "a", true},
19		{"abacada", "aca", true},
20	}
21	for _, test := range tests {
22		actual := Contains(test.param1, test.param2)
23		if actual != test.expected {
24			t.Errorf("Expected Contains(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual)
25		}
26	}
27}
28
29func TestMatches(t *testing.T) {
30	t.Parallel()
31
32	var tests = []struct {
33		param1   string
34		param2   string
35		expected bool
36	}{
37		{"123456789", "[0-9]+", true},
38		{"abacada", "cab$", false},
39		{"111222333", "((111|222|333)+)+", true},
40		{"abacaba", "((123+]", false},
41	}
42	for _, test := range tests {
43		actual := Matches(test.param1, test.param2)
44		if actual != test.expected {
45			t.Errorf("Expected Matches(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual)
46		}
47	}
48}
49
50func TestLeftTrim(t *testing.T) {
51	t.Parallel()
52
53	var tests = []struct {
54		param1   string
55		param2   string
56		expected string
57	}{
58		{"  \r\n\tfoo  \r\n\t   ", "", "foo  \r\n\t   "},
59		{"010100201000", "01", "201000"},
60	}
61	for _, test := range tests {
62		actual := LeftTrim(test.param1, test.param2)
63		if actual != test.expected {
64			t.Errorf("Expected LeftTrim(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual)
65		}
66	}
67}
68
69func TestRightTrim(t *testing.T) {
70	t.Parallel()
71
72	var tests = []struct {
73		param1   string
74		param2   string
75		expected string
76	}{
77		{"  \r\n\tfoo  \r\n\t   ", "", "  \r\n\tfoo"},
78		{"010100201000", "01", "0101002"},
79	}
80	for _, test := range tests {
81		actual := RightTrim(test.param1, test.param2)
82		if actual != test.expected {
83			t.Errorf("Expected RightTrim(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual)
84		}
85	}
86}
87
88func TestTrim(t *testing.T) {
89	t.Parallel()
90
91	var tests = []struct {
92		param1   string
93		param2   string
94		expected string
95	}{
96		{"  \r\n\tfoo  \r\n\t   ", "", "foo"},
97		{"010100201000", "01", "2"},
98		{"1234567890987654321", "1-8", "909"},
99	}
100	for _, test := range tests {
101		actual := Trim(test.param1, test.param2)
102		if actual != test.expected {
103			t.Errorf("Expected Trim(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual)
104		}
105	}
106}
107
108// This small example illustrate how to work with Trim function.
109func ExampleTrim() {
110	// Remove from left and right spaces and "\r", "\n", "\t" characters
111	println(Trim("   \r\r\ntext\r   \t\n", "") == "text")
112	// Remove from left and right characters that are between "1" and "8".
113	// "1-8" is like full list "12345678".
114	println(Trim("1234567890987654321", "1-8") == "909")
115}
116
117func TestWhiteList(t *testing.T) {
118	t.Parallel()
119
120	var tests = []struct {
121		param1   string
122		param2   string
123		expected string
124	}{
125		{"abcdef", "abc", "abc"},
126		{"aaaaaaaaaabbbbbbbbbb", "abc", "aaaaaaaaaabbbbbbbbbb"},
127		{"a1b2c3", "abc", "abc"},
128		{"   ", "abc", ""},
129		{"a3a43a5a4a3a2a23a4a5a4a3a4", "a-z", "aaaaaaaaaaaa"},
130	}
131	for _, test := range tests {
132		actual := WhiteList(test.param1, test.param2)
133		if actual != test.expected {
134			t.Errorf("Expected WhiteList(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual)
135		}
136	}
137}
138
139// This small example illustrate how to work with WhiteList function.
140func ExampleWhiteList() {
141	// Remove all characters from string ignoring characters between "a" and "z"
142	println(WhiteList("a3a43a5a4a3a2a23a4a5a4a3a4", "a-z") == "aaaaaaaaaaaa")
143}
144
145func TestBlackList(t *testing.T) {
146	t.Parallel()
147
148	var tests = []struct {
149		param1   string
150		param2   string
151		expected string
152	}{
153		{"abcdef", "abc", "def"},
154		{"aaaaaaaaaabbbbbbbbbb", "abc", ""},
155		{"a1b2c3", "abc", "123"},
156		{"   ", "abc", "   "},
157		{"a3a43a5a4a3a2a23a4a5a4a3a4", "a-z", "34354322345434"},
158	}
159	for _, test := range tests {
160		actual := BlackList(test.param1, test.param2)
161		if actual != test.expected {
162			t.Errorf("Expected BlackList(%q,%q) to be %v, got %v", test.param1, test.param2, test.expected, actual)
163		}
164	}
165}
166
167func TestStripLow(t *testing.T) {
168	t.Parallel()
169
170	var tests = []struct {
171		param1   string
172		param2   bool
173		expected string
174	}{
175		{"foo\x00", false, "foo"},
176		{"\x7Ffoo\x02", false, "foo"},
177		{"\x01\x09", false, ""},
178		{"foo\x0A\x0D", false, "foo"},
179		{"perch\u00e9", false, "perch\u00e9"},
180		{"\u20ac", false, "\u20ac"},
181		{"\u2206\x0A", false, "\u2206"},
182		{"foo\x0A\x0D", true, "foo\x0A\x0D"},
183		{"\x03foo\x0A\x0D", true, "foo\x0A\x0D"},
184	}
185	for _, test := range tests {
186		actual := StripLow(test.param1, test.param2)
187		if actual != test.expected {
188			t.Errorf("Expected StripLow(%q,%t) to be %v, got %v", test.param1, test.param2, test.expected, actual)
189		}
190	}
191}
192
193func TestReplacePattern(t *testing.T) {
194	t.Parallel()
195
196	var tests = []struct {
197		param1   string
198		param2   string
199		param3   string
200		expected string
201	}{
202		{"ab123ba", "[0-9]+", "aca", "abacaba"},
203		{"abacaba", "[0-9]+", "aca", "abacaba"},
204		{"httpftp://github.comio", "(ftp|io)", "", "http://github.com"},
205		{"aaaaaaaaaa", "a", "", ""},
206		{"http123123ftp://git534543hub.comio", "(ftp|io|[0-9]+)", "", "http://github.com"},
207	}
208	for _, test := range tests {
209		actual := ReplacePattern(test.param1, test.param2, test.param3)
210		if actual != test.expected {
211			t.Errorf("Expected ReplacePattern(%q,%q,%q) to be %v, got %v", test.param1, test.param2, test.param3, test.expected, actual)
212		}
213	}
214}
215
216// This small example illustrate how to work with ReplacePattern function.
217func ExampleReplacePattern() {
218	// Replace in "http123123ftp://git534543hub.comio" following (pattern "(ftp|io|[0-9]+)"):
219	// - Sequence "ftp".
220	// - Sequence "io".
221	// - Sequence of digits.
222	// with empty string.
223	println(ReplacePattern("http123123ftp://git534543hub.comio", "(ftp|io|[0-9]+)", "") == "http://github.com")
224}
225
226func TestEscape(t *testing.T) {
227	t.Parallel()
228
229	var tests = []struct {
230		param    string
231		expected string
232	}{
233		{`<img alt="foo&bar">`, "&lt;img alt=&#34;foo&amp;bar&#34;&gt;"},
234	}
235	for _, test := range tests {
236		actual := Escape(test.param)
237		if actual != test.expected {
238			t.Errorf("Expected Escape(%q) to be %v, got %v", test.param, test.expected, actual)
239		}
240	}
241}
242
243func TestUnderscoreToCamelCase(t *testing.T) {
244	t.Parallel()
245
246	var tests = []struct {
247		param    string
248		expected string
249	}{
250		{"a_b_c", "ABC"},
251		{"my_func", "MyFunc"},
252		{"1ab_cd", "1abCd"},
253	}
254	for _, test := range tests {
255		actual := UnderscoreToCamelCase(test.param)
256		if actual != test.expected {
257			t.Errorf("Expected UnderscoreToCamelCase(%q) to be %v, got %v", test.param, test.expected, actual)
258		}
259	}
260}
261
262func TestCamelCaseToUnderscore(t *testing.T) {
263	t.Parallel()
264
265	var tests = []struct {
266		param    string
267		expected string
268	}{
269		{"MyFunc", "my_func"},
270		{"ABC", "a_b_c"},
271		{"1B", "1_b"},
272		{"foo_bar", "foo_bar"},
273		{"FooV2Bar", "foo_v2_bar"},
274	}
275	for _, test := range tests {
276		actual := CamelCaseToUnderscore(test.param)
277		if actual != test.expected {
278			t.Errorf("Expected CamelCaseToUnderscore(%q) to be %v, got %v", test.param, test.expected, actual)
279		}
280	}
281}
282
283func TestReverse(t *testing.T) {
284	t.Parallel()
285
286	var tests = []struct {
287		param    string
288		expected string
289	}{
290		{"abc", "cba"},
291		{"カタカナ", "ナカタカ"},
292	}
293	for _, test := range tests {
294		actual := Reverse(test.param)
295		if actual != test.expected {
296			t.Errorf("Expected Reverse(%q) to be %v, got %v", test.param, test.expected, actual)
297		}
298	}
299}
300
301func TestGetLines(t *testing.T) {
302	t.Parallel()
303
304	var tests = []struct {
305		param    string
306		expected []string
307	}{
308		{"abc", []string{"abc"}},
309		{"a\nb\nc", []string{"a", "b", "c"}},
310	}
311	for _, test := range tests {
312		actual := GetLines(test.param)
313		if !reflect.DeepEqual(actual, test.expected) {
314			t.Errorf("Expected GetLines(%q) to be %v, got %v", test.param, test.expected, actual)
315		}
316	}
317}
318
319func TestGetLine(t *testing.T) {
320	t.Parallel()
321
322	var tests = []struct {
323		param1   string
324		param2   int
325		expected string
326	}{
327		{"abc", 0, "abc"},
328		{"a\nb\nc", 0, "a"},
329		{"abc", -1, ""},
330		{"abacaba\n", 1, ""},
331		{"abc", 3, ""},
332	}
333	for _, test := range tests {
334		actual, _ := GetLine(test.param1, test.param2)
335		if actual != test.expected {
336			t.Errorf("Expected GetLine(%q, %d) to be %v, got %v", test.param1, test.param2, test.expected, actual)
337		}
338	}
339}
340
341func TestRemoveTags(t *testing.T) {
342	t.Parallel()
343
344	var tests = []struct {
345		param    string
346		expected string
347	}{
348		{"abc", "abc"},
349		{"<!-- Test -->", ""},
350		{"<div><div><p><a>Text</a></p></div></div>", "Text"},
351		{`<a href="#">Link</a>`, "Link"},
352	}
353	for _, test := range tests {
354		actual := RemoveTags(test.param)
355		if actual != test.expected {
356			t.Errorf("Expected RemoveTags(%q) to be %v, got %v", test.param, test.expected, actual)
357		}
358	}
359}
360
361func TestSafeFileName(t *testing.T) {
362	t.Parallel()
363
364	var tests = []struct {
365		param    string
366		expected string
367	}{
368		{"abc", "abc"},
369		{"123456789     '_-?ASDF@£$%£%^é.html", "123456789-asdf.html"},
370		{"ReadMe.md", "readme.md"},
371		{"file:///c:/test.go", "test.go"},
372		{"../../../Hello World!.txt", "hello-world.txt"},
373	}
374	for _, test := range tests {
375		actual := SafeFileName(test.param)
376		if actual != test.expected {
377			t.Errorf("Expected SafeFileName(%q) to be %v, got %v", test.param, test.expected, actual)
378		}
379	}
380}
381
382func TestNormalizeEmail(t *testing.T) {
383	t.Parallel()
384
385	var tests = []struct {
386		param    string
387		expected string
388	}{
389		{`test@me.com`, `test@me.com`},
390		{`some.name@gmail.com`, `somename@gmail.com`},
391		{`some.name@googlemail.com`, `somename@gmail.com`},
392		{`some.name+extension@gmail.com`, `somename@gmail.com`},
393		{`some.name+extension@googlemail.com`, `somename@gmail.com`},
394		{`some.name.middlename+extension@gmail.com`, `somenamemiddlename@gmail.com`},
395		{`some.name.middlename+extension@googlemail.com`, `somenamemiddlename@gmail.com`},
396		{`some.name.midd.lena.me.+extension@gmail.com`, `somenamemiddlename@gmail.com`},
397		{`some.name.midd.lena.me.+extension@googlemail.com`, `somenamemiddlename@gmail.com`},
398		{`some.name+extension@unknown.com`, `some.name+extension@unknown.com`},
399		// TODO: {`hans@m端ller.com`, `hans@m端ller.com`},
400		{`hans`, ``},
401	}
402	for _, test := range tests {
403		actual, err := NormalizeEmail(test.param)
404		if actual != test.expected {
405			t.Errorf("Expected NormalizeEmail(%q) to be %v, got %v, err %v", test.param, test.expected, actual, err)
406		}
407	}
408}
409
410func TestTruncate(t *testing.T) {
411	t.Parallel()
412
413	var tests = []struct {
414		param1   string
415		param2   int
416		param3   string
417		expected string
418	}{
419		{`Lorem ipsum dolor sit amet, consectetur adipiscing elit.`, 25, `...`, `Lorem ipsum dolor sit amet...`},
420		{`Measuring programming progress by lines of code is like measuring aircraft building progress by weight.`, 35, ` new born babies!`, `Measuring programming progress by new born babies!`},
421		{`Testestestestestestestestestest testestestestestestestestest`, 7, `...`, `Testestestestestestestestestest...`},
422		{`Testing`, 7, `...`, `Testing`},
423	}
424	for _, test := range tests {
425		actual := Truncate(test.param1, test.param2, test.param3)
426		if actual != test.expected {
427			t.Errorf("Expected Truncate(%q, %d, %q) to be %v, got %v", test.param1, test.param2, test.param3, test.expected, actual)
428		}
429	}
430}
431
432func TestPadLeft(t *testing.T) {
433	t.Parallel()
434
435	var tests = []struct {
436		param1   string
437		param2   string
438		param3   int
439		expected string
440	}{
441		{"こんにちは", "xyz", 12, "xyzxyzxこんにちは"},
442		{"こんにちは", "xyz", 11, "xyzxyzこんにちは"},
443		{"abc", "x", 5, "xxabc"},
444		{"abc", "xyz", 5, "xyabc"},
445		{"abcde", "xyz", 5, "abcde"},
446		{"abcde", "xyz", 4, "abcde"},
447	}
448	for _, test := range tests {
449		actual := PadLeft(test.param1, test.param2, test.param3)
450		if actual != test.expected {
451			t.Errorf("Expected PadLeft(%q,%q,%q) to be %v, got %v", test.param1, test.param2, test.param3, test.expected, actual)
452		}
453	}
454}
455
456func TestPadRight(t *testing.T) {
457	t.Parallel()
458
459	var tests = []struct {
460		param1   string
461		param2   string
462		param3   int
463		expected string
464	}{
465		{"こんにちは", "xyz", 12, "こんにちはxyzxyzx"},
466		{"こんにちは", "xyz", 11, "こんにちはxyzxyz"},
467		{"abc", "x", 5, "abcxx"},
468		{"abc", "xyz", 5, "abcxy"},
469		{"abcde", "xyz", 5, "abcde"},
470		{"abcde", "xyz", 4, "abcde"},
471	}
472	for _, test := range tests {
473		actual := PadRight(test.param1, test.param2, test.param3)
474		if actual != test.expected {
475			t.Errorf("Expected PadRight(%q,%q,%q) to be %v, got %v", test.param1, test.param2, test.param3, test.expected, actual)
476		}
477	}
478}
479
480func TestPadBoth(t *testing.T) {
481	t.Parallel()
482
483	var tests = []struct {
484		param1   string
485		param2   string
486		param3   int
487		expected string
488	}{
489		{"こんにちは", "xyz", 12, "xyzこんにちはxyzx"},
490		{"こんにちは", "xyz", 11, "xyzこんにちはxyz"},
491		{"abc", "x", 5, "xabcx"},
492		{"abc", "xyz", 5, "xabcx"},
493		{"abcde", "xyz", 5, "abcde"},
494		{"abcde", "xyz", 4, "abcde"},
495	}
496	for _, test := range tests {
497		actual := PadBoth(test.param1, test.param2, test.param3)
498		if actual != test.expected {
499			t.Errorf("Expected PadBoth(%q,%q,%q) to be %v, got %v", test.param1, test.param2, test.param3, test.expected, actual)
500		}
501	}
502}
503