github.com/go-email-validator/go-email-validator@v0.0.0-20230409163946-b8b9e6a0552e/pkg/ev/validator_test.go (about)

     1  package ev
     2  
     3  import (
     4  	"errors"
     5  	"github.com/go-email-validator/go-email-validator/pkg/ev/evmail"
     6  	"github.com/go-email-validator/go-email-validator/pkg/ev/evtests"
     7  	"github.com/go-email-validator/go-email-validator/pkg/ev/utils"
     8  	mockevmail "github.com/go-email-validator/go-email-validator/test/mock/ev/evmail"
     9  	"github.com/stretchr/testify/require"
    10  	"reflect"
    11  	"sort"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  var (
    17  	emptyEmail                     = evmail.NewEmailAddress("", "")
    18  	emptyErrors                    = make([]error, 0)
    19  	validEmail                     = evmail.FromString(mockevmail.ValidEmailString)
    20  	invalidEmail                   = evmail.FromString("some%..@invalid.%.email")
    21  	validMockValidator   Validator = mockValidator{result: true}
    22  	inValidMockValidator Validator = mockValidator{result: false}
    23  	errorSimple                    = errors.New("errorSimple")
    24  	errorSimple2                   = errors.New("errorSimple2")
    25  	validResult                    = NewResult(true, nil, nil, OtherValidator)
    26  	invalidResult                  = NewResult(false, utils.Errs(newMockError()), nil, OtherValidator)
    27  )
    28  
    29  func sortErrors(errs []error) []error {
    30  	sort.Slice(errs, func(l, r int) bool {
    31  		return strings.Compare(errs[l].Error(), errs[r].Error()) >= 0
    32  	})
    33  
    34  	return errs
    35  }
    36  
    37  type mockContains struct {
    38  	t    *testing.T
    39  	want interface{}
    40  	ret  bool
    41  }
    42  
    43  func (m mockContains) Contains(value interface{}) bool {
    44  	require.Equal(m.t, value, m.want)
    45  
    46  	return m.ret
    47  }
    48  
    49  type mockInString struct {
    50  	t    *testing.T
    51  	want interface{}
    52  	ret  bool
    53  }
    54  
    55  func (m mockInString) Contains(value string) bool {
    56  	require.Equal(m.t, value, m.want)
    57  
    58  	return m.ret
    59  }
    60  
    61  func newMockError() error {
    62  	return mockError{}
    63  }
    64  
    65  type mockError struct{}
    66  
    67  func (mockError) Error() string {
    68  	return "mockError"
    69  }
    70  
    71  const mockValidatorName ValidatorName = "mockValidatorName"
    72  
    73  func newMockValidator(result bool) mockValidator {
    74  	return mockValidator{result: result}
    75  }
    76  
    77  type mockValidator struct {
    78  	result bool
    79  	deps   []ValidatorName
    80  }
    81  
    82  func (m mockValidator) Validate(_ Input, _ ...ValidationResult) ValidationResult {
    83  	var err error
    84  	if !m.result {
    85  		err = newMockError()
    86  	}
    87  
    88  	return NewResult(m.result, utils.Errs(err), nil, OtherValidator)
    89  }
    90  
    91  func (m mockValidator) GetDeps() []ValidatorName {
    92  	return m.deps
    93  }
    94  
    95  type mockValidationResult struct {
    96  	errs  []error
    97  	warns []error
    98  	name  ValidatorName
    99  }
   100  
   101  func (m mockValidationResult) IsValid() bool {
   102  	return m.HasErrors()
   103  }
   104  
   105  func (m mockValidationResult) Errors() []error {
   106  	return m.errs
   107  }
   108  
   109  func (m mockValidationResult) HasErrors() bool {
   110  	return reflect.ValueOf(m.Errors()).Len() > 0
   111  }
   112  
   113  func (m mockValidationResult) Warnings() []error {
   114  	return m.warns
   115  }
   116  
   117  func (m mockValidationResult) HasWarnings() bool {
   118  	return reflect.ValueOf(m.Warnings()).Len() > 0
   119  }
   120  
   121  func (m mockValidationResult) ValidatorName() ValidatorName {
   122  	return m.name
   123  }
   124  
   125  func TestMain(m *testing.M) {
   126  	evtests.TestMain(m)
   127  }
   128  
   129  func TestMockValidator(t *testing.T) {
   130  	cases := []struct {
   131  		validator mockValidator
   132  		expected  ValidationResult
   133  	}{
   134  		{
   135  			validator: newMockValidator(true),
   136  			expected:  NewResult(true, nil, nil, OtherValidator),
   137  		},
   138  		{
   139  			validator: newMockValidator(false),
   140  			expected:  NewResult(false, utils.Errs(newMockError()), nil, OtherValidator),
   141  		},
   142  	}
   143  
   144  	var emptyEmail evmail.Address
   145  	for _, c := range cases {
   146  		actual := c.validator.Validate(NewInput(emptyEmail))
   147  		require.Equal(t, c.expected, actual)
   148  	}
   149  }
   150  
   151  func TestAValidatorWithoutDeps(t *testing.T) {
   152  	validator := AValidatorWithoutDeps{}
   153  
   154  	require.Equal(t, emptyDeps, validator.GetDeps())
   155  }
   156  
   157  func TestNewValidatorResult(t *testing.T) {
   158  	type args struct {
   159  		isValid  bool
   160  		errors   []error
   161  		warnings []error
   162  		name     ValidatorName
   163  	}
   164  	tests := []struct {
   165  		name string
   166  		args args
   167  		want ValidationResult
   168  	}{
   169  		{
   170  			name: "empty name",
   171  			args: args{
   172  				isValid:  true,
   173  				errors:   nil,
   174  				warnings: nil,
   175  				name:     "",
   176  			},
   177  			want: &validationResult{true, nil, nil, OtherValidator},
   178  		},
   179  		{
   180  			name: "invalid with errors and warnings",
   181  			args: args{
   182  				isValid:  false,
   183  				errors:   []error{errorSimple},
   184  				warnings: []error{errorSimple},
   185  				name:     mockValidatorName,
   186  			},
   187  			want: &validationResult{false, []error{errorSimple}, []error{errorSimple}, mockValidatorName},
   188  		},
   189  		{
   190  			name: "invalid",
   191  			args: args{
   192  				isValid:  false,
   193  				errors:   nil,
   194  				warnings: nil,
   195  				name:     mockValidatorName,
   196  			},
   197  			want: &validationResult{false, nil, nil, mockValidatorName},
   198  		},
   199  	}
   200  	for _, tt := range tests {
   201  		t.Run(tt.name, func(t *testing.T) {
   202  			if got := NewResult(tt.args.isValid, tt.args.errors, tt.args.warnings, tt.args.name); !reflect.DeepEqual(got, tt.want) {
   203  				t.Errorf("NewResult() = %v, want %v", got, tt.want)
   204  			}
   205  		})
   206  	}
   207  }
   208  
   209  func TestValidatorName_String(t *testing.T) {
   210  	tests := []struct {
   211  		name string
   212  		v    ValidatorName
   213  		want string
   214  	}{
   215  		{
   216  			name: "success",
   217  			v:    mockValidatorName,
   218  			want: string(mockValidatorName),
   219  		},
   220  	}
   221  	for _, tt := range tests {
   222  		t.Run(tt.name, func(t *testing.T) {
   223  			if got := tt.v.String(); got != tt.want {
   224  				t.Errorf("String() = %v, want %v", got, tt.want)
   225  			}
   226  		})
   227  	}
   228  }
   229  
   230  func TestAValidationResult_HasWarnings(t *testing.T) {
   231  	type fields struct {
   232  		isValid  bool
   233  		errors   []error
   234  		warnings []error
   235  		name     ValidatorName
   236  	}
   237  	tests := []struct {
   238  		name   string
   239  		fields fields
   240  		want   bool
   241  	}{
   242  		{
   243  			name: "true",
   244  			fields: fields{
   245  				warnings: utils.Errs(errorSimple),
   246  			},
   247  			want: true,
   248  		},
   249  		{
   250  			name: "false empty",
   251  			fields: fields{
   252  				warnings: []error{},
   253  			},
   254  			want: false,
   255  		},
   256  		{
   257  			name: "false nil",
   258  			fields: fields{
   259  				warnings: nil,
   260  			},
   261  			want: false,
   262  		},
   263  	}
   264  	for _, tt := range tests {
   265  		t.Run(tt.name, func(t *testing.T) {
   266  			a := &AValidationResult{
   267  				isValid:  tt.fields.isValid,
   268  				errors:   tt.fields.errors,
   269  				warnings: tt.fields.warnings,
   270  				name:     tt.fields.name,
   271  			}
   272  			if got := a.HasWarnings(); got != tt.want {
   273  				t.Errorf("HasWarnings() = %v, want %v", got, tt.want)
   274  			}
   275  		})
   276  	}
   277  }
   278  
   279  func TestAValidationResult_ValidatorName(t *testing.T) {
   280  	type fields struct {
   281  		isValid  bool
   282  		errors   []error
   283  		warnings []error
   284  		name     ValidatorName
   285  	}
   286  	tests := []struct {
   287  		name   string
   288  		fields fields
   289  		want   ValidatorName
   290  	}{
   291  		{
   292  			name: "success",
   293  			fields: fields{
   294  				name: OtherValidator,
   295  			},
   296  			want: OtherValidator,
   297  		},
   298  	}
   299  	for _, tt := range tests {
   300  		t.Run(tt.name, func(t *testing.T) {
   301  			a := &AValidationResult{
   302  				isValid:  tt.fields.isValid,
   303  				errors:   tt.fields.errors,
   304  				warnings: tt.fields.warnings,
   305  				name:     tt.fields.name,
   306  			}
   307  			if got := a.ValidatorName(); got != tt.want {
   308  				t.Errorf("ValidatorName() = %v, want %v", got, tt.want)
   309  			}
   310  		})
   311  	}
   312  }