github.com/blend/go-sdk@v1.20240719.1/configutil/set_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package configutil
     9  
    10  import (
    11  	"context"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  func TestSetString(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	empty := String("")
    22  	hasValue := String("has value")
    23  	hasValue2 := String("has another value")
    24  
    25  	var value string
    26  	assert.Nil(SetString(&value, empty, hasValue, hasValue2)(context.TODO()))
    27  	assert.Equal("has value", value)
    28  }
    29  
    30  func TestSetStringPtr(t *testing.T) {
    31  	assert := assert.New(t)
    32  
    33  	empty := String("")
    34  	hasValue := String("has value")
    35  	hasValue2 := String("has another value")
    36  
    37  	var value *string
    38  	assert.Nil(SetStringPtr(&value, empty, hasValue, hasValue2)(context.TODO()))
    39  	assert.Equal("has value", *value)
    40  }
    41  
    42  func TestSetStrings(t *testing.T) {
    43  	assert := assert.New(t)
    44  
    45  	empty := Strings(nil)
    46  	hasValue := Strings([]string{"has value"})
    47  	hasValue2 := Strings([]string{"has another value"})
    48  
    49  	var value []string
    50  	assert.Nil(SetStrings(&value, empty, hasValue, hasValue2)(context.TODO()))
    51  	assert.Equal([]string{"has value"}, value)
    52  }
    53  
    54  func TestSetBool(t *testing.T) {
    55  	assert := assert.New(t)
    56  
    57  	empty := Bool(nil)
    58  	tv := true
    59  	hasValue := Bool(&tv)
    60  	fv := false
    61  	hasValue2 := Bool(&fv)
    62  
    63  	var value bool
    64  	assert.Nil(SetBool(&value, empty, hasValue, hasValue2)(context.TODO()))
    65  	assert.Equal(true, value)
    66  }
    67  
    68  func TestSetBoolPtr(t *testing.T) {
    69  	assert := assert.New(t)
    70  
    71  	empty := Bool(nil)
    72  	tv := true
    73  	hasValue := Bool(&tv)
    74  	fv := false
    75  	hasValue2 := Bool(&fv)
    76  
    77  	var value *bool
    78  	assert.Nil(SetBoolPtr(&value, empty, hasValue, hasValue2)(context.TODO()))
    79  	assert.Equal(true, *value)
    80  }
    81  
    82  func TestSetInt(t *testing.T) {
    83  	assert := assert.New(t)
    84  
    85  	empty := Parse(String(""))
    86  	hasValue := Int(1)
    87  	hasValue2 := Int(2)
    88  
    89  	var value int
    90  	assert.Nil(SetInt(&value, empty, hasValue, hasValue2)(context.TODO()))
    91  	assert.Equal(1, value)
    92  
    93  	errors := Parse(String("bad"))
    94  	assert.NotNil(SetInt(&value, errors))
    95  }
    96  
    97  func TestSetIntPtr(t *testing.T) {
    98  	assert := assert.New(t)
    99  
   100  	empty := Parse(String(""))
   101  	hasValue := Int(1)
   102  	hasValue2 := Int(2)
   103  
   104  	var value *int
   105  	assert.Nil(SetIntPtr(&value, empty, hasValue, hasValue2)(context.TODO()))
   106  	assert.Equal(1, *value)
   107  
   108  	errors := Parse(String("bad"))
   109  	assert.NotNil(SetIntPtr(&value, errors))
   110  }
   111  
   112  func TestSetFloat64(t *testing.T) {
   113  	assert := assert.New(t)
   114  
   115  	empty := Parse(String(""))
   116  	hasValue := Float64(1)
   117  	hasValue2 := Float64(2)
   118  
   119  	var value float64
   120  	assert.Nil(SetFloat64(&value, empty, hasValue, hasValue2)(context.TODO()))
   121  	assert.Equal(1, value)
   122  
   123  	errors := Parse(String("bad"))
   124  	assert.NotNil(SetFloat64(&value, errors))
   125  }
   126  
   127  func TestSetFloat64Ptr(t *testing.T) {
   128  	assert := assert.New(t)
   129  
   130  	empty := Parse(String(""))
   131  	hasValue := Float64(1)
   132  	hasValue2 := Float64(2)
   133  
   134  	var value *float64
   135  	assert.Nil(SetFloat64Ptr(&value, empty, hasValue, hasValue2)(context.TODO()))
   136  	assert.Equal(1, *value)
   137  
   138  	errors := Parse(String("bad"))
   139  	assert.NotNil(SetFloat64Ptr(&value, errors))
   140  }
   141  
   142  func TestSetDuration(t *testing.T) {
   143  	assert := assert.New(t)
   144  
   145  	empty := Parse(String(""))
   146  	hasValue := Duration(time.Second)
   147  	hasValue2 := Duration(2 * time.Second)
   148  
   149  	var value time.Duration
   150  	assert.Nil(SetDuration(&value, empty, hasValue, hasValue2)(context.TODO()))
   151  	assert.Equal(time.Second, value)
   152  
   153  	errors := Parse(String("bad"))
   154  	assert.NotNil(SetDuration(&value, errors))
   155  }
   156  
   157  func TestSetDurationPtr(t *testing.T) {
   158  	assert := assert.New(t)
   159  
   160  	empty := Parse(String(""))
   161  	hasValue := Duration(time.Second)
   162  	hasValue2 := Duration(2 * time.Second)
   163  
   164  	var value *time.Duration
   165  	assert.Nil(SetDurationPtr(&value, empty, hasValue, hasValue2)(context.TODO()))
   166  	assert.Equal(time.Second, *value)
   167  
   168  	errors := Parse(String("bad"))
   169  	assert.NotNil(SetDurationPtr(&value, errors))
   170  }