@@ -2,13 +2,24 @@ const t = require('tap')
22const noptLib = require ( '../lib/nopt-lib.js' )
33const Stream = require ( 'stream' )
44
5- const nopt = ( t , argv , opts , expected ) => {
5+ const logs = [ ]
6+ t . afterEach ( ( ) => {
7+ logs . length = 0
8+ } )
9+ process . on ( 'log' , ( ...msg ) => {
10+ logs . push ( msg )
11+ } )
12+
13+ const nopt = ( t , argv , opts , expected , expectedLogs ) => {
614 if ( Array . isArray ( argv ) ) {
715 t . strictSame ( noptLib . nopt ( argv , { typeDefs : noptLib . typeDefs , ...opts } ) , expected )
816 } else {
917 noptLib . clean ( argv , { typeDefs : noptLib . typeDefs , ...opts } )
1018 t . match ( argv , expected )
1119 }
20+ if ( expectedLogs ) {
21+ t . match ( expectedLogs , logs )
22+ }
1223 t . end ( )
1324}
1425
@@ -125,6 +136,43 @@ t.test('false invalid handler', (t) => {
125136 } )
126137} )
127138
139+ t . test ( 'longhand abbreviation' , ( t ) => {
140+ nopt ( t , [ '--lon' , 'text' ] , {
141+ types : {
142+ long : String ,
143+ } ,
144+ } , {
145+ long : 'text' ,
146+ argv : {
147+ remain : [ ] ,
148+ cooked : [ '--lon' , 'text' ] ,
149+ original : [ '--lon' , 'text' ] ,
150+ } ,
151+ } , [
152+ /* eslint-disable-next-line max-len */
153+ [ 'warn' , 'Expanding "--lon" to "--long". This will stop working in the next major version of npm.' ] ,
154+ ] )
155+ } )
156+
157+ t . test ( 'shorthand abbreviation' , ( t ) => {
158+ nopt ( t , [ '--shor' ] , {
159+ types : { } ,
160+ shorthands : {
161+ short : '--shorthand' ,
162+ } ,
163+ } , {
164+ shorthand : true ,
165+ argv : {
166+ remain : [ ] ,
167+ cooked : [ '--shorthand' ] ,
168+ original : [ '--shor' ] ,
169+ } ,
170+ } , [
171+ /* eslint-disable-next-line max-len */
172+ [ 'warn' , 'Expanding "--shor" to "--short". This will stop working in the next major version of npm.' ] ,
173+ ] )
174+ } )
175+
128176t . test ( 'shorthands that is the same' , ( t ) => {
129177 nopt ( t , [ '--sh' ] , {
130178 types : { } ,
@@ -142,14 +190,16 @@ t.test('shorthands that is the same', (t) => {
142190} )
143191
144192t . test ( 'unknown multiple' , ( t ) => {
145- nopt ( t , [ '--mult' , '--mult' , '--mult' ] , {
193+ nopt ( t , [ '--mult' , '--mult' , '--mult' , 'extra' ] , {
146194 types : { } ,
147195 } , {
148196 mult : [ true , true , true ] ,
149197 argv : {
150- remain : [ ] ,
151- cooked : [ '--mult' , '--mult' , '--mult' ] ,
152- original : [ '--mult' , '--mult' , '--mult' ] ,
198+ remain : [ 'extra' ] ,
199+ cooked : [ '--mult' , '--mult' , '--mult' , 'extra' ] ,
200+ original : [ '--mult' , '--mult' , '--mult' , 'extra' ] ,
153201 } ,
154- } )
202+ } , [
203+ [ 'warn' , '"extra" is being parsed as a normal command line argument.' ] ,
204+ ] )
155205} )
0 commit comments