Debian Codegraph @

spec_test.go

raw symbols SCIP
1 package opt_test
2
3 import (
4 "fmt"
5 "strconv"
6 "testing"
7
8 "git.sr.ht/~rjarry/go-opt/v2"
9 "github.com/stretchr/testify/assert"
10 )
11
12 type OptionStruct struct {
13 Jobs int `opt:"-j,--jobs" required:"true"`
14 Delay float64 `opt:"--delay" default:"0.5"`
15 Zero bool `opt:"-z" aliases:"baz"`
16 Backoff bool `opt:"-B,--backoff"`
17 Name string `opt:"name" aliases:"bar" action:"ParseName"`
18 Offsets []int `opt:"..." required:"false" action:"ParseOffset"`
19 }
20
21 func (o *OptionStruct) ParseName(arg string) error {
22 if arg == "invalid" {
23 return fmt.Errorf("%q invalid value", arg)
24 }
25 o.Name = arg
26 return nil
27 }
28
29 func (o *OptionStruct) ParseOffset(arg string) error {
30 i, err := strconv.Atoi(arg)
31 if err != nil {
32 return err
33 }
34 o.Offsets = append(o.Offsets, i)
35 return nil
36 }
37
38 func TestArgsToStructErrors(t *testing.T) {
39 vectors := []struct {
40 cmdline string
41 err string
42 }{
43 {"foo", "-j is required"},
44 {"foo -j", "-j takes a value"},
45 {"foo --delay -B", "--delay takes a value"},
46 {"bar -j4", "<name> is required"},
47 {"foo -j f", `strconv.ParseInt: parsing "f": invalid syntax.`},
48 {"foo --delay=m", `strconv.ParseFloat: parsing "m": invalid syntax.`},
49 {"foo -u8 hop", `-u unknown flag`},
50 {"foo -z", `-z unknown flag`},
51 {"bar -j4 invalid", `invalid value`},
52 {"bar -j4 name name", `strconv.Atoi: parsing "name": invalid syntax.`},
53 }
54
55 for _, v := range vectors {
56 t.Run(v.cmdline, func(t *testing.T) {
57 err := opt.CmdlineToStruct(v.cmdline, new(OptionStruct))
58 assert.ErrorContains(t, err, v.err)
59 })
60 }
61
62 spec := opt.NewCmdSpec("bar", new(OptionStruct))
63 assert.Equal(t, spec.Usage(), "bar -j <jobs> [--delay <delay>] [-B] <name> [<offsets>...]")
64 }
65
66 func TestArgsToStruct(t *testing.T) {
67 vectors := []struct {
68 cmdline string
69 expected OptionStruct
70 }{
71 {
72 cmdline: `bar -j4 'f o o \(°</ f o o'`,
73 expected: OptionStruct{
74 Jobs: 4,
75 Delay: 0.5,
76 Name: `f o o \(°</ f o o`,
77 },
78 },
79 {
80 cmdline: "foo --delay 0.1 -Bj 8",
81 expected: OptionStruct{
82 Jobs: 8,
83 Delay: 0.1,
84 Backoff: true,
85 },
86 },
87 {
88 cmdline: `baz -Bz --delay=0.1 -j8`,
89 expected: OptionStruct{
90 Jobs: 8,
91 Delay: 0.1,
92 Zero: true,
93 Backoff: true,
94 },
95 },
96 {
97 cmdline: `bar 'n a m e' -j7 --backoff`,
98 expected: OptionStruct{
99 Jobs: 7,
100 Delay: 0.5,
101 Backoff: true,
102 Name: "n a m e",
103 },
104 },
105 {
106 cmdline: `bar -j3 -- -j7`,
107 expected: OptionStruct{
108 Jobs: 3,
109 Delay: 0.5,
110 Name: "-j7",
111 },
112 },
113 {
114 cmdline: `bar -j3 -- -j1 1 42`,
115 expected: OptionStruct{
116 Jobs: 3,
117 Delay: 0.5,
118 Name: "-j1",
119 Offsets: []int{1, 42},
120 },
121 },
122 }
123
124 for _, v := range vectors {
125 t.Run(v.cmdline, func(t *testing.T) {
126 var s OptionStruct
127 assert.Nil(t, opt.CmdlineToStruct(v.cmdline, &s))
128 assert.Equal(t, v.expected, s)
129 })
130 }
131 }