-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions_test.go
121 lines (102 loc) · 3.58 KB
/
options_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
// Copyright (c) 2011, SoundCloud Ltd., Daniel Bornkessel
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Source code and contact info at http://github.com/kesselborn/go-getopt
package getopt
import "testing"
func TestIsOptionalOption(t *testing.T) {
options := Options{
"",
Definitions{
{"verbose", "show verbose output", Optional, ""},
{"logfile|l", "log to file <logfile>", Optional | NoLongOpt, ""},
{"method|m|MON_METHOD", "method: one of either 'heartbeat' or 'nagios'", 0, ""},
},
}
if options.IsOptional("verbose") != true {
t.Errorf("optional flag for 'verbose' not recognized")
}
if options.IsOptional("logfile") != false {
t.Errorf("optional flag for 'logfile' not recognized")
} // NoLongOpt
if options.IsOptional("l") != true {
t.Errorf("optional flag for 'l' not recognized")
}
if options.IsOptional("method") != false {
t.Errorf("non-optional flag for 'method' incorrectly recognized as optional flag for")
}
if options.IsOptional("m") != false {
t.Errorf("non-optional flag for 'm' incorrectly recognized as flag")
}
}
func TestIsRequiredOption(t *testing.T) {
options := Options{
"",
Definitions{
{"verbose", "show verbose output", Required, ""},
{"logfile|l", "log to file <logfile>", Required | NoLongOpt, ""},
{"method|m|MON_METHOD", "method: one of either 'heartbeat' or 'nagios'", 0, ""},
},
}
if options.IsRequired("verbose") != true {
t.Errorf("required flag for 'verbose' not recognized")
}
if options.IsRequired("logfile") != false {
t.Errorf("required flag for 'logfile' not recognized")
} // NoLongOpt
if options.IsRequired("l") != true {
t.Errorf("required flag for 'l' not recognized")
}
if options.IsRequired("method") != false {
t.Errorf("non-required flag for 'method' incorrectly recognized as required flag for")
}
if options.IsRequired("m") != false {
t.Errorf("non-required flag for 'm' incorrectly recognized as flag")
}
}
func TestIsFlagOption(t *testing.T) {
options := Options{
"",
Definitions{
{"verbose", "show verbose output", Flag, ""},
{"logfile|l", "log to file <logfile>", Flag | Optional | NoLongOpt, ""},
{"method|m|MON_METHOD", "method: one of either 'heartbeat' or 'nagios'", Required, ""},
},
}
if options.IsFlag("verbose") != true {
t.Errorf("flag 'verbose' not recognized")
}
if options.IsFlag("logfile") != false {
t.Errorf("flag 'logfile' not recognized")
} // NoLongOpt
if options.IsFlag("l") != true {
t.Errorf("flag 'l' not recognized")
}
if options.IsFlag("method") != false {
t.Errorf("non-flag 'method' incorrectly recognized as flag")
}
if options.IsFlag("m") != false {
t.Errorf("non-flag 'm' incorrectly recognized as flag")
}
}
func TestFindOption(t *testing.T) {
option1 := Option{"method|m|MON_METHOD", "method: one of either 'heartbeat' or 'nagios'", Required, ""}
option2 := Option{"logfile|l", "log to file <logfile>", Optional | NoLongOpt, ""}
option3 := Option{"verbose", "show verbose output", Flag, ""}
options := Options{"", Definitions{option1, option2, option3}}
if val, _ := options.FindOption("method"); val.neq(option1) {
t.Errorf("couldn't find option 'method'")
}
if _, found := options.FindOption("method"); found != true {
t.Errorf("couldn't find option 'method'")
}
if val, _ := options.FindOption("m"); val.neq(option1) {
t.Errorf("couldn't find option 'm'")
}
if _, found := options.FindOption("m"); found != true {
t.Errorf("couldn't find option 'm'")
}
if _, found := options.FindOption("logfile"); found == true {
t.Errorf("shouldn't find an option 'logfile'")
}
}