corso/src/internal/common/ptr/pointer_test.go
Danny eda4699d1a
GC: Use of Ptr extension (#2477)
## Description
Use of Pointer package in /connector/exchange

<!-- Insert PR description-->

## Does this PR need a docs update or release note?
Wanted to extend the package to generics, but it is incomplete

- [x]  No 

## Type of change

<!--- Please check the type of change your PR introduces: --->
- [x] 🌻 Feature


## Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* related  #2474<issue>

## Test Plan

- [x]  Unit test
2023-02-10 21:10:22 +00:00

100 lines
1.8 KiB
Go

package ptr_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/alcionai/corso/src/internal/common/ptr"
)
type PointerSuite struct {
suite.Suite
}
func TestPointerSuite(t *testing.T) {
suite.Run(t, new(PointerSuite))
}
// TestVal checks to ptr derefencing for the
// following types:
// - *string
// - *bool
// - *time.Time
func (suite *PointerSuite) TestVal() {
var (
t = suite.T()
created *time.Time
testString *string
testBool *bool
testInt *int
testInt32 *int32
testInt64 *int64
)
// String Checks
subject := ptr.Val(testString)
assert.Empty(t, subject)
hello := "Hello World"
testString = &hello
subject = ptr.Val(testString)
t.Logf("Received: %s", subject)
assert.NotEmpty(t, subject)
// Time Checks
myTime := ptr.Val(created)
assert.Empty(t, myTime)
assert.NotNil(t, myTime)
now := time.Now()
created = &now
myTime = ptr.Val(created)
assert.NotEmpty(t, myTime)
// Bool Checks
truth := true
myBool := ptr.Val(testBool)
assert.NotNil(t, myBool)
assert.False(t, myBool)
testBool = &truth
myBool = ptr.Val(testBool)
assert.NotNil(t, myBool)
assert.True(t, myBool)
// Int checks
myInt := ptr.Val(testInt)
myInt32 := ptr.Val(testInt32)
myInt64 := ptr.Val(testInt64)
assert.NotNil(t, myInt)
assert.NotNil(t, myInt32)
assert.NotNil(t, myInt64)
assert.Empty(t, myInt)
assert.Empty(t, myInt32)
assert.Empty(t, myInt64)
num := 4071
num32 := int32(num * 32)
num64 := int64(num * 2048)
testInt = &num
testInt32 = &num32
testInt64 = &num64
myInt = ptr.Val(testInt)
myInt32 = ptr.Val(testInt32)
myInt64 = ptr.Val(testInt64)
assert.NotNil(t, myInt)
assert.NotNil(t, myInt32)
assert.NotNil(t, myInt64)
assert.NotEmpty(t, myInt)
assert.NotEmpty(t, myInt32)
assert.NotEmpty(t, myInt64)
}