github.com/prebid/prebid-server/v2@v2.18.0/util/ptrutil/ptrutil_test.go (about)

     1  package ptrutil
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestValueOrDefault(t *testing.T) {
    10  	t.Run("int-nil", func(t *testing.T) {
    11  		var v *int = nil
    12  		r := ValueOrDefault(v)
    13  		assert.Equal(t, 0, r)
    14  	})
    15  
    16  	t.Run("int-0", func(t *testing.T) {
    17  		var v *int = ToPtr[int](0)
    18  		r := ValueOrDefault(v)
    19  		assert.Equal(t, 0, r)
    20  	})
    21  
    22  	t.Run("int-42", func(t *testing.T) {
    23  		var v *int = ToPtr[int](42)
    24  		r := ValueOrDefault(v)
    25  		assert.Equal(t, 42, r)
    26  	})
    27  
    28  	t.Run("string-nil", func(t *testing.T) {
    29  		var v *string = nil
    30  		r := ValueOrDefault(v)
    31  		assert.Equal(t, "", r)
    32  	})
    33  
    34  	t.Run("string-empty", func(t *testing.T) {
    35  		var v *string = ToPtr[string]("")
    36  		r := ValueOrDefault(v)
    37  		assert.Equal(t, "", r)
    38  	})
    39  
    40  	t.Run("string-something", func(t *testing.T) {
    41  		var v *string = ToPtr[string]("something")
    42  		r := ValueOrDefault(v)
    43  		assert.Equal(t, "something", r)
    44  	})
    45  }