File: tag_test.go

package info (click to toggle)
golang-github-alecthomas-kong 0.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 620 kB
  • sloc: makefile: 2
file content (151 lines) | stat: -rw-r--r-- 3,502 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package kong_test

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/alecthomas/kong"
)

func TestDefaultValueForOptionalArg(t *testing.T) {
	var cli struct {
		Arg string `kong:"arg,optional,default='👌'"`
	}
	p := mustNew(t, &cli)
	_, err := p.Parse(nil)
	require.NoError(t, err)
	require.Equal(t, "👌", cli.Arg)
}

func TestNoValueInTag(t *testing.T) {
	var cli struct {
		Empty1 string `kong:"default"`
		Empty2 string `kong:"default="`
	}
	p := mustNew(t, &cli)
	_, err := p.Parse(nil)
	require.NoError(t, err)
	require.Equal(t, "", cli.Empty1)
	require.Equal(t, "", cli.Empty2)
}

func TestCommaInQuotes(t *testing.T) {
	var cli struct {
		Numbers string `kong:"default='1,2'"`
	}
	p := mustNew(t, &cli)
	_, err := p.Parse(nil)
	require.NoError(t, err)
	require.Equal(t, "1,2", cli.Numbers)
}

func TestBadString(t *testing.T) {
	var cli struct {
		Numbers string `kong:"default='yay'n"`
	}
	_, err := kong.New(&cli)
	require.Error(t, err)
}

func TestNoQuoteEnd(t *testing.T) {
	var cli struct {
		Numbers string `kong:"default='yay"`
	}
	_, err := kong.New(&cli)
	require.Error(t, err)
}

func TestEscapedQuote(t *testing.T) {
	var cli struct {
		DoYouKnow string `kong:"default='i don\\'t know'"`
	}
	p := mustNew(t, &cli)
	_, err := p.Parse(nil)
	require.NoError(t, err)
	require.Equal(t, "i don't know", cli.DoYouKnow)
}

func TestBareTags(t *testing.T) {
	// nolint: govet
	var cli struct {
		Cmd struct {
			Arg  string `arg`
			Flag string `required default:"👌"`
		} `cmd`
	}

	p := mustNew(t, &cli)
	_, err := p.Parse([]string{"cmd", "arg", "--flag=hi"})
	require.NoError(t, err)
	require.Equal(t, "hi", cli.Cmd.Flag)
	require.Equal(t, "arg", cli.Cmd.Arg)
}

func TestBareTagsWithJsonTag(t *testing.T) {
	// nolint: govet
	var cli struct {
		Cmd struct {
			Arg  string `json:"-" optional arg`
			Flag string `json:"best_flag" default:"\"'👌'\""`
		} `cmd json:"CMD"`
	}

	p := mustNew(t, &cli)
	_, err := p.Parse([]string{"cmd"})
	require.NoError(t, err)
	require.Equal(t, "\"'👌'\"", cli.Cmd.Flag)
	require.Equal(t, "", cli.Cmd.Arg)
}

func TestManySeps(t *testing.T) {
	// nolint: govet
	var cli struct {
		Arg string `arg    optional    default:"hi"`
	}

	p := mustNew(t, &cli)
	_, err := p.Parse([]string{})
	require.NoError(t, err)
	require.Equal(t, "hi", cli.Arg)
}

func TestTagSetOnEmbeddedStruct(t *testing.T) {
	type Embedded struct {
		Key string `help:"A key from ${where}."`
	}
	var cli struct {
		Embedded `set:"where=somewhere"`
	}
	buf := &strings.Builder{}
	p := mustNew(t, &cli, kong.Writers(buf, buf), kong.Exit(func(int) {}))
	_, err := p.Parse([]string{"--help"})
	require.NoError(t, err)
	require.Contains(t, buf.String(), `A key from somewhere.`)
}

func TestTagSetOnCommand(t *testing.T) {
	type Command struct {
		Key string `help:"A key from ${where}."`
	}
	var cli struct {
		Command Command `set:"where=somewhere" cmd:""`
	}
	buf := &strings.Builder{}
	p := mustNew(t, &cli, kong.Writers(buf, buf), kong.Exit(func(int) {}))
	_, err := p.Parse([]string{"command", "--help"})
	require.NoError(t, err)
	require.Contains(t, buf.String(), `A key from somewhere.`)
}

func TestTagSetOnFlag(t *testing.T) {
	var cli struct {
		Flag string `set:"where=somewhere" help:"A key from ${where}."`
	}
	buf := &strings.Builder{}
	p := mustNew(t, &cli, kong.Writers(buf, buf), kong.Exit(func(int) {}))
	_, err := p.Parse([]string{"--help"})
	require.NoError(t, err)
	require.Contains(t, buf.String(), `A key from somewhere.`)
}