forked from rsc/getopt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetopt_test.go
162 lines (151 loc) · 3.19 KB
/
getopt_test.go
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
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2017 The Go Authors. All rights reserved.
// Copyright 2024 Peter Aronoff. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package getopt
import (
"bytes"
"errors"
"flag"
"fmt"
"strings"
"testing"
)
type testFlagSet struct {
A *bool
B *bool
C *bool
D *bool
I *int
Long *int
S *string
flag *FlagSet
Args []string
buf bytes.Buffer
}
func (tf *testFlagSet) str() string {
out := ""
if *tf.A {
out += " -a"
}
if *tf.B {
out += " -b"
}
if *tf.C {
out += " -c"
}
if *tf.D {
out += " -d"
}
if *tf.I != 0 {
out += fmt.Sprintf(" -i %d", *tf.I)
}
if *tf.Long != 0 {
out += fmt.Sprintf(" --long %d", *tf.Long)
}
if *tf.S != "" {
out += " -s " + *tf.S
}
if len(tf.Args) > 0 {
out += " " + strings.Join(tf.Args, " ")
}
if out == "" {
return out
}
return out[1:]
}
func newTestFlagSet() *testFlagSet {
tf := &testFlagSet{flag: NewFlagSet("x", flag.ContinueOnError)}
f := tf.flag
f.SetOutput(&tf.buf)
tf.A = f.Bool("a", false, "desc of a")
tf.B = f.Bool("b", false, "desc of b")
tf.C = f.Bool("c", false, "desc of c")
tf.D = f.Bool("d", false, "desc of d")
tf.Long = f.Int("long", 0, "long only")
f.Alias("a", "aah")
f.Aliases("b", "beeta", "c", "charlie")
tf.I = f.Int("i", 0, "i")
f.Alias("i", "india")
tf.S = f.String("sierra", "", "string")
f.Alias("s", "sierra")
return tf
}
func TestParse(t *testing.T) {
tests := []struct {
cmd string
out string
}{
{"-i 1", "-i 1"},
{"--india 1", "-i 1"},
{"--india=1", "-i 1"},
{"--i=1", "-i 1"},
{"-abc", "-a -b -c"},
{"-sfoo", "-s foo"},
{"-s foo", "-s foo"},
{"--s=foo", "-s foo"},
{"-s=foo", "-s =foo"},
{"--s=", ``},
{"-sfooi1 -i2", "-i 2 -s fooi1"},
{"-absfoo", "-a -b -s foo"},
{"-i1 -- arg", "-i 1 arg"},
{"-i1 - arg", "-i 1 - arg"},
{"-i1 arg", "-i 1 arg"},
{"--aah --charlie --beeta --sierra=123", "-a -b -c -s 123"},
{"-i1 --long=2", "-i 1 --long 2"},
}
for _, tt := range tests {
tf := newTestFlagSet()
err := tf.flag.Parse(strings.Fields(tt.cmd))
var out string
if err != nil {
t.Errorf("%s:\nhave %v\nwant <nil>\n", tt.cmd, err)
}
tf.Args = tf.flag.Args()
out = tf.str()
if out != tt.out {
t.Errorf("%s:\nhave %s\nwant %s", tt.cmd, out, tt.out)
}
}
}
func TestParseErr(t *testing.T) {
tests := []struct {
err error
cmd string
}{
{cmd: "-i=1", err: errors.New("parse error")},
{cmd: "--abc", err: errors.New("parse error")},
{cmd: "-s", err: errors.New("parse error")},
{cmd: "--s", err: errors.New("parse error")},
{cmd: "-i1 --- arg", err: errors.New("parse error")},
}
for _, tt := range tests {
tf := newTestFlagSet()
err := tf.flag.Parse(strings.Fields(tt.cmd))
if err == nil {
t.Errorf("%s:\nhave <nil>\nwant error\n", tt.cmd)
}
}
}
func TestHelpText(t *testing.T) {
wantHelpText := ` -a, --aah
desc of a
-b, --beeta
desc of b
-c, --charlie
desc of c
-d desc of d
-i, --india int
i
--long int
long only
-s, --sierra string
string
`
tf := newTestFlagSet()
tf.flag.PrintDefaults()
out := tf.buf.String()
if out != wantHelpText {
t.Errorf("have<\n%s>\nwant<\n%s>", out, wantHelpText)
}
}