github.com/blend/go-sdk@v1.20220411.3/validate/int_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  )
    15  
    16  func TestIntMin(t *testing.T) {
    17  	assert := assert.New(t)
    18  
    19  	var verr error
    20  	val := 10
    21  	verr = Int(&val).Min(1)()
    22  	assert.Nil(verr)
    23  
    24  	val = 10
    25  	verr = Int(&val).Min(10)()
    26  	assert.Nil(verr)
    27  
    28  	verr = Int(nil).Min(10)()
    29  	assert.NotNil(verr)
    30  	assert.Nil(ErrValue(verr))
    31  	assert.Equal(ErrIntMin, ErrCause(verr))
    32  
    33  	val = 1
    34  	verr = Int(&val).Min(10)()
    35  	assert.NotNil(verr)
    36  	assert.NotNil(ErrValue(verr))
    37  	assert.Equal(ErrIntMin, ErrCause(verr))
    38  
    39  	val = 10
    40  	verr = Int(&val).Min(10)()
    41  	assert.Nil(verr)
    42  }
    43  
    44  func TestIntMax(t *testing.T) {
    45  	assert := assert.New(t)
    46  
    47  	var verr error
    48  	val := 1
    49  	verr = Int(&val).Max(10)()
    50  	assert.Nil(verr)
    51  
    52  	verr = Int(nil).Max(10)()
    53  	assert.Nil(verr)
    54  
    55  	val = 10
    56  	verr = Int(&val).Max(10)()
    57  	assert.Nil(verr)
    58  
    59  	val = 11
    60  	verr = Int(&val).Max(10)()
    61  	assert.NotNil(verr)
    62  	assert.NotNil(ErrValue(verr))
    63  	assert.Equal(ErrIntMax, ErrCause(verr))
    64  }
    65  
    66  func TestIntBetween(t *testing.T) {
    67  	assert := assert.New(t)
    68  
    69  	var verr error
    70  	val := 5
    71  	verr = Int(&val).Between(1, 10)()
    72  	assert.Nil(verr)
    73  
    74  	verr = Int(nil).Between(5, 10)()
    75  	assert.NotNil(verr)
    76  	assert.Nil(ErrValue(verr))
    77  	assert.Equal(ErrIntMin, ErrCause(verr))
    78  
    79  	val = 1
    80  	verr = Int(&val).Between(5, 10)()
    81  	assert.NotNil(verr)
    82  	assert.Equal(1, ErrValue(verr))
    83  	assert.Equal(ErrIntMin, ErrCause(verr))
    84  
    85  	val = 5
    86  	verr = Int(&val).Between(5, 10)()
    87  	assert.Nil(verr)
    88  
    89  	val = 10
    90  	verr = Int(&val).Between(5, 10)()
    91  	assert.Nil(verr)
    92  
    93  	val = 11
    94  	verr = Int(&val).Between(5, 10)()
    95  	assert.NotNil(verr)
    96  	assert.Equal(11, ErrValue(verr))
    97  	assert.Equal(ErrIntMax, ErrCause(verr))
    98  }
    99  
   100  func TestIntPositive(t *testing.T) {
   101  	assert := assert.New(t)
   102  
   103  	var verr error
   104  	val := 5
   105  	verr = Int(&val).Positive()()
   106  	assert.Nil(verr)
   107  
   108  	verr = Int(nil).Positive()()
   109  	assert.NotNil(verr)
   110  	assert.Nil(ErrValue(verr))
   111  	assert.Equal(ErrIntPositive, ErrCause(verr))
   112  
   113  	val = -5
   114  	verr = Int(&val).Positive()()
   115  	assert.NotNil(verr)
   116  	assert.NotNil(ErrValue(verr))
   117  	assert.Equal(ErrIntPositive, ErrCause(verr))
   118  }
   119  
   120  func TestIntNegative(t *testing.T) {
   121  	assert := assert.New(t)
   122  
   123  	var verr error
   124  	val := -5
   125  	verr = Int(&val).Negative()()
   126  	assert.Nil(verr)
   127  
   128  	verr = Int(nil).Negative()()
   129  	assert.NotNil(verr)
   130  	assert.Nil(ErrValue(verr))
   131  	assert.Equal(ErrIntNegative, ErrCause(verr))
   132  
   133  	val = 5
   134  	verr = Int(&val).Negative()()
   135  	assert.NotNil(verr)
   136  	assert.NotNil(ErrValue(verr))
   137  	assert.Equal(ErrIntNegative, ErrCause(verr))
   138  }
   139  
   140  func TestIntZero(t *testing.T) {
   141  	assert := assert.New(t)
   142  
   143  	var verr error
   144  	val := 0
   145  	verr = Int(&val).Zero()()
   146  	assert.Nil(verr)
   147  
   148  	verr = Int(nil).Zero()()
   149  	assert.NotNil(verr)
   150  	assert.Nil(ErrValue(verr))
   151  	assert.Equal(ErrIntZero, ErrCause(verr))
   152  
   153  	val = 5
   154  	verr = Int(&val).Zero()()
   155  	assert.NotNil(verr)
   156  	assert.NotNil(ErrValue(verr))
   157  	assert.Equal(ErrIntZero, ErrCause(verr))
   158  }
   159  
   160  func TestIntNotZero(t *testing.T) {
   161  	assert := assert.New(t)
   162  
   163  	var verr error
   164  	val := 5
   165  	verr = Int(&val).NotZero()()
   166  	assert.Nil(verr)
   167  
   168  	verr = Int(nil).NotZero()()
   169  	assert.NotNil(verr)
   170  	assert.Nil(ErrValue(verr))
   171  	assert.Equal(ErrIntNotZero, ErrCause(verr))
   172  
   173  	val = 0
   174  	verr = Int(&val).NotZero()()
   175  	assert.NotNil(verr)
   176  	assert.NotNil(ErrValue(verr))
   177  	assert.Equal(ErrIntNotZero, ErrCause(verr))
   178  }