1
1
var nopt = require ( '../' )
2
- var test = require ( 'tap' ) . test
2
+ var t = require ( 'tap' )
3
3
var isWin = process . platform === 'win32'
4
4
5
- test ( 'empty array is fine if type includes Array' , function ( t ) {
5
+ t . test ( 'empty array is fine if type includes Array' , function ( t ) {
6
6
var typeDefs = {
7
7
arr : [ Array , String ] ,
8
8
}
@@ -14,35 +14,35 @@ test('empty array is fine if type includes Array', function (t) {
14
14
t . end ( )
15
15
} )
16
16
17
- test ( 'passing a string results in a string' , function ( t ) {
17
+ t . test ( 'passing a string results in a string' , function ( t ) {
18
18
var parsed = nopt ( { key : String } , { } , [ '--key' , 'myvalue' ] , 0 )
19
19
t . same ( parsed . key , 'myvalue' )
20
20
t . end ( )
21
21
} )
22
22
23
23
// https://github.com/npm/nopt/issues/31
24
- test ( 'Empty String results in empty string, not true' , function ( t ) {
24
+ t . test ( 'Empty String results in empty string, not true' , function ( t ) {
25
25
var parsed = nopt ( { empty : String } , { } , [ '--empty' ] , 0 )
26
26
t . same ( parsed . empty , '' )
27
27
t . end ( )
28
28
} )
29
29
30
30
// https://github.com/npm/nopt/issues/65
31
- test ( 'Empty String should not swallow next flag' , function ( t ) {
31
+ t . test ( 'Empty String should not swallow next flag' , function ( t ) {
32
32
var parsed = nopt ( { empty : String , foo : String } , { } , [ '--empty' , '--foo' ] , 0 )
33
33
t . same ( parsed . empty , '' )
34
34
t . same ( parsed . foo , '' )
35
35
t . end ( )
36
36
} )
37
37
38
38
// https://github.com/npm/nopt/issues/66
39
- test ( 'Empty String should not be true when type is single item Array' , function ( t ) {
39
+ t . test ( 'Empty String should not be true when type is single item Array' , function ( t ) {
40
40
var parsed = nopt ( { foo : [ String ] } , { } , [ '--foo' ] , 0 )
41
41
t . same ( parsed . foo , '' )
42
42
t . end ( )
43
43
} )
44
44
45
- test ( '~ path is resolved to ' + ( isWin ? '%USERPROFILE%' : '$HOME' ) , function ( t ) {
45
+ t . test ( '~ path is resolved to ' + ( isWin ? '%USERPROFILE%' : '$HOME' ) , function ( t ) {
46
46
var path = require ( 'path' )
47
47
var the
48
48
@@ -68,35 +68,35 @@ test('~ path is resolved to ' + (isWin ? '%USERPROFILE%' : '$HOME'), function (t
68
68
} )
69
69
70
70
// https://github.com/npm/nopt/issues/24
71
- test ( 'Unknown options are not parsed as numbers' , function ( t ) {
71
+ t . test ( 'Unknown options are not parsed as numbers' , function ( t ) {
72
72
var parsed = nopt ( { 'parse-me' : Number } , null , [ '--leave-as-is=1.20' , '--parse-me=1.20' ] , 0 )
73
73
t . equal ( parsed [ 'leave-as-is' ] , '1.20' )
74
74
t . equal ( parsed [ 'parse-me' ] , 1.2 )
75
75
t . end ( )
76
76
} )
77
77
78
78
// https://github.com/npm/nopt/issues/48
79
- test ( 'Check types based on name of type' , function ( t ) {
79
+ t . test ( 'Check types based on name of type' , function ( t ) {
80
80
var parsed = nopt ( { 'parse-me' : { name : 'Number' } } , null , [ '--parse-me=1.20' ] , 0 )
81
81
t . equal ( parsed [ 'parse-me' ] , 1.2 )
82
82
t . end ( )
83
83
} )
84
84
85
- test ( 'Missing types are not parsed' , function ( t ) {
85
+ t . test ( 'Missing types are not parsed' , function ( t ) {
86
86
var parsed = nopt ( { 'parse-me' : { } } , null , [ '--parse-me=1.20' ] , 0 )
87
87
// should only contain argv
88
88
t . equal ( Object . keys ( parsed ) . length , 1 )
89
89
t . end ( )
90
90
} )
91
91
92
- test ( 'Types passed without a name are not parsed' , function ( t ) {
92
+ t . test ( 'Types passed without a name are not parsed' , function ( t ) {
93
93
var parsed = nopt ( { 'parse-me' : { } } , { } , [ '--parse-me=1.20' ] , 0 )
94
94
// should only contain argv
95
95
t . equal ( Object . keys ( parsed ) . length , 1 )
96
96
t . end ( )
97
97
} )
98
98
99
- test ( 'other tests' , function ( t ) {
99
+ t . test ( 'other tests' , function ( t ) {
100
100
var Stream = require ( 'stream' )
101
101
var path = require ( 'path' )
102
102
var url = require ( 'url' )
@@ -312,3 +312,51 @@ test('other tests', function (t) {
312
312
} )
313
313
t . end ( )
314
314
} )
315
+
316
+ t . test ( 'argv toString()' , function ( t ) {
317
+ var parsed = nopt ( { key : String } , { } , [ '--key' , 'myvalue' ] , 0 )
318
+ t . same ( parsed . argv . toString ( ) , '"--key" "myvalue"' )
319
+ t . end ( )
320
+ } )
321
+
322
+ t . test ( 'custom invalidHandler' , function ( t ) {
323
+ t . teardown ( ( ) => {
324
+ delete nopt . invalidHandler
325
+ } )
326
+ nopt . invalidHandler = ( k , v ) => {
327
+ t . match ( k , 'key' )
328
+ t . match ( v , 'nope' )
329
+ t . end ( )
330
+ }
331
+ nopt ( { key : Number } , { } , [ '--key' , 'nope' ] , 0 )
332
+ } )
333
+
334
+ t . test ( 'numbered boolean' , function ( t ) {
335
+ var parsed = nopt ( { key : [ Boolean , String ] } , { } , [ '--key' , '0' ] , 0 )
336
+ t . same ( parsed . key , false )
337
+ t . end ( )
338
+ } )
339
+
340
+ t . test ( 'false string boolean' , function ( t ) {
341
+ var parsed = nopt ( { key : [ Boolean , String ] } , { } , [ '--key' , 'false' ] , 0 )
342
+ t . same ( parsed . key , false )
343
+ t . end ( )
344
+ } )
345
+
346
+ t . test ( 'true string boolean' , function ( t ) {
347
+ var parsed = nopt ( { key : [ Boolean , String ] } , { } , [ '--key' , 'true' ] , 0 )
348
+ t . same ( parsed . key , true )
349
+ t . end ( )
350
+ } )
351
+
352
+ t . test ( 'null string boolean' , function ( t ) {
353
+ var parsed = nopt ( { key : [ Boolean , String ] } , { } , [ '--key' , 'null' ] , 0 )
354
+ t . same ( parsed . key , false )
355
+ t . end ( )
356
+ } )
357
+
358
+ t . test ( 'other string boolean' , function ( t ) {
359
+ var parsed = nopt ( { key : [ Boolean , String ] } , { } , [ '--key' , 'yes' ] , 0 )
360
+ t . same ( parsed . key , true )
361
+ t . end ( )
362
+ } )
0 commit comments