7
7
*
8
8
*/
9
9
import { Arguments , Option , OptionType } from './interface' ;
10
- import { parseArguments } from './parser' ;
10
+ import { ParseArgumentException , parseArguments } from './parser' ;
11
11
12
12
describe ( 'parseArguments' , ( ) => {
13
13
const options : Option [ ] = [
@@ -32,10 +32,11 @@ describe('parseArguments', () => {
32
32
description : '' } ,
33
33
] ;
34
34
35
- const tests : { [ test : string ] : Partial < Arguments > } = {
35
+ const tests : { [ test : string ] : Partial < Arguments > | [ '!!!' , Partial < Arguments > , string [ ] ] } = {
36
36
'--bool' : { bool : true } ,
37
- '--bool=1' : { '--' : [ '--bool=1' ] } ,
38
- '--bool=yellow' : { '--' : [ '--bool=yellow' ] } ,
37
+ '--bool=1' : [ '!!!' , { } , [ '--bool=1' ] ] ,
38
+ '-- --bool=1' : { '--' : [ '--bool=1' ] } ,
39
+ '--bool=yellow' : [ '!!!' , { } , [ '--bool=yellow' ] ] ,
39
40
'--bool=true' : { bool : true } ,
40
41
'--bool=false' : { bool : false } ,
41
42
'--no-bool' : { bool : false } ,
@@ -45,7 +46,8 @@ describe('parseArguments', () => {
45
46
'--b true' : { bool : true } ,
46
47
'--b false' : { bool : false } ,
47
48
'--bool --num' : { bool : true , num : 0 } ,
48
- '--bool --num=true' : { bool : true , '--' : [ '--num=true' ] } ,
49
+ '--bool --num=true' : [ '!!!' , { bool : true } , [ '--num=true' ] ] ,
50
+ '-- --bool --num=true' : { '--' : [ '--bool' , '--num=true' ] } ,
49
51
'--bool=true --num' : { bool : true , num : 0 } ,
50
52
'--bool true --num' : { bool : true , num : 0 } ,
51
53
'--bool=false --num' : { bool : false , num : 0 } ,
@@ -85,40 +87,52 @@ describe('parseArguments', () => {
85
87
'--t2=true' : { t2 : true } ,
86
88
'--t2' : { t2 : true } ,
87
89
'--no-t2' : { t2 : false } ,
88
- '--t2=yellow' : { '--' : [ '--t2=yellow' ] } ,
90
+ '--t2=yellow' : [ '!!!' , { } , [ '--t2=yellow' ] ] ,
89
91
'--no-t2=true' : { '--' : [ '--no-t2=true' ] } ,
90
92
'--t2=123' : { t2 : 123 } ,
91
93
'--t3=a' : { t3 : 'a' } ,
92
94
'--t3' : { t3 : 0 } ,
93
95
'--t3 true' : { t3 : true } ,
94
96
'--e1 hello' : { e1 : 'hello' } ,
95
97
'--e1=hello' : { e1 : 'hello' } ,
96
- '--e1 yellow' : { p1 : 'yellow' , '--' : [ '--e1' ] } ,
97
- '--e1=yellow' : { '--' : [ '--e1=yellow' ] } ,
98
- '--e1' : { '--' : [ '--e1' ] } ,
99
- '--e1 true' : { p1 : 'true' , '--' : [ '--e1' ] } ,
100
- '--e1=true' : { '--' : [ '--e1=true' ] } ,
98
+ '--e1 yellow' : [ '!!!' , { p1 : 'yellow' } , [ '--e1' ] ] ,
99
+ '--e1=yellow' : [ '!!!' , { } , [ '--e1=yellow' ] ] ,
100
+ '--e1' : [ '!!!' , { } , [ '--e1' ] ] ,
101
+ '--e1 true' : [ '!!!' , { p1 : 'true' } , [ '--e1' ] ] ,
102
+ '--e1=true' : [ '!!!' , { } , [ '--e1=true' ] ] ,
101
103
'--e2 hello' : { e2 : 'hello' } ,
102
104
'--e2=hello' : { e2 : 'hello' } ,
103
105
'--e2 yellow' : { p1 : 'yellow' , e2 : '' } ,
104
- '--e2=yellow' : { '--' : [ '--e2=yellow' ] } ,
106
+ '--e2=yellow' : [ '!!!' , { } , [ '--e2=yellow' ] ] ,
105
107
'--e2' : { e2 : '' } ,
106
108
'--e2 true' : { p1 : 'true' , e2 : '' } ,
107
- '--e2=true' : { '--' : [ '--e2=true' ] } ,
109
+ '--e2=true' : [ '!!!' , { } , [ '--e2=true' ] ] ,
108
110
'--e3 json' : { e3 : 'json' } ,
109
111
'--e3=json' : { e3 : 'json' } ,
110
112
'--e3 yellow' : { p1 : 'yellow' , e3 : true } ,
111
- '--e3=yellow' : { '--' : [ '--e3=yellow' ] } ,
113
+ '--e3=yellow' : [ '!!!' , { } , [ '--e3=yellow' ] ] ,
112
114
'--e3' : { e3 : true } ,
113
115
'--e3 true' : { e3 : true } ,
114
116
'--e3=true' : { e3 : true } ,
115
117
} ;
116
118
117
119
Object . entries ( tests ) . forEach ( ( [ str , expected ] ) => {
118
120
it ( `works for ${ str } ` , ( ) => {
119
- const actual = parseArguments ( str . split ( / \s + / ) , options ) ;
121
+ try {
122
+ const actual = parseArguments ( str . split ( / \s + / ) , options ) ;
120
123
121
- expect ( actual ) . toEqual ( expected as Arguments ) ;
124
+ expect ( Array . isArray ( expected ) ) . toBe ( false ) ;
125
+ expect ( actual ) . toEqual ( expected as Arguments ) ;
126
+ } catch ( e ) {
127
+ if ( ! ( e instanceof ParseArgumentException ) ) {
128
+ throw e ;
129
+ }
130
+
131
+ // The expected values are an array.
132
+ expect ( Array . isArray ( expected ) ) . toBe ( true ) ;
133
+ expect ( e . parsed ) . toEqual ( expected [ 1 ] as Arguments ) ;
134
+ expect ( e . ignored ) . toEqual ( expected [ 2 ] as string [ ] ) ;
135
+ }
122
136
} ) ;
123
137
} ) ;
124
138
} ) ;
0 commit comments