77 *
88 */
99import { Arguments , Option , OptionType } from './interface' ;
10- import { parseArguments } from './parser' ;
10+ import { ParseArgumentException , parseArguments } from './parser' ;
1111
1212describe ( 'parseArguments' , ( ) => {
1313 const options : Option [ ] = [
@@ -32,10 +32,11 @@ describe('parseArguments', () => {
3232 description : '' } ,
3333 ] ;
3434
35- const tests : { [ test : string ] : Partial < Arguments > } = {
35+ const tests : { [ test : string ] : Partial < Arguments > | [ '!!!' , Partial < Arguments > , string [ ] ] } = {
3636 '--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' ] ] ,
3940 '--bool=true' : { bool : true } ,
4041 '--bool=false' : { bool : false } ,
4142 '--no-bool' : { bool : false } ,
@@ -45,7 +46,8 @@ describe('parseArguments', () => {
4546 '--b true' : { bool : true } ,
4647 '--b false' : { bool : false } ,
4748 '--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' ] } ,
4951 '--bool=true --num' : { bool : true , num : 0 } ,
5052 '--bool true --num' : { bool : true , num : 0 } ,
5153 '--bool=false --num' : { bool : false , num : 0 } ,
@@ -85,40 +87,52 @@ describe('parseArguments', () => {
8587 '--t2=true' : { t2 : true } ,
8688 '--t2' : { t2 : true } ,
8789 '--no-t2' : { t2 : false } ,
88- '--t2=yellow' : { '--' : [ '--t2=yellow' ] } ,
90+ '--t2=yellow' : [ '!!!' , { } , [ '--t2=yellow' ] ] ,
8991 '--no-t2=true' : { '--' : [ '--no-t2=true' ] } ,
9092 '--t2=123' : { t2 : 123 } ,
9193 '--t3=a' : { t3 : 'a' } ,
9294 '--t3' : { t3 : 0 } ,
9395 '--t3 true' : { t3 : true } ,
9496 '--e1 hello' : { e1 : 'hello' } ,
9597 '--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' ] ] ,
101103 '--e2 hello' : { e2 : 'hello' } ,
102104 '--e2=hello' : { e2 : 'hello' } ,
103105 '--e2 yellow' : { p1 : 'yellow' , e2 : '' } ,
104- '--e2=yellow' : { '--' : [ '--e2=yellow' ] } ,
106+ '--e2=yellow' : [ '!!!' , { } , [ '--e2=yellow' ] ] ,
105107 '--e2' : { e2 : '' } ,
106108 '--e2 true' : { p1 : 'true' , e2 : '' } ,
107- '--e2=true' : { '--' : [ '--e2=true' ] } ,
109+ '--e2=true' : [ '!!!' , { } , [ '--e2=true' ] ] ,
108110 '--e3 json' : { e3 : 'json' } ,
109111 '--e3=json' : { e3 : 'json' } ,
110112 '--e3 yellow' : { p1 : 'yellow' , e3 : true } ,
111- '--e3=yellow' : { '--' : [ '--e3=yellow' ] } ,
113+ '--e3=yellow' : [ '!!!' , { } , [ '--e3=yellow' ] ] ,
112114 '--e3' : { e3 : true } ,
113115 '--e3 true' : { e3 : true } ,
114116 '--e3=true' : { e3 : true } ,
115117 } ;
116118
117119 Object . entries ( tests ) . forEach ( ( [ str , expected ] ) => {
118120 it ( `works for ${ str } ` , ( ) => {
119- const actual = parseArguments ( str . split ( / \s + / ) , options ) ;
121+ try {
122+ const actual = parseArguments ( str . split ( / \s + / ) , options ) ;
120123
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+ }
122136 } ) ;
123137 } ) ;
124138} ) ;
0 commit comments