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

     1  package kong_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/alecthomas/assert/v2"
     7  )
     8  
     9  func TestModelApplicationCommands(t *testing.T) {
    10  	var cli struct {
    11  		One struct {
    12  			Two struct {
    13  			} `kong:"cmd"`
    14  			Three struct {
    15  				Four struct {
    16  					Four string `kong:"arg"`
    17  				} `kong:"arg"`
    18  			} `kong:"cmd"`
    19  		} `kong:"cmd"`
    20  	}
    21  	p := mustNew(t, &cli)
    22  	actual := []string{}
    23  	for _, cmd := range p.Model.Leaves(false) {
    24  		actual = append(actual, cmd.Path())
    25  	}
    26  	assert.Equal(t, []string{"one two", "one three <four>"}, actual)
    27  }
    28  
    29  func TestFlagString(t *testing.T) {
    30  	var cli struct {
    31  		String                  string
    32  		DefaultInt              int    `default:"42"`
    33  		DefaultStr              string `default:"hello"`
    34  		Placeholder             string `placeholder:"world"`
    35  		DefaultPlaceholder      string `default:"hello" placeholder:"world"`
    36  		SliceSep                []string
    37  		SliceNoSep              []string `sep:"none"`
    38  		SliceDefault            []string `default:"hello"`
    39  		SlicePlaceholder        []string `placeholder:"world"`
    40  		SliceDefaultPlaceholder []string `default:"hello" placeholder:"world"`
    41  		MapSep                  map[string]string
    42  		MapNoSep                map[string]string `mapsep:"none"`
    43  		MapDefault              map[string]string `mapsep:"none" default:"hello"`
    44  		MapPlaceholder          map[string]string `mapsep:"none" placeholder:"world"`
    45  		Counter                 int               `type:"counter"`
    46  	}
    47  	tests := map[string]string{
    48  		"help":                      "-h, --help",
    49  		"string":                    "--string=STRING",
    50  		"default-int":               "--default-int=42",
    51  		"default-str":               `--default-str="hello"`,
    52  		"placeholder":               "--placeholder=world",
    53  		"default-placeholder":       "--default-placeholder=world",
    54  		"slice-sep":                 "--slice-sep=SLICE-SEP,...",
    55  		"slice-no-sep":              "--slice-no-sep=SLICE-NO-SEP",
    56  		"slice-default":             "--slice-default=hello,...",
    57  		"slice-placeholder":         "--slice-placeholder=world,...",
    58  		"slice-default-placeholder": "--slice-default-placeholder=world,...",
    59  		"map-sep":                   "--map-sep=KEY=VALUE;...",
    60  		"map-no-sep":                "--map-no-sep=KEY=VALUE",
    61  		"map-default":               "--map-default=hello",
    62  		"map-placeholder":           "--map-placeholder=world",
    63  		"counter":                   "--counter",
    64  	}
    65  
    66  	p := mustNew(t, &cli)
    67  	for _, flag := range p.Model.Flags {
    68  		want, ok := tests[flag.Name]
    69  		assert.True(t, ok, "unknown flag name: %s", flag.Name)
    70  		assert.Equal(t, want, flag.String())
    71  	}
    72  }