Created
March 11, 2019 08:27
-
-
Save Crazy3lf/5cd87ae25fb21c92284b04201f1ffc5e to your computer and use it in GitHub Desktop.
Test cases for email validation for Go based on https://blogs.msdn.microsoft.com/testing123/2009/02/06/email-address-test-cases/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package email | |
| import ( | |
| "testing" | |
| ) | |
| func TestIsValidEmail(t *testing.T) { | |
| testCases := []struct { | |
| name string | |
| email string | |
| expected bool | |
| }{ | |
| //valid email | |
| { | |
| "Valid email", | |
| "email@domain.com", | |
| true, | |
| }, | |
| { | |
| "Email contains dot in the address field", | |
| "firstname.lastname@domain.com", | |
| true, | |
| }, { | |
| "Email contains dot with subdomain", | |
| "email@subdomain.domain.com", | |
| true, | |
| }, | |
| { | |
| "Plus sign is considered valid character", | |
| "firstname+lastname@domain.com", | |
| true, | |
| }, { | |
| "Valid email", | |
| "email@domain.com", | |
| true, | |
| }, | |
| { | |
| "Domain is valid IP address", | |
| "email@123.123.123.123", | |
| true, | |
| }, { | |
| "Square bracket around IP address is considered valid", | |
| "email@[123.123.123.123]", | |
| true, | |
| }, | |
| { | |
| "Digits in address are valid", | |
| "1234567890@domain.com", | |
| true, | |
| }, | |
| { | |
| "Dash in domain name is valid", | |
| "email@domain-one.com", | |
| true, | |
| }, { | |
| "Underscore in the address field is valid", | |
| "_______@domain.com", | |
| true, | |
| }, | |
| { | |
| ".name is valid Top Level Domain name", | |
| "email@domain.name", | |
| true, | |
| }, { | |
| "Dot in Top Level Domain name also considered valid (use co.jp as example here)", | |
| "email@domain.co.jp", | |
| true, | |
| }, | |
| { | |
| "Dash in address field is valid", | |
| "firstname-lastname@domain.com", | |
| true, | |
| }, | |
| // invalid email | |
| { | |
| "Missing @ sign and domain", | |
| "plainaddress", | |
| false, | |
| }, | |
| { | |
| "Garbage", | |
| "#@%^%#$@#$@#.com", | |
| false, | |
| }, { | |
| "Missing username", | |
| "@domain.com", | |
| false, | |
| }, { | |
| "Encoded html within email is invalid", | |
| "Joe Smith <email@domain.com>", | |
| false, | |
| }, { | |
| "Missing @", | |
| "email.domain.com ", | |
| false, | |
| }, { | |
| "Two @ sign", | |
| "email@domain@domain.com", | |
| false, | |
| }, { | |
| "Leading dot in address is not allowed", | |
| ".email@domain.com", | |
| false, | |
| }, { | |
| "Trailing dot in address is not allowed", | |
| "email.@domain.com", | |
| false, | |
| }, { | |
| "Multiple dots", | |
| "email..email@domain.com", | |
| false, | |
| }, { | |
| "Unicode char as address", | |
| "あいうえお@domain.com", | |
| false, | |
| }, { | |
| "Leading dash in front of domain is invalid", | |
| "email@-domain.com", | |
| false, | |
| }, { | |
| "Missing top level domain (.com/.net/.org/etc)", | |
| "email@domain", | |
| false, | |
| }, { | |
| "Text followed email is not allowed", | |
| "email@domain.com (Joe Smith)", | |
| false, | |
| }, { | |
| "Multiple dot in the domain portion is invalid", | |
| "email@domain..com", | |
| false, | |
| }, | |
| } | |
| for _, testCase := range testCases { | |
| t.Run(testCase.name, func(t *testing.T) { | |
| actual := IsValidEmail(testCase.email) | |
| if !(actual == testCase.expected) { | |
| t.Fail() | |
| } | |
| }) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This list does not cover all possible email address format specified in RFC5321 and RFC5322. Also, Unicode maybe allowed as specified in RFC6531.
More info: https://www.netmeister.org/blog/email.html