github.com/blend/go-sdk@v1.20220411.3/validate/any_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package validate
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  	"github.com/blend/go-sdk/ex"
    15  )
    16  
    17  func TestForbidden(t *testing.T) {
    18  	assert := assert.New(t)
    19  
    20  	var verr error
    21  	verr = Any(nil).Forbidden()()
    22  	assert.Nil(verr)
    23  
    24  	verr = Any((*string)(nil)).Forbidden()()
    25  	assert.Nil(verr)
    26  
    27  	verr = Any("foo").Forbidden()()
    28  	assert.NotNil(verr)
    29  	assert.Equal(ErrForbidden, ErrCause(verr))
    30  }
    31  
    32  func TestRequired(t *testing.T) {
    33  	assert := assert.New(t)
    34  
    35  	var verr error
    36  	verr = Any("foo").Required()()
    37  	assert.Nil(verr)
    38  
    39  	verr = Any(nil).Required()()
    40  	assert.NotNil(verr)
    41  	assert.Equal(ErrRequired, ErrCause(verr))
    42  
    43  	verr = Any((*string)(nil)).Required()()
    44  	assert.NotNil(verr)
    45  	assert.Equal(ErrRequired, ErrCause(verr))
    46  }
    47  
    48  func TestNotZero(t *testing.T) {
    49  	assert := assert.New(t)
    50  
    51  	var verr error
    52  	verr = Any("foo").NotZero()()
    53  	assert.Nil(verr)
    54  
    55  	verr = Any(nil).NotZero()()
    56  	assert.NotNil(verr)
    57  	assert.Equal(ErrNotZero, ErrCause(verr))
    58  
    59  	verr = Any((*string)(nil)).NotZero()()
    60  	assert.NotNil(verr)
    61  	assert.Equal(ErrNotZero, ErrCause(verr))
    62  }
    63  
    64  func TestAnyRefZero(t *testing.T) {
    65  	assert := assert.New(t)
    66  
    67  	type zeroTest struct {
    68  		ID    int
    69  		Value string
    70  	}
    71  
    72  	testCases := [...]struct {
    73  		Input    interface{}
    74  		Expected error
    75  	}{
    76  		{
    77  			Input:    nil,
    78  			Expected: nil,
    79  		},
    80  		{
    81  			Input:    (*string)(nil),
    82  			Expected: nil,
    83  		},
    84  		{
    85  			Input:    0,
    86  			Expected: nil,
    87  		},
    88  		{
    89  			Input:    1,
    90  			Expected: ErrZero,
    91  		},
    92  		{
    93  			Input:    "",
    94  			Expected: nil,
    95  		},
    96  		{
    97  			Input:    "foo",
    98  			Expected: ErrZero,
    99  		},
   100  		{
   101  			Input:    zeroTest{},
   102  			Expected: nil,
   103  		},
   104  		{
   105  			Input:    zeroTest{ID: 2},
   106  			Expected: ErrZero,
   107  		},
   108  	}
   109  
   110  	for index, tc := range testCases {
   111  		verr := Any(tc.Input).Zero()()
   112  		assert.Equal(tc.Expected, ErrCause(verr), index)
   113  	}
   114  }
   115  
   116  func TestAnyRefEmpty(t *testing.T) {
   117  	assert := assert.New(t)
   118  
   119  	testCases := [...]struct {
   120  		Input    interface{}
   121  		Expected error
   122  	}{
   123  		{
   124  			Input:    nil,
   125  			Expected: ErrNonLengthType,
   126  		},
   127  		{
   128  			Input:    0,
   129  			Expected: ErrNonLengthType,
   130  		},
   131  		{
   132  			Input:    []string{},
   133  			Expected: nil,
   134  		},
   135  		{
   136  			Input:    ([]string)(nil),
   137  			Expected: nil,
   138  		},
   139  		{
   140  			Input:    map[string]interface{}{},
   141  			Expected: nil,
   142  		},
   143  		{
   144  			Input:    (map[string]interface{})(nil),
   145  			Expected: nil,
   146  		},
   147  		{
   148  			Input:    "",
   149  			Expected: nil,
   150  		},
   151  		{
   152  			Input:    make(chan struct{}),
   153  			Expected: nil,
   154  		},
   155  		{
   156  			Input:    (chan struct{})(nil),
   157  			Expected: nil,
   158  		},
   159  		{
   160  			Input:    []string{"a", "b"},
   161  			Expected: ErrEmpty,
   162  		},
   163  		{
   164  			Input:    map[string]int{"hi": 1},
   165  			Expected: ErrEmpty,
   166  		},
   167  		{
   168  			Input:    "foo",
   169  			Expected: ErrEmpty,
   170  		},
   171  	}
   172  
   173  	for index, tc := range testCases {
   174  		verr := Any(tc.Input).Empty()()
   175  		assert.Equal(tc.Expected, ErrCause(verr), index)
   176  	}
   177  }
   178  
   179  func TestAnyRefNotEmpty(t *testing.T) {
   180  	assert := assert.New(t)
   181  
   182  	testCases := [...]struct {
   183  		Input    interface{}
   184  		Expected error
   185  	}{
   186  		{
   187  			Input:    nil,
   188  			Expected: ErrNonLengthType,
   189  		},
   190  		{
   191  			Input:    0,
   192  			Expected: ErrNonLengthType,
   193  		},
   194  		{
   195  			Input:    []string{},
   196  			Expected: ErrNotEmpty,
   197  		},
   198  		{
   199  			Input:    ([]string)(nil),
   200  			Expected: ErrNotEmpty,
   201  		},
   202  		{
   203  			Input:    map[string]interface{}{},
   204  			Expected: ErrNotEmpty,
   205  		},
   206  		{
   207  			Input:    (map[string]interface{})(nil),
   208  			Expected: ErrNotEmpty,
   209  		},
   210  		{
   211  			Input:    "",
   212  			Expected: ErrNotEmpty,
   213  		},
   214  		{
   215  			Input:    make(chan struct{}),
   216  			Expected: ErrNotEmpty,
   217  		},
   218  		{
   219  			Input:    (chan struct{})(nil),
   220  			Expected: ErrNotEmpty,
   221  		},
   222  		{
   223  			Input:    []string{"a", "b"},
   224  			Expected: nil,
   225  		},
   226  		{
   227  			Input:    map[string]int{"hi": 1},
   228  			Expected: nil,
   229  		},
   230  		{
   231  			Input:    "foo",
   232  			Expected: nil,
   233  		},
   234  	}
   235  
   236  	for index, tc := range testCases {
   237  		verr := Any(tc.Input).NotEmpty()()
   238  		assert.Equal(tc.Expected, ErrCause(verr), index)
   239  	}
   240  }
   241  
   242  func TestAnyRefLen(t *testing.T) {
   243  	assert := assert.New(t)
   244  
   245  	err := Any(1234).Len(10)()
   246  	assert.NotNil(err)
   247  	assert.Equal(ErrNonLengthType, ex.ErrClass(err))
   248  
   249  	verr := Any([]int{1, 2, 3, 4}).Len(4)()
   250  	assert.Nil(verr)
   251  
   252  	verr = Any(map[int]bool{1: true, 2: true}).Len(2)()
   253  	assert.Nil(verr)
   254  
   255  	verr = Any([]int{}).Len(4)()
   256  	assert.NotNil(verr)
   257  	assert.Equal(ErrLen, ErrCause(verr))
   258  }
   259  
   260  func TestAnyRefNil(t *testing.T) {
   261  	assert := assert.New(t)
   262  
   263  	var verr error
   264  	verr = Any(nil).Nil()()
   265  	assert.Nil(verr)
   266  
   267  	var nilPtr *string
   268  	verr = Any(nilPtr).Nil()()
   269  	assert.Nil(verr)
   270  
   271  	verr = Any("foo").Nil()()
   272  	assert.NotNil(verr)
   273  	assert.Equal(ErrNil, ErrCause(verr))
   274  }
   275  
   276  func TestAnyRefNotNil(t *testing.T) {
   277  	assert := assert.New(t)
   278  
   279  	var verr error
   280  	verr = Any("foo").NotNil()()
   281  	assert.Nil(verr)
   282  
   283  	verr = Any(nil).NotNil()()
   284  	assert.NotNil(verr)
   285  	assert.Equal(ErrNotNil, ErrCause(verr))
   286  
   287  	var nilPtr *string
   288  	verr = Any(nilPtr).NotNil()()
   289  	assert.NotNil(verr)
   290  	assert.Equal(ErrNotNil, ErrCause(verr))
   291  }
   292  
   293  func TestEquals(t *testing.T) {
   294  	assert := assert.New(t)
   295  
   296  	var verr error
   297  	verr = Any("foo").Equals("foo")()
   298  	assert.Nil(verr)
   299  
   300  	verr = Any(nil).Equals(nil)()
   301  	assert.Nil(verr)
   302  
   303  	verr = Any("foo").Equals("bar")()
   304  	assert.NotNil(verr)
   305  	assert.Equal(ErrEquals, ErrCause(verr))
   306  
   307  	verr = Any(nil).Equals("foo")()
   308  	assert.NotNil(verr)
   309  	assert.Equal(ErrEquals, ErrCause(verr))
   310  }
   311  
   312  func TestAnyRefNotEquals(t *testing.T) {
   313  	assert := assert.New(t)
   314  
   315  	var verr error
   316  	verr = Any("foo").NotEquals("bar")()
   317  	assert.Nil(verr)
   318  
   319  	verr = Any(nil).NotEquals("foo")()
   320  	assert.Nil(verr)
   321  
   322  	verr = Any("foo").NotEquals("foo")()
   323  	assert.NotNil(verr)
   324  	assert.Equal(ErrNotEquals, ErrCause(verr))
   325  
   326  	verr = Any(nil).NotEquals(nil)()
   327  	assert.NotNil(verr)
   328  	assert.Equal(ErrNotEquals, ErrCause(verr))
   329  }
   330  
   331  func TestAnyRefAllow(t *testing.T) {
   332  	assert := assert.New(t)
   333  
   334  	var verr error
   335  	verr = Any("foo").Allow("foo", "bar", "baz")()
   336  	assert.Nil(verr)
   337  	verr = Any("bar").Allow("foo", "bar", "baz")()
   338  	assert.Nil(verr)
   339  	verr = Any("baz").Allow("foo", "bar", "baz")()
   340  	assert.Nil(verr)
   341  
   342  	verr = Any("what").Allow("foo", "bar", "baz")()
   343  	assert.NotNil(verr)
   344  	assert.Equal(ErrAllowed, ErrCause(verr))
   345  }
   346  
   347  func TestAnyRefDisallow(t *testing.T) {
   348  	assert := assert.New(t)
   349  
   350  	var verr error
   351  	verr = Any("what").Disallow("foo", "bar", "baz")()
   352  	assert.Nil(verr)
   353  
   354  	verr = Any("foo").Disallow("foo", "bar", "baz")()
   355  	assert.NotNil(verr)
   356  	assert.Equal(ErrDisallowed, ErrCause(verr))
   357  	verr = Any("bar").Disallow("foo", "bar", "baz")()
   358  	assert.NotNil(verr)
   359  	assert.Equal(ErrDisallowed, ErrCause(verr))
   360  	verr = Any("baz").Disallow("foo", "bar", "baz")()
   361  	assert.NotNil(verr)
   362  	assert.Equal(ErrDisallowed, ErrCause(verr))
   363  }