| 
									
										
										
										
											2018-08-06 04:54:06 +08:00
										 |  |  | package util | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2020-08-13 17:10:48 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2018-08-06 04:54:06 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 17:10:48 +08:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							| 
									
										
										
										
											2018-08-06 04:54:06 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestIsEmail(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-08-13 17:10:48 +08:00
										 |  |  | 	t.Parallel() | 
					
						
							| 
									
										
										
										
											2018-08-06 04:54:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 17:10:48 +08:00
										 |  |  | 	emails := map[string]struct { | 
					
						
							|  |  |  | 		description string | 
					
						
							|  |  |  | 		valid       bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		"":                         {description: "the empty string", valid: false}, | 
					
						
							|  |  |  | 		"@.":                       {description: "at dot", valid: false}, | 
					
						
							|  |  |  | 		"me@":                      {description: "no domain", valid: false}, | 
					
						
							|  |  |  | 		"abcdef.com":               {description: "only a domain name", valid: false}, | 
					
						
							|  |  |  | 		"@example.org":             {description: "no recipient", valid: false}, | 
					
						
							|  |  |  | 		"please\x0Ano@example.org": {description: "new line", valid: false}, | 
					
						
							| 
									
										
										
										
											2018-08-06 04:54:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 17:10:48 +08:00
										 |  |  | 		"abc@def.com":                 {description: "a simple valid email", valid: true}, | 
					
						
							|  |  |  | 		"grapher+grafana@example.org": {description: "a gmail style alias", valid: true}, | 
					
						
							|  |  |  | 		"öhnej@example.se":            {description: "non-ASCII characters", valid: true}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for input, testcase := range emails { | 
					
						
							|  |  |  | 		validity := "invalid" | 
					
						
							|  |  |  | 		if testcase.valid { | 
					
						
							|  |  |  | 			validity = "valid" | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-08-06 04:54:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-13 17:10:48 +08:00
										 |  |  | 		t.Run(fmt.Sprintf("validating that %s is %s", testcase.description, validity), func(t *testing.T) { | 
					
						
							|  |  |  | 			assert.Equal(t, testcase.valid, IsEmail(input)) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-08-06 04:54:06 +08:00
										 |  |  | } |