github.com/maresnic/mr-kong@v1.0.0/defaults_test.go (about)

     1  package kong
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/alecthomas/assert/v2"
     8  )
     9  
    10  func TestApplyDefaults(t *testing.T) {
    11  	type CLI struct {
    12  		Str      string        `default:"str"`
    13  		Duration time.Duration `default:"30s"`
    14  	}
    15  	tests := []struct {
    16  		name     string
    17  		target   CLI
    18  		expected CLI
    19  	}{
    20  		{name: "DefaultsWhenNotSet",
    21  			expected: CLI{Str: "str", Duration: time.Second * 30}},
    22  		{name: "PartiallySetDefaults",
    23  			target:   CLI{Duration: time.Second},
    24  			expected: CLI{Str: "str", Duration: time.Second}},
    25  	}
    26  	for _, tt := range tests {
    27  		tt := tt
    28  		t.Run(tt.name, func(t *testing.T) {
    29  			err := ApplyDefaults(&tt.target)
    30  			assert.NoError(t, err)
    31  			assert.Equal(t, tt.expected, tt.target)
    32  		})
    33  	}
    34  }