forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
printusage_test.go
84 lines (75 loc) · 3.4 KB
/
printusage_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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag_test
import (
"bytes"
"io"
"testing"
"github.com/zulucmd/zflag/v2"
)
const expectedOutput = ` --long-form Some description
--long-form2 Some description
with multiline
-s, --long-name Some description
--[no-]long-name-negated Some description
-l, --[no-]long-name-negated2 Some description with
multiline
-t, --long-name2 Some description with
multiline
`
func setUpZFlagSet(buf io.Writer) *zflag.FlagSet {
f := zflag.NewFlagSet("test", zflag.ExitOnError)
f.Bool("long-form", false, "Some description")
f.Bool("long-form2", false, "Some description\n with multiline")
f.Bool("long-name", false, "Some description", zflag.OptShorthand('s'))
f.Bool("long-name2", false, "Some description with\n multiline", zflag.OptShorthand('t'))
f.Bool("long-name-negated", false, "Some description", zflag.OptAddNegative())
f.Bool("long-name-negated2", false, "Some description with\n multiline", zflag.OptAddNegative(), zflag.OptShorthand('l'))
f.SetOutput(buf)
return f
}
func TestPrintUsage(t *testing.T) {
var buf bytes.Buffer
f := setUpZFlagSet(&buf)
f.PrintDefaults()
res := buf.String()
if res != expectedOutput {
t.Errorf("Expected \n%s \nActual \n%s", expectedOutput, res)
}
}
func setUpZFlagSet2(buf io.Writer) *zflag.FlagSet {
f := zflag.NewFlagSet("test", zflag.ExitOnError)
f.Bool("long-form", false, "Some description", zflag.OptAddNegative())
f.Bool("long-form2", false, "Some description\n with multiline", zflag.OptAddNegative())
f.Bool("long-name", false, "Some description", zflag.OptShorthand('s'))
f.Bool("long-name2", false, "Some description with\n multiline", zflag.OptShorthand('t'))
f.String("some-very-long-arg", "test", "Some very long description having break the limit", zflag.OptShorthand('l'))
f.String("other-very-long-arg", "long-default-value", "Some very long description having break the limit", zflag.OptShorthand('o'))
f.String("some-very-long-arg2", "very long default value", "Some very long description\nwith line break\nmultiple")
f.SetOutput(buf)
return f
}
const expectedOutput2 = ` --[no-]long-form Some description
--[no-]long-form2 Some description
with multiline
-s, --long-name Some description
-t, --long-name2 Some description with
multiline
-o, --other-very-long-arg string Some very long description having
break the limit (default
"long-default-value")
-l, --some-very-long-arg string Some very long description having
break the limit (default "test")
--some-very-long-arg2 string Some very long description
with line break
multiple (default "very long default
value")
`
func TestPrintUsage_2(t *testing.T) {
var buf bytes.Buffer
f := setUpZFlagSet2(&buf)
res := f.FlagUsagesWrapped(80)
if res != expectedOutput2 {
t.Errorf("Expected \n%q \nActual \n%q", expectedOutput2, res)
}
}