github.com/greenpau/go-identity@v1.1.6/name_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 TestNewName(t *testing.T) { 26 testcases := []struct { 27 name string 28 fullName string 29 want map[string]interface{} 30 shouldErr bool 31 err error 32 }{ 33 { 34 name: "test name", 35 fullName: "John Smith", 36 want: map[string]interface{}{ 37 "claim": "Smith, John", 38 "full_name": "Smith, John", 39 }, 40 }, 41 { 42 name: "test parse name error", 43 fullName: "foobar", 44 shouldErr: true, 45 err: errors.ErrParseNameFailed.WithArgs("foobar"), 46 }, 47 } 48 for _, tc := range testcases { 49 t.Run(tc.name, func(t *testing.T) { 50 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 51 NewName() 52 entry, err := ParseName(tc.fullName) 53 if tests.EvalErrWithLog(t, err, "parse name", tc.shouldErr, tc.err, msgs) { 54 return 55 } 56 got := make(map[string]interface{}) 57 got["claim"] = entry.GetNameClaim() 58 got["full_name"] = entry.GetFullName() 59 tests.EvalObjectsWithLog(t, "eval", tc.want, got, msgs) 60 }) 61 } 62 }