-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathparser_test.go
More file actions
81 lines (68 loc) · 1.97 KB
/
parser_test.go
File metadata and controls
81 lines (68 loc) · 1.97 KB
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
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",
ConsumesNext: true,
},
}
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))
}
}
func TestFindArgsNotInParsedValuesSkipsEmptyConsumedValues(t *testing.T) {
t.Parallel()
args := []string{"-log.file.dir", ""}
parsedValues := []parsedValue{
{
Key: "log.file.dir",
Value: "",
ConsumesNext: true,
},
}
unusedArgs := findArgsNotInParsedValues(args, parsedValues)
if len(unusedArgs) != 0 {
t.Fatalf("expected no unused args, found %v", unusedArgs)
}
}