diff --git a/beneficiary_test.go b/beneficiary_test.go index ef3e25a9..2ab2241e 100644 --- a/beneficiary_test.go +++ b/beneficiary_test.go @@ -1,9 +1,10 @@ package wire import ( - "github.com/moov-io/base" "strings" "testing" + + "github.com/stretchr/testify/require" ) // mockBeneficiary creates a Beneficiary @@ -21,97 +22,96 @@ func mockBeneficiary() *Beneficiary { // TestMockBeneficiary validates mockBeneficiary func TestMockBeneficiary(t *testing.T) { ben := mockBeneficiary() - if err := ben.Validate(); err != nil { - t.Error("mockBeneficiary does not validate and will break other tests") - } + + require.NoError(t, ben.Validate(), "mockBeneficiary does not validate and will break other tests") } // TestBeneficiaryIdentificationCodeValid validates Beneficiary IdentificationCode func TestBeneficiaryIdentificationCodeValid(t *testing.T) { ben := mockBeneficiary() ben.Personal.IdentificationCode = "Baseball Card ID" - if err := ben.Validate(); err != nil { - if !base.Match(err, ErrIdentificationCode) { - t.Errorf("%T: %s", err, err) - } - } + + err := ben.Validate() + + require.NotNil(t, err) + require.Equal(t, fieldError("IdentificationCode", ErrIdentificationCode, ben.Personal.IdentificationCode).Error(), err.Error()) } // TestBeneficiaryIdentifierAlphaNumeric validates Beneficiary Identifier is alphanumeric func TestBeneficiaryIdentifierAlphaNumeric(t *testing.T) { ben := mockBeneficiary() ben.Personal.Identifier = "®" - if err := ben.Validate(); err != nil { - if !base.Match(err, ErrNonAlphanumeric) { - t.Errorf("%T: %s", err, err) - } - } + + err := ben.Validate() + + require.NotNil(t, err) + require.Equal(t, fieldError("Identifier", ErrNonAlphanumeric, ben.Personal.Identifier).Error(), err.Error()) } // TestBeneficiaryNameAlphaNumeric validates Beneficiary Name is alphanumeric func TestBeneficiaryNameAlphaNumeric(t *testing.T) { ben := mockBeneficiary() ben.Personal.Name = "®" - if err := ben.Validate(); err != nil { - if !base.Match(err, ErrNonAlphanumeric) { - t.Errorf("%T: %s", err, err) - } - } + + err := ben.Validate() + + require.NotNil(t, err) + require.Equal(t, fieldError("Name", ErrNonAlphanumeric, ben.Personal.Name).Error(), err.Error()) } // TestBeneficiaryAddressLineOneAlphaNumeric validates Beneficiary AddressLineOne is alphanumeric func TestBeneficiaryAddressLineOneAlphaNumeric(t *testing.T) { ben := mockBeneficiary() ben.Personal.Address.AddressLineOne = "®" - if err := ben.Validate(); err != nil { - if !base.Match(err, ErrNonAlphanumeric) { - t.Errorf("%T: %s", err, err) - } - } + + err := ben.Validate() + + require.NotNil(t, err) + require.Equal(t, fieldError("AddressLineOne", ErrNonAlphanumeric, ben.Personal.Address.AddressLineOne).Error(), err.Error()) } // TestBeneficiaryAddressLineTwoAlphaNumeric validates Beneficiary AddressLineTwo is alphanumeric func TestBeneficiaryAddressLineTwoAlphaNumeric(t *testing.T) { ben := mockBeneficiary() ben.Personal.Address.AddressLineTwo = "®" - if err := ben.Validate(); err != nil { - if !base.Match(err, ErrNonAlphanumeric) { - t.Errorf("%T: %s", err, err) - } - } + + err := ben.Validate() + + require.NotNil(t, err) + require.Equal(t, fieldError("AddressLineTwo", ErrNonAlphanumeric, ben.Personal.Address.AddressLineTwo).Error(), err.Error()) } // TestBeneficiaryAddressLineThreeAlphaNumeric validates Beneficiary AddressLineThree is alphanumeric func TestBeneficiaryAddressLineThreeAlphaNumeric(t *testing.T) { ben := mockBeneficiary() ben.Personal.Address.AddressLineThree = "®" - if err := ben.Validate(); err != nil { - if !base.Match(err, ErrNonAlphanumeric) { - t.Errorf("%T: %s", err, err) - } - } + + err := ben.Validate() + + require.NotNil(t, err) + require.Equal(t, fieldError("AddressLineThree", ErrNonAlphanumeric, ben.Personal.Address.AddressLineThree).Error(), err.Error()) } // TestBeneficiaryIdentificationCodeRequired validates Beneficiary IdentificationCode is required func TestBeneficiaryIdentificationCodeRequired(t *testing.T) { ben := mockBeneficiary() ben.Personal.IdentificationCode = "" - if err := ben.Validate(); err != nil { - if !base.Match(err, ErrFieldRequired) { - t.Errorf("%T: %s", err, err) - } - } + + err := ben.Validate() + + require.NotNil(t, err) + require.Equal(t, fieldError("IdentificationCode", ErrFieldRequired).Error(), err.Error()) } // TestBeneficiaryIdentifierRequired validates Beneficiary Identifier is required func TestBeneficiaryIdentifierRequired(t *testing.T) { ben := mockBeneficiary() ben.Personal.Identifier = "" - if err := ben.Validate(); err != nil { - if !base.Match(err, ErrFieldRequired) { - t.Errorf("%T: %s", err, err) - } - } + + err := ben.Validate() + + require.NotNil(t, err) + require.Equal(t, fieldError("Identifier", ErrFieldRequired).Error(), err.Error()) } // TestParseBeneficiaryWrongLength parses a wrong Beneficiary record length @@ -119,15 +119,11 @@ func TestParseBeneficiaryWrongLength(t *testing.T) { var line = "{4200}31234 Name Address One Address Two Address Three " r := NewReader(strings.NewReader(line)) r.line = line - fwm := new(FEDWireMessage) - ben := mockBeneficiary() - fwm.SetBeneficiary(ben) + err := r.parseBeneficiary() - if err != nil { - if !base.Match(err, NewTagWrongLengthErr(181, len(r.line))) { - t.Errorf("%T: %s", err, err) - } - } + + require.NotNil(t, err) + require.Contains(t, err.Error(), NewTagWrongLengthErr(181, len(r.line)).Error()) } // TestParseBeneficiaryReaderParseError parses a wrong Beneficiary reader parse error @@ -135,30 +131,25 @@ func TestParseBeneficiaryReaderParseError(t *testing.T) { var line = "{4200}31234 Na®e Address One Address Two Address Three " r := NewReader(strings.NewReader(line)) r.line = line - fwm := new(FEDWireMessage) - ben := mockBeneficiary() - fwm.SetBeneficiary(ben) + err := r.parseBeneficiary() - if err != nil { - if !base.Match(err, ErrNonAlphanumeric) { - t.Errorf("%T: %s", err, err) - } - } + + require.NotNil(t, err) + require.Contains(t, err.Error(), ErrNonAlphanumeric.Error()) + _, err = r.Read() - if err != nil { - if !base.Has(err, ErrNonAlphanumeric) { - t.Errorf("%T: %s", err, err) - } - } + + require.NotNil(t, err) + require.Contains(t, err.Error(), ErrNonAlphanumeric.Error()) } // TestBeneficiaryTagError validates Beneficiary tag func TestBeneficiaryTagError(t *testing.T) { ben := mockBeneficiary() ben.tag = "{9999}" - if err := ben.Validate(); err != nil { - if !base.Match(err, ErrValidTagForType) { - t.Errorf("%T: %s", err, err) - } - } + + err := ben.Validate() + + require.NotNil(t, err) + require.Equal(t, fieldError("tag", ErrValidTagForType, ben.tag).Error(), err.Error()) }