1
1
const commander = require ( '../' ) ;
2
2
3
3
// Testing default value and custom processing behaviours.
4
+ // Some double assertions in tests to check action argument and .processedArg
4
5
5
6
test ( 'when argument not specified then callback not called' , ( ) => {
6
7
const mockCoercion = jest . fn ( ) ;
@@ -34,7 +35,7 @@ test('when custom with starting value and argument not specified then callback n
34
35
expect ( mockCoercion ) . not . toHaveBeenCalled ( ) ;
35
36
} ) ;
36
37
37
- test ( 'when custom with starting value and argument not specified then action argument is starting value' , ( ) => {
38
+ test ( 'when custom with starting value and argument not specified with action handler then action argument is starting value' , ( ) => {
38
39
const startingValue = 1 ;
39
40
let actionValue ;
40
41
const program = new commander . Command ( ) ;
@@ -45,9 +46,19 @@ test('when custom with starting value and argument not specified then action arg
45
46
} ) ;
46
47
program . parse ( [ ] , { from : 'user' } ) ;
47
48
expect ( actionValue ) . toEqual ( startingValue ) ;
49
+ expect ( program . processedArgs ) . toEqual ( [ startingValue ] ) ;
48
50
} ) ;
49
51
50
- test ( 'when default value is defined (without custom processing) and argument not specified then action argument is default value' , ( ) => {
52
+ test ( 'when custom with starting value and argument not specified without action handler then .processedArgs has starting value' , ( ) => {
53
+ const startingValue = 1 ;
54
+ const program = new commander . Command ( ) ;
55
+ program
56
+ . argument ( '[n]' , 'number' , parseFloat , startingValue ) ;
57
+ program . parse ( [ ] , { from : 'user' } ) ;
58
+ expect ( program . processedArgs ) . toEqual ( [ startingValue ] ) ;
59
+ } ) ;
60
+
61
+ test ( 'when default value is defined (without custom processing) and argument not specified with action handler then action argument is default value' , ( ) => {
51
62
const defaultValue = 1 ;
52
63
let actionValue ;
53
64
const program = new commander . Command ( ) ;
@@ -58,6 +69,16 @@ test('when default value is defined (without custom processing) and argument not
58
69
} ) ;
59
70
program . parse ( [ ] , { from : 'user' } ) ;
60
71
expect ( actionValue ) . toEqual ( defaultValue ) ;
72
+ expect ( program . processedArgs ) . toEqual ( [ defaultValue ] ) ;
73
+ } ) ;
74
+
75
+ test ( 'when default value is defined (without custom processing) and argument not specified without action handler then .processedArgs is default value' , ( ) => {
76
+ const defaultValue = 1 ;
77
+ const program = new commander . Command ( ) ;
78
+ program
79
+ . argument ( '[n]' , 'number' , defaultValue ) ;
80
+ program . parse ( [ ] , { from : 'user' } ) ;
81
+ expect ( program . processedArgs ) . toEqual ( [ defaultValue ] ) ;
61
82
} ) ;
62
83
63
84
test ( 'when argument specified then callback called with value' , ( ) => {
@@ -71,7 +92,7 @@ test('when argument specified then callback called with value', () => {
71
92
expect ( mockCoercion ) . toHaveBeenCalledWith ( value , undefined ) ;
72
93
} ) ;
73
94
74
- test ( 'when argument specified then action value is as returned from callback' , ( ) => {
95
+ test ( 'when argument specified with action handler then action value is as returned from callback' , ( ) => {
75
96
const callbackResult = 2 ;
76
97
let actionValue ;
77
98
const program = new commander . Command ( ) ;
@@ -84,6 +105,18 @@ test('when argument specified then action value is as returned from callback', (
84
105
} ) ;
85
106
program . parse ( [ 'node' , 'test' , 'alpha' ] ) ;
86
107
expect ( actionValue ) . toEqual ( callbackResult ) ;
108
+ expect ( program . processedArgs ) . toEqual ( [ callbackResult ] ) ;
109
+ } ) ;
110
+
111
+ test ( 'when argument specified without action handler then .processedArgs is as returned from callback' , ( ) => {
112
+ const callbackResult = 2 ;
113
+ const program = new commander . Command ( ) ;
114
+ program
115
+ . argument ( '[n]' , 'number' , ( ) => {
116
+ return callbackResult ;
117
+ } ) ;
118
+ program . parse ( [ 'node' , 'test' , 'alpha' ] ) ;
119
+ expect ( program . processedArgs ) . toEqual ( [ callbackResult ] ) ;
87
120
} ) ;
88
121
89
122
test ( 'when argument specified then program.args has original rather than custom' , ( ) => {
@@ -124,6 +157,14 @@ test('when variadic argument specified multiple times then callback called with
124
157
expect ( mockCoercion ) . toHaveBeenNthCalledWith ( 2 , '2' , 'callback' ) ;
125
158
} ) ;
126
159
160
+ test ( 'when variadic argument without action handler then .processedArg has array' , ( ) => {
161
+ const program = new commander . Command ( ) ;
162
+ program
163
+ . argument ( '<n...>' , 'number' ) ;
164
+ program . parse ( [ '1' , '2' ] , { from : 'user' } ) ;
165
+ expect ( program . processedArgs ) . toEqual ( [ [ '1' , '2' ] ] ) ;
166
+ } ) ;
167
+
127
168
test ( 'when parseFloat "1e2" then action argument is 100' , ( ) => {
128
169
let actionValue ;
129
170
const program = new commander . Command ( ) ;
@@ -134,6 +175,7 @@ test('when parseFloat "1e2" then action argument is 100', () => {
134
175
} ) ;
135
176
program . parse ( [ '1e2' ] , { from : 'user' } ) ;
136
177
expect ( actionValue ) . toEqual ( 100 ) ;
178
+ expect ( program . processedArgs ) . toEqual ( [ actionValue ] ) ;
137
179
} ) ;
138
180
139
181
test ( 'when defined default value for required argument then throw' , ( ) => {
0 commit comments