@@ -77,6 +77,24 @@ func TestSameWords(t *testing.T) {
77
77
test (f , f , true )
78
78
}
79
79
80
+ func testOptionHelper (t * testing.T , cmd * cmds.Command , args string , expectedOpts kvs , expectedWords words , expectErr bool ) {
81
+ req := & cmds.Request {}
82
+ err := parse (req , strings .Split (args , " " ), cmd )
83
+ if err == nil {
84
+ err = req .FillDefaults ()
85
+ }
86
+ if expectErr {
87
+ if err == nil {
88
+ t .Errorf ("Command line '%v' parsing should have failed" , args )
89
+ }
90
+ } else if err != nil {
91
+ t .Errorf ("Command line '%v' failed to parse: %v" , args , err )
92
+ } else if ! sameWords (req .Arguments , expectedWords ) || ! sameKVs (kvs (req .Options ), expectedOpts ) {
93
+ t .Errorf ("Command line '%v':\n parsed as %v %v\n instead of %v %v" ,
94
+ args , req .Options , req .Arguments , expectedOpts , expectedWords )
95
+ }
96
+ }
97
+
80
98
func TestOptionParsing (t * testing.T ) {
81
99
cmd := & cmds.Command {
82
100
Options : []cmds.Option {
@@ -96,30 +114,12 @@ func TestOptionParsing(t *testing.T) {
96
114
},
97
115
}
98
116
99
- testHelper := func (args string , expectedOpts kvs , expectedWords words , expectErr bool ) {
100
- req := & cmds.Request {}
101
- err := parse (req , strings .Split (args , " " ), cmd )
102
- if err == nil {
103
- err = req .FillDefaults ()
104
- }
105
- if expectErr {
106
- if err == nil {
107
- t .Errorf ("Command line '%v' parsing should have failed" , args )
108
- }
109
- } else if err != nil {
110
- t .Errorf ("Command line '%v' failed to parse: %v" , args , err )
111
- } else if ! sameWords (req .Arguments , expectedWords ) || ! sameKVs (kvs (req .Options ), expectedOpts ) {
112
- t .Errorf ("Command line '%v':\n parsed as %v %v\n instead of %v %v" ,
113
- args , req .Options , req .Arguments , expectedOpts , expectedWords )
114
- }
115
- }
116
-
117
117
testFail := func (args string ) {
118
- testHelper ( args , kvs {}, words {}, true )
118
+ testOptionHelper ( t , cmd , args , kvs {}, words {}, true )
119
119
}
120
120
121
121
test := func (args string , expectedOpts kvs , expectedWords words ) {
122
- testHelper ( args , expectedOpts , expectedWords , false )
122
+ testOptionHelper ( t , cmd , args , expectedOpts , expectedWords , false )
123
123
}
124
124
125
125
test ("test -" , kvs {}, words {"-" })
0 commit comments