-
Notifications
You must be signed in to change notification settings - Fork 31
/
parser_test.go
62 lines (52 loc) · 1.54 KB
/
parser_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
package flaggy
import "testing"
func TestDoubleParse(t *testing.T) {
ResetParser()
DefaultParser.ShowHelpOnUnexpected = false
err := DefaultParser.Parse()
if err != nil {
t.Fatal(err)
}
err = DefaultParser.Parse()
if err == nil {
t.Fatal(err)
}
}
func TestDisableShowVersionFlag(t *testing.T) {
ResetParser()
// if this fails the function tested might be useless.
// Review if it's still useful and adjust.
if DefaultParser.ShowVersionWithVersionFlag != true {
t.Fatal("The tested function might not make sense any more.")
}
DefaultParser.DisableShowVersionWithVersion()
if DefaultParser.ShowVersionWithVersionFlag != false {
t.Fatal("ShowVersionWithVersionFlag should have been false.")
}
}
func TestFindArgsNotInParsedValues(t *testing.T) {
t.Parallel()
// ensure all 'test.' values are skipped
args := []string{"test.timeout=10s", "test.v=true"}
parsedValues := []parsedValue{}
unusedArgs := findArgsNotInParsedValues(args, parsedValues)
if len(unusedArgs) > 0 {
t.Fatal("Found 'test.' args as unused when they should be ignored")
}
// ensure regular values are not skipped
parsedValues = []parsedValue{
{
Key: "flaggy",
Value: "testing",
},
}
args = []string{"flaggy", "testing", "unusedFlag"}
unusedArgs = findArgsNotInParsedValues(args, parsedValues)
t.Log(unusedArgs)
if len(unusedArgs) == 0 {
t.Fatal("Found no args as unused when --flaggy=testing should have been detected")
}
if len(unusedArgs) != 1 {
t.Fatal("Invalid number of unused args found. Expected 1 but found", len(unusedArgs))
}
}