github.com/greenpau/go-identity@v1.1.6/email_test.go (about) 1 // Copyright 2020 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package identity 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/greenpau/go-identity/internal/tests" 22 "github.com/greenpau/go-identity/pkg/errors" 23 ) 24 25 func TestNewEmailAddress(t *testing.T) { 26 testcases := []struct { 27 name string 28 input string 29 primary bool 30 want map[string]interface{} 31 shouldErr bool 32 err error 33 }{ 34 { 35 name: "test valid email address", 36 input: "jsmith@gmail.com", 37 want: map[string]interface{}{ 38 "address": "jsmith@gmail.com", 39 "domain": "gmail.com", 40 "primary": false, 41 }, 42 }, 43 { 44 name: "test valid primary email address", 45 input: "jsmith@gmail.com", 46 primary: true, 47 want: map[string]interface{}{ 48 "address": "jsmith@gmail.com", 49 "domain": "gmail.com", 50 "primary": true, 51 }, 52 }, 53 { 54 name: "test input with domain only", 55 input: "gmail.com", 56 shouldErr: true, 57 err: errors.ErrEmailAddressInvalid, 58 }, 59 } 60 for _, tc := range testcases { 61 t.Run(tc.name, func(t *testing.T) { 62 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 63 entry, err := NewEmailAddress(tc.input) 64 if tests.EvalErrWithLog(t, err, "new email address", tc.shouldErr, tc.err, msgs) { 65 return 66 } 67 if tc.primary { 68 entry.isPrimary = true 69 } 70 got := make(map[string]interface{}) 71 got["address"] = entry.Address 72 got["domain"] = entry.Domain 73 got["primary"] = entry.Primary() 74 tests.EvalObjectsWithLog(t, "eval", tc.want, got, msgs) 75 }) 76 } 77 }