Files
nomad/helper/pointer/pointer_test.go
Seth Hoenig 7788f09567 cleanup: create pointer.Compare helper function
This PR creates a pointer.Compare helper for comparing equality of
two pointers. Strictly only works with primitive types we know are
safe to derefence and compare using '=='.
2022-08-26 08:55:59 -05:00

68 lines
1.1 KiB
Go

package pointer
import (
"testing"
"time"
"github.com/shoenig/test/must"
)
func Test_Of(t *testing.T) {
s := "hello"
sPtr := Of(s)
must.Eq(t, s, *sPtr)
b := "bye"
sPtr = &b
must.NotEq(t, s, *sPtr)
}
func Test_Copy(t *testing.T) {
orig := Of(1)
dup := Copy(orig)
orig = Of(7)
must.EqOp(t, 7, *orig)
must.EqOp(t, 1, *dup)
}
func Test_Compare(t *testing.T) {
t.Run("int", func(t *testing.T) {
a := 1
b := 2
c := 1
var n *int // nil
must.False(t, Eq(&a, &b))
must.True(t, Eq(&a, &c))
must.False(t, Eq(nil, &a))
must.False(t, Eq(n, &a))
must.True(t, Eq(n, nil))
})
t.Run("string", func(t *testing.T) {
a := "cat"
b := "dog"
c := "cat"
var n *string
must.False(t, Eq(&a, &b))
must.True(t, Eq(&a, &c))
must.False(t, Eq(nil, &a))
must.False(t, Eq(n, &a))
must.True(t, Eq(n, nil))
})
t.Run("duration", func(t *testing.T) {
a := time.Duration(1)
b := time.Duration(2)
c := time.Duration(1)
var n *time.Duration
must.False(t, Eq(&a, &b))
must.True(t, Eq(&a, &c))
must.False(t, Eq(nil, &a))
must.False(t, Eq(n, &a))
must.True(t, Eq(n, nil))
})
}