github.com/blend/go-sdk@v1.20220411.3/configutil/parse_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - 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 TestParse(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	stringSource := String("")
    22  
    23  	boolValue, err := Parse(stringSource).Bool(context.TODO())
    24  	assert.Nil(err)
    25  	assert.Nil(boolValue)
    26  
    27  	trueValues := []string{"1", "true", "yes", "on"}
    28  	for _, tv := range trueValues {
    29  		stringSource = String(tv)
    30  		boolValue, err = Parse(stringSource).Bool(context.TODO())
    31  		assert.Nil(err)
    32  		assert.NotNil(boolValue)
    33  		assert.True(*boolValue)
    34  	}
    35  
    36  	falseValues := []string{"0", "false", "no", "off"}
    37  	for _, fv := range falseValues {
    38  		stringSource = String(fv)
    39  		boolValue, err = Parse(stringSource).Bool(context.TODO())
    40  		assert.Nil(err)
    41  		assert.NotNil(boolValue)
    42  		assert.False(*boolValue)
    43  	}
    44  
    45  	stringSource = String("not a bool")
    46  	boolValue, err = Parse(stringSource).Bool(context.TODO())
    47  	assert.NotNil(err)
    48  	assert.Nil(boolValue)
    49  
    50  	stringSource = String("")
    51  	intValue, err := Parse(stringSource).Int(context.TODO())
    52  	assert.Nil(err)
    53  	assert.Nil(intValue)
    54  
    55  	stringSource = String("bad value")
    56  	intValue, err = Parse(stringSource).Int(context.TODO())
    57  	assert.NotNil(err)
    58  	assert.Nil(intValue)
    59  
    60  	stringSource = String("1234")
    61  	intValue, err = Parse(stringSource).Int(context.TODO())
    62  	assert.Nil(err)
    63  	assert.NotNil(intValue)
    64  	assert.Equal(1234, *intValue)
    65  
    66  	stringSource = String("")
    67  	floatValue, err := Parse(stringSource).Float64(context.TODO())
    68  	assert.Nil(err)
    69  	assert.Nil(floatValue)
    70  
    71  	stringSource = String("bad value")
    72  	floatValue, err = Parse(stringSource).Float64(context.TODO())
    73  	assert.NotNil(err)
    74  	assert.Nil(floatValue)
    75  
    76  	stringSource = String("1234.34")
    77  	floatValue, err = Parse(stringSource).Float64(context.TODO())
    78  	assert.Nil(err)
    79  	assert.NotNil(floatValue)
    80  	assert.Equal(1234.34, *floatValue)
    81  
    82  	stringSource = String("")
    83  	durationValue, err := Parse(stringSource).Duration(context.TODO())
    84  	assert.Nil(err)
    85  	assert.Nil(durationValue)
    86  
    87  	stringSource = String("bad value")
    88  	durationValue, err = Parse(stringSource).Duration(context.TODO())
    89  	assert.NotNil(err)
    90  	assert.Nil(durationValue)
    91  
    92  	stringSource = String("10s")
    93  	durationValue, err = Parse(stringSource).Duration(context.TODO())
    94  	assert.Nil(err)
    95  	assert.NotNil(durationValue)
    96  	assert.Equal(10*time.Second, *durationValue)
    97  }