-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_test.go
99 lines (84 loc) · 2.53 KB
/
email_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
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker
package checker_test
import (
"testing"
"github.com/cinar/checker"
)
func ExampleIsEmail() {
err := checker.IsEmail("[email protected]")
if err != nil {
// Send the errors back to the user
}
}
func TestCheckEmailNonString(t *testing.T) {
defer checker.FailIfNoPanic(t)
type User struct {
Email int `checkers:"email"`
}
user := &User{}
checker.Check(user)
}
func TestCheckEmailValid(t *testing.T) {
type User struct {
Email string `checkers:"email"`
}
user := &User{
Email: "[email protected]",
}
_, valid := checker.Check(user)
if !valid {
t.Fail()
}
}
func TestIsEmailValid(t *testing.T) {
validEmails := []string{
"test/[email protected]",
"\" \"@example.org",
"\"john..doe\"@example.org",
"\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@strange.example.com",
"user%[email protected]",
"postmaster@[123.123.123.123]",
"postmaster@[IPv6:2001:0db8:85a3:0000:0000:8a2e:0370:7334]",
}
for _, email := range validEmails {
if checker.IsEmail(email) != nil {
t.Fatal(email)
}
}
}
func TestIsEmailInvalid(t *testing.T) {
validEmails := []string{
"Abc.example.com",
"A@b@[email protected]",
"a\"b(c)d,e:f;g<h>i[j\\k][email protected]",
"just\"not\"[email protected]",
"this is\"not\\[email protected]",
"this\\ still\\\"not\\\\[email protected]",
"1234567890123456789012345678901234567890123456789012345678901234+x@example.com",
"i_like_underscore@but_its_not_allowed_in_this_part.example.com",
"QA[icon]CHOCOLATE[icon]@test.com",
"user@domaincannotbemorethan255charactersdomaincannotbemorethan255charactersdomaincannotbemorethan255charactersdomaincannotbemorethan255charactersdomaincannotbemorethan255charactersdomaincannotbemorethan255charactersdomaincannotbemorethan255charactersdomaincannotbemorethan255characters.com",
}
for _, email := range validEmails {
if checker.IsEmail(email) == nil {
t.Fatal(email)
}
}
}