33
44import * as colors from 'colors' ;
55
6+ import { CommandLineAction } from '../CommandLineAction' ;
7+ import { CommandLineParser } from '../CommandLineParser' ;
68import { DynamicCommandLineParser } from '../DynamicCommandLineParser' ;
79import { DynamicCommandLineAction } from '../DynamicCommandLineAction' ;
810
9- describe ( 'CommandLineParameter' , ( ) => {
11+ function createParser ( ) : DynamicCommandLineParser {
1012 const commandLineParser : DynamicCommandLineParser = new DynamicCommandLineParser (
1113 {
1214 toolFilename : 'example' ,
@@ -27,12 +29,17 @@ describe('CommandLineParameter', () => {
2729 commandLineParser . addAction ( action ) ;
2830
2931 action . defineChoiceParameter ( {
30- parameterLongName : '--choice' ,
31- parameterShortName : '-c' ,
32- description : 'A choice' ,
32+ parameterLongName : '--choice-with-default' ,
33+ description : 'A choice with a default' ,
3334 alternatives : [ 'one' , 'two' ] ,
3435 defaultValue : 'one'
3536 } ) ;
37+ action . defineChoiceParameter ( {
38+ parameterLongName : '--choice' ,
39+ parameterShortName : '-c' ,
40+ description : 'A choice without a default' ,
41+ alternatives : [ 'one' , 'two' ]
42+ } ) ;
3643 action . defineFlagParameter ( {
3744 parameterLongName : '--flag' ,
3845 parameterShortName : '-f' ,
@@ -56,14 +63,102 @@ describe('CommandLineParameter', () => {
5663 description : 'A string list' ,
5764 argumentName : 'LIST'
5865 } ) ;
66+ return commandLineParser ;
67+ }
68+
69+ function expectPropertiesToMatchSnapshot ( object : { } , propertyNames : string [ ] ) : void {
70+ const snapshotObject : { } = { } ;
71+
72+ for ( const propertyName of propertyNames ) {
73+ snapshotObject [ propertyName ] = object [ propertyName ] ;
74+ }
75+ expect ( snapshotObject ) . toMatchSnapshot ( ) ;
76+ }
5977
78+ describe ( 'CommandLineParameter' , ( ) => {
6079 it ( 'prints the global help' , ( ) => {
80+ const commandLineParser : CommandLineParser = createParser ( ) ;
6181 const helpText : string = colors . stripColors ( commandLineParser . renderHelpText ( ) ) ;
6282 expect ( helpText ) . toMatchSnapshot ( ) ;
6383 } ) ;
6484
6585 it ( 'prints the action help' , ( ) => {
66- const helpText : string = colors . stripColors ( action . renderHelpText ( ) ) ;
86+ const commandLineParser : CommandLineParser = createParser ( ) ;
87+ const helpText : string = colors . stripColors ( commandLineParser . getAction ( 'do-job' ) . renderHelpText ( ) ) ;
6788 expect ( helpText ) . toMatchSnapshot ( ) ;
6889 } ) ;
90+
91+ it ( 'parses an input with ALL parameters' , ( ) => {
92+ const commandLineParser : CommandLineParser = createParser ( ) ;
93+ const action : CommandLineAction = commandLineParser . getAction ( 'do-job' ) ;
94+
95+ const args : string [ ] = [ '-g' ,
96+ 'do-job' , '-c' , 'two' , '-f' , '-i' , '123' , '-s' , 'hello' , '-l' , 'first' , '-l' , 'second' ] ;
97+
98+ return commandLineParser . execute ( args ) . then ( ( ) => {
99+ expect ( commandLineParser . selectedAction ) . toBe ( action ) ;
100+
101+ expectPropertiesToMatchSnapshot (
102+ commandLineParser . getFlagParameter ( '--global-flag' ) ,
103+ [ 'description' , 'kind' , 'longName' , 'shortName' , 'value' ]
104+ ) ;
105+
106+ expectPropertiesToMatchSnapshot (
107+ action . getChoiceParameter ( '--choice' ) ,
108+ [ 'alternatives' , 'defaultValue' , 'description' , 'kind' , 'longName' , 'shortName' , 'value' ]
109+ ) ;
110+ expectPropertiesToMatchSnapshot (
111+ action . getFlagParameter ( '--flag' ) ,
112+ [ 'description' , 'kind' , 'longName' , 'shortName' , 'value' ]
113+ ) ;
114+ expectPropertiesToMatchSnapshot (
115+ action . getIntegerParameter ( '--integer' ) ,
116+ [ 'argumentName' , 'description' , 'kind' , 'longName' , 'shortName' , 'value' ]
117+ ) ;
118+ expectPropertiesToMatchSnapshot (
119+ action . getStringParameter ( '--string' ) ,
120+ [ 'argumentName' , 'description' , 'kind' , 'longName' , 'shortName' , 'value' ]
121+ ) ;
122+ expectPropertiesToMatchSnapshot (
123+ action . getStringListParameter ( '--string-list' ) ,
124+ [ 'argumentName' , 'description' , 'kind' , 'longName' , 'shortName' , 'values' ]
125+ ) ;
126+ } ) ;
127+ } ) ;
128+
129+ it ( 'parses an input with NO parameters' , ( ) => {
130+ const commandLineParser : CommandLineParser = createParser ( ) ;
131+ const action : CommandLineAction = commandLineParser . getAction ( 'do-job' ) ;
132+ const args : string [ ] = [ 'do-job' ] ;
133+
134+ return commandLineParser . execute ( args ) . then ( ( ) => {
135+ expect ( commandLineParser . selectedAction ) . toBe ( action ) ;
136+
137+ expectPropertiesToMatchSnapshot (
138+ commandLineParser . getFlagParameter ( '--global-flag' ) ,
139+ [ 'description' , 'kind' , 'longName' , 'shortName' , 'value' ]
140+ ) ;
141+
142+ expectPropertiesToMatchSnapshot (
143+ action . getChoiceParameter ( '--choice' ) ,
144+ [ 'alternatives' , 'defaultValue' , 'description' , 'kind' , 'longName' , 'shortName' , 'value' ]
145+ ) ;
146+ expectPropertiesToMatchSnapshot (
147+ action . getFlagParameter ( '--flag' ) ,
148+ [ 'description' , 'kind' , 'longName' , 'shortName' , 'value' ]
149+ ) ;
150+ expectPropertiesToMatchSnapshot (
151+ action . getIntegerParameter ( '--integer' ) ,
152+ [ 'argumentName' , 'description' , 'kind' , 'longName' , 'shortName' , 'value' ]
153+ ) ;
154+ expectPropertiesToMatchSnapshot (
155+ action . getStringParameter ( '--string' ) ,
156+ [ 'argumentName' , 'description' , 'kind' , 'longName' , 'shortName' , 'value' ]
157+ ) ;
158+ expectPropertiesToMatchSnapshot (
159+ action . getStringListParameter ( '--string-list' ) ,
160+ [ 'argumentName' , 'description' , 'kind' , 'longName' , 'shortName' , 'values' ]
161+ ) ;
162+ } ) ;
163+ } ) ;
69164} ) ;
0 commit comments