go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/configutil/resolve_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package configutil
     9  
    10  import (
    11  	"context"
    12  	"testing"
    13  	"time"
    14  
    15  	. "go.charczuk.com/sdk/assert"
    16  )
    17  
    18  type testConfig struct {
    19  	String   string
    20  	Int      int
    21  	Float64  float64
    22  	Duration time.Duration
    23  }
    24  
    25  func (tc *testConfig) Resolve(ctx context.Context) error {
    26  	return Resolve(ctx,
    27  		Set(&tc.String, Env[string]("STRING_FIELD"), Lazy(&tc.String), Const("StringFieldDefault")),
    28  		Set(&tc.Int, Env[int]("INT_FIELD"), Lazy(&tc.Int), Const(1234)),
    29  		Set(&tc.Float64, Env[float64]("FLOAT64_FIELD"), Lazy(&tc.Float64), Const(567.0)),
    30  		Set(&tc.Duration, Env[time.Duration]("DURATION_FIELD"), Lazy(&tc.Duration), Const(time.Millisecond)),
    31  	)
    32  }
    33  
    34  func Test_Resolve(t *testing.T) {
    35  	var cfg testConfig
    36  	err := (&cfg).Resolve(context.Background())
    37  	ItsNil(t, err)
    38  	ItsEqual(t, "StringFieldDefault", cfg.String)
    39  	ItsEqual(t, 1234, cfg.Int)
    40  	ItsEqual(t, 567.0, cfg.Float64)
    41  	ItsEqual(t, time.Millisecond, cfg.Duration)
    42  }