@@ -2,78 +2,150 @@ import massarg from "../src"
22import { OptionDef } from "../src/options"
33
44describe ( "Options" , ( ) => {
5- test ( "should parse properly" , ( ) => {
6- const options = massarg ( )
7- . option ( {
8- name : "number" ,
9- parse : ( v ) => parseInt ( v ) ,
10- } )
11- . parseArgs ( [ "--number" , "10" ] )
12- expect ( options ) . toHaveProperty ( "number" , 10 )
13- } )
5+ describe ( "basics" , ( ) => {
6+ test ( "should parse properly" , ( ) => {
7+ const options = massarg ( )
8+ . option ( {
9+ name : "number" ,
10+ parse : ( v ) => parseInt ( v ) ,
11+ } )
12+ . parseArgs ( [ "--number" , "10" ] )
13+ expect ( options ) . toHaveProperty ( "number" , 10 )
14+ } )
1415
15- test ( "should read from alias" , ( ) => {
16- const options = massarg ( )
17- . option ( {
18- name : "number" ,
19- aliases : [ "n" ] ,
20- parse : ( v ) => parseInt ( v ) ,
21- } )
22- . parseArgs ( [ "-n" , "10" ] )
23- expect ( options ) . toHaveProperty ( "number" , 10 )
24- expect ( options ) . toHaveProperty ( "n" , 10 )
16+ test ( "should read from alias" , ( ) => {
17+ const options = massarg ( )
18+ . option ( {
19+ name : "number" ,
20+ aliases : [ "n" ] ,
21+ parse : ( v ) => parseInt ( v ) ,
22+ } )
23+ . parseArgs ( [ "-n" , "10" ] )
24+ expect ( options ) . toHaveProperty ( "number" , 10 )
25+ expect ( options ) . toHaveProperty ( "n" , 10 )
26+ } )
2527 } )
2628
27- test ( "should camelCase names" , ( ) => {
28- const options = massarg ( )
29- . option ( {
30- name : "is-number" ,
31- aliases : [ "n" ] ,
32- parse : ( v ) => parseInt ( v ) ,
33- } )
34- . parseArgs ( [ "--is-number" , "10" ] )
29+ describe ( "parsing" , ( ) => {
30+ test ( "should camelCase names" , ( ) => {
31+ const options = massarg ( )
32+ . option ( {
33+ name : "is-number" ,
34+ aliases : [ "n" ] ,
35+ parse : ( v ) => parseInt ( v ) ,
36+ } )
37+ . parseArgs ( [ "--is-number" , "10" ] )
3538
36- expect ( options ) . toHaveProperty ( "isNumber" , 10 )
37- expect ( options ) . toHaveProperty ( "is-number" , 10 )
38- expect ( options ) . toHaveProperty ( "n" , 10 )
39- } )
39+ expect ( options ) . toHaveProperty ( "isNumber" , 10 )
40+ expect ( options ) . toHaveProperty ( "is-number" , 10 )
41+ expect ( options ) . toHaveProperty ( "n" , 10 )
42+ } )
4043
41- test ( "should parse bool in correct forms" , ( ) => {
42- const opts = {
43- name : "bool" ,
44- boolean : true ,
45- } as OptionDef < any , any >
46- const noArg = massarg ( ) . option ( opts ) . parseArgs ( [ "--bool" ] )
47- expect ( noArg ) . toHaveProperty ( "bool" , true )
44+ describe ( "bool" , ( ) => {
45+ test ( "should parse bool in correct forms" , ( ) => {
46+ const opts = {
47+ name : "bool" ,
48+ boolean : true ,
49+ } as OptionDef < any , any >
4850
49- const truthyNumArg = massarg ( ) . option ( opts ) . parseArgs ( [ "--bool" , "1 "] )
50- expect ( truthyNumArg ) . toHaveProperty ( "bool" , true )
51+ const noArg = massarg ( ) . option ( opts ) . parseArgs ( [ "--bool" ] )
52+ expect ( noArg ) . toHaveProperty ( "bool" , true )
5153
52- const falsyNumArg = massarg ( ) . option ( opts ) . parseArgs ( [ "--bool" , "0" ] )
53- expect ( falsyNumArg ) . toHaveProperty ( "bool" , false )
54- } )
54+ const truthyNumArg = massarg ( ) . option ( opts ) . parseArgs ( [ "--bool" , "1" ] )
55+ expect ( truthyNumArg ) . toHaveProperty ( "bool" , true )
5556
56- test ( "should expect value when not bool" , ( ) => {
57- const opts = {
58- name : "number" ,
59- parse : ( v ) => parseInt ( v ) ,
60- } as OptionDef < any , any >
61- expect ( ( ) => massarg ( ) . option ( opts ) . parseArgs ( [ "--number" ] ) ) . toThrow ( "Missing value for: number" )
62- } )
57+ const falsyNumArg = massarg ( ) . option ( opts ) . parseArgs ( [ "--bool" , "0" ] )
58+ expect ( falsyNumArg ) . toHaveProperty ( "bool" , false )
59+ } )
60+ } )
6361
64- test ( "should parse array in correct forms" , ( ) => {
65- const opts = {
66- name : "array" ,
67- array : true ,
68- } as OptionDef < any , any >
62+ describe ( "array" , ( ) => {
63+ test ( "should parse array in correct forms" , ( ) => {
64+ const opts = {
65+ name : "array" ,
66+ array : true ,
67+ } as OptionDef < any , any >
6968
70- const arr0el = massarg ( ) . option ( opts ) . parseArgs ( [ ] )
71- expect ( arr0el ) . toHaveProperty ( "array" , [ ] )
69+ const arr0el = massarg ( ) . option ( opts ) . parseArgs ( [ ] )
70+ expect ( arr0el ) . toHaveProperty ( "array" , [ ] )
7271
73- const arr1el = massarg ( ) . option ( opts ) . parseArgs ( [ "--array" , "something" ] )
74- expect ( arr1el ) . toHaveProperty ( "array" , [ "something" ] )
72+ const arr1el = massarg ( ) . option ( opts ) . parseArgs ( [ "--array" , "something" ] )
73+ expect ( arr1el ) . toHaveProperty ( "array" , [ "something" ] )
7574
76- const arr2el = massarg ( ) . option ( opts ) . parseArgs ( [ "--array" , "something" , "--array" , "another" ] )
77- expect ( arr2el ) . toHaveProperty ( "array" , [ "something" , "another" ] )
75+ const arr2el = massarg ( ) . option ( opts ) . parseArgs ( [ "--array" , "something" , "--array" , "another" ] )
76+ expect ( arr2el ) . toHaveProperty ( "array" , [ "something" , "another" ] )
77+ } )
78+ } )
79+
80+ test ( "should expect value when not bool" , ( ) => {
81+ expect ( ( ) =>
82+ massarg ( )
83+ . option ( {
84+ name : "number" ,
85+ parse : ( v ) => parseInt ( v ) ,
86+ } )
87+ . parseArgs ( [ "--number" ] )
88+ ) . toThrow ( "Missing value for: number" )
89+ } )
90+
91+ describe ( "required" , ( ) => {
92+ test ( "should throw on missing required value" , ( ) => {
93+ const mockConsoleError = jest . spyOn ( console , "error" ) . mockImplementation ( ( ) => void 0 )
94+ const mockConsoleLog = jest . spyOn ( console , "log" ) . mockImplementation ( ( ) => void 0 )
95+ expect ( ( ) =>
96+ massarg ( )
97+ . option ( {
98+ name : "number" ,
99+ parse : ( v ) => parseInt ( v ) ,
100+ required : true ,
101+ } )
102+ . parse ( [ "--not-number" , "abcdefg" ] )
103+ ) . toThrow ( "number is required, but was not defined" )
104+ mockConsoleError . mockRestore ( )
105+ mockConsoleLog . mockRestore ( )
106+ } )
107+
108+ test ( "should not throw on existing required value for command" , ( ) => {
109+ const mockConsoleError = jest . spyOn ( console , "error" ) . mockImplementation ( ( ) => void 0 )
110+ const mockConsoleLog = jest . spyOn ( console , "log" ) . mockImplementation ( ( ) => void 0 )
111+ expect ( ( ) =>
112+ massarg ( )
113+ . option ( {
114+ name : "number" ,
115+ parse : ( v ) => parseInt ( v ) ,
116+ commands : "cmd" ,
117+ required : true ,
118+ } )
119+ . command ( {
120+ name : "cmd" ,
121+ run : ( ) => void 0 ,
122+ } )
123+ . parse ( [ "cmd" , "--number" , "10" ] )
124+ ) . not . toThrow ( "number is required, but was not defined" )
125+ mockConsoleError . mockRestore ( )
126+ mockConsoleLog . mockRestore ( )
127+ } )
128+
129+ test ( "should throw on missing required value for command" , ( ) => {
130+ const mockConsoleError = jest . spyOn ( console , "error" ) . mockImplementation ( ( ) => void 0 )
131+ const mockConsoleLog = jest . spyOn ( console , "log" ) . mockImplementation ( ( ) => void 0 )
132+ expect ( ( ) =>
133+ massarg ( )
134+ . option ( {
135+ name : "number" ,
136+ parse : ( v ) => parseInt ( v ) ,
137+ commands : "cmd" ,
138+ required : true ,
139+ } )
140+ . command ( {
141+ name : "cmd" ,
142+ run : ( ) => void 0 ,
143+ } )
144+ . parse ( [ "cmd" ] )
145+ ) . toThrow ( "number is required, but was not defined" )
146+ mockConsoleError . mockRestore ( )
147+ mockConsoleLog . mockRestore ( )
148+ } )
149+ } )
78150 } )
79151} )
0 commit comments