github.com/blend/go-sdk@v1.20240719.1/ref/ref_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - 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 ref
     9  
    10  import (
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  )
    16  
    17  func TestRef(t *testing.T) {
    18  	assert := assert.New(t)
    19  
    20  	assert.NotNil(Ref("foo"))
    21  	assert.NotEmpty(Refs("foo", "bar"))
    22  	assert.NotNil(Ref(1))
    23  	assert.NotEmpty(Refs(1, 2))
    24  	assert.NotNil(Ref(time.Now()))
    25  	assert.NotEmpty(Refs(time.Time{}, time.Time{}))
    26  
    27  	assert.NotNil(String("foo"))
    28  	assert.NotEmpty(Strings("foo", "bar"))
    29  	assert.NotNil(Bool(true))
    30  	assert.NotNil(Byte('b'))
    31  	assert.NotNil(Rune('b'))
    32  	assert.NotNil(Uint8(0))
    33  	assert.NotNil(Uint16(0))
    34  	assert.NotNil(Uint32(0))
    35  	assert.NotNil(Uint64(0))
    36  	assert.NotNil(Int8(0))
    37  	assert.NotNil(Int16(0))
    38  	assert.NotNil(Int32(0))
    39  	assert.NotNil(Int64(0))
    40  	assert.NotNil(Int(0))
    41  	assert.NotNil(Float32(0))
    42  	assert.NotNil(Float64(0))
    43  	assert.NotNil(Time(time.Time{}))
    44  	assert.NotNil(Duration(0))
    45  }
    46  
    47  func TestDeref(t *testing.T) {
    48  	assert := assert.New(t)
    49  
    50  	// strings
    51  	populatedString := "Hello, world"
    52  	emptyString := ""
    53  	var nilStringPtr *string
    54  	assert.Equal(populatedString, Deref(&populatedString))
    55  	assert.Equal(emptyString, Deref(&emptyString))
    56  	assert.Equal(emptyString, Deref(nilStringPtr))
    57  
    58  	// ints
    59  	populatedInt := 10
    60  	zeroInt := 0
    61  	var nilIntPtr *int
    62  	assert.Equal(populatedInt, Deref(&populatedInt))
    63  	assert.Equal(zeroInt, Deref(&zeroInt))
    64  	assert.Equal(zeroInt, Deref(nilIntPtr))
    65  
    66  	// times
    67  	populatedTime := time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
    68  	zeroTime := time.Time{}
    69  	var nilTimePtr *time.Time
    70  	assert.Equal(populatedTime, Deref(&populatedTime))
    71  	assert.Equal(zeroTime, Deref(&zeroTime))
    72  	assert.Equal(zeroTime, Deref(nilTimePtr))
    73  
    74  	// slice of pointers
    75  	assert.Equal(Derefs(&populatedInt, nil, &zeroInt), []int{populatedInt, zeroInt, zeroInt})
    76  }