54 lines
1.0 KiB
Go
54 lines
1.0 KiB
Go
package ptr
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestDerefOrZero(t *testing.T) {
|
|
// Test with nil pointer
|
|
var nilPtr *int
|
|
result := DerefOrZero(nilPtr)
|
|
assert.Equal(t, 0, result)
|
|
|
|
// Test with non-nil pointer
|
|
value := 42
|
|
ptr := &value
|
|
result = DerefOrZero(ptr)
|
|
assert.Equal(t, 42, result)
|
|
|
|
// Test with string
|
|
var nilString *string
|
|
resultStr := DerefOrZero(nilString)
|
|
assert.Equal(t, "", resultStr)
|
|
|
|
// Test with non-nil string
|
|
strValue := "hello"
|
|
strPtr := &strValue
|
|
resultStr = DerefOrZero(strPtr)
|
|
assert.Equal(t, "hello", resultStr)
|
|
}
|
|
|
|
func TestOf(t *testing.T) {
|
|
// Test with int
|
|
value := 42
|
|
ptr := Of(value)
|
|
assert.NotNil(t, ptr)
|
|
assert.Equal(t, 42, *ptr)
|
|
|
|
// Test with string - using explicit typing
|
|
var strPtr *string
|
|
strValue := "hello"
|
|
strPtr = Of(strValue)
|
|
assert.NotNil(t, strPtr)
|
|
assert.Equal(t, "hello", *strPtr)
|
|
|
|
// Test with bool - using explicit typing
|
|
var boolPtr *bool
|
|
boolValue := true
|
|
boolPtr = Of(boolValue)
|
|
assert.NotNil(t, boolPtr)
|
|
assert.Equal(t, true, *boolPtr)
|
|
}
|