github.com/hernad/nomad@v1.6.112/helper/pointer/pointer_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package pointer
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/hernad/nomad/ci"
    11  	"github.com/shoenig/test/must"
    12  )
    13  
    14  func Test_Of(t *testing.T) {
    15  	ci.Parallel(t)
    16  
    17  	s := "hello"
    18  	sPtr := Of(s)
    19  
    20  	must.Eq(t, s, *sPtr)
    21  
    22  	b := "bye"
    23  	sPtr = &b
    24  	must.NotEq(t, s, *sPtr)
    25  }
    26  
    27  func Test_Copy(t *testing.T) {
    28  	ci.Parallel(t)
    29  
    30  	orig := Of(1)
    31  	dup := Copy(orig)
    32  	orig = Of(7)
    33  	must.EqOp(t, 7, *orig)
    34  	must.EqOp(t, 1, *dup)
    35  }
    36  
    37  func Test_Compare(t *testing.T) {
    38  	ci.Parallel(t)
    39  
    40  	t.Run("int", func(t *testing.T) {
    41  		a := 1
    42  		b := 2
    43  		c := 1
    44  		var n *int // nil
    45  		must.False(t, Eq(&a, &b))
    46  		must.True(t, Eq(&a, &c))
    47  		must.False(t, Eq(nil, &a))
    48  		must.False(t, Eq(n, &a))
    49  		must.True(t, Eq(n, nil))
    50  	})
    51  
    52  	t.Run("string", func(t *testing.T) {
    53  		a := "cat"
    54  		b := "dog"
    55  		c := "cat"
    56  		var n *string
    57  
    58  		must.False(t, Eq(&a, &b))
    59  		must.True(t, Eq(&a, &c))
    60  		must.False(t, Eq(nil, &a))
    61  		must.False(t, Eq(n, &a))
    62  		must.True(t, Eq(n, nil))
    63  	})
    64  
    65  	t.Run("duration", func(t *testing.T) {
    66  		a := time.Duration(1)
    67  		b := time.Duration(2)
    68  		c := time.Duration(1)
    69  		var n *time.Duration
    70  
    71  		must.False(t, Eq(&a, &b))
    72  		must.True(t, Eq(&a, &c))
    73  		must.False(t, Eq(nil, &a))
    74  		must.False(t, Eq(n, &a))
    75  		must.True(t, Eq(n, nil))
    76  	})
    77  }
    78  
    79  func Test_Merge(t *testing.T) {
    80  	ci.Parallel(t)
    81  	
    82  	a := 1
    83  	b := 2
    84  
    85  	ptrA := &a
    86  	ptrB := &b
    87  
    88  	t.Run("exists", func(t *testing.T) {
    89  		result := Merge(ptrA, ptrB)
    90  		must.Eq(t, 2, *result)
    91  	})
    92  
    93  	t.Run("nil", func(t *testing.T) {
    94  		result := Merge(ptrA, nil)
    95  		must.Eq(t, 1, *result)
    96  	})
    97  }