MCPcopy
hub / github.com/urfave/cli / TestIntFlag

Function TestIntFlag

flag_int_test.go:12–63  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

10)
11
12func TestIntFlag(t *testing.T) {
13 tests := []struct {
14 name string
15 flag Flag
16 arguments []string
17 expectedValue int
18 expectErr bool
19 }{
20 {
21 name: "valid",
22 flag: &IntFlag{
23 Name: "number",
24 Aliases: []string{"n"},
25 },
26 arguments: []string{"--number", "-234567"},
27 expectedValue: -234567,
28 },
29 {
30 name: "invalid",
31 flag: &IntFlag{
32 Name: "number",
33 },
34 arguments: []string{"--number", "gopher"},
35 expectErr: true,
36 },
37 }
38
39 for _, tt := range tests {
40 t.Run(tt.name, func(t *testing.T) {
41 cmd := &Command{
42 Name: "mock",
43 Flags: []Flag{tt.flag},
44 Writer: io.Discard,
45 ErrWriter: io.Discard,
46 }
47
48 err := cmd.Run(buildTestContext(t), append([]string{"mock"}, tt.arguments...))
49
50 if tt.expectErr {
51 require.Error(t, err)
52
53 return
54 }
55
56 require.NoError(t, err)
57
58 for _, name := range tt.flag.Names() {
59 assert.Equal(t, tt.expectedValue, cmd.Int(name))
60 }
61 })
62 }
63}
64
65func TestInt8Flag(t *testing.T) {
66 tests := []struct {

Callers

nothing calls this directly

Calls 5

RunMethod · 0.95
IntMethod · 0.95
buildTestContextFunction · 0.85
NamesMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected